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. 9
      app/src/main/java/io/legado/app/ui/main/MainActivity.kt
  5. 3
      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
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
@ -79,7 +80,8 @@ abstract class BaseActivity<VM : ViewModel>(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<VM : ViewModel>(private val fullScreen: Boolean = tr
}
open fun observeLiveBus() {
}
override fun finish() {

@ -1,5 +1,5 @@
package io.legado.app.constant
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.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)
}
}

@ -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,8 +110,7 @@ class MainActivity : BaseActivity<MainViewModel>(), BottomNavigationView.OnNavig
}
override fun observeLiveBus() {
LiveEventBus.get().with(Bus.recreate, String::class.java)
.observe(this, Observer {
observeEvent<String>(Bus.RECREATE, Observer {
recreate()
})
}

@ -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

@ -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