Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
machiav3lli committed Nov 15, 2024
1 parent 0a23bac commit dd2ea70
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 31 deletions.
4 changes: 0 additions & 4 deletions src/main/java/com/machiav3lli/backup/OABX.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import com.google.android.material.color.DynamicColors
import com.google.android.material.color.DynamicColorsOptions
import com.machiav3lli.backup.activities.MainActivityX
import com.machiav3lli.backup.activities.viewModelsModule
import com.machiav3lli.backup.dbs.ODatabase
import com.machiav3lli.backup.dbs.databaseModule
import com.machiav3lli.backup.dbs.entity.Backup
import com.machiav3lli.backup.dbs.entity.SpecialInfo
Expand Down Expand Up @@ -105,7 +104,6 @@ val RESCUE_NAV get() = "rescue"
class OABX : Application() {

val work: WorkHandler by inject()
val db: ODatabase by inject()

// TODO Add BroadcastReceiver for (UN)INSTALL_PACKAGE intents

Expand Down Expand Up @@ -471,8 +469,6 @@ class OABX : Application() {

var appsSuspendedChecked = false

val db: ODatabase get() = NB.db

var shellHandler: ShellHandler? = null
private set

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.res.stringResource
Expand All @@ -50,6 +52,10 @@ import com.machiav3lli.backup.OABX.Companion.addInfoLogText
import com.machiav3lli.backup.OABX.Companion.startup
import com.machiav3lli.backup.R
import com.machiav3lli.backup.RESCUE_NAV
import com.machiav3lli.backup.dbs.repository.AppExtrasRepository
import com.machiav3lli.backup.dbs.repository.BlocklistRepository
import com.machiav3lli.backup.dbs.repository.PackageRepository
import com.machiav3lli.backup.dbs.repository.ScheduleRepository
import com.machiav3lli.backup.dialogs.ActionsDialogUI
import com.machiav3lli.backup.dialogs.BaseDialog
import com.machiav3lli.backup.dialogs.DialogKey
Expand Down Expand Up @@ -116,7 +122,7 @@ class MainActivityX : BaseActivity() {
private lateinit var openDialog: MutableState<Boolean>
private lateinit var dialogKey: MutableState<DialogKey?>

val viewModel: MainVM by viewModel()
val viewModel: MainVM by viewModel() // TODO remove usage in other classes

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/machiav3lli/backup/entity/Package.kt
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ class Package {
fun deleteAllBackups() {
val backups = backupsNewestFirst.toMutableList()
while (backups.isNotEmpty())
_deleteBackup(backups.removeLast())
_deleteBackup(backups.removeLast()) // TODO
if (pref_paranoidBackupLists.value)
runOrLog { refreshBackupList() } // get real state of file system only once
}
Expand All @@ -324,7 +324,7 @@ class Package {
} --> delete ${TraceUtils.formatBackups(deletableBackups)}"
}
while (deletableBackups.size > 0) {
val backup = deletableBackups.removeLast()
val backup = deletableBackups.removeLast() // TODO
backups.remove(backup)
_deleteBackup(backup)
}
Expand All @@ -339,7 +339,7 @@ class Package {
}
while (keep < backups.size && deletableBackups.size > 0) {

val backup = deletableBackups.removeLast()
val backup = deletableBackups.removeLast() // TODO
backups.remove(backup)
_deleteBackup(backup)
}
Expand Down
67 changes: 56 additions & 11 deletions src/main/java/com/machiav3lli/backup/handler/BackendController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,9 @@ suspend fun scanBackups(
}
}

suspend fun handleDirectory(
file: StorageFile,
collector: FlowCollector<StorageFile>? = null
suspend fun handleDirectory(
file: StorageFile,
collector: FlowCollector<StorageFile>? = null
): Boolean {

hitBusy()
Expand Down Expand Up @@ -332,7 +332,14 @@ suspend fun scanBackups(
!name.contains(regexSpecialFile)
) {
val props = file
traceBackupsScanPackage { traceLine(">", level, props, "++++++++++++++++++++ props ok") }
traceBackupsScanPackage {
traceLine(
">",
level,
props,
"++++++++++++++++++++ props ok"
)
}

handleProps(props, path, name, onValidBackup)

Expand All @@ -352,7 +359,14 @@ suspend fun scanBackups(
dir.findFile(BACKUP_INSTANCE_PROPERTIES_INDIR) // indir props
?.let { props ->

traceBackupsScanPackage { traceLine(">", level, props, "++++++++++++++++++++ props indir ok") }
traceBackupsScanPackage {
traceLine(
">",
level,
props,
"++++++++++++++++++++ props indir ok"
)
}

handleProps(props, props.path, props.name, onValidBackup) {
runCatching {
Expand Down Expand Up @@ -410,7 +424,14 @@ suspend fun scanBackups(
name.contains(regexBackupInstance) // or backup instance
) {
if (forceTrace)
traceBackupsScanPackage { traceLine("B", level, file, "++++++++++++++++++++ backup") }
traceBackupsScanPackage {
traceLine(
"B",
level,
file,
"++++++++++++++++++++ backup"
)
}

if (path.contains(packageName)) { // package matches, empty matches all

Expand All @@ -423,14 +444,28 @@ suspend fun scanBackups(
if (file.isPropertyFile &&
!name.contains(regexSpecialFile) // non-instance props (wtf is that? probably a saved file)
) {
traceBackupsScanPackage { traceLine(">", level, file, "++++++++++++++++++++ non-instance props ok (a renamed backup?)") }
traceBackupsScanPackage {
traceLine(
">",
level,
file,
"++++++++++++++++++++ non-instance props ok (a renamed backup?)"
)
}

handleProps(file, path, name, onValidBackup)

} else {
if (file.isDirectory) { // non-instance-directory
val dir = file
traceBackupsScanPackage { traceLine("/", level, file, "++++++++++++++++++++ //////////////////// dir ok") }
traceBackupsScanPackage {
traceLine(
"/",
level,
file,
"++++++++++++++++++++ //////////////////// dir ok"
)
}

if (handleDirectory(dir).not()) {
// renameDamagedToERROR(dir, "empty-folder")
Expand All @@ -450,7 +485,14 @@ suspend fun scanBackups(
) {
val dir = file
if (forceTrace)
traceBackupsScanPackage { traceLine("F", level, file, "/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\ folder ok") }
traceBackupsScanPackage {
traceLine(
"F",
level,
file,
"/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\ folder ok"
)
}

if (handleDirectory(dir).not()) {
// renameDamagedToERROR(dir, "empty-folder")
Expand Down Expand Up @@ -629,7 +671,8 @@ fun Context.getPackageInfoList(filter: Int): List<PackageInfo> =
packageManager.getInstalledPackageInfosWithPermissions()
.filter { packageInfo: PackageInfo ->
val isSystem =
(packageInfo.applicationInfo?.flags ?: 0) and ApplicationInfo.FLAG_SYSTEM == ApplicationInfo.FLAG_SYSTEM
(packageInfo.applicationInfo?.flags ?: 0) and ApplicationInfo.FLAG_SYSTEM ==
ApplicationInfo.FLAG_SYSTEM
val isIgnored = packageInfo.packageName.matches(ignoredPackages)
if (isIgnored)
Timber.i("ignored package: ${packageInfo.packageName}")
Expand Down Expand Up @@ -889,6 +932,8 @@ fun Context.getSpecial(packageName: String) =

val PackageInfo.grantedPermissions: List<String>
get() = requestedPermissions?.filterIndexed { index, perm ->
(requestedPermissionsFlags?.getOrNull(index) ?: 0) and PackageInfo.REQUESTED_PERMISSION_GRANTED == PackageInfo.REQUESTED_PERMISSION_GRANTED &&
(requestedPermissionsFlags?.getOrNull(index)
?: 0) and PackageInfo.REQUESTED_PERMISSION_GRANTED ==
PackageInfo.REQUESTED_PERMISSION_GRANTED &&
perm !in IGNORED_PERMISSIONS
}.orEmpty()
7 changes: 3 additions & 4 deletions src/main/java/com/machiav3lli/backup/pages/AppPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ fun AppPage(
onDismiss()
}
}
AnimatedVisibility(visible = snackbarVisible) {
AnimatedVisibility(visible = snackbarVisible) { // TODO move to MainPage
Text(
text = snackbarText,
color = MaterialTheme.colorScheme.primary,
Expand All @@ -256,11 +256,10 @@ fun AppPage(
InfoChipsBlock(list = pkg.infoChips())
if (snackbarVisible)
LinearProgressIndicator(
modifier = Modifier
.fillMaxWidth()
.height(5.dp),
modifier = Modifier.fillMaxWidth(),
trackColor = MaterialTheme.colorScheme.surface,
color = MaterialTheme.colorScheme.primary,
gapSize = 2.dp,
)
HorizontalDivider(
thickness = 2.dp,
Expand Down
77 changes: 77 additions & 0 deletions src/main/java/com/machiav3lli/backup/preferences/NeoPrefs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import com.machiav3lli.backup.MAIN_FILTER_DEFAULT
import com.machiav3lli.backup.Sort
import com.machiav3lli.backup.UpdatedFilter
import com.machiav3lli.backup.batchModesSequence
import com.machiav3lli.backup.entity.SortFilterModel
import com.machiav3lli.backup.possibleMainFilters
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flowOn
import org.koin.core.component.KoinComponent
import org.koin.dsl.module
import org.koin.java.KoinJavaComponent.getKoin
Expand Down Expand Up @@ -208,6 +213,78 @@ class NeoPrefs private constructor(val context: Context) : KoinComponent {
entries = EnabledFilter.entries.map { it.ordinal },
)

fun homeSortFilterFlow(): Flow<SortFilterModel> = combine(
sortHome.get(),
sortAscHome.get(),
mainFilterHome.get(),
backupFilterHome.get(),
installedFilterHome.get(),
launchableFilterHome.get(),
updatedFilterHome.get(),
latestFilterHome.get(),
enabledFilterHome.get(),
) { args ->
SortFilterModel(
sort = args[0] as Int,
sortAsc = args[1] as Boolean,
mainFilter = args[2] as Int,
backupFilter = args[3] as Int,
installedFilter = args[4] as Int,
launchableFilter = args[5] as Int,
updatedFilter = args[6] as Int,
latestFilter = args[7] as Int,
enabledFilter = args[8] as Int,
)
}.flowOn(Dispatchers.IO)

fun backupSortFilterFlow(): Flow<SortFilterModel> = combine(
sortBackup.get(),
sortAscBackup.get(),
mainFilterBackup.get(),
backupFilterBackup.get(),
installedFilterBackup.get(),
launchableFilterBackup.get(),
updatedFilterBackup.get(),
latestFilterBackup.get(),
enabledFilterBackup.get(),
) { args ->
SortFilterModel(
sort = args[0] as Int,
sortAsc = args[1] as Boolean,
mainFilter = args[2] as Int,
backupFilter = args[3] as Int,
installedFilter = args[4] as Int,
launchableFilter = args[5] as Int,
updatedFilter = args[6] as Int,
latestFilter = args[7] as Int,
enabledFilter = args[8] as Int,
)
}.flowOn(Dispatchers.IO)

fun restoreSortFilterFlow(): Flow<SortFilterModel> = combine(
sortRestore.get(),
sortAscRestore.get(),
mainFilterRestore.get(),
backupFilterRestore.get(),
installedFilterRestore.get(),
launchableFilterRestore.get(),
updatedFilterRestore.get(),
latestFilterRestore.get(),
enabledFilterRestore.get(),
) { args ->
SortFilterModel(
sort = args[0] as Int,
sortAsc = args[1] as Boolean,
mainFilter = args[2] as Int,
backupFilter = args[3] as Int,
installedFilter = args[4] as Int,
launchableFilter = args[5] as Int,
updatedFilter = args[6] as Int,
latestFilter = args[7] as Int,
enabledFilter = args[8] as Int,
)
}.flowOn(Dispatchers.IO)

companion object {
val prefsModule = module {
single { NeoPrefs(get()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ import com.machiav3lli.backup.InstalledFilter
import com.machiav3lli.backup.LatestFilter
import com.machiav3lli.backup.LaunchableFilter
import com.machiav3lli.backup.MAIN_FILTER_DEFAULT
import com.machiav3lli.backup.OABX
import com.machiav3lli.backup.R
import com.machiav3lli.backup.UpdatedFilter
import com.machiav3lli.backup.enabledFilterChipItems
Expand Down Expand Up @@ -95,6 +94,7 @@ fun SortFilterSheet(
viewModel: MainVM = koinViewModel(),
onDismiss: () -> Unit,
) {
val context = LocalContext.current
val nestedScrollConnection = rememberNestedScrollInteropConnection()
val packageList by viewModel.notBlockedList.collectAsState()
var model by rememberSaveable {
Expand All @@ -110,7 +110,7 @@ fun SortFilterSheet(
fun currentStats() = getStats(
packageList.applyFilter(
model,
OABX.context,
context,
)
) //TODO hg42 use central function for all the filtering

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/machiav3lli/backup/utils/FilterUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fun filterPackages(

//---------------------------------------- filters for activity

fun List<Package>.applySearchAndFilter(
fun Collection<Package>.applySearchAndFilter(
context: Context,
query: String,
extras: Map<String, AppExtras>,
Expand All @@ -118,7 +118,7 @@ fun List<Package>.applySearchAndFilter(
.applyFilter(filter, context)
}

fun List<Package>.applyFilter(filter: SortFilterModel, context: Context): List<Package> {
fun Collection<Package>.applyFilter(filter: SortFilterModel, context: Context): List<Package> {
val predicate: (Package) -> Boolean = {
(if (filter.mainFilter and MAIN_FILTER_SYSTEM == MAIN_FILTER_SYSTEM) it.isSystem && !it.isSpecial else false)
|| (if (filter.mainFilter and MAIN_FILTER_USER == MAIN_FILTER_USER) !it.isSystem else false)
Expand All @@ -130,7 +130,7 @@ fun List<Package>.applyFilter(filter: SortFilterModel, context: Context): List<P
.applySort(filter.sort, filter.sortAsc)
}

private fun List<Package>.applyBackupFilter(backupFilter: Int): List<Package> {
private fun Collection<Package>.applyBackupFilter(backupFilter: Int): List<Package> {
val predicate: (Package) -> Boolean = {
(if (backupFilter and MODE_NONE == MODE_NONE) !it.hasBackups or !(it.hasApk or it.hasData)
else false)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/machiav3lli/backup/viewmodels/AppVM.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import timber.log.Timber

class AppVM(private val database: ODatabase) : ViewModel() {
class AppVM(private val database: ODatabase) : ViewModel() { // TODO add repos
private val thePackage: MutableStateFlow<Package?> = MutableStateFlow(null)

@OptIn(ExperimentalCoroutinesApi::class)
Expand All @@ -55,7 +55,7 @@ class AppVM(private val database: ODatabase) : ViewModel() {
AppExtras(thePackage.value?.packageName ?: "")
)

val snackbarText = MutableComposableStateFlow(
val snackbarText = MutableComposableStateFlow( // TODO change to MutableStateFlow
"",
viewModelScope,
"snackBarText"
Expand Down
Loading

0 comments on commit dd2ea70

Please sign in to comment.