Pass mainViewModel and Modifier via Navigation

35 Views Asked by At

I have an app which will have multiple different layout, and i switch between them via routes, here is the code:

MainActivity.kt

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import com.example.yilcheck.datastore.MainViewModel
import com.example.yilcheck.ui.theme.Nav
import com.example.yilcheck.ui.theme.YilCheckTheme

class MainActivity : ComponentActivity() {
    private val mainViewModel by viewModels<MainViewModel>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            YilCheckTheme {
                Nav()
                }
            }
        }
    }

Navigation.kt


import androidx.compose.runtime.Composable
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController

@Composable
fun Nav(){
    val navController = rememberNavController()
    NavHost(navController = navController, startDestination = "Start"){
        composable(route = "Start"){
            StartPage(navController)
        }
        composable(route = "Settings"){
            Settings(navController)
        }
    }
}

This is where i want to be able to use viewmodel so i can use my data variables

Settings.kt

import android.annotation.SuppressLint
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.navigation.NavHostController
import androidx.compose.runtime.*

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun Settings(navController: NavHostController, modifier: Modifier = Modifier) {
    //Variables
    val selectedSwitchGru by mainViewModel.switchGru.collectAsState(initial = false)
    val selectedSwitchCfs by mainViewModel.switchCfs.collectAsState(initial = false)
    val selectedSwitchEst by mainViewModel.switchEst.collectAsState(initial = false)
    val selectedSwitchGct by mainViewModel.switchGct.collectAsState(initial = false)

    Scaffold(
        //Header
        topBar= {
            CenterAlignedTopAppBar(
                title = {
                    Text(text = "Settings")
                },
                navigationIcon = {
                    IconButton(onClick = {
                        navController.navigate("Start") {
                            popUpTo("Start") { inclusive = true }
                        }
                    }) {
                        Icon(imageVector = Icons.Default.ArrowBack, contentDescription = "Back arrow")
                    }
                },
            )
        },
        //mer

    ) { contentPadding ->
        //Content
        Column(modifier.padding(contentPadding)){
            //Switches
            Switch(checked = selectedSwitchGru, onCheckedChange = { mainViewModel.updateSwitchGru(it) })
            Switch(checked = selectedSwitchCfs, onCheckedChange = { mainViewModel.updateSwitchCfs(it) })
            Switch(checked = selectedSwitchEst, onCheckedChange = { mainViewModel.updateSwitchEst(it) })
            Switch(checked = selectedSwitchGct, onCheckedChange = { mainViewModel.updateSwitchGct(it) })
        }
    }
}

I have made a dataStore for some variables that i need to use and i want to be able to use them on all my "pages" eventually but i cant get it to work.

I have tried passing mainViewmodel via Nav() and then further on to Settings but then i get the error that Modifier variable is not being passed and i tried it like this:

composable(route = "Settings"){
            Settings(navController, Modifier, mainViewModel)
        }

But then the program crashes, i very new to android development so i would really like some help how to make this work with the Modifier and mainViewModel and how to properly pass them through.

This is the error of the crash: Log

0

There are 0 best solutions below