Skip to content

Commit

Permalink
Update: Replace unsecure refs with MainVM injection in pages/sheets
Browse files Browse the repository at this point in the history
  • Loading branch information
machiav3lli committed Oct 26, 2024
1 parent 922e21b commit f11977c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 21 deletions.
7 changes: 5 additions & 2 deletions src/main/java/com/machiav3lli/backup/pages/AppPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -305,7 +308,7 @@ fun AppPage(
icon = Phosphor.Prohibit,
description = stringResource(id = R.string.global_blocklist_add)
) {
mActivity.viewModel.addToBlocklist(
mainVM.addToBlocklist(
pkg.packageName
)
}
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/com/machiav3lli/backup/pages/BatchPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/machiav3lli/backup/pages/HomePage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Any>()

Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/com/machiav3lli/backup/pages/MainPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -76,7 +79,7 @@ fun MainPage(

var query by rememberSaveable {
mutableStateOf(
OABX.main?.viewModel?.searchQuery?.value ?: ""
viewModel.searchQuery.value ?: ""
)
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/com/machiav3lli/backup/sheets/SortFilterSheet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit f11977c

Please sign in to comment.