Refactored some code, added Dagger-Hilt in SettingsViewModel.kt which uses Datastore preferences
This commit is contained in:
1 parent
dbf0bd9a58
commit
d2530210ab
16 files changed
+216
-13
No files matched your search
Generated
+14
-1
@@ -3,7 +3,20 @@
|
||||
<component name="deploymentTargetDropDown">
|
||||
<value>
|
||||
<entry key="app">
|
||||
<State />
|
||||
<State>
|
||||
<runningDeviceTargetSelectedWithDropDown>
|
||||
<Target>
|
||||
<type value="RUNNING_DEVICE_TARGET" />
|
||||
<deviceKey>
|
||||
<Key>
|
||||
<type value="SERIAL_NUMBER" />
|
||||
<value value="adb-RFCTA1MRS4H-skBDcQ._adb-tls-connect._tcp" />
|
||||
</Key>
|
||||
</deviceKey>
|
||||
</Target>
|
||||
</runningDeviceTargetSelectedWithDropDown>
|
||||
<timeTargetWasSelectedWithDropDown value="2023-05-31T19:20:02.273701400Z" />
|
||||
</State>
|
||||
</entry>
|
||||
</value>
|
||||
</component>
|
||||
|
||||
+12
-3
@@ -1,6 +1,8 @@
|
||||
plugins {
|
||||
id("com.android.application")
|
||||
id("org.jetbrains.kotlin.android")
|
||||
kotlin("kapt")
|
||||
id("com.google.dagger.hilt.android")
|
||||
}
|
||||
|
||||
android {
|
||||
@@ -38,6 +40,7 @@ android {
|
||||
}
|
||||
buildFeatures {
|
||||
compose = true
|
||||
viewBinding = true
|
||||
}
|
||||
composeOptions {
|
||||
kotlinCompilerExtensionVersion = "1.3.2"
|
||||
@@ -53,12 +56,12 @@ dependencies {
|
||||
implementation("androidx.core:core-ktx:1.10.1")
|
||||
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")
|
||||
implementation("androidx.activity:activity-compose:1.7.2")
|
||||
implementation(platform("androidx.compose:compose-bom:2022.10.00"))
|
||||
implementation(platform("androidx.compose:compose-bom:2023.05.01"))
|
||||
implementation("androidx.compose.ui:ui")
|
||||
implementation("androidx.compose.ui:ui-graphics")
|
||||
implementation("androidx.compose.ui:ui-tooling-preview")
|
||||
implementation("androidx.compose.material3:material3")
|
||||
implementation("com.google.accompanist:accompanist-systemuicontroller:0.31.2-alpha")
|
||||
implementation("com.google.accompanist:accompanist-systemuicontroller:0.31.3-beta")
|
||||
implementation("androidx.navigation:navigation-compose:2.7.0-alpha01")
|
||||
implementation("androidx.compose.material:material:1.5.0-beta01")
|
||||
implementation("androidx.datastore:datastore:1.1.0-alpha04")
|
||||
@@ -66,8 +69,14 @@ dependencies {
|
||||
testImplementation("junit:junit:4.13.2")
|
||||
androidTestImplementation("androidx.test.ext:junit:1.1.5")
|
||||
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
|
||||
androidTestImplementation(platform("androidx.compose:compose-bom:2022.10.00"))
|
||||
androidTestImplementation(platform("androidx.compose:compose-bom:2023.05.01"))
|
||||
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
|
||||
debugImplementation("androidx.compose.ui:ui-tooling")
|
||||
debugImplementation("androidx.compose.ui:ui-test-manifest")
|
||||
implementation("com.google.dagger:hilt-android:2.44.2")
|
||||
kapt("com.google.dagger:hilt-android-compiler:2.44")
|
||||
}
|
||||
|
||||
kapt {
|
||||
correctErrorTypes = true
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:name=".App"
|
||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||
android:fullBackupContent="@xml/backup_rules"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.anthonyla.livewallpaper
|
||||
|
||||
import android.app.Application
|
||||
import dagger.hilt.android.HiltAndroidApp
|
||||
|
||||
@HiltAndroidApp
|
||||
class App: Application() {
|
||||
}
|
||||
@@ -3,15 +3,21 @@ package com.anthonyla.livewallpaper
|
||||
import android.os.Bundle
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.viewModels
|
||||
import com.anthonyla.livewallpaper.data.settings.SettingsViewModel
|
||||
import com.anthonyla.livewallpaper.themes.LiveWallpaperTheme
|
||||
import com.anthonyla.livewallpaper.ui.LiveWallpaperApp
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
|
||||
@AndroidEntryPoint
|
||||
class MainActivity : ComponentActivity() {
|
||||
private val settingsViewModel: SettingsViewModel by viewModels()
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContent {
|
||||
LiveWallpaperTheme {
|
||||
LiveWallpaperApp()
|
||||
LiveWallpaperApp(settingsViewModel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.anthonyla.livewallpaper.data.settings
|
||||
|
||||
import android.content.Context
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
object SettingsAppModule {
|
||||
@Singleton
|
||||
@Provides
|
||||
fun providesSettingsDataStore (
|
||||
@ApplicationContext context: Context
|
||||
): SettingsDataStore = SettingsDataStoreImpl(context)
|
||||
|
||||
@Provides
|
||||
fun provideContext(
|
||||
@ApplicationContext context: Context,
|
||||
): Context {
|
||||
return context
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.anthonyla.livewallpaper.data.settings
|
||||
|
||||
object SettingsConstants {
|
||||
const val SETTINGS_DATASTORE = "settings_datastore"
|
||||
const val DARK_MODE_TYPE = "dark_mode_type"
|
||||
const val DYNAMIC_THEME_TYPE = "dynamic_theme_type"
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.anthonyla.livewallpaper.data.settings
|
||||
|
||||
interface SettingsDataStore {
|
||||
suspend fun putBoolean(key: String, value: Boolean)
|
||||
suspend fun putString(key: String, value: String)
|
||||
suspend fun getBoolean(key: String): Boolean?
|
||||
suspend fun getString(key: String): String?
|
||||
suspend fun deleteBoolean(key: String)
|
||||
suspend fun deleteString(key: String)
|
||||
suspend fun clearPreferences()
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.anthonyla.livewallpaper.data.settings
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import androidx.datastore.preferences.core.booleanPreferencesKey
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import androidx.datastore.preferences.preferencesDataStore
|
||||
import com.anthonyla.livewallpaper.data.settings.SettingsConstants.SETTINGS_DATASTORE
|
||||
import kotlinx.coroutines.flow.first
|
||||
import javax.inject.Inject
|
||||
|
||||
private val Context.dataStore : DataStore<Preferences> by preferencesDataStore(name = SETTINGS_DATASTORE)
|
||||
class SettingsDataStoreImpl @Inject constructor (private val context: Context): SettingsDataStore {
|
||||
override suspend fun putBoolean(key: String, value: Boolean) {
|
||||
val preferencesKey = booleanPreferencesKey(key)
|
||||
context.dataStore.edit {
|
||||
it[preferencesKey] = value
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun putString(key: String, value: String) {
|
||||
val preferencesKey = stringPreferencesKey(key)
|
||||
context.dataStore.edit {
|
||||
it[preferencesKey] = value
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getBoolean(key: String): Boolean? {
|
||||
val head = context.dataStore.data.first()
|
||||
val preferencesKey = booleanPreferencesKey(key)
|
||||
return head[preferencesKey]
|
||||
}
|
||||
|
||||
override suspend fun getString(key: String): String? {
|
||||
return try {
|
||||
val head = context.dataStore.data.first()
|
||||
val preferencesKey = stringPreferencesKey(key)
|
||||
head[preferencesKey]
|
||||
} catch (exception: Exception) {
|
||||
exception.printStackTrace()
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun deleteBoolean(key: String) {
|
||||
val preferencesKey = booleanPreferencesKey(key)
|
||||
context.dataStore.edit {
|
||||
if (it.contains(preferencesKey)) {
|
||||
it.remove(preferencesKey)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun deleteString(key: String) {
|
||||
val preferencesKey = stringPreferencesKey(key)
|
||||
context.dataStore.edit {
|
||||
if (it.contains(preferencesKey)) {
|
||||
it.remove(preferencesKey)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun clearPreferences() {
|
||||
context.dataStore.edit {
|
||||
it.clear()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.anthonyla.livewallpaper.data.settings
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import com.anthonyla.livewallpaper.data.settings.SettingsConstants.DARK_MODE_TYPE
|
||||
import com.anthonyla.livewallpaper.data.settings.SettingsConstants.DYNAMIC_THEME_TYPE
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
class SettingsViewModel @Inject constructor (private val settingsDataStoreImpl: SettingsDataStoreImpl): ViewModel() {
|
||||
fun setDarkMode(status: Boolean?) = runBlocking {
|
||||
when (status) {
|
||||
true -> { settingsDataStoreImpl.putBoolean(DARK_MODE_TYPE, true) }
|
||||
|
||||
false -> { settingsDataStoreImpl.putBoolean(DARK_MODE_TYPE, false) }
|
||||
|
||||
null -> { settingsDataStoreImpl.deleteBoolean(DARK_MODE_TYPE) }
|
||||
}
|
||||
}
|
||||
|
||||
fun getDarkMode() = runBlocking {
|
||||
return@runBlocking settingsDataStoreImpl.getBoolean(DARK_MODE_TYPE)
|
||||
}
|
||||
|
||||
fun setDynamic (status: Boolean?) = runBlocking {
|
||||
when (status) {
|
||||
true -> { settingsDataStoreImpl.putBoolean(DYNAMIC_THEME_TYPE, true) }
|
||||
|
||||
false -> { settingsDataStoreImpl.putBoolean(DYNAMIC_THEME_TYPE, false) }
|
||||
|
||||
null -> { settingsDataStoreImpl.deleteBoolean(DYNAMIC_THEME_TYPE) }
|
||||
}
|
||||
}
|
||||
|
||||
fun getDynamicTheme() = runBlocking {
|
||||
return@runBlocking settingsDataStoreImpl.getBoolean(DYNAMIC_THEME_TYPE)
|
||||
}
|
||||
|
||||
fun resetSettings() = runBlocking {
|
||||
settingsDataStoreImpl.clearPreferences()
|
||||
}
|
||||
}
|
||||
@@ -15,8 +15,8 @@ import com.anthonyla.livewallpaper.ui.Wallpaper
|
||||
*/
|
||||
fun NavGraphBuilder.bottomNav(navController: NavController, modifier: Modifier = Modifier) {
|
||||
navigation(startDestination = BottomNavScreens.Wallpaper.route, route = "bottomNavigation") {
|
||||
composable(BottomNavScreens.Wallpaper.route) { Wallpaper() }
|
||||
composable(BottomNavScreens.Library.route) { Library() }
|
||||
composable(BottomNavScreens.Configure.route) { Configure() }
|
||||
composable(BottomNavScreens.Wallpaper.route) { Wallpaper(navController) }
|
||||
composable(BottomNavScreens.Library.route) { Library(navController) }
|
||||
composable(BottomNavScreens.Configure.route) { Configure(navController) }
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,9 @@ package com.anthonyla.livewallpaper.ui
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.navigation.NavController
|
||||
|
||||
@Composable
|
||||
fun Configure(modifier: Modifier = Modifier) {
|
||||
fun Configure(navController: NavController, modifier: Modifier = Modifier) {
|
||||
Text(text = "configure")
|
||||
}
|
||||
@@ -3,8 +3,9 @@ package com.anthonyla.livewallpaper.ui
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.navigation.NavController
|
||||
|
||||
@Composable
|
||||
fun Library(modifier: Modifier = Modifier) {
|
||||
fun Library(navController: NavController, modifier: Modifier = Modifier) {
|
||||
Text(text = "library")
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import android.app.Activity
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.SideEffect
|
||||
import androidx.compose.ui.Modifier
|
||||
@@ -13,6 +15,7 @@ import androidx.compose.ui.platform.LocalView
|
||||
import androidx.core.view.WindowCompat
|
||||
import androidx.navigation.compose.NavHost
|
||||
import androidx.navigation.compose.rememberNavController
|
||||
import com.anthonyla.livewallpaper.data.settings.SettingsViewModel
|
||||
import com.anthonyla.livewallpaper.navigation.BottomNavScreens
|
||||
import com.anthonyla.livewallpaper.navigation.BottomNavigationBar
|
||||
import com.anthonyla.livewallpaper.navigation.bottomNav
|
||||
@@ -20,7 +23,7 @@ import com.google.accompanist.systemuicontroller.rememberSystemUiController
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun LiveWallpaperApp(modifier: Modifier = Modifier) {
|
||||
fun LiveWallpaperApp(settingsViewModel: SettingsViewModel, modifier: Modifier = Modifier) {
|
||||
SetTransparentSystemBars()
|
||||
|
||||
// Bottom navigation
|
||||
|
||||
@@ -3,8 +3,11 @@ package com.anthonyla.livewallpaper.ui
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.navigation.NavController
|
||||
|
||||
@Composable
|
||||
fun Wallpaper(modifier: Modifier = Modifier) {
|
||||
Text(text = "wallpaper")
|
||||
fun Wallpaper(navController: NavController, modifier: Modifier = Modifier) {
|
||||
Text(
|
||||
text = "wallpaper"
|
||||
)
|
||||
}
|
||||
@@ -3,4 +3,5 @@ plugins {
|
||||
id("com.android.application") version "8.2.0-alpha05" apply false
|
||||
id("com.android.library") version "8.2.0-alpha05" apply false
|
||||
id("org.jetbrains.kotlin.android") version "1.7.20" apply false
|
||||
id("com.google.dagger.hilt.android") version "2.44" apply false
|
||||
}
|
||||
Reference in new issue
Block a user