-
Notifications
You must be signed in to change notification settings - Fork 0
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
c28fb94
commit 62be178
Showing
12 changed files
with
183 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,16 +1,22 @@ | ||
package id.shaderboi.koffie.di | ||
|
||
import com.google.firebase.auth.FirebaseAuth | ||
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 id.shaderboi.koffie.core.data.repository.AppSettingsRepositoryImpl | ||
import id.shaderboi.koffie.core.domain.repository.AppSettingsRepository | ||
import java.text.DecimalFormat | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object AppModule { | ||
@Provides | ||
fun provideNumberFormatter(): DecimalFormat = DecimalFormat("#,###.00") | ||
|
||
@Provides | ||
fun provideAppSettingsRepository(@ApplicationContext context: Context): AppSettingsRepository = | ||
AppSettingsRepositoryImpl(context) | ||
} |
39 changes: 37 additions & 2 deletions
39
app/src/main/kotlin/id/shaderboi/koffie/ui/main/misc/PreferenceFragment.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 |
---|---|---|
@@ -1,14 +1,49 @@ | ||
package id.shaderboi.koffie.ui.main.misc | ||
|
||
import android.os.Bundle | ||
import android.view.View | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.lifecycleScope | ||
import androidx.lifecycle.repeatOnLifecycle | ||
import androidx.preference.ListPreference | ||
import androidx.preference.PreferenceFragmentCompat | ||
import dagger.hilt.EntryPoint | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import id.shaderboi.koffie.R | ||
import id.shaderboi.koffie.core.domain.model.theme.Theme | ||
import id.shaderboi.koffie.core.domain.repository.AppSettingsRepository | ||
import kotlinx.coroutines.flow.collectLatest | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
@AndroidEntryPoint | ||
class PreferenceFragment: PreferenceFragmentCompat() { | ||
class PreferenceFragment : PreferenceFragmentCompat() { | ||
@Inject | ||
lateinit var appSettingsRepository: AppSettingsRepository | ||
|
||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { | ||
setPreferencesFromResource(R.xml.profile_preference, rootKey) | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
|
||
val themeList = findPreference<ListPreference>("theme")!! | ||
|
||
viewLifecycleOwner.lifecycleScope.launch { | ||
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { | ||
themeList.setOnPreferenceChangeListener { preference, newValue -> | ||
launch { | ||
appSettingsRepository.setTheme(Theme.valueOf(newValue as String)) | ||
} | ||
true | ||
} | ||
|
||
launch { | ||
appSettingsRepository.flow.collectLatest { appSettings -> | ||
themeList.value = appSettings.theme.name | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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
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
16 changes: 16 additions & 0 deletions
16
.../kotlin/id/shaderboi/koffie/core/data/data_source/local/datastore/AppSettingsDataStore.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,16 @@ | ||
package id.shaderboi.koffie.core.data.data_source.local.datastore | ||
|
||
import android.content.Context | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.intPreferencesKey | ||
import androidx.datastore.preferences.preferencesDataStore | ||
import id.shaderboi.koffie.core.domain.model.theme.Theme | ||
|
||
val Context.settingsDataStore: DataStore<Preferences> by preferencesDataStore(name = "app_settings") | ||
|
||
data class AppSettings( | ||
val theme: Theme | ||
) | ||
|
||
val APP_THEME = intPreferencesKey("APP_THEME") |
27 changes: 27 additions & 0 deletions
27
core/src/main/kotlin/id/shaderboi/koffie/core/data/repository/AppSettingsRepositoryImpl.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,27 @@ | ||
package id.shaderboi.koffie.core.data.repository | ||
|
||
import android.content.Context | ||
import androidx.datastore.preferences.core.edit | ||
import id.shaderboi.koffie.core.data.data_source.local.datastore.APP_THEME | ||
import id.shaderboi.koffie.core.data.data_source.local.datastore.AppSettings | ||
import id.shaderboi.koffie.core.data.data_source.local.datastore.settingsDataStore | ||
import id.shaderboi.koffie.core.domain.model.theme.Theme | ||
import id.shaderboi.koffie.core.domain.repository.AppSettingsRepository | ||
import kotlinx.coroutines.flow.map | ||
import javax.inject.Inject | ||
|
||
class AppSettingsRepositoryImpl @Inject constructor( | ||
context: Context | ||
): AppSettingsRepository { | ||
private val settingsDataStore = context.settingsDataStore | ||
override val flow = settingsDataStore.data.map { pref -> | ||
val theme = Theme.values()[pref[APP_THEME] ?: Theme.FollowSystem.ordinal] | ||
AppSettings(theme) | ||
} | ||
|
||
override suspend fun setTheme(theme: Theme) { | ||
settingsDataStore.edit { pref -> | ||
pref[APP_THEME] = theme.ordinal | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
core/src/main/kotlin/id/shaderboi/koffie/core/domain/model/theme/Theme.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,7 @@ | ||
package id.shaderboi.koffie.core.domain.model.theme | ||
|
||
enum class Theme { | ||
FollowSystem, | ||
Dark, | ||
Light | ||
} |
14 changes: 14 additions & 0 deletions
14
core/src/main/kotlin/id/shaderboi/koffie/core/domain/repository/AppSettingsRepository.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,14 @@ | ||
package id.shaderboi.koffie.core.domain.repository | ||
|
||
import androidx.datastore.preferences.core.edit | ||
import id.shaderboi.koffie.core.data.data_source.local.datastore.APP_THEME | ||
import id.shaderboi.koffie.core.data.data_source.local.datastore.AppSettings | ||
import id.shaderboi.koffie.core.domain.model.theme.Theme | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
|
||
interface AppSettingsRepository { | ||
val flow: Flow<AppSettings> | ||
|
||
suspend fun setTheme(theme: Theme) | ||
} |