From 52c0d67489b4dc70fac33d40cf4a226daf3094a0 Mon Sep 17 00:00:00 2001 From: Administrator <1760316362@qq.com> Date: Tue, 30 Jul 2019 09:46:14 +0800 Subject: [PATCH] =?UTF-8?q?LiveEventBus=E7=AE=80=E5=8D=95=E5=B0=81?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/base/BaseActivity.kt | 7 +++-- .../main/java/io/legado/app/constant/Bus.kt | 2 +- .../app/ui/config/ThemeConfigFragment.kt | 11 +++---- .../io/legado/app/ui/main/MainActivity.kt | 13 +++----- .../io/legado/app/utils/ContextExtensions.kt | 5 +++- .../java/io/legado/app/utils/EventBusKt.kt | 30 +++++++++++++++++++ 6 files changed, 47 insertions(+), 21 deletions(-) create mode 100644 app/src/main/java/io/legado/app/utils/EventBusKt.kt diff --git a/app/src/main/java/io/legado/app/base/BaseActivity.kt b/app/src/main/java/io/legado/app/base/BaseActivity.kt index be1b4061a..103ce40cf 100644 --- a/app/src/main/java/io/legado/app/base/BaseActivity.kt +++ b/app/src/main/java/io/legado/app/base/BaseActivity.kt @@ -17,7 +17,8 @@ import kotlinx.coroutines.MainScope import kotlinx.coroutines.cancel -abstract class BaseActivity(private val fullScreen: Boolean = true) : AppCompatActivity(), CoroutineScope by MainScope() { +abstract class BaseActivity(private val fullScreen: Boolean = true) : AppCompatActivity(), + CoroutineScope by MainScope() { protected abstract val viewModel: VM @@ -79,7 +80,8 @@ abstract class BaseActivity(private val fullScreen: Boolean = tr private fun setupSystemBar() { if (fullScreen) { window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS or WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION) - window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE + window.decorView.systemUiVisibility = + View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) if (getPrefBoolean("transparentStatusBar")) { window.statusBarColor = Color.TRANSPARENT @@ -92,7 +94,6 @@ abstract class BaseActivity(private val fullScreen: Boolean = tr } open fun observeLiveBus() { - } override fun finish() { diff --git a/app/src/main/java/io/legado/app/constant/Bus.kt b/app/src/main/java/io/legado/app/constant/Bus.kt index 1b73ca92e..94d41fafb 100644 --- a/app/src/main/java/io/legado/app/constant/Bus.kt +++ b/app/src/main/java/io/legado/app/constant/Bus.kt @@ -1,5 +1,5 @@ package io.legado.app.constant object Bus { - const val recreate = "recreate" + const val RECREATE = "RECREATE" } \ No newline at end of file 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 bd3a1f6ae..0f9afe96c 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 @@ -11,10 +11,7 @@ import io.legado.app.App import io.legado.app.R import io.legado.app.constant.Bus import io.legado.app.lib.theme.ColorUtils -import io.legado.app.utils.getCompatColor -import io.legado.app.utils.getPrefBoolean -import io.legado.app.utils.putPrefInt -import io.legado.app.utils.upTint +import io.legado.app.utils.* class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener { @@ -37,7 +34,7 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar sharedPreferences ?: return when (key) { "transparentStatusBar" -> { - LiveEventBus.get().with(Bus.recreate).post("") + postEvent(Bus.RECREATE, "") Handler().postDelayed({ activity?.recreate() }, 100) } "colorPrimary", "colorAccent", "colorBackground" -> { @@ -101,7 +98,7 @@ 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("") + LiveEventBus.get().with(Bus.RECREATE).post("") Handler().postDelayed({ activity?.recreate() }, 100) } .setNegativeButton(R.string.cancel, null) @@ -133,7 +130,7 @@ 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("") + 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 6543c1896..5d14017c0 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 @@ -8,7 +8,6 @@ import androidx.fragment.app.FragmentPagerAdapter import androidx.lifecycle.Observer import androidx.viewpager.widget.ViewPager import com.google.android.material.bottomnavigation.BottomNavigationView -import com.jeremyliao.liveeventbus.LiveEventBus import io.legado.app.App import io.legado.app.R import io.legado.app.base.BaseActivity @@ -21,10 +20,7 @@ import io.legado.app.ui.main.bookshelf.BookshelfFragment import io.legado.app.ui.main.findbook.FindBookFragment import io.legado.app.ui.main.my.MyFragment import io.legado.app.ui.main.rss.RssFragment -import io.legado.app.utils.getCompatColor -import io.legado.app.utils.getPrefInt -import io.legado.app.utils.getViewModel -import io.legado.app.utils.putPrefInt +import io.legado.app.utils.* import kotlinx.android.synthetic.main.activity_main.* import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.launch @@ -114,9 +110,8 @@ class MainActivity : BaseActivity(), BottomNavigationView.OnNavig } override fun observeLiveBus() { - LiveEventBus.get().with(Bus.recreate, String::class.java) - .observe(this, Observer { - recreate() - }) + observeEvent(Bus.RECREATE, Observer { + recreate() + }) } } diff --git a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt index 50f9475a1..d5a4e74e3 100644 --- a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt @@ -5,8 +5,11 @@ import android.content.res.ColorStateList import android.graphics.drawable.Drawable import androidx.annotation.ColorRes import androidx.annotation.DrawableRes +import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat import androidx.core.content.edit +import com.jeremyliao.liveeventbus.LiveEventBus +import io.legado.app.R import org.jetbrains.anko.connectivityManager import org.jetbrains.anko.defaultSharedPreferences @@ -61,4 +64,4 @@ fun Context.getStatusBarHeight(): Int { fun Context.getNavigationBarHeight(): Int { val resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android") return resources.getDimensionPixelSize(resourceId) -} +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/utils/EventBusKt.kt b/app/src/main/java/io/legado/app/utils/EventBusKt.kt new file mode 100644 index 000000000..e4c505565 --- /dev/null +++ b/app/src/main/java/io/legado/app/utils/EventBusKt.kt @@ -0,0 +1,30 @@ +package io.legado.app.utils + +import androidx.appcompat.app.AppCompatActivity +import androidx.fragment.app.Fragment +import androidx.lifecycle.Observer +import com.jeremyliao.liveeventbus.LiveEventBus + +inline fun eventObservable(tag: String): LiveEventBus.Observable { + return LiveEventBus.get().with(tag, EVENT::class.java) +} + +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.observeEventSticky(tag: String, observer: Observer) { + return eventObservable(tag).observeSticky(this, observer) +} + +inline fun Fragment.observeEvent(tag: String, observer: Observer) { + return eventObservable(tag).observe(this, observer) +} + +inline fun Fragment.observeEventSticky(tag: String, observer: Observer) { + return eventObservable(tag).observeSticky(this, observer) +} \ No newline at end of file