Skip to content

Commit

Permalink
feature: Flow.collectAsStateWithLifecycle() 전면 적용 (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwy3055 authored Aug 17, 2024
1 parent f7687e3 commit 408883a
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ package com.ku_stacks.ku_ring.edit_departments.compose

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -26,13 +20,8 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import com.ku_stacks.ku_ring.designsystem.components.DarkPreview
import com.ku_stacks.ku_ring.designsystem.components.DepartmentWithAddIcon
import com.ku_stacks.ku_ring.designsystem.components.DepartmentWithCheckIcon
import com.ku_stacks.ku_ring.designsystem.components.DepartmentWithDeleteIcon
import com.ku_stacks.ku_ring.designsystem.components.LargeTopAppBar
import com.ku_stacks.ku_ring.designsystem.components.LightPreview
import com.ku_stacks.ku_ring.designsystem.components.SearchTextField
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.ku_stacks.ku_ring.designsystem.components.*
import com.ku_stacks.ku_ring.designsystem.kuringtheme.KuringTheme
import com.ku_stacks.ku_ring.designsystem.kuringtheme.values.Pretendard
import com.ku_stacks.ku_ring.domain.Department
Expand All @@ -46,7 +35,7 @@ internal fun EditDepartmentsScreen(
modifier: Modifier = Modifier,
viewModel: EditDepartmentsViewModel = hiltViewModel(),
) {
val departmentsUiModel by viewModel.departments.collectAsState()
val departmentsUiModel by viewModel.departments.collectAsStateWithLifecycle()

EditDepartmentsScreen(
onClose = onClose,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,7 @@ package com.ku_stacks.ku_ring.edit_subscription.compose
import androidx.compose.animation.Crossfade
import androidx.compose.foundation.ExperimentalFoundationApi
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.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
Expand All @@ -28,7 +19,6 @@ import androidx.compose.material.TabRowDefaults
import androidx.compose.material.TabRowDefaults.tabIndicatorOffset
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -44,6 +34,7 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.ku_stacks.ku_ring.designsystem.components.KuringCallToAction
import com.ku_stacks.ku_ring.designsystem.components.LightAndDarkPreview
import com.ku_stacks.ku_ring.designsystem.components.LightPreview
Expand All @@ -67,7 +58,7 @@ fun EditSubscriptionScreen(
modifier: Modifier = Modifier,
viewModel: EditSubscriptionViewModel = hiltViewModel(),
) {
val uiState by viewModel.uiState.collectAsState()
val uiState by viewModel.uiState.collectAsStateWithLifecycle()

EditSubscriptionScreen(
categories = uiState.categories,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.ku_stacks.ku_ring.main.archive.compose

import android.app.Activity
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -16,6 +14,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.ku_stacks.ku_ring.designsystem.components.KuringCallToAction
import com.ku_stacks.ku_ring.designsystem.components.LightAndDarkPreview
import com.ku_stacks.ku_ring.designsystem.kuringtheme.KuringTheme
Expand All @@ -34,10 +33,10 @@ fun ArchiveScreen(
modifier: Modifier = Modifier,
viewModel: ArchiveViewModel = hiltViewModel(),
) {
val notices by viewModel.savedNotices.collectAsState()
val isSelectModeEnabled by viewModel.isSelectedModeEnabled.collectAsState()
val selectedNoticeIds by viewModel.selectedNoticeIds.collectAsState()
val isAllNoticesSelected by viewModel.isAllNoticesSelected.collectAsState()
val notices by viewModel.savedNotices.collectAsStateWithLifecycle()
val isSelectModeEnabled by viewModel.isSelectedModeEnabled.collectAsStateWithLifecycle()
val selectedNoticeIds by viewModel.selectedNoticeIds.collectAsStateWithLifecycle()
val isAllNoticesSelected by viewModel.isAllNoticesSelected.collectAsStateWithLifecycle()
var isDeleteDialogVisible by rememberSaveable { mutableStateOf(false) }
val navigator = LocalNavigator.current
val context = LocalContext.current.findActivity()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -18,6 +17,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.paging.compose.LazyPagingItems
import androidx.paging.compose.collectAsLazyPagingItems
import com.ku_stacks.ku_ring.designsystem.components.LazyPagingNoticeItemColumn
Expand All @@ -36,7 +36,7 @@ internal fun CategoryNoticeScreen(
viewModel.getNotices(shortCategoryName)
}

val noticesFlow by viewModel.noticesFlow.collectAsState()
val noticesFlow by viewModel.noticesFlow.collectAsStateWithLifecycle()
val notices = noticesFlow?.collectAsLazyPagingItems()

var isRefreshing by remember { mutableStateOf(false) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,14 @@ package com.ku_stacks.ku_ring.main.notice.compose.inner_screen

import androidx.compose.animation.core.tween
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.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Icon
import androidx.compose.material.ModalBottomSheetLayout
import androidx.compose.material.ModalBottomSheetState
import androidx.compose.material.ModalBottomSheetValue
import androidx.compose.material.Text
import androidx.compose.material.*
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.PullRefreshState
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.material.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
Expand All @@ -40,6 +19,7 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.paging.compose.LazyPagingItems
import androidx.paging.compose.collectAsLazyPagingItems
import com.ku_stacks.ku_ring.designsystem.components.KuringCallToAction
Expand All @@ -64,8 +44,8 @@ internal fun DepartmentNoticeScreen(
onNavigateToEditDepartment: () -> Unit,
modifier: Modifier = Modifier,
) {
val selectedDepartments by viewModel.subscribedDepartments.collectAsState()
val noticesFlow by viewModel.currentDepartmentNotice.collectAsState()
val selectedDepartments by viewModel.subscribedDepartments.collectAsStateWithLifecycle()
val noticesFlow by viewModel.currentDepartmentNotice.collectAsStateWithLifecycle()
val notices = noticesFlow?.collectAsLazyPagingItems()

var isRefreshing by remember { mutableStateOf(false) }
Expand All @@ -79,7 +59,7 @@ internal fun DepartmentNoticeScreen(
refreshThreshold = 75.dp,
)

val departmentNoticeScreenState by viewModel.departmentNoticeScreenState.collectAsState()
val departmentNoticeScreenState by viewModel.departmentNoticeScreenState.collectAsStateWithLifecycle()

when (departmentNoticeScreenState) {
DepartmentNoticeScreenState.InitialLoading -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import androidx.activity.compose.BackHandler
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
Expand Down Expand Up @@ -67,7 +67,7 @@ private fun NavGraphBuilder.settingNavGraph(
modifier: Modifier = Modifier,
) {
composable(SettingDestinations.SETTING_SCREEN) {
val isExtNotificationAllowed by viewModel.isExtNotificationAllowed.collectAsState()
val isExtNotificationAllowed by viewModel.isExtNotificationAllowed.collectAsStateWithLifecycle()
SettingScreen(
onNavigateToEditSubscription = navigateToEditSubscription,
isExtNotificationEnabled = isExtNotificationAllowed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@ package com.ku_stacks.ku_ring.onboarding.compose.inner_screen

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -27,6 +20,7 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.ku_stacks.ku_ring.designsystem.components.KuringCallToAction
import com.ku_stacks.ku_ring.designsystem.components.LightAndDarkPreview
import com.ku_stacks.ku_ring.designsystem.kuringtheme.KuringTheme
Expand All @@ -43,7 +37,7 @@ internal fun ConfirmDepartmentScreen(
onCancel: () -> Unit,
modifier: Modifier = Modifier,
) {
val selectedDepartment by viewModel.selectedDepartment.collectAsState()
val selectedDepartment by viewModel.selectedDepartment.collectAsStateWithLifecycle()

ConfirmDepartmentScreen(
selectedDepartment = selectedDepartment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@ package com.ku_stacks.ku_ring.onboarding.compose.inner_screen

import androidx.compose.animation.Crossfade
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -27,6 +21,7 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.ku_stacks.ku_ring.designsystem.components.DepartmentWithAddIcon
import com.ku_stacks.ku_ring.designsystem.components.DepartmentWithCheckIcon
import com.ku_stacks.ku_ring.designsystem.components.KuringCallToAction
Expand All @@ -45,10 +40,10 @@ internal fun SetDepartmentScreen(
onSetDepartmentComplete: () -> Unit,
modifier: Modifier = Modifier,
) {
val query by viewModel.query.collectAsState()
val isInitialSearch by viewModel.isInitialSearch.collectAsState()
val departments by viewModel.departments.collectAsState()
val selectedDepartment by viewModel.selectedDepartment.collectAsState()
val query by viewModel.query.collectAsStateWithLifecycle()
val isInitialSearch by viewModel.isInitialSearch.collectAsStateWithLifecycle()
val departments by viewModel.departments.collectAsStateWithLifecycle()
val selectedDepartment by viewModel.selectedDepartment.collectAsStateWithLifecycle()

SetDepartmentScreen(
query = query,
Expand Down

0 comments on commit 408883a

Please sign in to comment.