From 3b45a13f4dc09b62b3973ecc91fa4e071d72a579 Mon Sep 17 00:00:00 2001 From: wisemuji Date: Fri, 28 Jul 2023 02:02:40 +0900 Subject: [PATCH 1/9] =?UTF-8?q?#72=20=ED=9B=84=EC=9B=90=EC=82=AC=20?= =?UTF-8?q?=EC=B9=B4=EB=93=9C=20LazyRow=EB=A1=9C=20=EB=B3=80=EA=B2=BD=20?= =?UTF-8?q?=EB=B0=8F=20=ED=8C=A8=EB=94=A9=EA=B0=92=20=EC=A1=B0=EC=A0=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app2023/feature/home/SponsorCard.kt | 58 +++++++++++-------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt b/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt index 963475bd..5326e368 100644 --- a/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt +++ b/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt @@ -1,18 +1,25 @@ package com.droidknights.app2023.feature.home import androidx.compose.foundation.Image +import androidx.compose.foundation.MutatePriority import androidx.compose.foundation.background import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row 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.items +import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.remember +import androidx.compose.runtime.toMutableStateList import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip @@ -36,45 +43,48 @@ fun SponsorCard( Column( modifier = Modifier .fillMaxWidth() - .padding(horizontal = 24.dp) ) { - Text( - text = stringResource(id = R.string.sponsor_card_title), - style = KnightsTheme.typography.headlineSmallBL, - color = Color(0xFF000000), - modifier = Modifier.padding(top = 24.dp), - ) - Text( - text = stringResource( - id = R.string.sponsor_card_description, - uiState.platinumCount, - uiState.goldCount - ), - style = KnightsTheme.typography.titleSmallR, - color = Color(0xFF868686), - modifier = Modifier.padding(top = 8.dp), - ) + Column(modifier = Modifier.padding(horizontal = 24.dp)) { + Text( + text = stringResource(id = R.string.sponsor_card_title), + style = KnightsTheme.typography.headlineSmallBL, + color = Color(0xFF000000), + modifier = Modifier.padding(top = 24.dp), + ) + Text( + text = stringResource( + id = R.string.sponsor_card_description, + uiState.platinumCount, + uiState.goldCount + ), + style = KnightsTheme.typography.titleSmallR, + color = Color(0xFF868686), + modifier = Modifier.padding(top = 8.dp), + ) + } SponsorGroup(uiState.sponsors) } } } -// TODO: 이미지 연결 및 동적으로 배치 +// TODO: 세로 두 줄 고정 필요 @Composable private fun SponsorGroup( sponsors: List, ) { - Row( + val itemsListState = remember { sponsors.toMutableStateList() } + + LazyRow( horizontalArrangement = Arrangement .spacedBy( - space = 8.dp, + space = 14.dp, alignment = Alignment.CenterHorizontally, ), modifier = Modifier - .fillMaxWidth() - .padding(24.dp), + .padding(vertical = 24.dp) + .fillMaxWidth(), ) { - sponsors.forEach { + items(itemsListState, key = { it.name }) { SponsorLogo(it) } } From 7bcf2b6f1909e47b7d801d9ba7e106aefd109039 Mon Sep 17 00:00:00 2001 From: wisemuji Date: Fri, 28 Jul 2023 02:27:54 +0900 Subject: [PATCH 2/9] =?UTF-8?q?#72=20=EB=AC=B4=ED=95=9C=20=EC=9E=90?= =?UTF-8?q?=EB=8F=99=20=EC=8A=A4=ED=81=AC=EB=A1=A4=20=EC=95=A0=EB=8B=88?= =?UTF-8?q?=EB=A9=94=EC=9D=B4=EC=85=98=20=EC=A0=95=EC=9D=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app2023/feature/home/SponsorCard.kt | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt b/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt index 5326e368..5397fa7e 100644 --- a/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt +++ b/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt @@ -33,6 +33,10 @@ 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 kotlinx.coroutines.delay + +private const val SCROLL_DELAY_MILLIS = 20L +private const val SCROLL_PIXEL_UNIT = 4f @Composable fun SponsorCard( @@ -72,9 +76,11 @@ fun SponsorCard( private fun SponsorGroup( sponsors: List, ) { - val itemsListState = remember { sponsors.toMutableStateList() } + val itemsState = remember { sponsors.toMutableStateList() } + val scrollState = rememberLazyListState() LazyRow( + state = scrollState, horizontalArrangement = Arrangement .spacedBy( space = 14.dp, @@ -84,10 +90,17 @@ private fun SponsorGroup( .padding(vertical = 24.dp) .fillMaxWidth(), ) { - items(itemsListState, key = { it.name }) { + items(itemsState, key = { it.name }) { SponsorLogo(it) } } + LaunchedEffect(scrollState.canScrollForward) { + if (scrollState.canScrollForward) return@LaunchedEffect + itemsState.addAll(sponsors) + } + LaunchedEffect(Unit) { + autoScroll(scrollState) + } } @Composable @@ -119,6 +132,15 @@ private fun SponsorLogo( } } +private tailrec suspend fun autoScroll(lazyListState: LazyListState) { + lazyListState.scroll(MutatePriority.PreventUserInput) { + scrollBy(SCROLL_PIXEL_UNIT) + } + delay(SCROLL_DELAY_MILLIS) + + autoScroll(lazyListState) +} + @Preview @Composable private fun SponsorCardPreview() { From dfdad178477c7da50f9780b45dfb26f555c9bd34 Mon Sep 17 00:00:00 2001 From: wisemuji Date: Fri, 28 Jul 2023 03:13:19 +0900 Subject: [PATCH 3/9] =?UTF-8?q?#72=20LifeCycle=20State=EB=A5=BC=20?= =?UTF-8?q?=EA=B4=80=EC=B0=B0=ED=95=98=EA=B8=B0=20=EC=9C=84=ED=95=9C=20obs?= =?UTF-8?q?erveAsState=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/droidknights/app2023/core/ui/.gitkeep | 0 .../droidknights/app2023/core/ui/Lifecycle.kt | 25 +++++++++++++++++++ 2 files changed, 25 insertions(+) delete mode 100644 core/ui/src/main/java/com/droidknights/app2023/core/ui/.gitkeep create mode 100644 core/ui/src/main/java/com/droidknights/app2023/core/ui/Lifecycle.kt diff --git a/core/ui/src/main/java/com/droidknights/app2023/core/ui/.gitkeep b/core/ui/src/main/java/com/droidknights/app2023/core/ui/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/core/ui/src/main/java/com/droidknights/app2023/core/ui/Lifecycle.kt b/core/ui/src/main/java/com/droidknights/app2023/core/ui/Lifecycle.kt new file mode 100644 index 00000000..a15dea92 --- /dev/null +++ b/core/ui/src/main/java/com/droidknights/app2023/core/ui/Lifecycle.kt @@ -0,0 +1,25 @@ +package com.droidknights.app2023.core.ui + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisposableEffect +import androidx.compose.runtime.State +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.LifecycleEventObserver + +@Composable +fun Lifecycle.observeAsState(): State { + val lifecycleEvent = remember { mutableStateOf(Lifecycle.Event.ON_ANY) } + + DisposableEffect(this) { + val observer = LifecycleEventObserver { _, event -> + lifecycleEvent.value = event + } + this@observeAsState.addObserver(observer) + onDispose { + this@observeAsState.removeObserver(observer) + } + } + return lifecycleEvent +} From 905b42651787cedff58b9a62a17820061913be1b Mon Sep 17 00:00:00 2001 From: wisemuji Date: Fri, 28 Jul 2023 03:14:19 +0900 Subject: [PATCH 4/9] =?UTF-8?q?#72=20Lifecycle=EC=9D=B4=20Resume=20?= =?UTF-8?q?=EC=83=81=ED=83=9C=EC=9D=BC=20=EB=95=8C=EB=A7=8C=20=EC=9E=90?= =?UTF-8?q?=EB=8F=99=20=EC=8A=A4=ED=81=AC=EB=A1=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app2023/feature/home/SponsorCard.kt | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt b/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt index 5397fa7e..8323f4af 100644 --- a/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt +++ b/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt @@ -25,16 +25,22 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.painter.ColorPainter +import androidx.compose.ui.platform.LocalLifecycleOwner import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp +import androidx.lifecycle.Lifecycle 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 androidx.lifecycle.compose.LifecycleResumeEffect +//import androidx.lifecycle.compose.currentStateAsState + private const val SCROLL_DELAY_MILLIS = 20L private const val SCROLL_PIXEL_UNIT = 4f @@ -45,8 +51,7 @@ fun SponsorCard( ) { KnightsCard { Column( - modifier = Modifier - .fillMaxWidth() + modifier = Modifier.fillMaxWidth() ) { Column(modifier = Modifier.padding(horizontal = 24.dp)) { Text( @@ -78,14 +83,14 @@ private fun SponsorGroup( ) { val itemsState = remember { sponsors.toMutableStateList() } val scrollState = rememberLazyListState() + val lifecycleState = LocalLifecycleOwner.current.lifecycle.observeAsState() LazyRow( state = scrollState, - horizontalArrangement = Arrangement - .spacedBy( - space = 14.dp, - alignment = Alignment.CenterHorizontally, - ), + horizontalArrangement = Arrangement.spacedBy( + space = 14.dp, + alignment = Alignment.CenterHorizontally, + ), modifier = Modifier .padding(vertical = 24.dp) .fillMaxWidth(), @@ -98,8 +103,15 @@ private fun SponsorGroup( if (scrollState.canScrollForward) return@LaunchedEffect itemsState.addAll(sponsors) } - LaunchedEffect(Unit) { - autoScroll(scrollState) + LaunchedEffect(lifecycleState.value) { + when (lifecycleState.value) { + Lifecycle.Event.ON_RESUME -> { + while (true) { + autoScroll(scrollState) + } + } + else -> {} + } } } @@ -132,13 +144,11 @@ private fun SponsorLogo( } } -private tailrec suspend fun autoScroll(lazyListState: LazyListState) { +private suspend fun autoScroll(lazyListState: LazyListState) { lazyListState.scroll(MutatePriority.PreventUserInput) { scrollBy(SCROLL_PIXEL_UNIT) } delay(SCROLL_DELAY_MILLIS) - - autoScroll(lazyListState) } @Preview From e3cee99d4b48b033593c41b18b83e28cc44f6e7e Mon Sep 17 00:00:00 2001 From: wisemuji Date: Mon, 31 Jul 2023 19:49:37 +0900 Subject: [PATCH 5/9] =?UTF-8?q?#72=20=EA=B0=80=EC=A7=9C=20=ED=9B=84?= =?UTF-8?q?=EC=9B=90=EC=82=AC=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20=EC=97=B0?= =?UTF-8?q?=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app2023/feature/home/SponsorCard.kt | 43 +++++++++++++++++-- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt b/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt index 8323f4af..d903bdf0 100644 --- a/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt +++ b/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt @@ -38,16 +38,51 @@ import com.droidknights.app2023.core.model.Sponsor import com.droidknights.app2023.core.ui.observeAsState import kotlinx.coroutines.delay -//import androidx.lifecycle.compose.LifecycleResumeEffect -//import androidx.lifecycle.compose.currentStateAsState - private const val SCROLL_DELAY_MILLIS = 20L private const val SCROLL_PIXEL_UNIT = 4f @Composable fun SponsorCard( // TODO: 실제 데이터와 연결 - uiState: SponsorsUiState = SponsorsUiState(emptyList()), + uiState: SponsorsUiState = + listOf( + Sponsor( + name = "Sponsor1", + homepage = "https://www.instagram.com/droid_knights", + grade = Sponsor.Grade.GOLD, + imageUrl = "https://picsum.photos/id/237/200/200", + ), + Sponsor( + name = "Sponsor2", + homepage = "https://www.instagram.com/droid_knights", + grade = Sponsor.Grade.PLATINUM, + imageUrl = "https://picsum.photos/id/204/200/200", + ), + Sponsor( + name = "Sponsor3", + homepage = "https://www.instagram.com/droid_knights", + grade = Sponsor.Grade.PLATINUM, + imageUrl = "https://picsum.photos/id/203/200/200", + ), + Sponsor( + name = "Sponsor4", + homepage = "https://www.instagram.com/droid_knights", + grade = Sponsor.Grade.PLATINUM, + imageUrl = "https://picsum.photos/id/202/200/200", + ), + Sponsor( + name = "Sponsor5", + homepage = "https://www.instagram.com/droid_knights", + grade = Sponsor.Grade.PLATINUM, + imageUrl = "https://picsum.photos/id/201/200/200", + ), + Sponsor( + name = "Sponsor6", + homepage = "https://www.instagram.com/droid_knights", + grade = Sponsor.Grade.PLATINUM, + imageUrl = "https://picsum.photos/id/200/200/200", + ), + ).let(::SponsorsUiState), ) { KnightsCard { Column( From a278951ebfc07fc3d04922408e463b57a16e7df6 Mon Sep 17 00:00:00 2001 From: wisemuji Date: Mon, 31 Jul 2023 19:51:31 +0900 Subject: [PATCH 6/9] =?UTF-8?q?#72=20LazyRow=EC=97=90=20userScrollEnabled?= =?UTF-8?q?=20=EC=98=B5=EC=85=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/droidknights/app2023/feature/home/SponsorCard.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt b/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt index d903bdf0..1f11d81d 100644 --- a/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt +++ b/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt @@ -126,6 +126,7 @@ private fun SponsorGroup( space = 14.dp, alignment = Alignment.CenterHorizontally, ), + userScrollEnabled = false, modifier = Modifier .padding(vertical = 24.dp) .fillMaxWidth(), @@ -145,6 +146,7 @@ private fun SponsorGroup( autoScroll(scrollState) } } + else -> {} } } @@ -180,7 +182,7 @@ private fun SponsorLogo( } private suspend fun autoScroll(lazyListState: LazyListState) { - lazyListState.scroll(MutatePriority.PreventUserInput) { + lazyListState.scroll { scrollBy(SCROLL_PIXEL_UNIT) } delay(SCROLL_DELAY_MILLIS) From 1f9acc0615467d478b3b9e97c6350a9aab7eac50 Mon Sep 17 00:00:00 2001 From: wisemuji Date: Mon, 31 Jul 2023 20:14:29 +0900 Subject: [PATCH 7/9] =?UTF-8?q?#72=20LazyRow=20=EC=8A=A4=ED=81=AC=EB=A1=A4?= =?UTF-8?q?=20=EC=83=81=ED=83=9C=EC=97=90=20=EB=A7=9E=EC=B6=B0=20=EC=95=84?= =?UTF-8?q?=EC=9D=B4=ED=85=9C=EC=9D=84=20=EC=B6=94=EA=B0=80=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EB=B0=A9=EC=8B=9D=EC=97=90=EC=84=9C=20index=20?= =?UTF-8?q?=EA=B8=B0=EB=B0=98=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app2023/feature/home/SponsorCard.kt | 17 +++++------------ .../app2023/feature/home/SponsorsUiState.kt | 2 ++ 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt b/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt index 1f11d81d..fd05654b 100644 --- a/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt +++ b/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt @@ -1,7 +1,6 @@ package com.droidknights.app2023.feature.home import androidx.compose.foundation.Image -import androidx.compose.foundation.MutatePriority import androidx.compose.foundation.background import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box @@ -11,15 +10,12 @@ 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.items import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.remember -import androidx.compose.runtime.toMutableStateList import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip @@ -106,7 +102,9 @@ fun SponsorCard( modifier = Modifier.padding(top = 8.dp), ) } - SponsorGroup(uiState.sponsors) + if (uiState.isNotEmpty()) { + SponsorGroup(uiState.sponsors) + } } } } @@ -116,7 +114,6 @@ fun SponsorCard( private fun SponsorGroup( sponsors: List, ) { - val itemsState = remember { sponsors.toMutableStateList() } val scrollState = rememberLazyListState() val lifecycleState = LocalLifecycleOwner.current.lifecycle.observeAsState() @@ -131,14 +128,10 @@ private fun SponsorGroup( .padding(vertical = 24.dp) .fillMaxWidth(), ) { - items(itemsState, key = { it.name }) { - SponsorLogo(it) + items(count = Int.MAX_VALUE) { index -> + SponsorLogo(sponsor = sponsors[index % sponsors.size]) } } - LaunchedEffect(scrollState.canScrollForward) { - if (scrollState.canScrollForward) return@LaunchedEffect - itemsState.addAll(sponsors) - } LaunchedEffect(lifecycleState.value) { when (lifecycleState.value) { Lifecycle.Event.ON_RESUME -> { diff --git a/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorsUiState.kt b/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorsUiState.kt index 912d9675..0147bb31 100644 --- a/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorsUiState.kt +++ b/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorsUiState.kt @@ -8,4 +8,6 @@ data class SponsorsUiState(val sponsors: List) { val goldCount: Int get() = sponsors.count { it.grade == Sponsor.Grade.GOLD } + + fun isNotEmpty() = sponsors.isNotEmpty() } From 542cedfc6885476bc7b268943b0fa30ba1078ae6 Mon Sep 17 00:00:00 2001 From: wisemuji Date: Mon, 31 Jul 2023 20:18:01 +0900 Subject: [PATCH 8/9] =?UTF-8?q?#72=20LifeCycle=20aware=ED=95=9C=20?= =?UTF-8?q?=EB=8F=99=EC=9E=91=20=EC=A0=95=EC=9D=98=EB=A5=BC=20=EC=9C=84?= =?UTF-8?q?=ED=95=B4=20repeatOnLifecycle=20=ED=99=9C=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../droidknights/app2023/core/ui/Lifecycle.kt | 25 ----------------- .../app2023/feature/home/SponsorCard.kt | 27 +++++++------------ 2 files changed, 9 insertions(+), 43 deletions(-) delete mode 100644 core/ui/src/main/java/com/droidknights/app2023/core/ui/Lifecycle.kt diff --git a/core/ui/src/main/java/com/droidknights/app2023/core/ui/Lifecycle.kt b/core/ui/src/main/java/com/droidknights/app2023/core/ui/Lifecycle.kt deleted file mode 100644 index a15dea92..00000000 --- a/core/ui/src/main/java/com/droidknights/app2023/core/ui/Lifecycle.kt +++ /dev/null @@ -1,25 +0,0 @@ -package com.droidknights.app2023.core.ui - -import androidx.compose.runtime.Composable -import androidx.compose.runtime.DisposableEffect -import androidx.compose.runtime.State -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.lifecycle.Lifecycle -import androidx.lifecycle.LifecycleEventObserver - -@Composable -fun Lifecycle.observeAsState(): State { - val lifecycleEvent = remember { mutableStateOf(Lifecycle.Event.ON_ANY) } - - DisposableEffect(this) { - val observer = LifecycleEventObserver { _, event -> - lifecycleEvent.value = event - } - this@observeAsState.addObserver(observer) - onDispose { - this@observeAsState.removeObserver(observer) - } - } - return lifecycleEvent -} diff --git a/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt b/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt index fd05654b..278bc019 100644 --- a/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt +++ b/feature/home/src/main/java/com/droidknights/app2023/feature/home/SponsorCard.kt @@ -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 @@ -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 @@ -115,7 +116,7 @@ private fun SponsorGroup( sponsors: List, ) { val scrollState = rememberLazyListState() - val lifecycleState = LocalLifecycleOwner.current.lifecycle.observeAsState() + val lifecycleOwner = LocalLifecycleOwner.current LazyRow( state = scrollState, @@ -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 -> {} } } } @@ -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() { From 5a22b93ef320a853d8dadf2c57fb2af4acdd21ea Mon Sep 17 00:00:00 2001 From: wisemuji Date: Mon, 31 Jul 2023 20:22:31 +0900 Subject: [PATCH 9/9] =?UTF-8?q?#72=20.gitkeep=20=EB=B3=B5=EA=B5=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/ui/src/main/java/com/droidknights/app2023/core/ui/.gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 core/ui/src/main/java/com/droidknights/app2023/core/ui/.gitkeep diff --git a/core/ui/src/main/java/com/droidknights/app2023/core/ui/.gitkeep b/core/ui/src/main/java/com/droidknights/app2023/core/ui/.gitkeep new file mode 100644 index 00000000..e69de29b