diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index 580d2359f..64d4d9371 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -51,7 +51,7 @@ class App : Application() { .autoClear(false) } - fun initNightTheme() { + fun initNightTheme() {//对整个应用生效,不要手动调用recreate if (getPrefBoolean("isNightTheme")) { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) } else { diff --git a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt index 0a727d354..2b709ec4b 100644 --- a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt @@ -7,8 +7,6 @@ import android.view.View import androidx.appcompat.app.AlertDialog import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat -import com.jaredrummler.android.colorpicker.ColorPreferenceCompat -import com.jeremyliao.liveeventbus.LiveEventBus import io.legado.app.App import io.legado.app.R import io.legado.app.constant.Bus @@ -106,8 +104,6 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar .putInt("colorBackgroundNight", App.INSTANCE.getCompatColor(R.color.md_grey_800)) .apply() App.INSTANCE.upThemeStore() - LiveEventBus.get().with(Bus.RECREATE).post("") - Handler().postDelayed({ activity?.recreate() }, 100) } .setNegativeButton(R.string.cancel, null) .show().upTint() @@ -138,8 +134,6 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar private fun upTheme(isNightTheme: Boolean) { if (App.INSTANCE.getPrefBoolean("isNightTheme") == isNightTheme) { App.INSTANCE.upThemeStore() - LiveEventBus.get().with(Bus.RECREATE).post("") - Handler().postDelayed({ activity?.recreate() }, 100) } } diff --git a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt index efe3c82a1..b92d71a03 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt @@ -5,7 +5,6 @@ import android.view.MenuItem import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentPagerAdapter -import androidx.lifecycle.Observer import androidx.viewpager.widget.ViewPager import com.google.android.material.bottomnavigation.BottomNavigationView import io.legado.app.App @@ -112,8 +111,8 @@ class MainActivity : BaseActivity(), BottomNavigationView.OnNavig } override fun observeLiveBus() { - observeEvent(Bus.RECREATE, Observer { + observeEvent(Bus.RECREATE) { recreate() - }) + } } } diff --git a/app/src/main/java/io/legado/app/utils/EventBusKt.kt b/app/src/main/java/io/legado/app/utils/EventBusKt.kt index e4c505565..116540e75 100644 --- a/app/src/main/java/io/legado/app/utils/EventBusKt.kt +++ b/app/src/main/java/io/legado/app/utils/EventBusKt.kt @@ -13,18 +13,26 @@ inline fun postEvent(tag: String, event: EVENT) { return LiveEventBus.get().with(tag, EVENT::class.java).post(event) } -inline fun AppCompatActivity.observeEvent(tag: String, observer: Observer) { - return eventObservable(tag).observe(this, observer) +inline fun AppCompatActivity.observeEvent(tag: String, crossinline observer: (EVENT) -> Unit) { + return eventObservable(tag).observe(this, Observer { + observer(it) + }) } -inline fun AppCompatActivity.observeEventSticky(tag: String, observer: Observer) { - return eventObservable(tag).observeSticky(this, observer) +inline fun AppCompatActivity.observeEventSticky(tag: String, crossinline observer: (EVENT) -> Unit) { + return eventObservable(tag).observeSticky(this, Observer { + observer(it) + }) } -inline fun Fragment.observeEvent(tag: String, observer: Observer) { - return eventObservable(tag).observe(this, observer) +inline fun Fragment.observeEvent(tag: String, crossinline observer: (EVENT) -> Unit) { + return eventObservable(tag).observe(this, Observer { + observer(it) + }) } -inline fun Fragment.observeEventSticky(tag: String, observer: Observer) { - return eventObservable(tag).observeSticky(this, observer) +inline fun Fragment.observeEventSticky(tag: String, crossinline observer: (EVENT) -> Unit) { + return eventObservable(tag).observeSticky(this, Observer { + observer(it) + }) } \ No newline at end of file