From 28981d9c572d28e319cc675bee0b440f291265b9 Mon Sep 17 00:00:00 2001 From: Guri999 <116724657+Guri999@users.noreply.github.com> Date: Tue, 7 Jan 2025 20:14:49 +0900 Subject: [PATCH 01/11] Refactor: TopBarState onBodyPress -> show --- .../src/main/java/kr/co/pdf/PdfViewModel.kt | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt diff --git a/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt b/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt new file mode 100644 index 0000000..a21ea98 --- /dev/null +++ b/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt @@ -0,0 +1,70 @@ +package kr.co.pdf + +import android.graphics.pdf.PdfRenderer +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.update +import kotlinx.coroutines.launch +import kr.co.pdf.model.UiIntent +import kr.co.pdf.model.UiState +import kr.co.util.PdfToBitmap + +internal class PdfViewModel : ViewModel() { + + private val _uiState = MutableStateFlow(UiState(viewModelScope)) + val uiState: StateFlow = _uiState.asStateFlow() + + private var pdfToBitmap: PdfToBitmap? = null + + fun handleIntent(intent: UiIntent) { + when (intent) { + is UiIntent.Init -> init(intent.renderer) + is UiIntent.ShowTopBar -> showTopBar() + is UiIntent.ChangePage -> changePage(intent.page) + is UiIntent.RenderPage -> renderPage(intent.page) + } + } + + private fun init(renderer: PdfRenderer) { + pdfToBitmap = PdfToBitmap(renderer) + + _uiState.update { + it.copy( + totalPage = renderer.pageCount + ) + } + } + + private fun showTopBar() { + _uiState.value.topBarState.show() + } + + private fun changePage(page: Int) { + _uiState.update { + it.copy(currentPage = page) + } + } + + private fun renderPage(page: Int) { + _uiState.update { + it.copy(isLoading = true) + } + + viewModelScope.launch { + pdfToBitmap?.renderPage(page) + + pdfToBitmap?.bitmap?.collect { bitmaps -> + _uiState.update { + it.copy(bitmaps = bitmaps) + } + } + + _uiState.update { + it.copy(isLoading = false) + } + } + } +} \ No newline at end of file From dd3db459f6661cece4e7acb76edcbd2fc2fcc0c4 Mon Sep 17 00:00:00 2001 From: Guri999 <116724657+Guri999@users.noreply.github.com> Date: Tue, 7 Jan 2025 20:40:00 +0900 Subject: [PATCH 02/11] =?UTF-8?q?Chore:=20=EC=95=88=EC=93=B0=EB=8A=94=20?= =?UTF-8?q?=EB=9E=8C=EB=8B=A4=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/kr/co/main/navigation/MainNavHost.kt | 4 +- .../main/java/kr/co/navigation/PdfNavGraph.kt | 2 - .../pdf/src/main/java/kr/co/pdf/PdfScreen.kt | 68 +++++++++---------- 3 files changed, 34 insertions(+), 40 deletions(-) diff --git a/feature/main/src/main/java/kr/co/main/navigation/MainNavHost.kt b/feature/main/src/main/java/kr/co/main/navigation/MainNavHost.kt index 7595c5a..46cd8f7 100644 --- a/feature/main/src/main/java/kr/co/main/navigation/MainNavHost.kt +++ b/feature/main/src/main/java/kr/co/main/navigation/MainNavHost.kt @@ -33,8 +33,6 @@ internal fun MainNavHost( padding = padding ) - pdfNavGraph( - popBackStack = navigator::popBackStack - ) + pdfNavGraph() } } diff --git a/feature/pdf/src/main/java/kr/co/navigation/PdfNavGraph.kt b/feature/pdf/src/main/java/kr/co/navigation/PdfNavGraph.kt index e8cd185..efe17f7 100644 --- a/feature/pdf/src/main/java/kr/co/navigation/PdfNavGraph.kt +++ b/feature/pdf/src/main/java/kr/co/navigation/PdfNavGraph.kt @@ -6,12 +6,10 @@ import androidx.navigation.toRoute import kr.co.pdf.PdfRoute fun NavGraphBuilder.pdfNavGraph( - popBackStack: () -> Unit = {} ) { composable { PdfRoute( path = it.toRoute().path, - popBackStack = popBackStack ) } } \ No newline at end of file diff --git a/feature/pdf/src/main/java/kr/co/pdf/PdfScreen.kt b/feature/pdf/src/main/java/kr/co/pdf/PdfScreen.kt index eebeccc..7f475d2 100644 --- a/feature/pdf/src/main/java/kr/co/pdf/PdfScreen.kt +++ b/feature/pdf/src/main/java/kr/co/pdf/PdfScreen.kt @@ -28,6 +28,7 @@ import androidx.compose.material3.Text import androidx.compose.material3.TopAppBar import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope @@ -43,65 +44,59 @@ import androidx.compose.ui.unit.dp import androidx.core.text.isDigitsOnly import androidx.lifecycle.compose.collectAsStateWithLifecycle import kotlinx.coroutines.launch +import kr.co.pdf.model.UiIntent +import kr.co.pdf.model.UiState import kr.co.ui.theme.SeeDocsTheme import kr.co.ui.theme.Theme -import kr.co.ui.util.rememberTopBarState import kr.co.ui.widget.SimpleTextField import kr.co.ui.widget.TextFieldInputType -import kr.co.util.PdfToBitmap -import kr.co.util.rememberPdfState import net.engawapg.lib.zoomable.rememberZoomState import net.engawapg.lib.zoomable.zoomable +import org.koin.androidx.compose.koinViewModel import java.io.File @Composable internal fun PdfRoute( path: String, - popBackStack: () -> Unit = {}, + viewModel: PdfViewModel = koinViewModel(), ) { + val uiState by viewModel.uiState.collectAsStateWithLifecycle() + val uri = remember { Uri.fromFile(File(path)) } val context = LocalContext.current val scope = rememberCoroutineScope() - val renderer = remember(uri) { + LaunchedEffect(path) { context.contentResolver.openFileDescriptor( uri, "r" - )?.let { PdfRenderer(it) } + )?.let { viewModel.handleIntent(UiIntent.Init(PdfRenderer(it))) } } - val tabBarState = rememberTopBarState() - val listState = rememberLazyListState() - renderer?.let { - PdfScreen( - renderer = it, - listState = listState, - isTopBarVisible = tabBarState.topBarVisible, - onPdfBodyPressed = tabBarState::show, - onPageIndexChange = { page -> - scope.launch { - listState.scrollToItem(page - 1) - } - }, - ) - } + PdfScreen( + uiState = uiState, + listState = listState, + handleIntent = viewModel::handleIntent, + onPageIndexChange = { page -> + scope.launch { + listState.scrollToItem(page - 1) + } + }, + ) + } @Composable private fun PdfScreen( - renderer: PdfRenderer, + uiState: UiState = UiState(), listState: LazyListState = rememberLazyListState(), - pdfState: PdfToBitmap = rememberPdfState(renderer), - isTopBarVisible: Boolean = false, - onPdfBodyPressed: () -> Unit, + handleIntent: (UiIntent) -> Unit = {}, onPageIndexChange: (Int) -> Unit = {}, ) { - val bitmaps = pdfState.bitmap.collectAsStateWithLifecycle() - Box( modifier = Modifier .fillMaxSize() @@ -114,29 +109,29 @@ private fun PdfScreen( .pointerInput(null) { detectTapGestures( onTap = { - onPdfBodyPressed() + handleIntent(UiIntent.ShowTopBar) }, onPress = { - onPdfBodyPressed() + handleIntent(UiIntent.ShowTopBar) } ) }, state = listState, verticalArrangement = Arrangement.spacedBy(4.dp) ) { - items(renderer.pageCount) { page -> + items(uiState.totalPage) { page -> LaunchedEffect(page) { - pdfState.renderPage(page) + handleIntent(UiIntent.RenderPage(page)) } PdfImage( - bitmap = bitmaps.value[page], + bitmap = uiState.bitmaps[page], ) } } AnimatedVisibility( - visible = isTopBarVisible, + visible = uiState.topBarState.topBarVisible, enter = slideIn { IntOffset(0, -it.height) }, @@ -146,8 +141,11 @@ private fun PdfScreen( ) { PdfTopBar( currentPage = listState.firstVisibleItemIndex + 1, - totalPage = renderer.pageCount, - onPageIndexChange = onPageIndexChange + totalPage = uiState.totalPage, + onPageIndexChange = { + handleIntent(UiIntent.ChangePage(it)) + onPageIndexChange(it) + } ) } } From 33e0673b345e1737d6b08af1261f4681967c60d3 Mon Sep 17 00:00:00 2001 From: Guri999 <116724657+Guri999@users.noreply.github.com> Date: Tue, 7 Jan 2025 21:21:47 +0900 Subject: [PATCH 03/11] Rebase --- build-logic/convention/build.gradle.kts | 4 --- .../java/SeeDocsFeatureConventionPlugin.kt | 3 ++- .../main/java/SeeDocsHiltConventionPlugin.kt | 26 ------------------- .../java/SeeDocsLibraryConventionPlugin.kt | 4 ++- .../kr/co/convention/ProjectExtensions.kt | 5 +++- .../src/main/java/kr/co/pdf/model/UiIntent.kt | 10 +++++++ .../src/main/java/kr/co/pdf/model/UiState.kt | 15 +++++++++++ 7 files changed, 34 insertions(+), 33 deletions(-) delete mode 100644 build-logic/convention/src/main/java/SeeDocsHiltConventionPlugin.kt create mode 100644 feature/pdf/src/main/java/kr/co/pdf/model/UiIntent.kt create mode 100644 feature/pdf/src/main/java/kr/co/pdf/model/UiState.kt diff --git a/build-logic/convention/build.gradle.kts b/build-logic/convention/build.gradle.kts index 6893d7e..a590793 100644 --- a/build-logic/convention/build.gradle.kts +++ b/build-logic/convention/build.gradle.kts @@ -54,9 +54,5 @@ gradlePlugin { id = "seedocs.feature" implementationClass = "SeeDocsFeatureConventionPlugin" } - register("SeeDocsHilt") { - id = "seedocs.hilt" - implementationClass = "SeeDocsHiltConventionPlugin" - } } } \ No newline at end of file diff --git a/build-logic/convention/src/main/java/SeeDocsFeatureConventionPlugin.kt b/build-logic/convention/src/main/java/SeeDocsFeatureConventionPlugin.kt index e1c7246..d98a9e1 100644 --- a/build-logic/convention/src/main/java/SeeDocsFeatureConventionPlugin.kt +++ b/build-logic/convention/src/main/java/SeeDocsFeatureConventionPlugin.kt @@ -1,4 +1,5 @@ import kr.co.convention.implementations +import kr.co.convention.libs import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.kotlin.dsl.dependencies @@ -9,7 +10,6 @@ class SeeDocsFeatureConventionPlugin : Plugin { with(target) { pluginManager.apply { apply("seedocs.library") - apply("seedocs.hilt") } dependencies { @@ -19,6 +19,7 @@ class SeeDocsFeatureConventionPlugin : Plugin { project(":core:data"), project(":core:navigation"), project(":core:model"), + libs.koin.compose ) } } diff --git a/build-logic/convention/src/main/java/SeeDocsHiltConventionPlugin.kt b/build-logic/convention/src/main/java/SeeDocsHiltConventionPlugin.kt deleted file mode 100644 index 2cf4565..0000000 --- a/build-logic/convention/src/main/java/SeeDocsHiltConventionPlugin.kt +++ /dev/null @@ -1,26 +0,0 @@ -import kr.co.convention.implementations -import kr.co.convention.ksp -import kr.co.convention.libs -import org.gradle.api.Plugin -import org.gradle.api.Project -import org.gradle.kotlin.dsl.dependencies - -class SeeDocsHiltConventionPlugin : Plugin { - override fun apply(target: Project) { - with(target){ - with(pluginManager){ - apply("com.google.devtools.ksp") - apply("dagger.hilt.android.plugin") - } - - dependencies { - implementations( - libs.hilt.android - ) - ksp( - libs.hilt.compiler - ) - } - } - } -} \ No newline at end of file diff --git a/build-logic/convention/src/main/java/SeeDocsLibraryConventionPlugin.kt b/build-logic/convention/src/main/java/SeeDocsLibraryConventionPlugin.kt index 4239fe3..bd70823 100644 --- a/build-logic/convention/src/main/java/SeeDocsLibraryConventionPlugin.kt +++ b/build-logic/convention/src/main/java/SeeDocsLibraryConventionPlugin.kt @@ -18,6 +18,7 @@ class SeeDocsLibraryConventionPlugin : Plugin { with(pluginManager) { apply("com.android.library") apply("org.jetbrains.kotlin.android") + apply("com.google.devtools.ksp") } extensions.configure { @@ -31,7 +32,8 @@ class SeeDocsLibraryConventionPlugin : Plugin { dependencies { implementations( - libs.timber + libs.timber, + libs.koin.core ) testImplementations( kotlin("test") diff --git a/build-logic/convention/src/main/java/kr/co/convention/ProjectExtensions.kt b/build-logic/convention/src/main/java/kr/co/convention/ProjectExtensions.kt index 6c5405b..574d8fd 100644 --- a/build-logic/convention/src/main/java/kr/co/convention/ProjectExtensions.kt +++ b/build-logic/convention/src/main/java/kr/co/convention/ProjectExtensions.kt @@ -9,6 +9,9 @@ import org.gradle.kotlin.dsl.the internal val Project.libs get() = the() fun Project.App() { + project.pluginManager.apply { + apply("com.google.devtools.ksp") + } project.dependencies { implementations( libs.androidx.core.splashscreen, @@ -22,7 +25,7 @@ fun Project.App() { libs.compose.activity, libs.compose.lifecycle.runtime, libs.compose.navigation, - libs.compose.hilt.navigation, + libs.koin.compose, ) testImplementations( diff --git a/feature/pdf/src/main/java/kr/co/pdf/model/UiIntent.kt b/feature/pdf/src/main/java/kr/co/pdf/model/UiIntent.kt new file mode 100644 index 0000000..035b335 --- /dev/null +++ b/feature/pdf/src/main/java/kr/co/pdf/model/UiIntent.kt @@ -0,0 +1,10 @@ +package kr.co.pdf.model + +import android.graphics.pdf.PdfRenderer + +internal sealed class UiIntent { + data class Init(val renderer: PdfRenderer) : UiIntent() + data object ShowTopBar : UiIntent() + data class ChangePage(val page: Int) : UiIntent() + data class RenderPage(val page: Int) : UiIntent() +} \ No newline at end of file diff --git a/feature/pdf/src/main/java/kr/co/pdf/model/UiState.kt b/feature/pdf/src/main/java/kr/co/pdf/model/UiState.kt new file mode 100644 index 0000000..960dc5a --- /dev/null +++ b/feature/pdf/src/main/java/kr/co/pdf/model/UiState.kt @@ -0,0 +1,15 @@ +package kr.co.pdf.model + +import android.graphics.Bitmap +import kotlinx.coroutines.CoroutineScope +import kr.co.ui.util.TopBarState +import kotlin.coroutines.EmptyCoroutineContext + +internal data class UiState( + val scope: CoroutineScope = CoroutineScope(EmptyCoroutineContext), + val bitmaps: Map = emptyMap(), + val topBarState: TopBarState = TopBarState.create(scope), + val currentPage: Int = 1, + val totalPage: Int = 1, + val isLoading: Boolean = false +) \ No newline at end of file From 414e37d285ee7ced4b5d1c975cbd23bc903aa8d6 Mon Sep 17 00:00:00 2001 From: Guri999 <116724657+Guri999@users.noreply.github.com> Date: Tue, 7 Jan 2025 20:10:14 +0900 Subject: [PATCH 04/11] =?UTF-8?q?Feat:=20Koin=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 91e5fa541e0ae5b304f1884599eaf707a5d0679f) --- app/src/main/AndroidManifest.xml | 1 + app/src/main/java/kr/co/seedocs/SeeDocs.kt | 20 +++++++++++++--- build.gradle.kts | 1 - core/data/build.gradle.kts | 1 - core/database/build.gradle.kts | 1 - .../java/kr/co/explore/ExploreViewModel.kt | 5 ---- .../src/main/java/kr/co/main/di/MainModule.kt | 11 +++++++++ .../pdf/src/main/java/kr/co/di/PdfModule.kt | 10 ++++++++ gradle/libs.versions.toml | 23 ++++++++----------- 9 files changed, 49 insertions(+), 24 deletions(-) delete mode 100644 feature/explore/src/main/java/kr/co/explore/ExploreViewModel.kt create mode 100644 feature/main/src/main/java/kr/co/main/di/MainModule.kt create mode 100644 feature/pdf/src/main/java/kr/co/di/PdfModule.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 027b28a..65fe9fe 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,6 +7,7 @@ android:minSdkVersion="30" /> Date: Tue, 7 Jan 2025 23:08:02 +0900 Subject: [PATCH 05/11] =?UTF-8?q?Fix:=20currentPage=20TopBar=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- feature/pdf/src/main/java/kr/co/pdf/PdfScreen.kt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/feature/pdf/src/main/java/kr/co/pdf/PdfScreen.kt b/feature/pdf/src/main/java/kr/co/pdf/PdfScreen.kt index 7f475d2..6936b03 100644 --- a/feature/pdf/src/main/java/kr/co/pdf/PdfScreen.kt +++ b/feature/pdf/src/main/java/kr/co/pdf/PdfScreen.kt @@ -32,6 +32,7 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.snapshotFlow import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.asImageBitmap @@ -77,6 +78,11 @@ internal fun PdfRoute( val listState = rememberLazyListState() + LaunchedEffect(listState) { + snapshotFlow { listState.firstVisibleItemIndex } + .collect { viewModel.handleIntent(UiIntent.ChangePage(it + 1)) } + } + PdfScreen( uiState = uiState, listState = listState, @@ -140,7 +146,7 @@ private fun PdfScreen( } ) { PdfTopBar( - currentPage = listState.firstVisibleItemIndex + 1, + currentPage = uiState.currentPage, totalPage = uiState.totalPage, onPageIndexChange = { handleIntent(UiIntent.ChangePage(it)) From 6ef782b215185a5ad01ed6cf4c83d2da02305b3f Mon Sep 17 00:00:00 2001 From: Guri999 <116724657+Guri999@users.noreply.github.com> Date: Wed, 8 Jan 2025 14:41:37 +0900 Subject: [PATCH 06/11] =?UTF-8?q?Refactor:=20ui=20=EC=83=81=ED=83=9C=20sco?= =?UTF-8?q?pe=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/src/main/java/kr/co/ui/util/TopBarState.kt | 3 ++- feature/pdf/src/main/java/kr/co/pdf/PdfScreen.kt | 14 ++++++++++++-- .../pdf/src/main/java/kr/co/pdf/PdfViewModel.kt | 15 ++++++++++----- .../pdf/src/main/java/kr/co/pdf/model/UiIntent.kt | 7 ++++++- .../pdf/src/main/java/kr/co/pdf/model/UiState.kt | 3 +-- 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/core/ui/src/main/java/kr/co/ui/util/TopBarState.kt b/core/ui/src/main/java/kr/co/ui/util/TopBarState.kt index 98b232e..ab177c5 100644 --- a/core/ui/src/main/java/kr/co/ui/util/TopBarState.kt +++ b/core/ui/src/main/java/kr/co/ui/util/TopBarState.kt @@ -7,6 +7,7 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.MainCoroutineDispatcher import kotlinx.coroutines.delay @@ -43,6 +44,6 @@ class TopBarState private constructor( @Composable fun rememberTopBarState( - scope: CoroutineScope = rememberCoroutineScope() + scope: CoroutineScope = rememberCoroutineScope{Dispatchers.Main.immediate} ) : TopBarState = remember { TopBarState.create(scope) } \ No newline at end of file diff --git a/feature/pdf/src/main/java/kr/co/pdf/PdfScreen.kt b/feature/pdf/src/main/java/kr/co/pdf/PdfScreen.kt index 6936b03..57b9933 100644 --- a/feature/pdf/src/main/java/kr/co/pdf/PdfScreen.kt +++ b/feature/pdf/src/main/java/kr/co/pdf/PdfScreen.kt @@ -49,6 +49,7 @@ import kr.co.pdf.model.UiIntent import kr.co.pdf.model.UiState import kr.co.ui.theme.SeeDocsTheme import kr.co.ui.theme.Theme +import kr.co.ui.util.rememberTopBarState import kr.co.ui.widget.SimpleTextField import kr.co.ui.widget.TextFieldInputType import net.engawapg.lib.zoomable.rememberZoomState @@ -69,11 +70,20 @@ internal fun PdfRoute( val scope = rememberCoroutineScope() + val topBarState = rememberTopBarState() + LaunchedEffect(path) { context.contentResolver.openFileDescriptor( uri, "r" - )?.let { viewModel.handleIntent(UiIntent.Init(PdfRenderer(it))) } + )?.let { + viewModel.handleIntent( + UiIntent.Init( + PdfRenderer(it), + topBarState + ) + ) + } } val listState = rememberLazyListState() @@ -137,7 +147,7 @@ private fun PdfScreen( } AnimatedVisibility( - visible = uiState.topBarState.topBarVisible, + visible = uiState.topBarState?.topBarVisible?: false, enter = slideIn { IntOffset(0, -it.height) }, diff --git a/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt b/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt index a21ea98..95f6f8e 100644 --- a/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt +++ b/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt @@ -10,36 +10,41 @@ import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import kr.co.pdf.model.UiIntent import kr.co.pdf.model.UiState +import kr.co.ui.util.TopBarState import kr.co.util.PdfToBitmap internal class PdfViewModel : ViewModel() { - private val _uiState = MutableStateFlow(UiState(viewModelScope)) + private val _uiState = MutableStateFlow(UiState()) val uiState: StateFlow = _uiState.asStateFlow() private var pdfToBitmap: PdfToBitmap? = null fun handleIntent(intent: UiIntent) { when (intent) { - is UiIntent.Init -> init(intent.renderer) + is UiIntent.Init -> init(intent.renderer, intent.topBarState) is UiIntent.ShowTopBar -> showTopBar() is UiIntent.ChangePage -> changePage(intent.page) is UiIntent.RenderPage -> renderPage(intent.page) } } - private fun init(renderer: PdfRenderer) { + private fun init( + renderer: PdfRenderer, + topBarState: TopBarState, + ) { pdfToBitmap = PdfToBitmap(renderer) _uiState.update { it.copy( - totalPage = renderer.pageCount + totalPage = renderer.pageCount, + topBarState = topBarState ) } } private fun showTopBar() { - _uiState.value.topBarState.show() + _uiState.value.topBarState?.show() } private fun changePage(page: Int) { diff --git a/feature/pdf/src/main/java/kr/co/pdf/model/UiIntent.kt b/feature/pdf/src/main/java/kr/co/pdf/model/UiIntent.kt index 035b335..77e73ab 100644 --- a/feature/pdf/src/main/java/kr/co/pdf/model/UiIntent.kt +++ b/feature/pdf/src/main/java/kr/co/pdf/model/UiIntent.kt @@ -1,9 +1,14 @@ package kr.co.pdf.model import android.graphics.pdf.PdfRenderer +import kr.co.ui.util.TopBarState internal sealed class UiIntent { - data class Init(val renderer: PdfRenderer) : UiIntent() + data class Init( + val renderer: PdfRenderer, + val topBarState: TopBarState + ) : UiIntent() + data object ShowTopBar : UiIntent() data class ChangePage(val page: Int) : UiIntent() data class RenderPage(val page: Int) : UiIntent() diff --git a/feature/pdf/src/main/java/kr/co/pdf/model/UiState.kt b/feature/pdf/src/main/java/kr/co/pdf/model/UiState.kt index 960dc5a..9c69efd 100644 --- a/feature/pdf/src/main/java/kr/co/pdf/model/UiState.kt +++ b/feature/pdf/src/main/java/kr/co/pdf/model/UiState.kt @@ -6,9 +6,8 @@ import kr.co.ui.util.TopBarState import kotlin.coroutines.EmptyCoroutineContext internal data class UiState( - val scope: CoroutineScope = CoroutineScope(EmptyCoroutineContext), val bitmaps: Map = emptyMap(), - val topBarState: TopBarState = TopBarState.create(scope), + val topBarState: TopBarState? = null, val currentPage: Int = 1, val totalPage: Int = 1, val isLoading: Boolean = false From 50ce34b3b0f942bcb060fe59b4a9bcff65edb1e2 Mon Sep 17 00:00:00 2001 From: Guri999 <116724657+Guri999@users.noreply.github.com> Date: Wed, 8 Jan 2025 14:49:37 +0900 Subject: [PATCH 07/11] =?UTF-8?q?Chore:=20UiIntent=20UiState=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pdf/src/main/java/kr/co/pdf/PdfScreen.kt | 20 +++++++++---------- .../src/main/java/kr/co/pdf/PdfViewModel.kt | 18 ++++++++--------- .../main/java/kr/co/pdf/model/PdfUiIntent.kt | 15 ++++++++++++++ .../pdf/model/{UiState.kt => PdfUiState.kt} | 4 +--- .../src/main/java/kr/co/pdf/model/UiIntent.kt | 15 -------------- 5 files changed, 35 insertions(+), 37 deletions(-) create mode 100644 feature/pdf/src/main/java/kr/co/pdf/model/PdfUiIntent.kt rename feature/pdf/src/main/java/kr/co/pdf/model/{UiState.kt => PdfUiState.kt} (70%) delete mode 100644 feature/pdf/src/main/java/kr/co/pdf/model/UiIntent.kt diff --git a/feature/pdf/src/main/java/kr/co/pdf/PdfScreen.kt b/feature/pdf/src/main/java/kr/co/pdf/PdfScreen.kt index 57b9933..12a6353 100644 --- a/feature/pdf/src/main/java/kr/co/pdf/PdfScreen.kt +++ b/feature/pdf/src/main/java/kr/co/pdf/PdfScreen.kt @@ -45,8 +45,8 @@ import androidx.compose.ui.unit.dp import androidx.core.text.isDigitsOnly import androidx.lifecycle.compose.collectAsStateWithLifecycle import kotlinx.coroutines.launch -import kr.co.pdf.model.UiIntent -import kr.co.pdf.model.UiState +import kr.co.pdf.model.PdfUiIntent +import kr.co.pdf.model.PdfUiState import kr.co.ui.theme.SeeDocsTheme import kr.co.ui.theme.Theme import kr.co.ui.util.rememberTopBarState @@ -78,7 +78,7 @@ internal fun PdfRoute( "r" )?.let { viewModel.handleIntent( - UiIntent.Init( + PdfUiIntent.Init( PdfRenderer(it), topBarState ) @@ -90,7 +90,7 @@ internal fun PdfRoute( LaunchedEffect(listState) { snapshotFlow { listState.firstVisibleItemIndex } - .collect { viewModel.handleIntent(UiIntent.ChangePage(it + 1)) } + .collect { viewModel.handleIntent(PdfUiIntent.ChangePage(it + 1)) } } PdfScreen( @@ -108,9 +108,9 @@ internal fun PdfRoute( @Composable private fun PdfScreen( - uiState: UiState = UiState(), + uiState: PdfUiState = PdfUiState(), listState: LazyListState = rememberLazyListState(), - handleIntent: (UiIntent) -> Unit = {}, + handleIntent: (PdfUiIntent) -> Unit = {}, onPageIndexChange: (Int) -> Unit = {}, ) { Box( @@ -125,10 +125,10 @@ private fun PdfScreen( .pointerInput(null) { detectTapGestures( onTap = { - handleIntent(UiIntent.ShowTopBar) + handleIntent(PdfUiIntent.ShowTopBar) }, onPress = { - handleIntent(UiIntent.ShowTopBar) + handleIntent(PdfUiIntent.ShowTopBar) } ) }, @@ -137,7 +137,7 @@ private fun PdfScreen( ) { items(uiState.totalPage) { page -> LaunchedEffect(page) { - handleIntent(UiIntent.RenderPage(page)) + handleIntent(PdfUiIntent.RenderPage(page)) } PdfImage( @@ -159,7 +159,7 @@ private fun PdfScreen( currentPage = uiState.currentPage, totalPage = uiState.totalPage, onPageIndexChange = { - handleIntent(UiIntent.ChangePage(it)) + handleIntent(PdfUiIntent.ChangePage(it)) onPageIndexChange(it) } ) diff --git a/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt b/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt index 95f6f8e..4f7beaa 100644 --- a/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt +++ b/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt @@ -8,24 +8,24 @@ import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch -import kr.co.pdf.model.UiIntent -import kr.co.pdf.model.UiState +import kr.co.pdf.model.PdfUiIntent +import kr.co.pdf.model.PdfUiState import kr.co.ui.util.TopBarState import kr.co.util.PdfToBitmap internal class PdfViewModel : ViewModel() { - private val _uiState = MutableStateFlow(UiState()) - val uiState: StateFlow = _uiState.asStateFlow() + private val _uiState = MutableStateFlow(PdfUiState()) + val uiState: StateFlow = _uiState.asStateFlow() private var pdfToBitmap: PdfToBitmap? = null - fun handleIntent(intent: UiIntent) { + fun handleIntent(intent: PdfUiIntent) { when (intent) { - is UiIntent.Init -> init(intent.renderer, intent.topBarState) - is UiIntent.ShowTopBar -> showTopBar() - is UiIntent.ChangePage -> changePage(intent.page) - is UiIntent.RenderPage -> renderPage(intent.page) + is PdfUiIntent.Init -> init(intent.renderer, intent.topBarState) + is PdfUiIntent.ShowTopBar -> showTopBar() + is PdfUiIntent.ChangePage -> changePage(intent.page) + is PdfUiIntent.RenderPage -> renderPage(intent.page) } } diff --git a/feature/pdf/src/main/java/kr/co/pdf/model/PdfUiIntent.kt b/feature/pdf/src/main/java/kr/co/pdf/model/PdfUiIntent.kt new file mode 100644 index 0000000..6e5f5bc --- /dev/null +++ b/feature/pdf/src/main/java/kr/co/pdf/model/PdfUiIntent.kt @@ -0,0 +1,15 @@ +package kr.co.pdf.model + +import android.graphics.pdf.PdfRenderer +import kr.co.ui.util.TopBarState + +internal sealed class PdfUiIntent { + data class Init( + val renderer: PdfRenderer, + val topBarState: TopBarState + ) : PdfUiIntent() + + data object ShowTopBar : PdfUiIntent() + data class ChangePage(val page: Int) : PdfUiIntent() + data class RenderPage(val page: Int) : PdfUiIntent() +} \ No newline at end of file diff --git a/feature/pdf/src/main/java/kr/co/pdf/model/UiState.kt b/feature/pdf/src/main/java/kr/co/pdf/model/PdfUiState.kt similarity index 70% rename from feature/pdf/src/main/java/kr/co/pdf/model/UiState.kt rename to feature/pdf/src/main/java/kr/co/pdf/model/PdfUiState.kt index 9c69efd..520e241 100644 --- a/feature/pdf/src/main/java/kr/co/pdf/model/UiState.kt +++ b/feature/pdf/src/main/java/kr/co/pdf/model/PdfUiState.kt @@ -1,11 +1,9 @@ package kr.co.pdf.model import android.graphics.Bitmap -import kotlinx.coroutines.CoroutineScope import kr.co.ui.util.TopBarState -import kotlin.coroutines.EmptyCoroutineContext -internal data class UiState( +internal data class PdfUiState( val bitmaps: Map = emptyMap(), val topBarState: TopBarState? = null, val currentPage: Int = 1, diff --git a/feature/pdf/src/main/java/kr/co/pdf/model/UiIntent.kt b/feature/pdf/src/main/java/kr/co/pdf/model/UiIntent.kt deleted file mode 100644 index 77e73ab..0000000 --- a/feature/pdf/src/main/java/kr/co/pdf/model/UiIntent.kt +++ /dev/null @@ -1,15 +0,0 @@ -package kr.co.pdf.model - -import android.graphics.pdf.PdfRenderer -import kr.co.ui.util.TopBarState - -internal sealed class UiIntent { - data class Init( - val renderer: PdfRenderer, - val topBarState: TopBarState - ) : UiIntent() - - data object ShowTopBar : UiIntent() - data class ChangePage(val page: Int) : UiIntent() - data class RenderPage(val page: Int) : UiIntent() -} \ No newline at end of file From a224f7f14163f1ee310a91ac8c5c400276b753e8 Mon Sep 17 00:00:00 2001 From: Guri999 <116724657+Guri999@users.noreply.github.com> Date: Wed, 8 Jan 2025 16:24:01 +0900 Subject: [PATCH 08/11] =?UTF-8?q?Refactor:=20BaseViewModel=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/ui/build.gradle.kts | 8 ++- .../java/kr/co/ui/base/BaseMviViewModel.kt | 49 +++++++++++++++++++ .../src/main/java/kr/co/ui/base/UiIntent.kt | 3 ++ .../ui/src/main/java/kr/co/ui/base/UiState.kt | 3 ++ .../src/main/java/kr/co/pdf/PdfViewModel.kt | 39 ++++++--------- .../main/java/kr/co/pdf/model/PdfUiIntent.kt | 3 +- .../main/java/kr/co/pdf/model/PdfUiState.kt | 3 +- 7 files changed, 81 insertions(+), 27 deletions(-) create mode 100644 core/ui/src/main/java/kr/co/ui/base/BaseMviViewModel.kt create mode 100644 core/ui/src/main/java/kr/co/ui/base/UiIntent.kt create mode 100644 core/ui/src/main/java/kr/co/ui/base/UiState.kt diff --git a/core/ui/build.gradle.kts b/core/ui/build.gradle.kts index 1f742f2..e11b878 100644 --- a/core/ui/build.gradle.kts +++ b/core/ui/build.gradle.kts @@ -5,4 +5,10 @@ plugins { alias(libs.plugins.seedocs.library.compose) } -setNamespace("core.ui") \ No newline at end of file +setNamespace("core.ui") + +android { + buildFeatures { + buildConfig = true + } +} \ No newline at end of file diff --git a/core/ui/src/main/java/kr/co/ui/base/BaseMviViewModel.kt b/core/ui/src/main/java/kr/co/ui/base/BaseMviViewModel.kt new file mode 100644 index 0000000..7f844a2 --- /dev/null +++ b/core/ui/src/main/java/kr/co/ui/base/BaseMviViewModel.kt @@ -0,0 +1,49 @@ +package kr.co.ui.base + +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.flow.update +import kotlinx.coroutines.launch +import kr.co.seedocs.core.ui.BuildConfig +import timber.log.Timber + +abstract class BaseMviViewModel(initialState: S) : ViewModel() { + private val logTag = javaClass.simpleName + + private val _uiState = MutableStateFlow(initialState) + val uiState: StateFlow = _uiState.asStateFlow() + + abstract fun handleIntent(intent: I) + + protected inline fun launch(crossinline block: suspend CoroutineScope.() -> Unit) { + viewModelScope.launch { + block() + } + } + + protected fun update( + block: S.() -> S + ) { + _uiState.update(block) + } +// + protected fun Flow.debugLog(subject: String): Flow = + if (BuildConfig.DEBUG) { + onEach { Timber.tag(logTag).d("$subject: $it") } + } else { + this + } + + protected fun T.debugLog(subject: String): T = + if (BuildConfig.DEBUG) { + also { Timber.tag(logTag).d("$subject: $this") } + } else { + this + } +} \ No newline at end of file diff --git a/core/ui/src/main/java/kr/co/ui/base/UiIntent.kt b/core/ui/src/main/java/kr/co/ui/base/UiIntent.kt new file mode 100644 index 0000000..a304d7c --- /dev/null +++ b/core/ui/src/main/java/kr/co/ui/base/UiIntent.kt @@ -0,0 +1,3 @@ +package kr.co.ui.base + +interface UiIntent \ No newline at end of file diff --git a/core/ui/src/main/java/kr/co/ui/base/UiState.kt b/core/ui/src/main/java/kr/co/ui/base/UiState.kt new file mode 100644 index 0000000..a41c0bf --- /dev/null +++ b/core/ui/src/main/java/kr/co/ui/base/UiState.kt @@ -0,0 +1,3 @@ +package kr.co.ui.base + +interface UiState \ No newline at end of file diff --git a/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt b/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt index 4f7beaa..72070f5 100644 --- a/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt +++ b/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt @@ -1,26 +1,17 @@ package kr.co.pdf import android.graphics.pdf.PdfRenderer -import androidx.lifecycle.ViewModel -import androidx.lifecycle.viewModelScope -import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.flow.StateFlow -import kotlinx.coroutines.flow.asStateFlow -import kotlinx.coroutines.flow.update -import kotlinx.coroutines.launch import kr.co.pdf.model.PdfUiIntent import kr.co.pdf.model.PdfUiState +import kr.co.ui.base.BaseMviViewModel import kr.co.ui.util.TopBarState import kr.co.util.PdfToBitmap -internal class PdfViewModel : ViewModel() { - - private val _uiState = MutableStateFlow(PdfUiState()) - val uiState: StateFlow = _uiState.asStateFlow() +internal class PdfViewModel : BaseMviViewModel(PdfUiState()) { private var pdfToBitmap: PdfToBitmap? = null - fun handleIntent(intent: PdfUiIntent) { + override fun handleIntent(intent: PdfUiIntent) { when (intent) { is PdfUiIntent.Init -> init(intent.renderer, intent.topBarState) is PdfUiIntent.ShowTopBar -> showTopBar() @@ -35,8 +26,8 @@ internal class PdfViewModel : ViewModel() { ) { pdfToBitmap = PdfToBitmap(renderer) - _uiState.update { - it.copy( + update { + copy( totalPage = renderer.pageCount, topBarState = topBarState ) @@ -44,31 +35,31 @@ internal class PdfViewModel : ViewModel() { } private fun showTopBar() { - _uiState.value.topBarState?.show() + uiState.value.topBarState?.show() } private fun changePage(page: Int) { - _uiState.update { - it.copy(currentPage = page) + update { + copy(currentPage = page) } } private fun renderPage(page: Int) { - _uiState.update { - it.copy(isLoading = true) + update { + copy(isLoading = true) } - viewModelScope.launch { + launch { pdfToBitmap?.renderPage(page) pdfToBitmap?.bitmap?.collect { bitmaps -> - _uiState.update { - it.copy(bitmaps = bitmaps) + update { + copy(bitmaps = bitmaps) } } - _uiState.update { - it.copy(isLoading = false) + update { + copy(isLoading = false) } } } diff --git a/feature/pdf/src/main/java/kr/co/pdf/model/PdfUiIntent.kt b/feature/pdf/src/main/java/kr/co/pdf/model/PdfUiIntent.kt index 6e5f5bc..13a5f5e 100644 --- a/feature/pdf/src/main/java/kr/co/pdf/model/PdfUiIntent.kt +++ b/feature/pdf/src/main/java/kr/co/pdf/model/PdfUiIntent.kt @@ -1,9 +1,10 @@ package kr.co.pdf.model import android.graphics.pdf.PdfRenderer +import kr.co.ui.base.UiIntent import kr.co.ui.util.TopBarState -internal sealed class PdfUiIntent { +internal sealed class PdfUiIntent: UiIntent { data class Init( val renderer: PdfRenderer, val topBarState: TopBarState diff --git a/feature/pdf/src/main/java/kr/co/pdf/model/PdfUiState.kt b/feature/pdf/src/main/java/kr/co/pdf/model/PdfUiState.kt index 520e241..f1038fd 100644 --- a/feature/pdf/src/main/java/kr/co/pdf/model/PdfUiState.kt +++ b/feature/pdf/src/main/java/kr/co/pdf/model/PdfUiState.kt @@ -1,6 +1,7 @@ package kr.co.pdf.model import android.graphics.Bitmap +import kr.co.ui.base.UiState import kr.co.ui.util.TopBarState internal data class PdfUiState( @@ -9,4 +10,4 @@ internal data class PdfUiState( val currentPage: Int = 1, val totalPage: Int = 1, val isLoading: Boolean = false -) \ No newline at end of file +): UiState \ No newline at end of file From 9951db83bae76962913011fcfdd2cbf423dc2435 Mon Sep 17 00:00:00 2001 From: Guri999 <116724657+Guri999@users.noreply.github.com> Date: Wed, 8 Jan 2025 19:20:27 +0900 Subject: [PATCH 09/11] =?UTF-8?q?Refactor:=20Job=20=EC=A2=85=EB=A3=8C=20?= =?UTF-8?q?=ED=9B=84=20=EC=9E=91=EC=97=85=20invokeOnCompletion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/ui/src/main/java/kr/co/ui/base/BaseMviViewModel.kt | 6 +++--- feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/ui/src/main/java/kr/co/ui/base/BaseMviViewModel.kt b/core/ui/src/main/java/kr/co/ui/base/BaseMviViewModel.kt index 7f844a2..e22f2d4 100644 --- a/core/ui/src/main/java/kr/co/ui/base/BaseMviViewModel.kt +++ b/core/ui/src/main/java/kr/co/ui/base/BaseMviViewModel.kt @@ -3,6 +3,7 @@ package kr.co.ui.base import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Job import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow @@ -21,18 +22,17 @@ abstract class BaseMviViewModel(initialState: S) : Vi abstract fun handleIntent(intent: I) - protected inline fun launch(crossinline block: suspend CoroutineScope.() -> Unit) { + protected inline fun launch(crossinline block: suspend CoroutineScope.() -> Unit): Job = viewModelScope.launch { block() } - } protected fun update( block: S.() -> S ) { _uiState.update(block) } -// + protected fun Flow.debugLog(subject: String): Flow = if (BuildConfig.DEBUG) { onEach { Timber.tag(logTag).d("$subject: $it") } diff --git a/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt b/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt index 72070f5..d15fcb7 100644 --- a/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt +++ b/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt @@ -57,7 +57,7 @@ internal class PdfViewModel : BaseMviViewModel(PdfUiSta copy(bitmaps = bitmaps) } } - + }.invokeOnCompletion { update { copy(isLoading = false) } From 07e9fe98f7c5e271a7a0226c67153d6010021612 Mon Sep 17 00:00:00 2001 From: Guri999 <116724657+Guri999@users.noreply.github.com> Date: Wed, 8 Jan 2025 19:20:47 +0900 Subject: [PATCH 10/11] Refactor: update -> reduce --- .../ui/src/main/java/kr/co/ui/base/BaseMviViewModel.kt | 2 +- feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/core/ui/src/main/java/kr/co/ui/base/BaseMviViewModel.kt b/core/ui/src/main/java/kr/co/ui/base/BaseMviViewModel.kt index e22f2d4..13f9849 100644 --- a/core/ui/src/main/java/kr/co/ui/base/BaseMviViewModel.kt +++ b/core/ui/src/main/java/kr/co/ui/base/BaseMviViewModel.kt @@ -27,7 +27,7 @@ abstract class BaseMviViewModel(initialState: S) : Vi block() } - protected fun update( + protected fun reduce( block: S.() -> S ) { _uiState.update(block) diff --git a/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt b/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt index d15fcb7..fea827b 100644 --- a/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt +++ b/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt @@ -26,7 +26,7 @@ internal class PdfViewModel : BaseMviViewModel(PdfUiSta ) { pdfToBitmap = PdfToBitmap(renderer) - update { + reduce { copy( totalPage = renderer.pageCount, topBarState = topBarState @@ -39,13 +39,13 @@ internal class PdfViewModel : BaseMviViewModel(PdfUiSta } private fun changePage(page: Int) { - update { + reduce { copy(currentPage = page) } } private fun renderPage(page: Int) { - update { + reduce { copy(isLoading = true) } @@ -53,12 +53,12 @@ internal class PdfViewModel : BaseMviViewModel(PdfUiSta pdfToBitmap?.renderPage(page) pdfToBitmap?.bitmap?.collect { bitmaps -> - update { + reduce { copy(bitmaps = bitmaps) } } }.invokeOnCompletion { - update { + reduce { copy(isLoading = false) } } From 5d7ee0f87aa4e0023c018aabb2e114c1de542b7d Mon Sep 17 00:00:00 2001 From: Guri999 <116724657+Guri999@users.noreply.github.com> Date: Wed, 8 Jan 2025 19:37:43 +0900 Subject: [PATCH 11/11] =?UTF-8?q?Refactor:=20bitmap=20collect=20=EC=9C=84?= =?UTF-8?q?=EC=B9=98=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pdf/src/main/java/kr/co/pdf/PdfViewModel.kt | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt b/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt index fea827b..c8451da 100644 --- a/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt +++ b/feature/pdf/src/main/java/kr/co/pdf/PdfViewModel.kt @@ -1,6 +1,7 @@ package kr.co.pdf import android.graphics.pdf.PdfRenderer +import kotlinx.coroutines.flow.collectLatest import kr.co.pdf.model.PdfUiIntent import kr.co.pdf.model.PdfUiState import kr.co.ui.base.BaseMviViewModel @@ -32,6 +33,14 @@ internal class PdfViewModel : BaseMviViewModel(PdfUiSta topBarState = topBarState ) } + + launch { + pdfToBitmap?.bitmap?.collectLatest { bitmaps -> + reduce { + copy(bitmaps = bitmaps) + } + } + } } private fun showTopBar() { @@ -51,12 +60,6 @@ internal class PdfViewModel : BaseMviViewModel(PdfUiSta launch { pdfToBitmap?.renderPage(page) - - pdfToBitmap?.bitmap?.collect { bitmaps -> - reduce { - copy(bitmaps = bitmaps) - } - } }.invokeOnCompletion { reduce { copy(isLoading = false)