Skip to content

Commit

Permalink
refactor: Migrated from MMKV to DataStore for preferences
Browse files Browse the repository at this point in the history
This commit replaces MMKV with DataStore for managing application preferences.
This change improves the overall structure and maintainability of the preferences system by utilizing a more modern and robust solution offered by Jetpack DataStore.

The `AppPreferences` class now handles preference management with DataStore, and a `PreferenceDelegate` was introduced for type-safe preference delegation.

Additionally, the `CoreSettings` class, previously used with MMKV, has been removed and its logic integrated into the `AppPreferences` structure.
  • Loading branch information
BobbyESP committed Jan 24, 2025
1 parent b52baf3 commit d21c6e0
Show file tree
Hide file tree
Showing 25 changed files with 480 additions and 503 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ dependencies {
annotationProcessor(libs.room.compiler)

//-------------------Key-value Storage-------------------//
implementation(libs.mmkv)
implementation(libs.datastore.preferences)

//-------------------Image Loading-------------------//
implementation(libs.landscapist.coil)
Expand Down
17 changes: 2 additions & 15 deletions app/src/main/java/com/bobbyesp/metadator/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,11 @@ import com.bobbyesp.crashhandler.ReportInfo
import com.bobbyesp.metadator.di.appCoroutinesScope
import com.bobbyesp.metadator.di.appMainViewModels
import com.bobbyesp.metadator.di.appSystemManagers
import com.bobbyesp.metadator.di.coreFunctionalitiesModule
import com.bobbyesp.metadator.di.mediaplayerViewModels
import com.bobbyesp.metadator.di.utilitiesViewModels
import com.bobbyesp.metadator.features.spotify.di.spotifyMainModule
import com.bobbyesp.metadator.features.spotify.di.spotifyServicesModule
import com.bobbyesp.metadator.util.preferences.PreferencesKeys
import com.bobbyesp.metadator.util.preferences.PreferencesKeys.BooleanPreferenceDefaults
import com.bobbyesp.metadator.util.preferences.PreferencesKeys.IntPreferenceDefaults
import com.bobbyesp.metadator.util.preferences.PreferencesKeys.StringPreferenceDefaults
import com.bobbyesp.utilities.Preferences
import com.tencent.mmkv.MMKV
import mediaplayerInternalsModule
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
Expand All @@ -27,17 +22,10 @@ import kotlin.properties.Delegates

class App : Application() {
override fun onCreate() {
MMKV.initialize(this)
preferences = Preferences(
PreferencesKeys.MMKV_PREFERENCES_NAME,
IntPreferenceDefaults,
StringPreferenceDefaults,
BooleanPreferenceDefaults
)
startKoin {
androidLogger()
androidContext(this@App)
modules(appSystemManagers, appCoroutinesScope)
modules(appSystemManagers, appCoroutinesScope, coreFunctionalitiesModule)
modules(mediaplayerInternalsModule)
modules(appMainViewModels, utilitiesViewModels, mediaplayerViewModels)
modules(spotifyMainModule, spotifyServicesModule)
Expand All @@ -61,7 +49,6 @@ class App : Application() {
}

companion object {
lateinit var preferences: Preferences
lateinit var packageInfo: PackageInfo
var isPlayStoreBuild by Delegates.notNull<Boolean>()

Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/com/bobbyesp/metadator/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.bobbyesp.mediaplayer.service.MediaplayerService
import com.bobbyesp.metadator.presentation.Navigator
import com.bobbyesp.metadator.presentation.common.AppLocalSettingsProvider
import com.bobbyesp.metadator.presentation.theme.MetadatorTheme
import com.bobbyesp.metadator.util.preferences.CoreSettings
import com.bobbyesp.metadator.util.preferences.AppPreferences
import org.koin.android.ext.android.inject
import org.koin.compose.KoinContext
import org.koin.core.component.KoinComponent
Expand All @@ -24,6 +24,7 @@ class MainActivity : ComponentActivity(), KoinComponent {
private var isMusicPlayerServiceStarted = false

private val connectionHandler: ConnectionHandler by inject()
private val appPreferences: AppPreferences by inject()

@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -37,7 +38,7 @@ class MainActivity : ComponentActivity(), KoinComponent {
AppLocalSettingsProvider(
windowWidthSize = windowSizeClass.widthSizeClass,
playerConnectionHandler = connectionHandler,
coreSettings = CoreSettings.initialize(App.preferences.kv)
appPreferences = appPreferences
) {
MetadatorTheme {
Navigator()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.bobbyesp.metadator.di

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import com.bobbyesp.metadator.util.preferences.AppPreferences
import com.bobbyesp.metadator.util.preferences.datastore.dataStore
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import org.koin.android.ext.koin.androidContext
import org.koin.core.qualifier.named
import org.koin.dsl.module

val appCoroutinesScope = module {
single<CoroutineScope>(
qualifier = named("AppMainSupervisedScope")
) { CoroutineScope(SupervisorJob()) }
}

val coreFunctionalitiesModule = module {
single<DataStore<Preferences>> { androidContext().dataStore }
single<AppPreferences> {
AppPreferences(
dataStore = androidContext().dataStore,
scope = get(qualifier = named("AppMainSupervisedScope"))
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import org.koin.android.ext.koin.androidApplication
import org.koin.android.ext.koin.androidContext
import org.koin.core.qualifier.named
import org.koin.dsl.module

val appSystemManagers = module {
single<ClipboardManager> { androidApplication().getSystemService(CLIPBOARD_SERVICE) as ClipboardManager }
single<ConnectivityManager> { androidContext().getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager }
}

val appCoroutinesScope = module {
single { CoroutineScope(SupervisorJob()) }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bobbyesp.metadator.presentation.pages.home
package com.bobbyesp.metadator.domain.enums

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.rounded.List
Expand Down
18 changes: 17 additions & 1 deletion app/src/main/java/com/bobbyesp/metadator/ext/String.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.stringResource
import com.bobbyesp.metadator.R
import com.bobbyesp.metadator.util.executeIfDebugging

object TagLib {
@Composable
Expand Down Expand Up @@ -56,4 +57,19 @@ object TagLib {
else -> Icons.Rounded.RunningWithErrors
}
}
}
}

object StringPreferencesExtensions {

inline fun <reified T : Enum<T>> String?.toEnum(defaultValue: T): T =
if (this == null) defaultValue
else try {
enumValueOf(this)
} catch (e: IllegalArgumentException) {
executeIfDebugging {
e.printStackTrace()
}
defaultValue
}

}

This file was deleted.

Loading

0 comments on commit d21c6e0

Please sign in to comment.