Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
mutableStateOf approach using
//--------------Utils Class start--------------------
sealed class MenuOptions {
object TaskList : MenuOptions()
object AddTask : MenuOptions()
data class ModifyTask(val task: Task) : MenuOptions()
}
object AppMain {
private var current_screen: MenuOptions = MenuOptions.TaskList
var route_screen by mutableStateOf(current_screen)
var taskList by mutableStateOf(mutableListOf<Task>())
}
fun routingActivity(activity: MenuOptions) {
route_screen = activity
}
//--------------Utils Class end----------------------
//--------------Main Activity------------------------
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
TODOAppGreeting()
}
}
override fun onBackPressed() {
if (AppMain.route_screen !is MenuOptions.TaskList)
routingActivity(MenuOptions.TaskList)
else
super.onBackPressed()
}
}
@Composable
fun TODOAppGreeting() {
MaterialTheme {
AppContent(AppState())
}
}
@Composable
fun AppContent(app: AppMain) {
Crossfade(current = app.route_screen) { activity ->
Surface(color = MaterialTheme.colors.background) {
when (activity) {
is MenuOptions.AddTask -> AddTask()
is MenuOptions.TaskList -> TaskList()
is MenuOptions.ModifyTask -> AddTask(activity.task)
}
}
}
}
@Composable
fun AppState(): AppMain {
//Initialize state
return remember { AppMain }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment