From d31276ef815a7e055717ea43a30a1639ebd9c13a Mon Sep 17 00:00:00 2001 From: brreynie Date: Mon, 8 May 2023 21:21:14 +0200 Subject: [PATCH] refactor Feed --- .../sel/studeez/navigation/StudeezNavGraph.kt | 4 +- .../be/ugent/sel/studeez/screens/home/Feed.kt | 43 +++++++++++-------- .../sel/studeez/screens/home/FeedViewModel.kt | 2 +- .../sel/studeez/screens/home/HomeScreen.kt | 17 ++++---- 4 files changed, 38 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/be/ugent/sel/studeez/navigation/StudeezNavGraph.kt b/app/src/main/java/be/ugent/sel/studeez/navigation/StudeezNavGraph.kt index a09846a..bd12413 100644 --- a/app/src/main/java/be/ugent/sel/studeez/navigation/StudeezNavGraph.kt +++ b/app/src/main/java/be/ugent/sel/studeez/navigation/StudeezNavGraph.kt @@ -15,6 +15,7 @@ import be.ugent.sel.studeez.common.composable.navbar.NavigationBarActions import be.ugent.sel.studeez.common.composable.navbar.NavigationBarViewModel import be.ugent.sel.studeez.common.composable.navbar.getNavigationBarActions import be.ugent.sel.studeez.screens.home.HomeRoute +import be.ugent.sel.studeez.screens.home.getFeedActions import be.ugent.sel.studeez.screens.log_in.LoginRoute import be.ugent.sel.studeez.screens.profile.EditProfileRoute import be.ugent.sel.studeez.screens.profile.ProfileRoute @@ -67,7 +68,8 @@ fun StudeezNavGraph( open, viewModel = hiltViewModel(), drawerActions = drawerActions, - navigationBarActions = navigationBarActions + navigationBarActions = navigationBarActions, + feedActions = getFeedActions(hiltViewModel(), open), ) } diff --git a/app/src/main/java/be/ugent/sel/studeez/screens/home/Feed.kt b/app/src/main/java/be/ugent/sel/studeez/screens/home/Feed.kt index af42203..96a9623 100644 --- a/app/src/main/java/be/ugent/sel/studeez/screens/home/Feed.kt +++ b/app/src/main/java/be/ugent/sel/studeez/screens/home/Feed.kt @@ -9,22 +9,40 @@ import androidx.compose.runtime.collectAsState import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp -import androidx.hilt.navigation.compose.hiltViewModel import be.ugent.sel.studeez.common.composable.DateText +import be.ugent.sel.studeez.data.local.models.FeedEntry import be.ugent.sel.studeez.data.local.models.timer_functional.HoursMinutesSeconds +import kotlinx.coroutines.flow.Flow + +data class FeedActions( + val getFeedEntries: () -> Flow>>, + val continueTask: (String, String) -> Unit, +) + +fun getFeedActions( + viewmodel: FeedViewModel, + open: (String) -> Unit, +): FeedActions { + return FeedActions( + getFeedEntries = viewmodel::getFeedEntries, + continueTask = { subjectId, taskId -> + viewmodel.continueTask( + open, + subjectId, + taskId, + ) + }, + ) +} @Composable fun Feed( - open: (String) -> Unit, - viewModel: FeedViewModel = hiltViewModel() + feedActions: FeedActions, ) { - val feedEntries = viewModel.getFeedEntries().collectAsState(initial = emptyMap()) - + val feedEntries = feedActions.getFeedEntries().collectAsState(initial = emptyMap()) LazyColumn { - items(feedEntries.value.toList()) { (date, feedEntries) -> Row( horizontalArrangement = Arrangement.SpaceBetween, @@ -43,19 +61,10 @@ fun Feed( } feedEntries.forEach { feedEntry -> FeedEntry(feedEntry = feedEntry) { - viewModel.continueWithTask(open, feedEntry.subjectId, feedEntry.taskId) + feedActions.continueTask(feedEntry.subjectId, feedEntry.taskId) } } Spacer(modifier = Modifier.height(20.dp)) } } -} - -@Preview -@Composable -fun FeedPreview() { - Feed( - open = {}, - viewModel = hiltViewModel(), - ) } \ No newline at end of file diff --git a/app/src/main/java/be/ugent/sel/studeez/screens/home/FeedViewModel.kt b/app/src/main/java/be/ugent/sel/studeez/screens/home/FeedViewModel.kt index aca17c8..1f57175 100644 --- a/app/src/main/java/be/ugent/sel/studeez/screens/home/FeedViewModel.kt +++ b/app/src/main/java/be/ugent/sel/studeez/screens/home/FeedViewModel.kt @@ -27,7 +27,7 @@ class FeedViewModel @Inject constructor( return entries } - fun continueWithTask(open: (String) -> Unit, subjectId: String, taskId: String) { + fun continueTask(open: (String) -> Unit, subjectId: String, taskId: String) { viewModelScope.launch { val task = taskDAO.getTask(subjectId, taskId) selectedTask.set(task) diff --git a/app/src/main/java/be/ugent/sel/studeez/screens/home/HomeScreen.kt b/app/src/main/java/be/ugent/sel/studeez/screens/home/HomeScreen.kt index 0911fd9..79f03e3 100644 --- a/app/src/main/java/be/ugent/sel/studeez/screens/home/HomeScreen.kt +++ b/app/src/main/java/be/ugent/sel/studeez/screens/home/HomeScreen.kt @@ -5,15 +5,13 @@ import androidx.compose.material.IconButton import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Person import androidx.compose.runtime.Composable -import androidx.compose.ui.Modifier import androidx.compose.ui.tooling.preview.Preview import be.ugent.sel.studeez.R -import be.ugent.sel.studeez.common.composable.BasicButton import be.ugent.sel.studeez.common.composable.PrimaryScreenTemplate import be.ugent.sel.studeez.common.composable.drawer.DrawerActions import be.ugent.sel.studeez.common.composable.navbar.NavigationBarActions -import be.ugent.sel.studeez.common.ext.basicButton import be.ugent.sel.studeez.resources +import kotlinx.coroutines.flow.flowOf @Composable fun HomeRoute( @@ -21,21 +19,22 @@ fun HomeRoute( viewModel: HomeViewModel, drawerActions: DrawerActions, navigationBarActions: NavigationBarActions, + feedActions: FeedActions, ) { HomeScreen( - onStartSessionClick = { viewModel.onStartSessionClick(open) }, drawerActions = drawerActions, open = open, navigationBarActions = navigationBarActions, + feedActions = feedActions, ) } @Composable fun HomeScreen( - onStartSessionClick: () -> Unit, open: (String) -> Unit, drawerActions: DrawerActions, - navigationBarActions: NavigationBarActions + navigationBarActions: NavigationBarActions, + feedActions: FeedActions, ) { PrimaryScreenTemplate( title = resources().getString(R.string.home), @@ -43,7 +42,7 @@ fun HomeScreen( navigationBarActions = navigationBarActions, // TODO barAction = { FriendsAction() } ) { - Feed(open) + Feed(feedActions) } } @@ -61,9 +60,9 @@ fun FriendsAction() { @Composable fun HomeScreenPreview() { HomeScreen( - onStartSessionClick = {}, drawerActions = DrawerActions({}, {}, {}, {}, {}), navigationBarActions = NavigationBarActions({ false }, {}, {}, {}, {}, {}, {}, {}), - open = {} + open = {}, + feedActions = FeedActions({ flowOf() }, { _, _ -> run {} }) ) }