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" implementation "org.jetbrains.anko:anko-sdk27-listeners:$anko_version"
//liveEventBus //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' def coroutines_version = '1.3.3'

@ -169,7 +169,9 @@
-dontwarn com.hwangjr.rxbus.** -dontwarn com.hwangjr.rxbus.**
-dontwarn okhttp3.** -dontwarn okhttp3.**
-dontwarn org.conscrypt.** -dontwarn org.conscrypt.**
-dontwarn com.jeremyliao.liveeventbus.**
-keep class com.jeremyliao.liveeventbus.** { *; }
-keep class retrofit2.**{*;} -keep class retrofit2.**{*;}
-keep class okhttp3.**{*;} -keep class okhttp3.**{*;}
-keep class okio.**{*;} -keep class okio.**{*;}

@ -50,7 +50,7 @@ class App : Application() {
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) createChannelId() if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) createChannelId()
applyDayNight() applyDayNight()
LiveEventBus.get() LiveEventBus
.config() .config()
.supportBroadcast(this) .supportBroadcast(this)
.lifecycleObserverAlwaysActive(true) .lifecycleObserverAlwaysActive(true)

@ -34,14 +34,17 @@ class MainActivity : VMBaseActivity<MainViewModel>(R.layout.activity_main),
get() = getViewModel(MainViewModel::class.java) get() = getViewModel(MainViewModel::class.java)
private var pagePosition = 0 private var pagePosition = 0
private val fragmentList = arrayListOf<Fragment>() private val fragmentMap = mapOf<String, Fragment>(
private var rssFragment: RssFragment? = null Pair("bookshelf", BookshelfFragment()),
Pair("explore", ExploreFragment()),
Pair("rss", RssFragment()),
Pair("myConfig", MyFragment())
)
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
ATH.applyEdgeEffectColor(view_pager_main) ATH.applyEdgeEffectColor(view_pager_main)
ATH.applyBottomNavigationColor(bottom_navigation_view) ATH.applyBottomNavigationColor(bottom_navigation_view)
view_pager_main.offscreenPageLimit = 3 view_pager_main.offscreenPageLimit = 3
upFragmentList()
view_pager_main.adapter = TabFragmentPageAdapter(supportFragmentManager) view_pager_main.adapter = TabFragmentPageAdapter(supportFragmentManager)
view_pager_main.addOnPageChangeListener(this) view_pager_main.addOnPageChangeListener(this)
bottom_navigation_view.setOnNavigationItemSelectedListener(this) bottom_navigation_view.setOnNavigationItemSelectedListener(this)
@ -69,22 +72,6 @@ class MainActivity : VMBaseActivity<MainViewModel>(R.layout.activity_main),
return false 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() { private fun upVersion() {
if (getPrefInt(PreferKey.versionCode) != App.INSTANCE.versionCode) { if (getPrefInt(PreferKey.versionCode) != App.INSTANCE.versionCode) {
putPrefInt(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) { observeEvent<String>(EventBus.SHOW_RSS) {
bottom_navigation_view.menu.findItem(R.id.menu_rss).isVisible = AppConfig.isShowRSS bottom_navigation_view.menu.findItem(R.id.menu_rss).isVisible = AppConfig.isShowRSS
upFragmentList()
view_pager_main.adapter?.notifyDataSetChanged() view_pager_main.adapter?.notifyDataSetChanged()
if (AppConfig.isShowRSS) { if (AppConfig.isShowRSS) {
view_pager_main.setCurrentItem(3, false) view_pager_main.setCurrentItem(3, false)
@ -160,7 +146,16 @@ class MainActivity : VMBaseActivity<MainViewModel>(R.layout.activity_main),
} }
override fun getItem(position: Int): Fragment { 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 { override fun getCount(): Int {

@ -4,13 +4,14 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import com.jeremyliao.liveeventbus.LiveEventBus import com.jeremyliao.liveeventbus.LiveEventBus
import com.jeremyliao.liveeventbus.core.Observable
inline fun <reified EVENT> eventObservable(tag: String): LiveEventBus.Observable<EVENT> { inline fun <reified EVENT> eventObservable(tag: String): Observable<EVENT> {
return LiveEventBus.get().with(tag, EVENT::class.java) return LiveEventBus.get(tag, EVENT::class.java)
} }
inline fun <reified EVENT> postEvent(tag: String, event: EVENT) { 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( inline fun <reified EVENT> AppCompatActivity.observeEvent(

Loading…
Cancel
Save