Refactored app name to Paperize

Moved functions isDarkMode and isDynamicTheming to SettingsViewModel
This commit is contained in:
Anthony La committed 2023-06-06 13:13:54 -07:00
1 parent 10a5dd381a
commit 9caea3b006
31 files changed
+118 -128

No files matched your search

@@ -0,0 +1,24 @@
package com.anthonyla.paperize.data
import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.compose.runtime.Composable
import androidx.core.content.ContextCompat.startActivity
@Composable
fun Contact(context: Context) {
val email = "anthonyyla.dev@gmail.com"
val cc = ""
val subject = "[Contact] Paperize - A Wallpaper Changer"
val bodyText = "This is regarding the Paperize app for Android:\n"
val mailto = "mailto:" + Uri.encode(email) +
"?cc=" + Uri.encode(cc) +
"&subject=" + Uri.encode(subject) +
"&body=" + Uri.encode(bodyText)
val emailIntent = Intent(Intent.ACTION_SENDTO)
emailIntent.setData(Uri.parse(mailto))
startActivity(context, emailIntent, null)
}
@@ -0,0 +1,21 @@
package com.anthonyla.paperize.data.navigation
import androidx.annotation.StringRes
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AddToPhotos
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material.icons.outlined.AddToPhotos
import androidx.compose.material.icons.outlined.Home
import androidx.compose.material.icons.outlined.Settings
import androidx.compose.ui.graphics.vector.ImageVector
import com.anthonyla.paperize.R
/**
* List of screens for the bottom navigation bar
*/
sealed class BottomNavScreens(val route: String, @StringRes val resourceId: Int, val unfilledIcon: ImageVector, val filledIcon: ImageVector) {
object Wallpaper : BottomNavScreens("wallpaper", R.string.wallpaper, Icons.Outlined.Home, Icons.Filled.Home)
object Library : BottomNavScreens("library", R.string.library, Icons.Outlined.AddToPhotos, Icons.Filled.AddToPhotos)
object Configure : BottomNavScreens("configure", R.string.configure, Icons.Outlined.Settings, Icons.Filled.Settings)
}
@@ -0,0 +1,23 @@
package com.anthonyla.paperize.data.navigation
import androidx.compose.ui.Modifier
import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import androidx.navigation.compose.navigation
import com.anthonyla.paperize.ui.screens.Library
import com.anthonyla.paperize.ui.screens.Configure
import com.anthonyla.paperize.ui.screens.Settings
import com.anthonyla.paperize.ui.screens.Wallpaper
/**
* The Nav graph builder links the routes for the different screens in the app
*/
fun NavGraphBuilder.navGraph(navController: NavController, modifier: Modifier = Modifier) {
navigation(startDestination = BottomNavScreens.Wallpaper.route, route = "bottomNavigation") {
composable(BottomNavScreens.Wallpaper.route) { Wallpaper(navController) }
composable(BottomNavScreens.Library.route) { Library(navController) }
composable(BottomNavScreens.Configure.route) { Configure(navController) }
composable(SettingsNavScreens.Settings.route) { Settings(navController) }
}
}
@@ -0,0 +1,15 @@
package com.anthonyla.paperize.data.navigation
import androidx.annotation.StringRes
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material.icons.outlined.Settings
import androidx.compose.ui.graphics.vector.ImageVector
import com.anthonyla.paperize.R
/**
* List of screens for app settings
*/
sealed class SettingsNavScreens(val route: String, @StringRes val resourceId: Int, val unfilledIcon: ImageVector, val filledIcon: ImageVector) {
object Settings : SettingsNavScreens("settings", R.string.settings, Icons.Outlined.Settings, Icons.Filled.Settings)
}
@@ -0,0 +1,26 @@
package com.anthonyla.paperize.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.paperize.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.paperize.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.paperize.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.paperize.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,57 @@
package com.anthonyla.paperize.data.settings
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.Composable
import androidx.lifecycle.ViewModel
import com.anthonyla.paperize.data.settings.SettingsConstants.DARK_MODE_TYPE
import com.anthonyla.paperize.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) }
}
}
private 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) }
}
}
private fun getDynamicTheme() = runBlocking {
return@runBlocking settingsDataStoreImpl.getBoolean(DYNAMIC_THEME_TYPE)
}
fun resetSettings() = runBlocking {
settingsDataStoreImpl.clearPreferences()
}
@Composable
fun isDarkMode(): Boolean {
return when (getDarkMode()) {
true -> true
false -> false
null -> isSystemInDarkTheme()
}
}
fun isDynamicTheming(): Boolean {
return when (getDynamicTheme()) {
true, null -> true
false -> false
}
}
}