feat: 优化代码

pull/133/head
kunfei 5 years ago
parent daba2f481e
commit 2ed01ecd9c
  1. 2
      app/build.gradle
  2. 2
      app/proguard-rules.pro
  3. 2
      app/src/main/java/io/legado/app/App.kt
  4. 37
      app/src/main/java/io/legado/app/ui/main/MainActivity.kt
  5. 7
      app/src/main/java/io/legado/app/utils/EventBusKt.kt

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

@ -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.**{*;}

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

@ -34,14 +34,17 @@ class MainActivity : VMBaseActivity<MainViewModel>(R.layout.activity_main),
get() = getViewModel(MainViewModel::class.java)
private var pagePosition = 0
private val fragmentList = arrayListOf<Fragment>()
private var rssFragment: RssFragment? = null
private val fragmentMap = mapOf<String, Fragment>(
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<MainViewModel>(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<MainViewModel>(R.layout.activity_main),
}
observeEvent<String>(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<MainViewModel>(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 {

@ -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 <reified EVENT> eventObservable(tag: String): LiveEventBus.Observable<EVENT> {
return LiveEventBus.get().with(tag, EVENT::class.java)
inline fun <reified EVENT> eventObservable(tag: String): Observable<EVENT> {
return LiveEventBus.get(tag, EVENT::class.java)
}
inline fun <reified EVENT> postEvent(tag: String, event: EVENT) {
LiveEventBus.get().with(tag, EVENT::class.java).post(event)
LiveEventBus.get(tag).post(event)
}
inline fun <reified EVENT> AppCompatActivity.observeEvent(

Loading…
Cancel
Save