Skip to content

Commit

Permalink
app theme change improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
queuejw committed Sep 2, 2024
1 parent 21ef0c6 commit 6eacd67
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 113 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ android {
applicationId "ru.dimon6018.metrolauncher"
minSdk 21
targetSdk 35
versionCode 51
versionName "1.0 Pre-Alpha 5.1.2.1"
versionCode 52
versionName "1.0 Pre-Alpha 5.1.2.2"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled false
Expand Down
21 changes: 8 additions & 13 deletions app/src/main/java/ru/dimon6018/metrolauncher/Application.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package ru.dimon6018.metrolauncher

//import ru.dimon6018.metrolauncher.content.data.ExperimentPrefs
import android.annotation.SuppressLint
import android.app.Activity
import android.app.Application
import android.app.UiModeManager
import android.content.pm.ActivityInfo
import android.os.Build
import android.os.Bundle
import androidx.appcompat.app.AppCompatDelegate
import com.google.android.material.color.DynamicColors
//import ru.dimon6018.metrolauncher.content.data.ExperimentPrefs
import ru.dimon6018.metrolauncher.content.data.Prefs
import ru.dimon6018.metrolauncher.helpers.bsod.BsodDetector
import ru.dimon6018.metrolauncher.helpers.utils.Utils.Companion.launcherAccentTheme
Expand All @@ -24,14 +23,14 @@ class Application : Application() {
Thread.setDefaultUncaughtExceptionHandler(BsodDetector())
PREFS = Prefs(applicationContext)
//EXP_PREFS = ExperimentPrefs(applicationContext)
setNightMode()
if(PREFS.accentColor == 21 && DynamicColors.isDynamicColorAvailable()) {
DynamicColors.applyToActivitiesIfAvailable(this)
}
super.onCreate()
registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
@SuppressLint("SourceLockedOrientationActivity")
override fun onActivityPreCreated(activity: Activity, savedInstanceState: Bundle?) {
setNightMode()
activity.setTheme(launcherAccentTheme())
when(PREFS.orientation) {
"p" -> {
Expand All @@ -52,20 +51,16 @@ class Application : Application() {
override fun onActivityDestroyed(activity: Activity) {}
})
}
fun setNightMode() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val uiMan: UiModeManager = (applicationContext.getSystemService(UI_MODE_SERVICE) as UiModeManager)
if(PREFS.isLightThemeUsed) {
uiMan.setApplicationNightMode(UiModeManager.MODE_NIGHT_NO)
} else {
uiMan.setApplicationNightMode(UiModeManager.MODE_NIGHT_YES)
}
private fun setNightMode() {
when(PREFS.appTheme) {
0 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
1 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
2 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
}
}
companion object {
lateinit var PREFS: Prefs
//var EXP_PREFS: ExperimentPrefs? = null

var isUpdateDownloading = false
var isAppOpened = false
var isStartMenuOpened = false
Expand Down
61 changes: 20 additions & 41 deletions app/src/main/java/ru/dimon6018/metrolauncher/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.annotation.SuppressLint
import android.content.Intent
import android.content.res.Configuration
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
Expand All @@ -14,11 +13,9 @@ import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.AutoCompleteTextView
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.toBitmap
import androidx.core.view.WindowCompat
Expand Down Expand Up @@ -58,6 +55,7 @@ import ru.dimon6018.metrolauncher.helpers.utils.Utils.Companion.applyWindowInset
import ru.dimon6018.metrolauncher.helpers.utils.Utils.Companion.getDefaultLocale
import ru.dimon6018.metrolauncher.helpers.utils.Utils.Companion.isDevMode
import ru.dimon6018.metrolauncher.helpers.utils.Utils.Companion.launcherAccentColor
import ru.dimon6018.metrolauncher.helpers.utils.Utils.Companion.launcherOnSurfaceColor
import ru.dimon6018.metrolauncher.helpers.utils.Utils.Companion.launcherSurfaceColor
import ru.dimon6018.metrolauncher.helpers.utils.Utils.Companion.registerPackageReceiver
import ru.dimon6018.metrolauncher.helpers.utils.Utils.Companion.sendCrash
Expand All @@ -72,6 +70,9 @@ class Main : AppCompatActivity() {
private val packageReceiver: PackageChangesReceiver by lazy { PackageChangesReceiver() }
private val defaultIconSize: Int by lazy { resources.getDimensionPixelSize(R.dimen.tile_default_size) }

private val accentColor: Int by lazy { launcherAccentColor(this@Main.theme) }
private val onSurfaceColor: Int by lazy { launcherOnSurfaceColor(this@Main.theme) }

private var searchAdapter: SearchAdapter? = null
private var filteredList = mutableListOf<App>()

Expand All @@ -81,7 +82,7 @@ class Main : AppCompatActivity() {
private lateinit var binding: MainScreenLaucnherBinding

override fun onCreate(savedInstanceState: Bundle?) {
setAppTheme()
isDarkMode = resources.getBoolean(R.bool.isDark) && PREFS.appTheme != 2
handleDevMode()
super.onCreate(savedInstanceState)

Expand Down Expand Up @@ -169,29 +170,15 @@ class Main : AppCompatActivity() {
}

private fun createPageChangeCallback() = object : ViewPager2.OnPageChangeCallback() {
val blackColor = ContextCompat.getColor(this@Main, android.R.color.black)
val whiteColor = ContextCompat.getColor(this@Main, android.R.color.white)

override fun onPageSelected(position: Int) {
super.onPageSelected(position)
updateNavigationBarColors(position)
}

private fun updateNavigationBarColors(position: Int) {
if (!PREFS.isSearchBarEnabled && PREFS.navBarColor != 2) {
val (startColor, searchColor) = when {
position == 0 && (PREFS.navBarColor == 1 || PREFS.isLightThemeUsed) -> {
launcherAccentColor(this@Main.theme) to blackColor
}
position == 0 -> {
launcherAccentColor(this@Main.theme) to whiteColor
}
(PREFS.navBarColor == 1 || PREFS.isLightThemeUsed) -> {
blackColor to launcherAccentColor(this@Main.theme)
}
else -> {
whiteColor to launcherAccentColor(this@Main.theme)
}
position == 0 -> accentColor to onSurfaceColor
else -> onSurfaceColor to accentColor
}
binding.navigationStartBtn.setColorFilter(startColor)
binding.navigationSearchBtn.setColorFilter(searchColor)
Expand Down Expand Up @@ -250,21 +237,25 @@ class Main : AppCompatActivity() {
close()
}
}
diskCache = initDiskCache(this)
mainViewModel.getAppList().forEach { app ->
if (app.type != 1) {
withContext(Dispatchers.IO) {
withContext(Dispatchers.IO) {
diskCache = initDiskCache(this@Main)
mainViewModel.getAppList().forEach { app ->
if (app.type != 1) {
val icon = diskCache?.let { loadIconFromDiskCache(it, app.appPackage!!) }
if (icon == null) {
generateIcon(app.appPackage!!, isCustomIconsInstalled)
saveIconToDiskCache(diskCache, app.appPackage!!, mainViewModel.getIconFromCache(app.appPackage!!))
saveIconToDiskCache(
diskCache,
app.appPackage!!,
mainViewModel.getIconFromCache(app.appPackage!!)
)
} else {
mainViewModel.addIconToCache(app.appPackage!!, icon)
}
}
}
diskCache?.let { closeDiskCache(it) }
}
diskCache?.let { closeDiskCache(it) }
}

private fun checkIconPack(disk: DiskLruCache?): Boolean {
Expand Down Expand Up @@ -353,8 +344,7 @@ class Main : AppCompatActivity() {
private fun setupNavigationBar() {
if (bottomViewReady) return
bottomViewReady = true
val bottomView: FrameLayout = findViewById(R.id.navigation)
bottomView.setBackgroundColor(getNavBarColor())
binding.navigation.setBackgroundColor(getNavBarColor())
configureBottomBar()
}

Expand All @@ -364,7 +354,7 @@ class Main : AppCompatActivity() {
1 -> ContextCompat.getColor(this, android.R.color.background_light)
2 -> accentColorFromPrefs(this)
3 -> {
findViewById<FrameLayout>(R.id.navigation).visibility = View.GONE
binding.navigation.visibility = View.GONE
return ContextCompat.getColor(this, android.R.color.transparent)
}
else -> launcherSurfaceColor(theme)
Expand Down Expand Up @@ -480,20 +470,9 @@ class Main : AppCompatActivity() {
filteredList.sortWith(compareBy { it.appLabel })
searchAdapter?.setData(filteredList)
}

private fun setAppTheme() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
val nightMode = if (PREFS.isLightThemeUsed) {
AppCompatDelegate.MODE_NIGHT_NO
} else {
AppCompatDelegate.MODE_NIGHT_YES
}
AppCompatDelegate.setDefaultNightMode(nightMode)
}
}

companion object {
var isLandscape: Boolean = false
var isDarkMode: Boolean = false
}

inner class WinAdapter(fragment: FragmentActivity) : FragmentStateAdapter(fragment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import ru.dimon6018.metrolauncher.Application.Companion.PREFS
import ru.dimon6018.metrolauncher.Application.Companion.isAppOpened
import ru.dimon6018.metrolauncher.Application.Companion.isStartMenuOpened
import ru.dimon6018.metrolauncher.Main
import ru.dimon6018.metrolauncher.Main.Companion.isDarkMode
import ru.dimon6018.metrolauncher.Main.Companion.isLandscape
import ru.dimon6018.metrolauncher.MainViewModel
import ru.dimon6018.metrolauncher.R
Expand Down Expand Up @@ -593,7 +594,7 @@ class NewStart: Fragment(), OnStartDragListener {
binding.backgroundWallpaper.animate().scaleX(0.9f).scaleY(0.82f).setDuration(300).start()
if(PREFS.isParallaxEnabled || !PREFS.isWallpaperUsed) {
binding.startFrame.setBackgroundColor(ContextCompat.getColor(
context, if(PREFS.isLightThemeUsed) android.R.color.background_light else android.R.color.background_dark
context, if(isDarkMode) android.R.color.background_light else android.R.color.background_dark
)
)
}
Expand Down Expand Up @@ -1296,7 +1297,7 @@ class ParallaxScroll(private val wallpaper: ImageView, private val adapter: NewS
@Suppress("DEPRECATION")
class BackgroundDecoration() : RecyclerView.ItemDecoration() {

private val backgroundColor = if(PREFS.isLightThemeUsed) Color.WHITE else Color.BLACK
private val backgroundColor = if(isDarkMode) Color.WHITE else Color.BLACK

override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
super.onDraw(c, parent, state)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Prefs(context: Context) {
private val updateNotificationPref = "updateNotificationEnabled"
private val feedbackPref = "feedbackEnabled"
private val launcherStatePref = "launcherState"
private val lightThemePref = "useLightTheme"
private val lightThemePref = "launcherTheme"
private val navbarColorPref = "navBarColor"
private val showMoreTilesPref = "isMoreTilesEnabled"
private val wallpaperPref = "wallpaperUsing"
Expand Down Expand Up @@ -72,9 +72,9 @@ class Prefs(context: Context) {
get() = prefs.getInt(maxCrashLogsPref, 1)
set(value) = prefs.edit().putInt(maxCrashLogsPref, value).apply()

var isLightThemeUsed: Boolean
get() = prefs.getBoolean(lightThemePref, false)
set(value) = prefs.edit().putBoolean(lightThemePref, value).apply()
var appTheme: Int
get() = prefs.getInt(lightThemePref, 0) // 0 - auto, 1 - dark, 2 - light
set(value) = prefs.edit().putInt(lightThemePref, value).apply()

var isFeedbackEnabled: Boolean
get() = prefs.getBoolean(feedbackPref, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.view.WindowCompat
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.CoroutineScope
Expand All @@ -19,7 +17,6 @@ import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import leakcanary.LeakCanary
import ru.dimon6018.metrolauncher.Application
import ru.dimon6018.metrolauncher.Application.Companion.PREFS
import ru.dimon6018.metrolauncher.R
import ru.dimon6018.metrolauncher.content.settings.activities.AboutSettingsActivity
Expand Down Expand Up @@ -67,7 +64,6 @@ class SettingsActivity : AppCompatActivity() {
}

override fun onCreate(savedInstanceState: Bundle?) {
setAppTheme()
super.onCreate(savedInstanceState)
binding = LauncherSettingsMainBinding.inflate(layoutInflater)
setContentView(binding.root)
Expand Down Expand Up @@ -226,21 +222,6 @@ class SettingsActivity : AppCompatActivity() {
}
ObjectAnimator.ofFloat(view, "alpha", 1f, 0f).start()
}
private fun setAppTheme() {
if (PREFS.isLightThemeUsed) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
(application as Application).setNightMode()
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
}
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
(application as Application).setNightMode()
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
}
}
}
private fun isHomeApp(): Boolean {
val intent = Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_HOME)
Expand Down
Loading

0 comments on commit 6eacd67

Please sign in to comment.