refactor Feed
This commit is contained in:
parent
08c779030e
commit
d31276ef81
4 changed files with 38 additions and 28 deletions
|
@ -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),
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Map<String, List<FeedEntry>>>,
|
||||
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(),
|
||||
)
|
||||
}
|
|
@ -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)
|
||||
|
|
|
@ -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 {} })
|
||||
)
|
||||
}
|
||||
|
|
Reference in a new issue