-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79886ff
commit 7a27ef6
Showing
7 changed files
with
89 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
package org.michaelbel.template | ||
|
||
import android.app.Application | ||
import org.koin.android.ext.koin.androidContext | ||
import org.koin.core.context.startKoin | ||
|
||
class App: Application() | ||
class App: Application() { | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
startKoin { | ||
androidContext(this@App) | ||
modules(appModule) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.michaelbel.template | ||
|
||
import androidx.datastore.preferences.core.PreferenceDataStoreFactory | ||
import okio.Path.Companion.toPath | ||
import org.koin.android.ext.koin.androidContext | ||
import org.koin.androidx.viewmodel.dsl.viewModelOf | ||
import org.koin.dsl.module | ||
import org.michaelbel.template.datastore.AppPreferences | ||
|
||
val appModule = module { | ||
single<AppPreferences> { | ||
val dataStore = PreferenceDataStoreFactory.createWithPath( | ||
migrations = emptyList(), | ||
produceFile = { androidContext().filesDir.resolve(AppPreferences.DATA_STORE_NAME).absolutePath.toPath() } | ||
) | ||
AppPreferences(dataStore) | ||
} | ||
viewModelOf(::MainViewModel) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.michaelbel.template | ||
|
||
import org.michaelbel.core.viewmodel.BaseViewModel | ||
import org.michaelbel.template.datastore.AppPreferences | ||
|
||
class MainViewModel( | ||
private val appPreferences: AppPreferences | ||
): BaseViewModel() |
43 changes: 43 additions & 0 deletions
43
app/src/main/kotlin/org/michaelbel/template/datastore/AppPreferences.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.michaelbel.template.datastore | ||
|
||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.edit | ||
import androidx.datastore.preferences.core.stringPreferencesKey | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.flow.map | ||
|
||
class AppPreferences( | ||
private val dataStore: DataStore<Preferences> | ||
) { | ||
val usernameFlow: Flow<String?> | ||
get() = dataStore.data.map { preferences -> preferences[PREFERENCE_USERNAME_KEY] } | ||
|
||
suspend fun username(): String { | ||
return dataStore.data.first()[PREFERENCE_USERNAME_KEY].orEmpty() | ||
} | ||
|
||
suspend fun <T> setValue(key: PreferenceKey<T>, value: T) { | ||
dataStore.edit { preferences -> | ||
preferences[key.preferenceKey] = value | ||
} | ||
} | ||
|
||
suspend fun <T> removeValue(key: PreferenceKey<T>) { | ||
dataStore.edit { preferences -> | ||
preferences.remove(key.preferenceKey) | ||
} | ||
} | ||
|
||
companion object { | ||
const val DATA_STORE_NAME = "app.preferences_pb" | ||
private val PREFERENCE_USERNAME_KEY = stringPreferencesKey("username") | ||
} | ||
|
||
sealed class PreferenceKey<T>( | ||
val preferenceKey: Preferences.Key<T> | ||
) { | ||
data object PreferenceUsernameKey: PreferenceKey<String>(PREFERENCE_USERNAME_KEY) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters