add previews for feed
This commit is contained in:
		
							parent
							
								
									6ba0018765
								
							
						
					
					
						commit
						366f236f98
					
				
					 3 changed files with 80 additions and 41 deletions
				
			
		| 
						 | 
				
			
			@ -16,34 +16,11 @@ import androidx.compose.ui.unit.sp
 | 
			
		|||
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
 | 
			
		||||
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
 | 
			
		||||
fun Feed(
 | 
			
		||||
    feedActions: FeedActions,
 | 
			
		||||
    uiState: FeedUiState,
 | 
			
		||||
    continueTask: (String, String) -> Unit,
 | 
			
		||||
) {
 | 
			
		||||
    when (uiState) {
 | 
			
		||||
        FeedUiState.Loading -> {
 | 
			
		||||
| 
						 | 
				
			
			@ -58,7 +35,6 @@ fun Feed(
 | 
			
		|||
            }
 | 
			
		||||
        }
 | 
			
		||||
        is FeedUiState.Succes -> {
 | 
			
		||||
//            val feedEntries = feedActions.getFeedEntries().collectAsState(initial = emptyMap())
 | 
			
		||||
            val feedEntries = uiState.feedEntries
 | 
			
		||||
            LazyColumn {
 | 
			
		||||
                items(feedEntries.toList()) { (date, feedEntries) ->
 | 
			
		||||
| 
						 | 
				
			
			@ -79,7 +55,7 @@ fun Feed(
 | 
			
		|||
                    }
 | 
			
		||||
                    feedEntries.forEach { feedEntry ->
 | 
			
		||||
                        FeedEntry(feedEntry = feedEntry) {
 | 
			
		||||
                            feedActions.continueTask(feedEntry.subjectId, feedEntry.taskId)
 | 
			
		||||
                            continueTask(feedEntry.subjectId, feedEntry.taskId)
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                    Spacer(modifier = Modifier.height(20.dp))
 | 
			
		||||
| 
						 | 
				
			
			@ -89,11 +65,49 @@ fun Feed(
 | 
			
		|||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@Preview
 | 
			
		||||
@Composable
 | 
			
		||||
fun FeedLoadingPreview() {
 | 
			
		||||
    Feed(
 | 
			
		||||
        uiState = FeedUiState.Loading,
 | 
			
		||||
        continueTask = { _, _ -> run {} },
 | 
			
		||||
    )
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@Preview
 | 
			
		||||
@Composable
 | 
			
		||||
fun FeedPreview() {
 | 
			
		||||
    Feed(
 | 
			
		||||
        feedActions = FeedActions({ flowOf() }, { _, _ -> run {} }),
 | 
			
		||||
        uiState = FeedUiState.Loading,
 | 
			
		||||
        uiState = FeedUiState.Succes(
 | 
			
		||||
            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 {} },
 | 
			
		||||
    )
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -21,8 +21,6 @@ class FeedViewModel @Inject constructor(
 | 
			
		|||
    logService: LogService
 | 
			
		||||
) : StudeezViewModel(logService) {
 | 
			
		||||
 | 
			
		||||
    private val entries: Flow<Map<String, List<FeedEntry>>> = feedDAO.getFeedEntries()
 | 
			
		||||
 | 
			
		||||
    val uiState: StateFlow<FeedUiState> = feedDAO.getFeedEntries()
 | 
			
		||||
        .map { FeedUiState.Succes(it) }
 | 
			
		||||
        .stateIn(
 | 
			
		||||
| 
						 | 
				
			
			@ -31,10 +29,6 @@ class FeedViewModel @Inject constructor(
 | 
			
		|||
            started = SharingStarted.Eagerly,
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    fun getFeedEntries(): Flow<Map<String, List<FeedEntry>>> {
 | 
			
		||||
        return entries
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun continueTask(open: (String) -> Unit, subjectId: String, taskId: String) {
 | 
			
		||||
        viewModelScope.launch {
 | 
			
		||||
            val task = taskDAO.getTask(subjectId, taskId)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -11,10 +11,12 @@ import androidx.compose.ui.tooling.preview.Preview
 | 
			
		|||
import be.ugent.sel.studeez.R
 | 
			
		||||
import be.ugent.sel.studeez.common.composable.PrimaryScreenTemplate
 | 
			
		||||
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.data.local.models.FeedEntry
 | 
			
		||||
import be.ugent.sel.studeez.resources
 | 
			
		||||
import kotlinx.coroutines.flow.flowOf
 | 
			
		||||
 | 
			
		||||
@Composable
 | 
			
		||||
fun HomeRoute(
 | 
			
		||||
| 
						 | 
				
			
			@ -28,8 +30,8 @@ fun HomeRoute(
 | 
			
		|||
        drawerActions = drawerActions,
 | 
			
		||||
        open = open,
 | 
			
		||||
        navigationBarActions = navigationBarActions,
 | 
			
		||||
        feedActions = getFeedActions(feedViewModel, open),
 | 
			
		||||
        feedUiState = feedUiState,
 | 
			
		||||
        continueTask = { subjectId, taskId -> feedViewModel.continueTask(open, subjectId, taskId) },
 | 
			
		||||
    )
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -38,8 +40,8 @@ fun HomeScreen(
 | 
			
		|||
    open: (String) -> Unit,
 | 
			
		||||
    drawerActions: DrawerActions,
 | 
			
		||||
    navigationBarActions: NavigationBarActions,
 | 
			
		||||
    feedActions: FeedActions,
 | 
			
		||||
    feedUiState: FeedUiState,
 | 
			
		||||
    continueTask: (String, String) -> Unit,
 | 
			
		||||
) {
 | 
			
		||||
    PrimaryScreenTemplate(
 | 
			
		||||
        title = resources().getString(R.string.home),
 | 
			
		||||
| 
						 | 
				
			
			@ -47,7 +49,7 @@ fun HomeScreen(
 | 
			
		|||
        navigationBarActions = navigationBarActions,
 | 
			
		||||
        // TODO barAction = { FriendsAction() }
 | 
			
		||||
    ) {
 | 
			
		||||
        Feed(feedActions, feedUiState)
 | 
			
		||||
        Feed(feedUiState, continueTask)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -68,7 +70,36 @@ fun HomeScreenPreview() {
 | 
			
		|||
        drawerActions = DrawerActions({}, {}, {}, {}, {}),
 | 
			
		||||
        navigationBarActions = NavigationBarActions({ false }, {}, {}, {}, {}, {}, {}, {}),
 | 
			
		||||
        open = {},
 | 
			
		||||
        feedActions = FeedActions({ flowOf() }, { _, _ -> run {} }),
 | 
			
		||||
        feedUiState = FeedUiState.Loading,
 | 
			
		||||
        feedUiState = FeedUiState.Succes(
 | 
			
		||||
            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 {} },
 | 
			
		||||
    )
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Reference in a new issue