Skip to content

Commit

Permalink
Fix ui flickering in some devices (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ishan09811 authored Jan 3, 2025
1 parent a649516 commit ced874f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
8 changes: 6 additions & 2 deletions app/src/main/java/emu/skyline/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
package emu.skyline

import android.content.Intent
import android.util.TypedValue
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.provider.DocumentsContract
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.view.ViewGroup.MarginLayoutParams
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
Expand All @@ -36,6 +38,7 @@ import com.google.android.material.R as MaterialR
import com.google.android.material.color.MaterialColors
import dagger.hilt.android.AndroidEntryPoint
import emu.skyline.adapter.*
import emu.skyline.di.getSettings
import emu.skyline.data.AppItem
import emu.skyline.data.AppItemTag
import emu.skyline.databinding.MainActivityBinding
Expand All @@ -52,6 +55,7 @@ import emu.skyline.SkylineApplication
import javax.inject.Inject
import kotlin.math.ceil
import kotlinx.coroutines.launch
import kotlinx.coroutines.flow.distinctUntilChanged

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
Expand Down Expand Up @@ -110,6 +114,7 @@ class MainActivity : AppCompatActivity() {
else -> AppCompatDelegate.MODE_NIGHT_UNSPECIFIED
}
)
setTheme(if (getSettings().useMaterialYou) R.style.AppTheme_MaterialYou else R.style.AppTheme)
super.onCreate(savedInstanceState)

setContentView(binding.root)
Expand Down Expand Up @@ -182,8 +187,7 @@ class MainActivity : AppCompatActivity() {
// we collect the themeChanges and apply
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
SkylineApplication.themeChangeFlow.collect { themeId ->
setTheme(themeId)
SkylineApplication.themeChangeFlow.distinctUntilChanged().collect { themeId ->
recreate()
}
}
Expand Down
16 changes: 10 additions & 6 deletions app/src/main/java/emu/skyline/SkylineApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import java.io.File
import kotlin.math.roundToInt
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
* @return The optimal directory for putting public files inside, this may return a private directory if a public directory cannot be retrieved
Expand All @@ -36,7 +39,7 @@ class SkylineApplication : Application() {

val context : Context get() = instance.applicationContext

private val _themeChangeFlow = MutableSharedFlow<Int>(replay = 1)
private val _themeChangeFlow = MutableSharedFlow<Int>()
val themeChangeFlow = _themeChangeFlow.asSharedFlow()

const val NAV_TYPE_THREE_BUTTON = 0
Expand Down Expand Up @@ -87,11 +90,13 @@ class SkylineApplication : Application() {
}
}

private var currentTheme: Int? = null

fun setTheme(newValue: Boolean) {
if (newValue) {
_themeChangeFlow.tryEmit(R.style.AppTheme_MaterialYou)
} else {
_themeChangeFlow.tryEmit(R.style.AppTheme)
val newTheme = if (newValue) R.style.AppTheme_MaterialYou else R.style.AppTheme
if (currentTheme != newTheme) {
CoroutineScope(Dispatchers.Main).launch { _themeChangeFlow.emit(newTheme) }
currentTheme = newTheme
}
}
}
Expand All @@ -100,6 +105,5 @@ class SkylineApplication : Application() {
super.onCreate()
instance = this
System.loadLibrary("skyline")
setTheme(getSettings().useMaterialYou)
}
}
15 changes: 15 additions & 0 deletions app/src/main/java/emu/skyline/preference/GpuDriverActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.WindowCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewbinding.ViewBinding
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.repeatOnLifecycle
import com.google.android.material.snackbar.Snackbar
import dagger.hilt.android.AndroidEntryPoint
import emu.skyline.R
Expand All @@ -30,9 +33,12 @@ import emu.skyline.utils.GpuDriverHelper
import emu.skyline.utils.GpuDriverInstallResult
import emu.skyline.utils.WindowInsetsHelper
import emu.skyline.utils.serializable
import emu.skyline.di.getSettings
import emu.skyline.SkylineApplication
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.flow.distinctUntilChanged

/**
* This activity is used to manage the installed gpu drivers and select one to use.
Expand Down Expand Up @@ -125,6 +131,7 @@ class GpuDriverActivity : AppCompatActivity() {
}

override fun onCreate(savedInstanceState : Bundle?) {
setTheme(if (getSettings().useMaterialYou) R.style.AppTheme_MaterialYou else R.style.AppTheme)
super.onCreate(savedInstanceState)

setContentView(binding.root)
Expand Down Expand Up @@ -190,6 +197,14 @@ class GpuDriverActivity : AppCompatActivity() {
}

populateAdapter()

lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
SkylineApplication.themeChangeFlow.distinctUntilChanged().collect { themeId ->
recreate()
}
}
}
}

private fun resolveInstallResultString(result : GpuDriverInstallResult) = when (result) {
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/emu/skyline/preference/SearchLocationActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.WindowCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewbinding.ViewBinding
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.repeatOnLifecycle
import com.google.android.material.snackbar.Snackbar
import dagger.hilt.android.AndroidEntryPoint
import emu.skyline.R
Expand All @@ -30,6 +33,10 @@ import emu.skyline.utils.SearchLocationHelper
import emu.skyline.utils.SearchLocationResult
import emu.skyline.utils.WindowInsetsHelper
import emu.skyline.utils.serializable
import emu.skyline.di.getSettings
import emu.skyline.SkylineApplication
import kotlinx.coroutines.launch
import kotlinx.coroutines.flow.distinctUntilChanged

/**
* This activity is used to manage the selected search locations to use.
Expand Down Expand Up @@ -88,6 +95,7 @@ class SearchLocationActivity : AppCompatActivity() {
}

override fun onCreate(savedInstanceState : Bundle?) {
setTheme(if (getSettings().useMaterialYou) R.style.AppTheme_MaterialYou else R.style.AppTheme)
super.onCreate(savedInstanceState)

setContentView(binding.root)
Expand Down Expand Up @@ -142,6 +150,14 @@ class SearchLocationActivity : AppCompatActivity() {
}

populateAdapter()

lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
SkylineApplication.themeChangeFlow.distinctUntilChanged().collect { themeId ->
recreate()
}
}
}
}

private fun resolveActionResultString(result : SearchLocationResult) = when (result) {
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/emu/skyline/settings/SettingsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ import emu.skyline.preference.dialog.EditTextPreferenceMaterialDialogFragmentCom
import emu.skyline.preference.dialog.IntegerListPreferenceMaterialDialogFragmentCompat
import emu.skyline.preference.dialog.ListPreferenceMaterialDialogFragmentCompat
import emu.skyline.utils.WindowInsetsHelper
import emu.skyline.di.getSettings
import emu.skyline.SkylineApplication
import kotlinx.coroutines.launch
import kotlinx.coroutines.flow.distinctUntilChanged

private const val PREFERENCE_DIALOG_FRAGMENT_TAG = "androidx.preference.PreferenceFragment.DIALOG"

Expand All @@ -70,6 +72,7 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere
* This initializes all of the elements in the activity and displays the settings fragment
*/
override fun onCreate(savedInstanceState : Bundle?) {
setTheme(if (getSettings().useMaterialYou) R.style.AppTheme_MaterialYou else R.style.AppTheme)
super.onCreate(savedInstanceState)

setContentView(binding.root)
Expand Down Expand Up @@ -160,8 +163,7 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere
// we collect the themeChanges and apply
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
SkylineApplication.themeChangeFlow.collect { themeId ->
setTheme(themeId)
SkylineApplication.themeChangeFlow.distinctUntilChanged().collect { themeId ->
recreate()
}
}
Expand Down

0 comments on commit ced874f

Please sign in to comment.