Skip to content

Commit

Permalink
Adjust view model storage
Browse files Browse the repository at this point in the history
Store ScoutingScheduleManager and view models in AppContainer
Access view models through parameters
Remove getViewModel()
Add and use composableContext
Make composable parameter order consistent
  • Loading branch information
KangarooKoala committed Jul 6, 2023
1 parent b550198 commit 3e1809e
Show file tree
Hide file tree
Showing 17 changed files with 117 additions and 105 deletions.
15 changes: 15 additions & 0 deletions app/src/main/java/com/scouting/app/AppContainer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.scouting.app

import com.scouting.app.misc.ScoutingScheduleManager
import com.scouting.app.viewmodel.HomePageViewModel
import com.scouting.app.viewmodel.ScoutingViewModel
import com.scouting.app.viewmodel.SettingsViewModel
import com.scouting.app.viewmodel.TemplateEditorViewModel

class AppContainer {
val scheduleManager = ScoutingScheduleManager()
val homePageViewModel = HomePageViewModel()
val scoutingViewModel = ScoutingViewModel()
val settingsViewModel = SettingsViewModel()
val templateEditorViewModel = TemplateEditorViewModel()
}
36 changes: 20 additions & 16 deletions app/src/main/java/com/scouting/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ import com.scouting.app.misc.RequestCode.PIT_SCOUTING_SCHEDULE_FILE_PICK
import com.scouting.app.misc.RequestCode.PIT_TEMPLATE_FILE_PICK
import com.scouting.app.misc.RequestCode.TEMPLATE_EDITOR_IMAGE_FILE_PICK
import com.scouting.app.misc.RequestCode.TEMPLATE_EDITOR_IMPORT_FILE_PICK
import com.scouting.app.misc.ScoutingScheduleManager
import com.scouting.app.theme.ScoutingTheme
import com.scouting.app.utilities.getViewModel
import com.scouting.app.view.HomePageView
import com.scouting.app.view.scouting.FinishScoutingView
import com.scouting.app.view.scouting.ScoutingView
Expand All @@ -42,22 +40,21 @@ import com.scouting.app.view.settings.SettingsView
import com.scouting.app.view.template.EditCSVOrderView
import com.scouting.app.view.template.TemplateEditorView
import com.scouting.app.view.template.TemplateSaveView
import com.scouting.app.viewmodel.SettingsViewModel
import com.scouting.app.viewmodel.TemplateEditorViewModel
import com.tencent.mmkv.MMKV
import java.io.File


class MainActivity : ComponentActivity() {

private lateinit var navigationController: NavHostController
private val scoutingScheduleManager = ScoutingScheduleManager()
lateinit var appContainer: AppContainer

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
MMKV.initialize(this)
configureStorage()
scoutingScheduleManager.apply {
appContainer = AppContainer()
appContainer.scheduleManager.apply {
loadCachedSchedule(true)
loadCachedSchedule(false)
}
Expand All @@ -72,6 +69,11 @@ class MainActivity : ComponentActivity() {
@OptIn(ExperimentalAnimationApi::class)
fun NavigationHost() {
navigationController = rememberAnimatedNavController()
val scheduleManager = appContainer.scheduleManager
val homePageViewModel = appContainer.homePageViewModel
val scoutingViewModel = appContainer.scoutingViewModel
val settingsViewModel = appContainer.settingsViewModel
val templateEditorViewModel = appContainer.templateEditorViewModel
AnimatedNavHost(
navController = navigationController,
startDestination = NavDestination.HomePage,
Expand All @@ -87,46 +89,48 @@ class MainActivity : ComponentActivity() {
},
builder = {
composable(NavDestination.HomePage) {
HomePageView(navigationController, scoutingScheduleManager)
HomePageView(navigationController, scheduleManager, homePageViewModel, settingsViewModel)
}
composable(
route = "${NavDestination.TemplateEditor}/{type}",
arguments = listOf(navArgument("type") { type = NavType.StringType })
) {
TemplateEditorView(
navController = navigationController,
viewModel = templateEditorViewModel,
type = it.arguments?.getString("type", "match")!!
)
}
composable(NavDestination.TemplateEditor) {
TemplateEditorView(navigationController)
TemplateEditorView(navigationController, templateEditorViewModel)
}
composable(NavDestination.EditCSVOrder) {
EditCSVOrderView(navigationController)
EditCSVOrderView(navigationController, templateEditorViewModel)
}
composable(NavDestination.TemplateSave) {
TemplateSaveView(navigationController)
TemplateSaveView(navigationController, templateEditorViewModel, settingsViewModel)
}
composable(NavDestination.StartMatchScouting) {
StartMatchView(navigationController, scoutingScheduleManager)
StartMatchView(navigationController, scheduleManager, scoutingViewModel)
}
composable(NavDestination.StartPitScouting) {
StartPitScoutingView(navigationController, scoutingScheduleManager)
StartPitScoutingView(navigationController, scheduleManager, scoutingViewModel)
}
composable(
route = "${NavDestination.Scouting}/{type}",
arguments = listOf(navArgument("type") { type = NavType.BoolType })
) {
ScoutingView(
navController = navigationController,
viewModel = scoutingViewModel,
scoutingMatch = it.arguments?.getBoolean("type", true) ?: false
)
}
composable(NavDestination.Settings) {
SettingsView(navigationController, scoutingScheduleManager)
SettingsView(navigationController, scheduleManager, settingsViewModel)
}
composable(NavDestination.FinishScouting) {
FinishScoutingView(navigationController)
FinishScoutingView(navigationController, scoutingViewModel)
}
}
)
Expand Down Expand Up @@ -184,8 +188,8 @@ class MainActivity : ComponentActivity() {
super.onActivityResult(requestCode, resultCode, resultData)
if (resultCode == RESULT_OK) {
resultData?.let { data ->
val settingsViewModel = getViewModel(SettingsViewModel::class.java)
val templateEditorViewModel = getViewModel(TemplateEditorViewModel::class.java)
val settingsViewModel = appContainer.settingsViewModel
val templateEditorViewModel = appContainer.templateEditorViewModel
when (requestCode) {
MATCH_TEMPLATE_FILE_PICK, PIT_TEMPLATE_FILE_PICK -> {
settingsViewModel.processTemplateFilePickerResult(
Expand Down
9 changes: 3 additions & 6 deletions app/src/main/java/com/scouting/app/utilities/Utilities.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package com.scouting.app.utilities

import android.content.Context
import androidx.activity.ComponentActivity
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.zIndex
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.NavController
import androidx.navigation.navOptions
import com.scouting.app.MainActivity

/**
* Returns a [MutableState] that will be initialized with [value] during the first composition and when a different
Expand All @@ -21,8 +19,7 @@ import androidx.navigation.navOptions
@Composable
fun <T> rememberInitial(value: T) = remember(value) { mutableStateOf(value) }

fun <T : ViewModel> Context.getViewModel(type: Class<T>): T =
ViewModelProvider(this as ComponentActivity)[type]
val composableContext @Composable get() = LocalContext.current as MainActivity

fun Modifier.longPressEffect(offset: Float?): Modifier =
this
Expand Down
18 changes: 8 additions & 10 deletions app/src/main/java/com/scouting/app/view/HomePageView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import com.scouting.app.MainActivity
import com.scouting.app.R
import com.scouting.app.components.LargeButton
import com.scouting.app.misc.AllianceType
Expand All @@ -27,19 +26,19 @@ import com.scouting.app.misc.ScoutingScheduleManager
import com.scouting.app.theme.AffirmativeGreenDark
import com.scouting.app.theme.ErrorRedDark
import com.scouting.app.theme.SecondaryPurpleDark
import com.scouting.app.utilities.getViewModel
import com.scouting.app.view.settings.DevicePositionDialog
import com.scouting.app.view.template.TemplateTypeDialog
import com.scouting.app.viewmodel.HomePageViewModel
import com.scouting.app.viewmodel.SettingsViewModel
import com.tencent.mmkv.MMKV

@Composable
fun HomePageView(navController: NavController, scoutingScheduleManager: ScoutingScheduleManager) {
val context = navController.context as MainActivity
val viewModel = context.getViewModel(HomePageViewModel::class.java)
val settingsViewModel =
context.getViewModel(SettingsViewModel::class.java) // might be bad practice lolz
fun HomePageView(
navController: NavController,
scoutingScheduleManager: ScoutingScheduleManager,
viewModel: HomePageViewModel,
settingsViewModel: SettingsViewModel
) {
val preferences = MMKV.defaultMMKV()
LaunchedEffect(true) {
settingsViewModel.loadSavedPreferences()
Expand Down Expand Up @@ -203,10 +202,9 @@ fun HomePageView(navController: NavController, scoutingScheduleManager: Scouting
}
}
}
TemplateTypeDialog(viewModel, navController)
TemplateTypeDialog(navController, viewModel)
DevicePositionDialog(
viewModel = settingsViewModel,
navController = navController
viewModel = settingsViewModel
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.scouting.app.MainActivity
import com.scouting.app.R
import com.scouting.app.components.BasicInputField
import com.scouting.app.components.LargeHeaderBar
Expand All @@ -20,14 +19,13 @@ import com.scouting.app.components.SpacedRow
import com.scouting.app.misc.NavDestination
import com.scouting.app.theme.AffirmativeGreen
import com.scouting.app.theme.ScoutingTheme
import com.scouting.app.utilities.getViewModel
import com.scouting.app.utilities.composableContext
import com.scouting.app.utilities.returnTo
import com.scouting.app.viewmodel.ScoutingViewModel

@Composable
fun FinishScoutingView(navController: NavController) {
val context = navController.context as MainActivity
val viewModel = context.getViewModel(ScoutingViewModel::class.java)
fun FinishScoutingView(navController: NavController, viewModel: ScoutingViewModel) {
val context = composableContext
ScoutingTheme {
Surface(modifier = Modifier.fillMaxSize()) {
Column {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import com.scouting.app.theme.ScoutingTheme
import com.scouting.app.viewmodel.ScoutingViewModel

@Composable
fun NoTemplateDialog(viewModel: ScoutingViewModel, navController: NavController) {
fun NoTemplateDialog(navController: NavController, viewModel: ScoutingViewModel) {
if (viewModel.showingNoTemplateDialog) {
ScoutingTheme {
DialogScaffold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
Expand All @@ -28,13 +27,11 @@ import com.scouting.app.model.TemplateItem
import com.scouting.app.theme.AffirmativeGreen
import com.scouting.app.theme.ScoutingTheme
import com.scouting.app.theme.SecondaryPurple
import com.scouting.app.utilities.getViewModel
import com.scouting.app.viewmodel.ScoutingViewModel

@OptIn(ExperimentalAnimationApi::class)
@Composable
fun ScoutingView(navController: NavController, scoutingMatch: Boolean) {
val viewModel = LocalContext.current.getViewModel(ScoutingViewModel::class.java)
fun ScoutingView(navController: NavController, viewModel: ScoutingViewModel, scoutingMatch: Boolean) {
ScoutingTheme {
Surface(modifier = Modifier.fillMaxSize()) {
Column {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,23 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.scouting.app.MainActivity
import com.scouting.app.R
import com.scouting.app.components.*
import com.scouting.app.misc.*
import com.scouting.app.theme.AffirmativeGreen
import com.scouting.app.theme.AffirmativeGreenDark
import com.scouting.app.theme.ScoutingTheme
import com.scouting.app.utilities.getViewModel
import com.scouting.app.utilities.composableContext
import com.scouting.app.viewmodel.ScoutingViewModel
import com.tencent.mmkv.MMKV

@Composable
fun StartMatchView(navController: NavController, scoutingScheduleManager: ScoutingScheduleManager) {
val context = navController.context as MainActivity
val viewModel = context.getViewModel(ScoutingViewModel::class.java)
fun StartMatchView(
navController: NavController,
scoutingScheduleManager: ScoutingScheduleManager,
viewModel: ScoutingViewModel
) {
val context = composableContext
var managedMatch by remember { mutableStateOf(false) }
LaunchedEffect(true) {
viewModel.apply {
Expand Down Expand Up @@ -129,5 +131,5 @@ fun StartMatchView(navController: NavController, scoutingScheduleManager: Scouti
}
}
}
NoTemplateDialog(viewModel, navController)
NoTemplateDialog(navController, viewModel)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import androidx.compose.foundation.layout.width
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.scouting.app.MainActivity
import com.scouting.app.R
import com.scouting.app.components.BasicInputField
import com.scouting.app.components.LargeButton
Expand All @@ -26,13 +26,16 @@ import com.scouting.app.misc.ScoutingType
import com.scouting.app.theme.AffirmativeGreen
import com.scouting.app.theme.AffirmativeGreenDark
import com.scouting.app.theme.ScoutingTheme
import com.scouting.app.utilities.getViewModel
import com.scouting.app.utilities.composableContext
import com.scouting.app.viewmodel.ScoutingViewModel

@Composable
fun StartPitScoutingView(navController: NavController, scoutingScheduleManager: ScoutingScheduleManager) {
val context = navController.context as MainActivity
val viewModel = context.getViewModel(ScoutingViewModel::class.java)
fun StartPitScoutingView(
navController: NavController,
scoutingScheduleManager: ScoutingScheduleManager,
viewModel: ScoutingViewModel
) {
val context = composableContext
LaunchedEffect(true) {
viewModel.apply {
this.scoutingScheduleManager = scoutingScheduleManager
Expand Down Expand Up @@ -108,5 +111,5 @@ fun StartPitScoutingView(navController: NavController, scoutingScheduleManager:
}
}
}
NoTemplateDialog(viewModel, navController)
NoTemplateDialog(navController, viewModel)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.scouting.app.MainActivity
import com.scouting.app.R
import com.scouting.app.components.DialogScaffold
import com.scouting.app.components.RatingBar
import com.scouting.app.components.SmallButton
import com.scouting.app.components.SpacedRow
import com.scouting.app.misc.AllianceType
import com.scouting.app.utilities.composableContext
import com.scouting.app.viewmodel.SettingsViewModel

@Composable
fun DevicePositionDialog(viewModel: SettingsViewModel, navController: NavController) {
val context = navController.context as MainActivity
fun DevicePositionDialog(viewModel: SettingsViewModel) {
val context = composableContext
if (viewModel.showingDevicePositionDialog) {
DialogScaffold(
icon = painterResource(id = R.drawable.ic_machine_learning),
Expand Down
Loading

0 comments on commit 3e1809e

Please sign in to comment.