LiveEventBus简单封装

pull/32/head
Administrator 5 years ago
parent 5de24ec41a
commit 52c0d67489
  1. 7
      app/src/main/java/io/legado/app/base/BaseActivity.kt
  2. 2
      app/src/main/java/io/legado/app/constant/Bus.kt
  3. 11
      app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt
  4. 13
      app/src/main/java/io/legado/app/ui/main/MainActivity.kt
  5. 5
      app/src/main/java/io/legado/app/utils/ContextExtensions.kt
  6. 30
      app/src/main/java/io/legado/app/utils/EventBusKt.kt

@ -17,7 +17,8 @@ import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel import kotlinx.coroutines.cancel
abstract class BaseActivity<VM : ViewModel>(private val fullScreen: Boolean = true) : AppCompatActivity(), CoroutineScope by MainScope() { abstract class BaseActivity<VM : ViewModel>(private val fullScreen: Boolean = true) : AppCompatActivity(),
CoroutineScope by MainScope() {
protected abstract val viewModel: VM protected abstract val viewModel: VM
@ -79,7 +80,8 @@ abstract class BaseActivity<VM : ViewModel>(private val fullScreen: Boolean = tr
private fun setupSystemBar() { private fun setupSystemBar() {
if (fullScreen) { if (fullScreen) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS or WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION) 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) window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
if (getPrefBoolean("transparentStatusBar")) { if (getPrefBoolean("transparentStatusBar")) {
window.statusBarColor = Color.TRANSPARENT window.statusBarColor = Color.TRANSPARENT
@ -92,7 +94,6 @@ abstract class BaseActivity<VM : ViewModel>(private val fullScreen: Boolean = tr
} }
open fun observeLiveBus() { open fun observeLiveBus() {
} }
override fun finish() { override fun finish() {

@ -1,5 +1,5 @@
package io.legado.app.constant package io.legado.app.constant
object Bus { object Bus {
const val recreate = "recreate" const val RECREATE = "RECREATE"
} }

@ -11,10 +11,7 @@ import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.constant.Bus import io.legado.app.constant.Bus
import io.legado.app.lib.theme.ColorUtils import io.legado.app.lib.theme.ColorUtils
import io.legado.app.utils.getCompatColor import io.legado.app.utils.*
import io.legado.app.utils.getPrefBoolean
import io.legado.app.utils.putPrefInt
import io.legado.app.utils.upTint
class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener { class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener {
@ -37,7 +34,7 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar
sharedPreferences ?: return sharedPreferences ?: return
when (key) { when (key) {
"transparentStatusBar" -> { "transparentStatusBar" -> {
LiveEventBus.get().with(Bus.recreate).post("") postEvent(Bus.RECREATE, "")
Handler().postDelayed({ activity?.recreate() }, 100) Handler().postDelayed({ activity?.recreate() }, 100)
} }
"colorPrimary", "colorAccent", "colorBackground" -> { "colorPrimary", "colorAccent", "colorBackground" -> {
@ -101,7 +98,7 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar
.putInt("colorBackgroundNight", App.INSTANCE.getCompatColor(R.color.md_grey_800)) .putInt("colorBackgroundNight", App.INSTANCE.getCompatColor(R.color.md_grey_800))
.apply() .apply()
App.INSTANCE.upThemeStore() App.INSTANCE.upThemeStore()
LiveEventBus.get().with(Bus.recreate).post("") LiveEventBus.get().with(Bus.RECREATE).post("")
Handler().postDelayed({ activity?.recreate() }, 100) Handler().postDelayed({ activity?.recreate() }, 100)
} }
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)
@ -133,7 +130,7 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar
private fun upTheme(isNightTheme: Boolean) { private fun upTheme(isNightTheme: Boolean) {
if (App.INSTANCE.getPrefBoolean("isNightTheme") == isNightTheme) { if (App.INSTANCE.getPrefBoolean("isNightTheme") == isNightTheme) {
App.INSTANCE.upThemeStore() App.INSTANCE.upThemeStore()
LiveEventBus.get().with(Bus.recreate).post("") LiveEventBus.get().with(Bus.RECREATE).post("")
Handler().postDelayed({ activity?.recreate() }, 100) Handler().postDelayed({ activity?.recreate() }, 100)
} }
} }

@ -8,7 +8,6 @@ import androidx.fragment.app.FragmentPagerAdapter
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.viewpager.widget.ViewPager import androidx.viewpager.widget.ViewPager
import com.google.android.material.bottomnavigation.BottomNavigationView import com.google.android.material.bottomnavigation.BottomNavigationView
import com.jeremyliao.liveeventbus.LiveEventBus
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseActivity 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.findbook.FindBookFragment
import io.legado.app.ui.main.my.MyFragment import io.legado.app.ui.main.my.MyFragment
import io.legado.app.ui.main.rss.RssFragment import io.legado.app.ui.main.rss.RssFragment
import io.legado.app.utils.getCompatColor import io.legado.app.utils.*
import io.legado.app.utils.getPrefInt
import io.legado.app.utils.getViewModel
import io.legado.app.utils.putPrefInt
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -114,9 +110,8 @@ class MainActivity : BaseActivity<MainViewModel>(), BottomNavigationView.OnNavig
} }
override fun observeLiveBus() { override fun observeLiveBus() {
LiveEventBus.get().with(Bus.recreate, String::class.java) observeEvent<String>(Bus.RECREATE, Observer {
.observe(this, Observer { recreate()
recreate() })
})
} }
} }

@ -5,8 +5,11 @@ import android.content.res.ColorStateList
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import androidx.annotation.ColorRes import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes import androidx.annotation.DrawableRes
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.content.edit import androidx.core.content.edit
import com.jeremyliao.liveeventbus.LiveEventBus
import io.legado.app.R
import org.jetbrains.anko.connectivityManager import org.jetbrains.anko.connectivityManager
import org.jetbrains.anko.defaultSharedPreferences import org.jetbrains.anko.defaultSharedPreferences
@ -61,4 +64,4 @@ fun Context.getStatusBarHeight(): Int {
fun Context.getNavigationBarHeight(): Int { fun Context.getNavigationBarHeight(): Int {
val resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android") val resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android")
return resources.getDimensionPixelSize(resourceId) return resources.getDimensionPixelSize(resourceId)
} }

@ -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 <reified EVENT> eventObservable(tag: String): LiveEventBus.Observable<EVENT> {
return LiveEventBus.get().with(tag, EVENT::class.java)
}
inline fun <reified EVENT> postEvent(tag: String, event: EVENT) {
return LiveEventBus.get().with(tag, EVENT::class.java).post(event)
}
inline fun <reified EVENT> AppCompatActivity.observeEvent(tag: String, observer: Observer<EVENT>) {
return eventObservable<EVENT>(tag).observe(this, observer)
}
inline fun <reified EVENT> AppCompatActivity.observeEventSticky(tag: String, observer: Observer<EVENT>) {
return eventObservable<EVENT>(tag).observeSticky(this, observer)
}
inline fun <reified EVENT> Fragment.observeEvent(tag: String, observer: Observer<EVENT>) {
return eventObservable<EVENT>(tag).observe(this, observer)
}
inline fun <reified EVENT> Fragment.observeEventSticky(tag: String, observer: Observer<EVENT>) {
return eventObservable<EVENT>(tag).observeSticky(this, observer)
}
Loading…
Cancel
Save