Skip to content

Commit

Permalink
Update project
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbel committed Nov 9, 2024
1 parent 79886ff commit 7a27ef6
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 3 deletions.
13 changes: 12 additions & 1 deletion app/src/main/kotlin/org/michaelbel/template/App.kt
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)
}
}
}
19 changes: 19 additions & 0 deletions app/src/main/kotlin/org/michaelbel/template/AppModule.kt
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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import org.michaelbel.template.ui.MainActivityContent

class MainActivity: AppCompatActivity() {

Expand Down
8 changes: 8 additions & 0 deletions app/src/main/kotlin/org/michaelbel/template/MainViewModel.kt
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()
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)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@file:OptIn(ExperimentalMaterial3Api::class)

package org.michaelbel.template
package org.michaelbel.template.ui

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
Expand Down Expand Up @@ -46,10 +46,12 @@ import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import kotlinx.coroutines.launch
import org.michaelbel.template.ui.AppTheme
import org.koin.androidx.compose.koinViewModel
import org.michaelbel.template.MainViewModel

@Composable
fun MainActivityContent(
viewModel: MainViewModel = koinViewModel(),
modifier: Modifier = Modifier
) {
val navHostController = rememberNavController()
Expand Down
2 changes: 2 additions & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ dependencies {
api(libs.androidx.compose.ui.tooling)
api(libs.androidx.core.ktx)
api(libs.androidx.core.splashscreen)
api(libs.androidx.datastore.preferences)
api(libs.androidx.lifecycle.viewmodel.compose)
api(libs.androidx.navigation.compose)
api(libs.androidx.paging.compose)
api(libs.koin.androidx.compose)
}

0 comments on commit 7a27ef6

Please sign in to comment.