Skip to content

Commit

Permalink
Reworked the app's main code for better structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihai-Cristian Condrea committed Oct 20, 2024
1 parent 9cdf416 commit 67b1dd8
Show file tree
Hide file tree
Showing 26 changed files with 254 additions and 159 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ android {
applicationId = "com.d4rk.cleaner"
minSdk = 23
targetSdk = 35
versionCode = 134
versionCode = 138
versionName = "3.0.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
resourceConfigurations += listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ open class DataStoreCoreManager(protected val context: Context) {
lateinit var dataStore: DataStore

suspend fun initializeDataStore(): Boolean = coroutineScope {
dataStore = DataStore.getInstance(context.applicationContext)
dataStore = DataStore.getInstance(context = context.applicationContext)

listOf(
async { dataStore.getStartupPage().firstOrNull() ?: BottomBarRoutes.HOME },
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/kotlin/com/d4rk/cleaner/data/datastore/DataStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,29 @@ class DataStore(context: Context) {
}
}

private val trashSizeKey = longPreferencesKey(name = "trash_size")
val trashSize: Flow<Long> = dataStore.data.map { preferences ->
preferences[trashSizeKey] ?: 0L
}

suspend fun addTrashSize(size: Long) {
dataStore.edit { preferences ->
preferences[trashSizeKey] = (preferences[trashSizeKey] ?: 0L) + size
}
}

suspend fun subtractTrashSize(size: Long) {
dataStore.edit { preferences ->
preferences[trashSizeKey] = (preferences[trashSizeKey] ?: 0L) - size
}
}

suspend fun clearTrashSize() {
dataStore.edit { preferences ->
preferences[trashSizeKey] = 0L
}
}

private val genericFilterKey =
booleanPreferencesKey(name = DataStoreNamesConstants.DATA_STORE_GENERIC_FILTER)
val genericFilter: Flow<Boolean> = dataStore.data.map { preferences ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package com.d4rk.cleaner.data.model.ui.navigation
import androidx.compose.ui.graphics.vector.ImageVector

data class NavigationDrawerItem(
val title: Int, val selectedIcon: ImageVector, val badgeCount: Int? = null
val title: Int, val selectedIcon: ImageVector, val badgeText: String = ""
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.d4rk.cleaner.data.model.ui.screens

import com.d4rk.cleaner.data.model.ui.navigation.BottomNavigationScreen

data class UiMainModel(
val isNavigationDrawerOpen: Boolean = false,
val currentBottomNavigationScreen: BottomNavigationScreen = BottomNavigationScreen.Home,
val trashSize: String = "0 KB",
)
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import kotlinx.coroutines.runBlocking
*/
class AppUsageNotificationWorker(context: Context, workerParams: WorkerParameters) :
Worker(context, workerParams) {
private val dataStore = DataStore.getInstance(context)
private val dataStore = DataStore.getInstance(context = context)
private val appUsageChannelId = "app_usage_channel"
private val appUsageNotificationId = 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fun Modifier.bounceClick(
) : Modifier = composed {
var buttonState : ButtonState by remember { mutableStateOf(ButtonState.Idle) }
val context : Context = LocalContext.current
val dataStore : DataStore = DataStore.getInstance(context)
val dataStore : DataStore = DataStore.getInstance(context = context)
val bouncyButtonsEnabled : Boolean by dataStore.bouncyButtons.collectAsState(initial = true)
val scale : Float by animateFloatAsState(
if (buttonState == ButtonState.Pressed && animationEnabled && bouncyButtonsEnabled) 0.96f else 1f ,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fun ErrorAlertDialog(
AlertDialog(
onDismissRequest = onDismiss,
title = { Text(text = "Error") },
text = { Text(errorMessage) },
text = { Text(text = errorMessage) },
confirmButton = {
TextButton(onClick = onDismiss) {
Text(text = stringResource(id = android.R.string.ok))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ fun SelectLanguageAlertDialog(
onLanguageSelected(selectedLanguage.value)
onDismiss()
}) {
Text(stringResource(id = android.R.string.ok))
Text(text = stringResource(id = android.R.string.ok))
}
}, dismissButton = {
TextButton(onClick = onDismiss) {
Text(stringResource(id = android.R.string.cancel))
Text(text = stringResource(id = android.R.string.cancel))
}
})
}
Expand All @@ -74,7 +74,7 @@ fun SelectLanguageAlertDialogContent(
}

Column {
Text(stringResource(id = R.string.dialog_language_subtitle))
Text(text = stringResource(id = R.string.dialog_language_subtitle))
Box(
modifier = Modifier
.fillMaxWidth()
Expand Down Expand Up @@ -103,7 +103,7 @@ fun SelectLanguageAlertDialogContent(
Spacer(modifier = Modifier.height(24.dp))
Icon(imageVector = Icons.Outlined.Info, contentDescription = null)
Spacer(modifier = Modifier.height(12.dp))
Text(stringResource(id = R.string.dialog_info_language))
Text(text = stringResource(id = R.string.dialog_info_language))
}

LaunchedEffect(selectedLanguage.value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import androidx.navigation.NavHostController
import com.d4rk.cleaner.R
import com.d4rk.cleaner.data.datastore.DataStore
import com.d4rk.cleaner.data.model.ui.navigation.NavigationDrawerItem
import com.d4rk.cleaner.data.model.ui.screens.UiMainModel
import com.d4rk.cleaner.ui.components.animations.bounceClick
import com.d4rk.cleaner.ui.components.animations.hapticDrawerSwipe
import com.d4rk.cleaner.ui.screens.help.HelpActivity
Expand All @@ -57,15 +58,15 @@ fun NavigationDrawer(
view: View,
dataStore: DataStore,
context: Context,
uiState: UiMainModel,
) {
val scope: CoroutineScope = rememberCoroutineScope()

val drawerItems: List<NavigationDrawerItem> = listOf(
NavigationDrawerItem(
title = R.string.image_optimizer, selectedIcon = Icons.Outlined.Image
),
NavigationDrawerItem(
title = R.string.trash, selectedIcon = Icons.Outlined.DeleteOutline
title = R.string.trash, selectedIcon = Icons.Outlined.DeleteOutline, badgeText = uiState.trashSize,
),
NavigationDrawerItem(
title = R.string.settings,
Expand Down Expand Up @@ -164,7 +165,9 @@ fun NavigationDrawer(
)
},
badge = {
item.badgeCount?.let { Text(text = it.toString()) }
item.badgeText.isNotBlank().let {
Text(text = item.badgeText)
}
},
modifier = Modifier
.padding(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,10 @@ class HomeViewModel(application : Application) : BaseViewModel(application) {
*/
fun moveToTrash() {
viewModelScope.launch(context = Dispatchers.Default + coroutineExceptionHandler) {
showLoading()
val filesToMove =
_uiState.value.analyzeState.fileSelectionMap.filter { it.value }.keys.toList()
showLoading()
val totalTrashSize = filesToMove.sumOf { it.length() }
repository.moveToTrash(filesToMove) {
_uiState.update { currentUiState ->
currentUiState.copy(analyzeState = currentUiState.analyzeState.copy(
Expand All @@ -237,6 +238,7 @@ class HomeViewModel(application : Application) : BaseViewModel(application) {
}
updateStorageInfo()
}
repository.addTrashSize(totalTrashSize)
hideLoading()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ class HomeRepository(
}
}

suspend fun addTrashSize(size: Long) {
dataStore.addTrashSize(size)
}

/**
* Restores the specified files from the trash directory.
* @param filesToRestore The set of files to restore from trash.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fun ImageOptimizerComposable(
activity: ImageOptimizerActivity , viewModel: ImageOptimizerViewModel
) {
val context: Context = LocalContext.current
val dataStore: DataStore = DataStore.getInstance(context)
val dataStore: DataStore = DataStore.getInstance(context = context)
val coroutineScope: CoroutineScope = rememberCoroutineScope()
val adsState: State<Boolean> = dataStore.ads.collectAsState(initial = true)
val tabs: List<String> = listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fun ImagePickerComposable(
) {
val context: Context = LocalContext.current
val view: View = LocalView.current
val dataStore: DataStore = DataStore.getInstance(context)
val dataStore: DataStore = DataStore.getInstance(context = context)
val adsState: State<Boolean> = dataStore.ads.collectAsState(initial = true)

LaunchedEffect(key1 = viewModel.selectedImageUri) {
Expand Down
Loading

0 comments on commit 67b1dd8

Please sign in to comment.