Skip to content

Commit

Permalink
Merge pull request #65 from meikpiep/dynamic-colors
Browse files Browse the repository at this point in the history
Add support of dynamic colors
  • Loading branch information
meikpiep authored Jan 21, 2024
2 parents 7fe2bd3 + a63ec52 commit 9dcb290
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Added all difficulty levels of 4x4 and 5x5 grids.
- Added support of dynamic colors.

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package org.piepmeyer.gauguin

import android.app.Application
import android.content.Context
import com.google.android.material.color.DynamicColors
import com.google.android.material.color.DynamicColorsOptions
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin
Expand All @@ -14,6 +16,7 @@ import org.piepmeyer.gauguin.preferences.ApplicationPreferencesImpl
import org.piepmeyer.gauguin.preferences.StatisticsManager
import org.piepmeyer.gauguin.preferences.StatisticsManagerImpl
import org.piepmeyer.gauguin.ui.ActivityUtils
import org.piepmeyer.gauguin.ui.DynamicColorsPrecondition
import org.piepmeyer.gauguin.ui.grid.GridCellSizeService

class MainApplication : Application() {
Expand All @@ -22,6 +25,14 @@ class MainApplication : Application() {

val applicationPreferences = ApplicationPreferencesImpl(this)

val options =
DynamicColorsOptions.Builder()
.setThemeOverlay(R.style.AppTheme_Overlay)
.setPrecondition(DynamicColorsPrecondition())
.build()

DynamicColors.applyToActivitiesIfAvailable(this, options)

startKoin {
androidLogger()
androidContext(this@MainApplication)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ class ActivityUtils : KoinComponent {

fun configureNightMode() {
val theme: Theme = applicationPreferences.theme
if (theme === Theme.LIGHT) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
} else if (theme === Theme.DARK) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
when (theme) {
Theme.LIGHT -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
}
Theme.DARK -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
}
Theme.SYSTEM_DEFAULT -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
else -> {}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.piepmeyer.gauguin.ui

import android.app.Activity
import com.google.android.material.color.DynamicColors.Precondition
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import org.piepmeyer.gauguin.Theme
import org.piepmeyer.gauguin.preferences.ApplicationPreferences

class DynamicColorsPrecondition : KoinComponent, Precondition {
private val applicationPreferences: ApplicationPreferences by inject()

override fun shouldApplyDynamicColors(
activity: Activity,
theme: Int,
): Boolean {
return applicationPreferences.theme == Theme.DYNAMIC_COLORS
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.piepmeyer.gauguin.ui.main

import android.content.Intent
import android.content.SharedPreferences
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
Expand Down Expand Up @@ -47,8 +49,9 @@ class MainActivity : AppCompatActivity(), GridCreationListener {

private var gameEndedSnackbar: Snackbar? = null

private lateinit var specialListener: OnSharedPreferenceChangeListener

public override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.MainScreenTheme)
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
Expand Down Expand Up @@ -86,6 +89,16 @@ class MainActivity : AppCompatActivity(), GridCreationListener {
calculationService.addListener(createGridCalculationListener())
loadApplicationPreferences()

specialListener =
OnSharedPreferenceChangeListener { _: SharedPreferences, key: String? ->
if (key == "theme") {
this.recreate()
}
}

val preferences = PreferenceManager.getDefaultSharedPreferences(this.applicationContext)
preferences.registerOnSharedPreferenceChangeListener(specialListener)

freshGridWasCreated()

bottomAppBarService.updateAppBarState()
Expand Down
1 change: 1 addition & 0 deletions gauguin-app/src/main/res/values-de-rDE/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
<item>Hell</item>
<item>Dunkel</item>
<item>Systemeinstellung</item>
<item>Dynamische Farben</item>
</string-array>
</resources>
4 changes: 4 additions & 0 deletions gauguin-app/src/main/res/values-v27/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@
<item name="android:windowTranslucentStatus">true</item>
<item name="android:immersive">true</item>
</style>


<style name="AppTheme.Overlay" parent="ThemeOverlay.Material3.DynamicColors.DayNight">
</style>
</resources>
2 changes: 2 additions & 0 deletions gauguin-app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
<item>Light</item>
<item>Dark</item>
<item>System Default</item>
<item>Dynamic Colors</item>
</string-array>
<string-array name="setting_theme_values">
<item>LIGHT</item>
<item>DARK</item>
<item>SYSTEM_DEFAULT</item>
<item>DYNAMIC_COLORS</item>
</string-array>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ enum class Theme {
LIGHT,
DARK,
SYSTEM_DEFAULT,
DYNAMIC_COLORS,
}

0 comments on commit 9dcb290

Please sign in to comment.