Skip to content

Commit

Permalink
#72 LifeCycle aware한 동작 정의를 위해 repeatOnLifecycle 활용
Browse files Browse the repository at this point in the history
  • Loading branch information
wisemuji committed Jul 31, 2023
1 parent 1f9acc0 commit 542cedf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 43 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package com.droidknights.app2023.feature.home

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.scrollBy
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.CircleShape
Expand All @@ -27,12 +27,13 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.repeatOnLifecycle
import com.droidknights.app2023.core.designsystem.component.KnightsCard
import com.droidknights.app2023.core.designsystem.component.NetworkImage
import com.droidknights.app2023.core.designsystem.theme.KnightsTheme
import com.droidknights.app2023.core.model.Sponsor
import com.droidknights.app2023.core.ui.observeAsState
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive

private const val SCROLL_DELAY_MILLIS = 20L
private const val SCROLL_PIXEL_UNIT = 4f
Expand Down Expand Up @@ -115,7 +116,7 @@ private fun SponsorGroup(
sponsors: List<Sponsor>,
) {
val scrollState = rememberLazyListState()
val lifecycleState = LocalLifecycleOwner.current.lifecycle.observeAsState()
val lifecycleOwner = LocalLifecycleOwner.current

LazyRow(
state = scrollState,
Expand All @@ -132,15 +133,12 @@ private fun SponsorGroup(
SponsorLogo(sponsor = sponsors[index % sponsors.size])
}
}
LaunchedEffect(lifecycleState.value) {
when (lifecycleState.value) {
Lifecycle.Event.ON_RESUME -> {
while (true) {
autoScroll(scrollState)
}
LaunchedEffect(Unit) {
lifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
while (isActive) {
scrollState.scrollBy(SCROLL_PIXEL_UNIT)
delay(SCROLL_DELAY_MILLIS)
}

else -> {}
}
}
}
Expand Down Expand Up @@ -174,13 +172,6 @@ private fun SponsorLogo(
}
}

private suspend fun autoScroll(lazyListState: LazyListState) {
lazyListState.scroll {
scrollBy(SCROLL_PIXEL_UNIT)
}
delay(SCROLL_DELAY_MILLIS)
}

@Preview
@Composable
private fun SponsorCardPreview() {
Expand Down

0 comments on commit 542cedf

Please sign in to comment.