Skip to content

Commit

Permalink
Clean up dark mode code (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kwasow committed Aug 22, 2023
1 parent 05277d6 commit 206ea3b
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 102 deletions.
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 2 additions & 40 deletions app/src/main/java/com/kwasow/musekit/MainApplication.kt
Original file line number Diff line number Diff line change
@@ -1,50 +1,12 @@
package com.kwasow.musekit

import android.app.Application
import android.content.Context
import android.os.Build
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.edit
import com.google.android.material.color.DynamicColors

// TODO: Light/Dark theme circles could be optimized (we don't need a separate file for the two of these)
import com.kwasow.musekit.utils.ThemeUtils

class MainApplication : Application() {
override fun onCreate() {
super.onCreate()

setupDynamicColors()
setupNightMode()
}

private fun setupDynamicColors() {
DynamicColors.applyToActivitiesIfAvailable(this)
}

private fun setupNightMode() {
val sharedPreferences = getSharedPreferences(
getString(R.string.preferences_file_key),
Context.MODE_PRIVATE
)
val defaultNightMode =
if (Build.VERSION.SDK_INT >= 29) {
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
} else {
AppCompatDelegate.MODE_NIGHT_NO
}
val nightMode = sharedPreferences.getInt(
getString(R.string.preferences_night_mode),
defaultNightMode
)

sharedPreferences.edit {
putInt(
getString(R.string.preferences_night_mode),
nightMode
)
apply()
}

AppCompatDelegate.setDefaultNightMode(nightMode)
ThemeUtils.init(this)
}
}
52 changes: 34 additions & 18 deletions app/src/main/java/com/kwasow/musekit/fragments/SettingsFragment.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package com.kwasow.musekit.fragments

import android.app.Dialog
import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RawRes
import androidx.appcompat.app.AppCompatDelegate
import androidx.fragment.app.Fragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.kwasow.musekit.BuildConfig
import com.kwasow.musekit.R
import com.kwasow.musekit.databinding.DialogLicensesBinding
import com.kwasow.musekit.databinding.DialogThemeSettingsBinding
import com.kwasow.musekit.databinding.FragmentSettingsBinding
import com.kwasow.musekit.utils.ThemeUtils

class SettingsFragment : Fragment() {
private lateinit var binding: FragmentSettingsBinding
Expand Down Expand Up @@ -120,34 +124,46 @@ class SettingsFragment : Fragment() {
.create()

// Theme settings
dialogBinding.itemThemeFollowSystem.setOnClickListener {
when (ThemeUtils.getNightMode()) {
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM -> {
dialogBinding.itemThemeFollowSystem.getTrailingImageView()
.setImageResource(R.drawable.ic_check)
}

AppCompatDelegate.MODE_NIGHT_NO -> {
dialogBinding.itemThemeLight.getTrailingImageView()
.setImageResource(R.drawable.ic_check)
}

AppCompatDelegate.MODE_NIGHT_YES -> {
dialogBinding.itemThemeDark.getTrailingImageView()
.setImageResource(R.drawable.ic_check)
}
}

if (Build.VERSION.SDK_INT >= 29) {
dialogBinding.itemThemeFollowSystem.setOnClickListener {
setNightMode(dialog, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
} else {
dialogBinding.itemThemeFollowSystem.visibility = View.GONE
}

dialogBinding.itemThemeLight.getLeadingImageView().setColorFilter(Color.WHITE)
dialogBinding.itemThemeLight.setOnClickListener {
setNightMode(dialog, AppCompatDelegate.MODE_NIGHT_NO)
}

dialogBinding.itemThemeDark.getLeadingImageView().setColorFilter(Color.BLACK)
dialogBinding.itemThemeDark.setOnClickListener {
}

// Accent colours
dialogBinding.itemAccentGreen.getLeadingImageView().setColorFilter(Color.GREEN)
dialogBinding.itemAccentGreen.setOnClickListener {
}

dialogBinding.itemAccentRed.getLeadingImageView().setColorFilter(Color.RED)
dialogBinding.itemAccentRed.setOnClickListener {
}

dialogBinding.itemAccentBlue.getLeadingImageView().setColorFilter(Color.BLUE)
dialogBinding.itemAccentBlue.setOnClickListener {
}

dialogBinding.itemAccentYellow.getLeadingImageView().setColorFilter(Color.YELLOW)
dialogBinding.itemAccentYellow.setOnClickListener {
setNightMode(dialog, AppCompatDelegate.MODE_NIGHT_YES)
}

dialog.show()
}

private fun setNightMode(dialog: Dialog, nightMode: Int) {
ThemeUtils.setNightMode(nightMode)
dialog.dismiss()
}
}
75 changes: 75 additions & 0 deletions app/src/main/java/com/kwasow/musekit/utils/ThemeUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.kwasow.musekit.utils

import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import android.os.Build
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.edit
import com.google.android.material.color.DynamicColors
import com.kwasow.musekit.R

class ThemeUtils {
companion object {
private lateinit var application: Application
private lateinit var sharedPreferences: SharedPreferences
private lateinit var sharedPreferencesNightMode: String

fun init(application: Application) {
this.application = application

sharedPreferences = application.getSharedPreferences(
application.getString(R.string.preferences_file_key),
Context.MODE_PRIVATE
)
sharedPreferencesNightMode =
application.getString(R.string.preferences_night_mode)

// Create the setting if it does not exist
if (!sharedPreferences.contains(sharedPreferencesNightMode)) {
sharedPreferences.edit {
putInt(
sharedPreferencesNightMode,
getDefaultNightMode()
)
apply()
}
}

DynamicColors.applyToActivitiesIfAvailable(application)
AppCompatDelegate.setDefaultNightMode(getNightMode())
}

private fun getDefaultNightMode(): Int {
return if (Build.VERSION.SDK_INT >= 29) {
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
} else {
AppCompatDelegate.MODE_NIGHT_NO
}
}

fun getNightMode(): Int {
if (!::application.isInitialized) {
throw Exception("ThemeUtils have not been initialized. Application context is null")
}

return sharedPreferences.getInt(sharedPreferencesNightMode, getDefaultNightMode())
}

fun setNightMode(nightMode: Int) {
if (!::application.isInitialized) {
throw Exception("ThemeUtils have not been initialized. Application context is null")
}

sharedPreferences.edit {
putInt(
sharedPreferencesNightMode,
nightMode
)
apply()
}

AppCompatDelegate.setDefaultNightMode(nightMode)
}
}
}
40 changes: 0 additions & 40 deletions app/src/main/res/layout/dialog_theme_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,44 +37,4 @@

</com.kwasow.musekit.views.MenuSection>

<com.kwasow.musekit.views.MenuSection
app:sectionTitle="@string/accent_colour"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<com.kwasow.musekit.views.MenuItem
android:id="@+id/itemAccentGreen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemTitle="@string/green"
app:leadingIcon="@drawable/ic_circle_with_outline"
app:useTint="false" />

<com.kwasow.musekit.views.MenuItem
android:id="@+id/itemAccentRed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemTitle="@string/red"
app:leadingIcon="@drawable/ic_circle_with_outline"
app:useTint="false" />

<com.kwasow.musekit.views.MenuItem
android:id="@+id/itemAccentBlue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemTitle="@string/blue"
app:leadingIcon="@drawable/ic_circle_with_outline"
app:useTint="false" />

<com.kwasow.musekit.views.MenuItem
android:id="@+id/itemAccentYellow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemTitle="@string/yellow"
app:leadingIcon="@drawable/ic_circle_with_outline"
app:useTint="false" />

</com.kwasow.musekit.views.MenuSection>


</LinearLayout>

0 comments on commit 206ea3b

Please sign in to comment.