added appstate
This commit is contained in:
		
							parent
							
								
									11d3d5c5d2
								
							
						
					
					
						commit
						a9e35be714
					
				
					 1 changed files with 51 additions and 0 deletions
				
			
		
							
								
								
									
										51
									
								
								app/src/main/java/be/ugent/sel/studeez/StudeezAppstate.kt
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								app/src/main/java/be/ugent/sel/studeez/StudeezAppstate.kt
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,51 @@
 | 
			
		|||
package be.ugent.sel.studeez
 | 
			
		||||
 | 
			
		||||
import android.content.res.Resources
 | 
			
		||||
import androidx.compose.material.ScaffoldState
 | 
			
		||||
import androidx.compose.runtime.Stable
 | 
			
		||||
import androidx.navigation.NavHostController
 | 
			
		||||
import be.ugent.sel.studeez.common.snackbar.SnackbarManager
 | 
			
		||||
import be.ugent.sel.studeez.common.snackbar.SnackbarMessage.Companion.toMessage
 | 
			
		||||
import kotlinx.coroutines.CoroutineScope
 | 
			
		||||
import kotlinx.coroutines.flow.filterNotNull
 | 
			
		||||
import kotlinx.coroutines.launch
 | 
			
		||||
 | 
			
		||||
@Stable
 | 
			
		||||
class StudeezAppstate(
 | 
			
		||||
    val scaffoldState: ScaffoldState,
 | 
			
		||||
    val navController: NavHostController,
 | 
			
		||||
    private val snackbarManager: SnackbarManager,
 | 
			
		||||
    private val resources: Resources,
 | 
			
		||||
    coroutineScope: CoroutineScope
 | 
			
		||||
) {
 | 
			
		||||
    init {
 | 
			
		||||
        coroutineScope.launch {
 | 
			
		||||
            snackbarManager.snackbarMessages.filterNotNull().collect { snackbarMessage ->
 | 
			
		||||
                val text = snackbarMessage.toMessage(resources)
 | 
			
		||||
                scaffoldState.snackbarHostState.showSnackbar(text)
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun popUp() {
 | 
			
		||||
        navController.popBackStack()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun navigate(route: String) {
 | 
			
		||||
        navController.navigate(route) { launchSingleTop = true }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun navigateAndPopUp(route: String, popUp: String) {
 | 
			
		||||
        navController.navigate(route) {
 | 
			
		||||
            launchSingleTop = true
 | 
			
		||||
            popUpTo(popUp) { inclusive = true }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun clearAndNavigate(route: String) {
 | 
			
		||||
        navController.navigate(route) {
 | 
			
		||||
            launchSingleTop = true
 | 
			
		||||
            popUpTo(0) { inclusive = true }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in a new issue