diff --git a/src/main/java/com/machiav3lli/backup/pages/AppPage.kt b/src/main/java/com/machiav3lli/backup/pages/AppPage.kt index 182b713fc..465937ce2 100644 --- a/src/main/java/com/machiav3lli/backup/pages/AppPage.kt +++ b/src/main/java/com/machiav3lli/backup/pages/AppPage.kt @@ -108,6 +108,8 @@ import com.machiav3lli.backup.ui.compose.show import com.machiav3lli.backup.utils.TraceUtils import com.machiav3lli.backup.utils.infoChips import com.machiav3lli.backup.viewmodels.AppVM +import com.machiav3lli.backup.viewmodels.MainVM +import org.koin.androidx.compose.koinViewModel import timber.log.Timber const val DIALOG_BACKUP = 1 @@ -127,6 +129,7 @@ const val DIALOG_NOTE_BACKUP = 12 fun AppPage( viewModel: AppVM, packageName: String, + mainVM: MainVM = koinViewModel(), onDismiss: () -> Unit, ) { val context = LocalContext.current @@ -136,7 +139,7 @@ fun AppPage( mutableStateOf(Pair(DIALOG_NONE, Schedule())) } - val thePackages by mActivity.viewModel.packageMap.collectAsState() + val thePackages by mainVM.packageMap.collectAsState() val thePackage: Package? = thePackages[packageName] val snackbarText by viewModel.snackbarText.flow.collectAsState("") val appExtras by viewModel.appExtras.collectAsState() @@ -305,7 +308,7 @@ fun AppPage( icon = Phosphor.Prohibit, description = stringResource(id = R.string.global_blocklist_add) ) { - mActivity.viewModel.addToBlocklist( + mainVM.addToBlocklist( pkg.packageName ) } diff --git a/src/main/java/com/machiav3lli/backup/pages/BatchPage.kt b/src/main/java/com/machiav3lli/backup/pages/BatchPage.kt index 53a532f81..91810fcd9 100644 --- a/src/main/java/com/machiav3lli/backup/pages/BatchPage.kt +++ b/src/main/java/com/machiav3lli/backup/pages/BatchPage.kt @@ -71,14 +71,20 @@ import com.machiav3lli.backup.ui.compose.theme.ColorAPK import com.machiav3lli.backup.ui.compose.theme.ColorData import com.machiav3lli.backup.utils.altModeToMode import com.machiav3lli.backup.viewmodels.BatchVM +import com.machiav3lli.backup.viewmodels.MainVM import kotlinx.coroutines.launch +import org.koin.androidx.compose.koinViewModel @OptIn(ExperimentalMaterial3Api::class) @Composable -fun BatchPage(viewModel: BatchVM, backupBoolean: Boolean) { +fun BatchPage( + viewModel: BatchVM, + mainVM: MainVM = koinViewModel(), + backupBoolean: Boolean +) { val main = OABX.main!! val scope = rememberCoroutineScope() - val filteredList by main.viewModel.filteredList.collectAsState(emptyList()) + val filteredList by mainVM.filteredList.collectAsState(emptyList()) val scaffoldState = rememberBottomSheetScaffoldState() val openBatchDialog = remember { mutableStateOf(false) } val openBlocklist = rememberSaveable { mutableStateOf(false) } @@ -281,11 +287,11 @@ fun BatchPage(viewModel: BatchVM, backupBoolean: Boolean) { if (openBlocklist.value) BaseDialog(openDialogCustom = openBlocklist) { GlobalBlockListDialogUI( - currentBlocklist = OABX.main?.viewModel?.getBlocklist()?.toSet() + currentBlocklist = mainVM.getBlocklist()?.toSet() ?: emptySet(), openDialogCustom = openBlocklist, ) { newSet -> - OABX.main?.viewModel?.setBlocklist(newSet) + mainVM.setBlocklist(newSet) } } if (openBatchDialog.value) BaseDialog(openDialogCustom = openBatchDialog) { diff --git a/src/main/java/com/machiav3lli/backup/pages/HomePage.kt b/src/main/java/com/machiav3lli/backup/pages/HomePage.kt index a1ce8adbb..6aef0cd48 100644 --- a/src/main/java/com/machiav3lli/backup/pages/HomePage.kt +++ b/src/main/java/com/machiav3lli/backup/pages/HomePage.kt @@ -90,16 +90,17 @@ import com.machiav3lli.backup.ui.compose.recycler.HomePackageRecycler import com.machiav3lli.backup.ui.compose.recycler.UpdatedPackageRecycler import com.machiav3lli.backup.utils.altModeToMode import com.machiav3lli.backup.viewmodels.AppVM +import com.machiav3lli.backup.viewmodels.MainVM import kotlinx.coroutines.launch +import org.koin.androidx.compose.koinViewModel @SuppressLint("UnusedMaterial3ScaffoldPaddingParameter") // TODO remove Scaffold @OptIn(ExperimentalMaterial3Api::class, ExperimentalMaterial3AdaptiveApi::class) @Composable -fun HomePage() { +fun HomePage(viewModel: MainVM = koinViewModel()) { // TODO include tags in search val mActivity = OABX.main!! val scope = rememberCoroutineScope() - val viewModel = mActivity.viewModel val scaffoldState = rememberBottomSheetScaffoldState() val paneNavigator = rememberListDetailPaneScaffoldNavigator() @@ -377,11 +378,10 @@ fun HomePage() { if (openBlocklist.value) BaseDialog(openDialogCustom = openBlocklist) { GlobalBlockListDialogUI( - currentBlocklist = OABX.main?.viewModel?.getBlocklist()?.toSet() - ?: emptySet(), + currentBlocklist = viewModel.getBlocklist().toSet(), openDialogCustom = openBlocklist, ) { newSet -> - OABX.main?.viewModel?.setBlocklist(newSet) + viewModel.setBlocklist(newSet) } } if (openBatchDialog.value) BaseDialog(openDialogCustom = openBatchDialog) { diff --git a/src/main/java/com/machiav3lli/backup/pages/MainPage.kt b/src/main/java/com/machiav3lli/backup/pages/MainPage.kt index 85c3769e7..3d2e98301 100644 --- a/src/main/java/com/machiav3lli/backup/pages/MainPage.kt +++ b/src/main/java/com/machiav3lli/backup/pages/MainPage.kt @@ -53,12 +53,15 @@ import com.machiav3lli.backup.ui.compose.recycler.FullScreenBackground import com.machiav3lli.backup.ui.navigation.NavItem import com.machiav3lli.backup.ui.navigation.NeoNavigationSuiteScaffold import com.machiav3lli.backup.ui.navigation.SlidePager +import com.machiav3lli.backup.viewmodels.MainVM import kotlinx.collections.immutable.persistentListOf import kotlinx.coroutines.launch +import org.koin.androidx.compose.koinViewModel @Composable fun MainPage( navController: NavHostController, + viewModel: MainVM = koinViewModel(), ) { val scope = rememberCoroutineScope() val pages = persistentListOf( @@ -76,7 +79,7 @@ fun MainPage( var query by rememberSaveable { mutableStateOf( - OABX.main?.viewModel?.searchQuery?.value ?: "" + viewModel.searchQuery.value ?: "" ) } @@ -122,11 +125,11 @@ fun MainPage( onQueryChanged = { newQuery -> //if (newQuery != query) // empty string doesn't work... query = newQuery - OABX.main?.viewModel?.searchQuery?.value = query + viewModel.searchQuery?.value = query }, onClose = { query = "" - OABX.main?.viewModel?.searchQuery?.value = "" + viewModel.searchQuery.value = "" } ) AnimatedVisibility(!searchExpanded.value) { @@ -156,11 +159,10 @@ fun MainPage( if (openBlocklist.value) BaseDialog(openDialogCustom = openBlocklist) { GlobalBlockListDialogUI( - currentBlocklist = OABX.main?.viewModel?.getBlocklist()?.toSet() - ?: emptySet(), + currentBlocklist = viewModel.getBlocklist().toSet(), openDialogCustom = openBlocklist, ) { newSet -> - OABX.main?.viewModel?.setBlocklist(newSet) + viewModel.setBlocklist(newSet) } } } diff --git a/src/main/java/com/machiav3lli/backup/sheets/SortFilterSheet.kt b/src/main/java/com/machiav3lli/backup/sheets/SortFilterSheet.kt index 724542b76..b9a1bce97 100644 --- a/src/main/java/com/machiav3lli/backup/sheets/SortFilterSheet.kt +++ b/src/main/java/com/machiav3lli/backup/sheets/SortFilterSheet.kt @@ -82,13 +82,16 @@ import com.machiav3lli.backup.utils.applyFilter import com.machiav3lli.backup.utils.getStats import com.machiav3lli.backup.utils.sortFilterModel import com.machiav3lli.backup.utils.specialBackupsEnabled +import com.machiav3lli.backup.viewmodels.MainVM +import org.koin.androidx.compose.koinViewModel @Composable -fun SortFilterSheet(onDismiss: () -> Unit) { // TODO add key for each page (HOME, BACKUP, RESTORE) - val context = LocalContext.current - val mActivity = OABX.main!! +fun SortFilterSheet( + viewModel: MainVM = koinViewModel(), + onDismiss: () -> Unit, +) { // TODO add key for each page (HOME, BACKUP, RESTORE) val nestedScrollConnection = rememberNestedScrollInteropConnection() - val packageList by mActivity.viewModel.notBlockedList.collectAsState() + val packageList by viewModel.notBlockedList.collectAsState() var model by rememberSaveable { mutableStateOf(sortFilterModel) } fun currentStats() = getStats( packageList.applyFilter(