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.NavigationBarViewModel | ||||||
| import be.ugent.sel.studeez.common.composable.navbar.getNavigationBarActions | import be.ugent.sel.studeez.common.composable.navbar.getNavigationBarActions | ||||||
| import be.ugent.sel.studeez.screens.home.HomeRoute | 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.log_in.LoginRoute | ||||||
| import be.ugent.sel.studeez.screens.profile.EditProfileRoute | import be.ugent.sel.studeez.screens.profile.EditProfileRoute | ||||||
| import be.ugent.sel.studeez.screens.profile.ProfileRoute | import be.ugent.sel.studeez.screens.profile.ProfileRoute | ||||||
|  | @ -67,7 +68,8 @@ fun StudeezNavGraph( | ||||||
|                 open, |                 open, | ||||||
|                 viewModel = hiltViewModel(), |                 viewModel = hiltViewModel(), | ||||||
|                 drawerActions = drawerActions, |                 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.Alignment | ||||||
| import androidx.compose.ui.Modifier | import androidx.compose.ui.Modifier | ||||||
| import androidx.compose.ui.text.font.FontWeight | 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.dp | ||||||
| import androidx.compose.ui.unit.sp | 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.common.composable.DateText | ||||||
|  | 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 | ||||||
|  | 
 | ||||||
|  | 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( | ||||||
|     open: (String) -> Unit, |     feedActions: FeedActions, | ||||||
|     viewModel: FeedViewModel = hiltViewModel() |  | ||||||
| ) { | ) { | ||||||
|     val feedEntries = viewModel.getFeedEntries().collectAsState(initial = emptyMap()) |     val feedEntries = feedActions.getFeedEntries().collectAsState(initial = emptyMap()) | ||||||
| 
 |  | ||||||
|     LazyColumn { |     LazyColumn { | ||||||
| 
 |  | ||||||
|         items(feedEntries.value.toList()) { (date, feedEntries) -> |         items(feedEntries.value.toList()) { (date, feedEntries) -> | ||||||
|             Row( |             Row( | ||||||
|                 horizontalArrangement = Arrangement.SpaceBetween, |                 horizontalArrangement = Arrangement.SpaceBetween, | ||||||
|  | @ -43,19 +61,10 @@ fun Feed( | ||||||
|             } |             } | ||||||
|             feedEntries.forEach { feedEntry -> |             feedEntries.forEach { feedEntry -> | ||||||
|                 FeedEntry(feedEntry = feedEntry) { |                 FeedEntry(feedEntry = feedEntry) { | ||||||
|                     viewModel.continueWithTask(open, feedEntry.subjectId, feedEntry.taskId) |                     feedActions.continueTask(feedEntry.subjectId, feedEntry.taskId) | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|             Spacer(modifier = Modifier.height(20.dp)) |             Spacer(modifier = Modifier.height(20.dp)) | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 |  | ||||||
| @Preview |  | ||||||
| @Composable |  | ||||||
| fun FeedPreview() { |  | ||||||
|     Feed( |  | ||||||
|         open = {}, |  | ||||||
|         viewModel = hiltViewModel(), |  | ||||||
|     ) |  | ||||||
| } |  | ||||||
|  | @ -27,7 +27,7 @@ class FeedViewModel @Inject constructor( | ||||||
|         return entries |         return entries | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     fun continueWithTask(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) | ||||||
|             selectedTask.set(task) |             selectedTask.set(task) | ||||||
|  |  | ||||||
|  | @ -5,15 +5,13 @@ import androidx.compose.material.IconButton | ||||||
| import androidx.compose.material.icons.Icons | import androidx.compose.material.icons.Icons | ||||||
| import androidx.compose.material.icons.filled.Person | import androidx.compose.material.icons.filled.Person | ||||||
| import androidx.compose.runtime.Composable | import androidx.compose.runtime.Composable | ||||||
| import androidx.compose.ui.Modifier |  | ||||||
| import androidx.compose.ui.tooling.preview.Preview | 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.BasicButton |  | ||||||
| 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.navbar.NavigationBarActions | import be.ugent.sel.studeez.common.composable.navbar.NavigationBarActions | ||||||
| import be.ugent.sel.studeez.common.ext.basicButton |  | ||||||
| import be.ugent.sel.studeez.resources | import be.ugent.sel.studeez.resources | ||||||
|  | import kotlinx.coroutines.flow.flowOf | ||||||
| 
 | 
 | ||||||
| @Composable | @Composable | ||||||
| fun HomeRoute( | fun HomeRoute( | ||||||
|  | @ -21,21 +19,22 @@ fun HomeRoute( | ||||||
|     viewModel: HomeViewModel, |     viewModel: HomeViewModel, | ||||||
|     drawerActions: DrawerActions, |     drawerActions: DrawerActions, | ||||||
|     navigationBarActions: NavigationBarActions, |     navigationBarActions: NavigationBarActions, | ||||||
|  |     feedActions: FeedActions, | ||||||
| ) { | ) { | ||||||
|     HomeScreen( |     HomeScreen( | ||||||
|         onStartSessionClick = { viewModel.onStartSessionClick(open) }, |  | ||||||
|         drawerActions = drawerActions, |         drawerActions = drawerActions, | ||||||
|         open = open, |         open = open, | ||||||
|         navigationBarActions = navigationBarActions, |         navigationBarActions = navigationBarActions, | ||||||
|  |         feedActions = feedActions, | ||||||
|     ) |     ) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @Composable | @Composable | ||||||
| fun HomeScreen( | fun HomeScreen( | ||||||
|     onStartSessionClick: () -> Unit, |  | ||||||
|     open: (String) -> Unit, |     open: (String) -> Unit, | ||||||
|     drawerActions: DrawerActions, |     drawerActions: DrawerActions, | ||||||
|     navigationBarActions: NavigationBarActions |     navigationBarActions: NavigationBarActions, | ||||||
|  |     feedActions: FeedActions, | ||||||
| ) { | ) { | ||||||
|     PrimaryScreenTemplate( |     PrimaryScreenTemplate( | ||||||
|         title = resources().getString(R.string.home), |         title = resources().getString(R.string.home), | ||||||
|  | @ -43,7 +42,7 @@ fun HomeScreen( | ||||||
|         navigationBarActions = navigationBarActions, |         navigationBarActions = navigationBarActions, | ||||||
|         // TODO barAction = { FriendsAction() } |         // TODO barAction = { FriendsAction() } | ||||||
|     ) { |     ) { | ||||||
|         Feed(open) |         Feed(feedActions) | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -61,9 +60,9 @@ fun FriendsAction() { | ||||||
| @Composable | @Composable | ||||||
| fun HomeScreenPreview() { | fun HomeScreenPreview() { | ||||||
|     HomeScreen( |     HomeScreen( | ||||||
|         onStartSessionClick = {}, |  | ||||||
|         drawerActions = DrawerActions({}, {}, {}, {}, {}), |         drawerActions = DrawerActions({}, {}, {}, {}, {}), | ||||||
|         navigationBarActions = NavigationBarActions({ false }, {}, {}, {}, {}, {}, {}, {}), |         navigationBarActions = NavigationBarActions({ false }, {}, {}, {}, {}, {}, {}, {}), | ||||||
|         open = {} |         open = {}, | ||||||
|  |         feedActions = FeedActions({ flowOf() }, { _, _ -> run {} }) | ||||||
|     ) |     ) | ||||||
| } | } | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 brreynie
						brreynie