diff --git a/app/build.gradle b/app/build.gradle index e36182ccc..3c8a18f6c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -140,7 +140,7 @@ dependencies { implementation "org.jetbrains.anko:anko-sdk27-listeners:$anko_version" //liveEventBus - implementation 'com.jeremyliao:live-event-bus-x:1.4.5' + implementation 'com.jeremyliao:live-event-bus-x:1.5.7' //协程 def coroutines_version = '1.3.3' diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro index 2c9c17581..affa763de 100644 --- a/app/proguard-rules.pro +++ b/app/proguard-rules.pro @@ -169,7 +169,9 @@ -dontwarn com.hwangjr.rxbus.** -dontwarn okhttp3.** -dontwarn org.conscrypt.** +-dontwarn com.jeremyliao.liveeventbus.** +-keep class com.jeremyliao.liveeventbus.** { *; } -keep class retrofit2.**{*;} -keep class okhttp3.**{*;} -keep class okio.**{*;} diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index 88379b333..d1a3e9464 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -50,7 +50,7 @@ class App : Application() { } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) createChannelId() applyDayNight() - LiveEventBus.get() + LiveEventBus .config() .supportBroadcast(this) .lifecycleObserverAlwaysActive(true) 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 8b4705ae0..d91581221 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 @@ -34,14 +34,17 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), get() = getViewModel(MainViewModel::class.java) private var pagePosition = 0 - private val fragmentList = arrayListOf() - private var rssFragment: RssFragment? = null + private val fragmentMap = mapOf( + Pair("bookshelf", BookshelfFragment()), + Pair("explore", ExploreFragment()), + Pair("rss", RssFragment()), + Pair("myConfig", MyFragment()) + ) override fun onActivityCreated(savedInstanceState: Bundle?) { ATH.applyEdgeEffectColor(view_pager_main) ATH.applyBottomNavigationColor(bottom_navigation_view) view_pager_main.offscreenPageLimit = 3 - upFragmentList() view_pager_main.adapter = TabFragmentPageAdapter(supportFragmentManager) view_pager_main.addOnPageChangeListener(this) bottom_navigation_view.setOnNavigationItemSelectedListener(this) @@ -69,22 +72,6 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), return false } - private fun upFragmentList() { - if (fragmentList.isEmpty()) { - fragmentList.add(BookshelfFragment()) - fragmentList.add(ExploreFragment()) - fragmentList.add(RssFragment()) - fragmentList.add(MyFragment()) - } - if (AppConfig.isShowRSS && fragmentList.size < 4) { - fragmentList.add(2, - rssFragment ?: RssFragment().apply { rssFragment = this }) - } - if (!AppConfig.isShowRSS && fragmentList.size == 4) { - fragmentList.removeAt(2) - } - } - private fun upVersion() { if (getPrefInt(PreferKey.versionCode) != App.INSTANCE.versionCode) { putPrefInt(PreferKey.versionCode, App.INSTANCE.versionCode) @@ -144,7 +131,6 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), } observeEvent(EventBus.SHOW_RSS) { bottom_navigation_view.menu.findItem(R.id.menu_rss).isVisible = AppConfig.isShowRSS - upFragmentList() view_pager_main.adapter?.notifyDataSetChanged() if (AppConfig.isShowRSS) { view_pager_main.setCurrentItem(3, false) @@ -160,7 +146,16 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), } override fun getItem(position: Int): Fragment { - return fragmentList[position] + return when (position) { + 0 -> fragmentMap.getValue("bookshelf") + 1 -> fragmentMap.getValue("explore") + 2 -> if (AppConfig.isShowRSS) { + fragmentMap.getValue("rss") + } else { + fragmentMap.getValue("myConfig") + } + else -> fragmentMap.getValue("myConfig") + } } override fun getCount(): Int { 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 82c3f0f30..6731e06d6 100644 --- a/app/src/main/java/io/legado/app/utils/EventBusKt.kt +++ b/app/src/main/java/io/legado/app/utils/EventBusKt.kt @@ -4,13 +4,14 @@ import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.Fragment import androidx.lifecycle.Observer import com.jeremyliao.liveeventbus.LiveEventBus +import com.jeremyliao.liveeventbus.core.Observable -inline fun eventObservable(tag: String): LiveEventBus.Observable { - return LiveEventBus.get().with(tag, EVENT::class.java) +inline fun eventObservable(tag: String): Observable { + return LiveEventBus.get(tag, EVENT::class.java) } inline fun postEvent(tag: String, event: EVENT) { - LiveEventBus.get().with(tag, EVENT::class.java).post(event) + LiveEventBus.get(tag).post(event) } inline fun AppCompatActivity.observeEvent(