add previews for feed

This commit is contained in:
brreynie 2023-05-08 22:14:17 +02:00
parent 6ba0018765
commit 366f236f98
3 changed files with 80 additions and 41 deletions

View file

@ -16,34 +16,11 @@ import androidx.compose.ui.unit.sp
import be.ugent.sel.studeez.common.composable.DateText 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.FeedEntry
import be.ugent.sel.studeez.data.local.models.timer_functional.HoursMinutesSeconds import be.ugent.sel.studeez.data.local.models.timer_functional.HoursMinutesSeconds
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
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 @Composable
fun Feed( fun Feed(
feedActions: FeedActions,
uiState: FeedUiState, uiState: FeedUiState,
continueTask: (String, String) -> Unit,
) { ) {
when (uiState) { when (uiState) {
FeedUiState.Loading -> { FeedUiState.Loading -> {
@ -58,7 +35,6 @@ fun Feed(
} }
} }
is FeedUiState.Succes -> { is FeedUiState.Succes -> {
// val feedEntries = feedActions.getFeedEntries().collectAsState(initial = emptyMap())
val feedEntries = uiState.feedEntries val feedEntries = uiState.feedEntries
LazyColumn { LazyColumn {
items(feedEntries.toList()) { (date, feedEntries) -> items(feedEntries.toList()) { (date, feedEntries) ->
@ -79,7 +55,7 @@ fun Feed(
} }
feedEntries.forEach { feedEntry -> feedEntries.forEach { feedEntry ->
FeedEntry(feedEntry = feedEntry) { FeedEntry(feedEntry = feedEntry) {
feedActions.continueTask(feedEntry.subjectId, feedEntry.taskId) continueTask(feedEntry.subjectId, feedEntry.taskId)
} }
} }
Spacer(modifier = Modifier.height(20.dp)) Spacer(modifier = Modifier.height(20.dp))
@ -89,11 +65,49 @@ fun Feed(
} }
} }
@Preview
@Composable
fun FeedLoadingPreview() {
Feed(
uiState = FeedUiState.Loading,
continueTask = { _, _ -> run {} },
)
}
@Preview @Preview
@Composable @Composable
fun FeedPreview() { fun FeedPreview() {
Feed( Feed(
feedActions = FeedActions({ flowOf() }, { _, _ -> run {} }), uiState = FeedUiState.Succes(
uiState = FeedUiState.Loading, mapOf(
"08 May 2023" to listOf(
FeedEntry(
argb_color = 0xFFFFD200,
subJectName = "Test Subject",
taskName = "Test Task",
totalStudyTime = 600,
),
FeedEntry(
argb_color = 0xFFFFD200,
subJectName = "Test Subject",
taskName = "Test Task",
totalStudyTime = 20,
),
),
"09 May 2023" to listOf(
FeedEntry(
argb_color = 0xFFFD1200,
subJectName = "Test Subject",
taskName = "Test Task",
),
FeedEntry(
argb_color = 0xFFFFD200,
subJectName = "Test Subject",
taskName = "Test Task",
),
)
)
),
continueTask = { _, _ -> run {} },
) )
} }

View file

@ -21,8 +21,6 @@ class FeedViewModel @Inject constructor(
logService: LogService logService: LogService
) : StudeezViewModel(logService) { ) : StudeezViewModel(logService) {
private val entries: Flow<Map<String, List<FeedEntry>>> = feedDAO.getFeedEntries()
val uiState: StateFlow<FeedUiState> = feedDAO.getFeedEntries() val uiState: StateFlow<FeedUiState> = feedDAO.getFeedEntries()
.map { FeedUiState.Succes(it) } .map { FeedUiState.Succes(it) }
.stateIn( .stateIn(
@ -31,10 +29,6 @@ class FeedViewModel @Inject constructor(
started = SharingStarted.Eagerly, started = SharingStarted.Eagerly,
) )
fun getFeedEntries(): Flow<Map<String, List<FeedEntry>>> {
return entries
}
fun continueTask(open: (String) -> Unit, subjectId: String, taskId: String) { fun continueTask(open: (String) -> Unit, subjectId: String, taskId: String) {
viewModelScope.launch { viewModelScope.launch {
val task = taskDAO.getTask(subjectId, taskId) val task = taskDAO.getTask(subjectId, taskId)

View file

@ -11,10 +11,12 @@ import androidx.compose.ui.tooling.preview.Preview
import be.ugent.sel.studeez.R import be.ugent.sel.studeez.R
import be.ugent.sel.studeez.common.composable.PrimaryScreenTemplate import be.ugent.sel.studeez.common.composable.PrimaryScreenTemplate
import be.ugent.sel.studeez.common.composable.drawer.DrawerActions import be.ugent.sel.studeez.common.composable.drawer.DrawerActions
import be.ugent.sel.studeez.common.composable.feed.* import be.ugent.sel.studeez.common.composable.feed.Feed
import be.ugent.sel.studeez.common.composable.feed.FeedUiState
import be.ugent.sel.studeez.common.composable.feed.FeedViewModel
import be.ugent.sel.studeez.common.composable.navbar.NavigationBarActions import be.ugent.sel.studeez.common.composable.navbar.NavigationBarActions
import be.ugent.sel.studeez.data.local.models.FeedEntry
import be.ugent.sel.studeez.resources import be.ugent.sel.studeez.resources
import kotlinx.coroutines.flow.flowOf
@Composable @Composable
fun HomeRoute( fun HomeRoute(
@ -28,8 +30,8 @@ fun HomeRoute(
drawerActions = drawerActions, drawerActions = drawerActions,
open = open, open = open,
navigationBarActions = navigationBarActions, navigationBarActions = navigationBarActions,
feedActions = getFeedActions(feedViewModel, open),
feedUiState = feedUiState, feedUiState = feedUiState,
continueTask = { subjectId, taskId -> feedViewModel.continueTask(open, subjectId, taskId) },
) )
} }
@ -38,8 +40,8 @@ fun HomeScreen(
open: (String) -> Unit, open: (String) -> Unit,
drawerActions: DrawerActions, drawerActions: DrawerActions,
navigationBarActions: NavigationBarActions, navigationBarActions: NavigationBarActions,
feedActions: FeedActions,
feedUiState: FeedUiState, feedUiState: FeedUiState,
continueTask: (String, String) -> Unit,
) { ) {
PrimaryScreenTemplate( PrimaryScreenTemplate(
title = resources().getString(R.string.home), title = resources().getString(R.string.home),
@ -47,7 +49,7 @@ fun HomeScreen(
navigationBarActions = navigationBarActions, navigationBarActions = navigationBarActions,
// TODO barAction = { FriendsAction() } // TODO barAction = { FriendsAction() }
) { ) {
Feed(feedActions, feedUiState) Feed(feedUiState, continueTask)
} }
} }
@ -68,7 +70,36 @@ fun HomeScreenPreview() {
drawerActions = DrawerActions({}, {}, {}, {}, {}), drawerActions = DrawerActions({}, {}, {}, {}, {}),
navigationBarActions = NavigationBarActions({ false }, {}, {}, {}, {}, {}, {}, {}), navigationBarActions = NavigationBarActions({ false }, {}, {}, {}, {}, {}, {}, {}),
open = {}, open = {},
feedActions = FeedActions({ flowOf() }, { _, _ -> run {} }), feedUiState = FeedUiState.Succes(
feedUiState = FeedUiState.Loading, mapOf(
"08 May 2023" to listOf(
FeedEntry(
argb_color = 0xFFABD200,
subJectName = "Test Subject",
taskName = "Test Task",
totalStudyTime = 600,
),
FeedEntry(
argb_color = 0xFFFFD200,
subJectName = "Test Subject",
taskName = "Test Task",
totalStudyTime = 20,
),
),
"09 May 2023" to listOf(
FeedEntry(
argb_color = 0xFFFD1200,
subJectName = "Test Subject",
taskName = "Test Task",
),
FeedEntry(
argb_color = 0xFFFF5C89,
subJectName = "Test Subject",
taskName = "Test Task",
),
)
)
),
continueTask = { _, _ -> run {} },
) )
} }