Created
August 9, 2020 16:07
-
-
Save AliAzaz/8cdf71836e48d8d92aab14185920afb1 to your computer and use it in GitHub Desktop.
mutableStateOf approach using
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//--------------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