pull/2380/head
kunfei 2 years ago
parent fd943bfa38
commit 9e6c3ab779
  1. 15
      app/src/main/java/io/legado/app/base/BaseDialogFragment.kt
  2. 117
      app/src/main/java/io/legado/app/ui/main/MainActivity.kt

@ -1,5 +1,7 @@
package io.legado.app.base package io.legado.app.base
import android.content.DialogInterface
import android.content.DialogInterface.OnDismissListener
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import androidx.annotation.LayoutRes import androidx.annotation.LayoutRes
@ -18,9 +20,13 @@ import kotlin.coroutines.CoroutineContext
abstract class BaseDialogFragment( abstract class BaseDialogFragment(
@LayoutRes layoutID: Int, @LayoutRes layoutID: Int,
private val adaptationSoftKeyboard: Boolean = false private val adaptationSoftKeyboard: Boolean = false
) : DialogFragment(layoutID), ) : DialogFragment(layoutID), CoroutineScope by MainScope() {
CoroutineScope by MainScope() { private var onDismissListener: OnDismissListener? = null
fun setOnDismissListener(onDismissListener: OnDismissListener?) {
this.onDismissListener = onDismissListener
}
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
@ -51,6 +57,11 @@ abstract class BaseDialogFragment(
} }
} }
override fun onDismiss(dialog: DialogInterface) {
super.onDismiss(dialog)
onDismissListener?.onDismiss(dialog)
}
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
cancel() cancel()

@ -47,6 +47,8 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
/** /**
* 主界面 * 主界面
@ -92,21 +94,25 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
override fun onPostCreate(savedInstanceState: Bundle?) { override fun onPostCreate(savedInstanceState: Bundle?) {
super.onPostCreate(savedInstanceState) super.onPostCreate(savedInstanceState)
upVersion() launch {
privacyPolicy() //隐私协议
//请求设置书籍保存位置 if (!privacyPolicy()) return@launch
setBookStorage() //版本更新
//自动更新书籍 upVersion()
val isAutoRefreshedBook = savedInstanceState?.getBoolean("isAutoRefreshedBook") ?: false //请求设置书籍保存位置
if (AppConfig.autoRefreshBook && !isAutoRefreshedBook) { setBookStorage()
binding.viewPagerMain.postDelayed(1000) { //自动更新书籍
viewModel.upAllBookToc() val isAutoRefreshedBook = savedInstanceState?.getBoolean("isAutoRefreshedBook") ?: false
if (AppConfig.autoRefreshBook && !isAutoRefreshedBook) {
binding.viewPagerMain.postDelayed(1000) {
viewModel.upAllBookToc()
}
} }
binding.viewPagerMain.postDelayed(3000) {
viewModel.postLoad()
}
syncAlert()
} }
binding.viewPagerMain.postDelayed(3000) {
viewModel.postLoad()
}
syncAlert()
} }
override fun onNavigationItemSelected(item: MenuItem): Boolean = binding.run { override fun onNavigationItemSelected(item: MenuItem): Boolean = binding.run {
@ -142,17 +148,57 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
} }
} }
private fun upVersion() { /**
if (LocalConfig.versionCode != appInfo.versionCode) { * 用户隐私与协议
LocalConfig.versionCode = appInfo.versionCode */
if (LocalConfig.isFirstOpenApp) { private suspend fun privacyPolicy(): Boolean = suspendCoroutine { block ->
val help = String(assets.open("help/appHelp.md").readBytes()) if (LocalConfig.privacyPolicyOk) {
showDialogFragment(TextDialog(help, TextDialog.Mode.MD)) block.resume(true)
} else if (!BuildConfig.DEBUG) { return@suspendCoroutine
val log = String(assets.open("updateLog.md").readBytes()) }
showDialogFragment(TextDialog(log, TextDialog.Mode.MD)) val privacyPolicy = String(assets.open("privacyPolicy.md").readBytes())
alert("用户隐私与协议", privacyPolicy) {
noButton {
finish()
block.resume(false)
}
yesButton {
LocalConfig.privacyPolicyOk = true
block.resume(true)
}
onCancelled {
finish()
block.resume(false)
}
}
}
/**
* 版本更新日志
*/
private suspend fun upVersion() = suspendCoroutine { block ->
if (LocalConfig.versionCode == appInfo.versionCode) {
block.resume(Unit)
return@suspendCoroutine
}
LocalConfig.versionCode = appInfo.versionCode
viewModel.upVersion()
if (LocalConfig.isFirstOpenApp) {
val help = String(assets.open("help/appHelp.md").readBytes())
val dialog = TextDialog(help, TextDialog.Mode.MD)
dialog.setOnDismissListener {
block.resume(Unit)
} }
viewModel.upVersion() showDialogFragment(dialog)
} else if (!BuildConfig.DEBUG) {
val log = String(assets.open("updateLog.md").readBytes())
val dialog = TextDialog(log, TextDialog.Mode.MD)
dialog.setOnDismissListener {
block.resume(Unit)
}
showDialogFragment(dialog)
} else {
block.resume(Unit)
} }
} }
@ -173,23 +219,6 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
} }
} }
/**
* 用户隐私与协议
*/
private fun privacyPolicy() {
if (LocalConfig.privacyPolicyOk) return
val privacyPolicy = String(assets.open("privacyPolicy.md").readBytes())
alert("用户隐私与协议", privacyPolicy) {
noButton()
yesButton {
LocalConfig.privacyPolicyOk = true
}
onCancelled {
finish()
}
}
}
/** /**
* 设置书籍保存位置 * 设置书籍保存位置
*/ */
@ -200,10 +229,10 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
val storageHelp = String(assets.open("storageHelp.md").readBytes()) val storageHelp = String(assets.open("storageHelp.md").readBytes())
alert("设置书籍保存位置", storageHelp) { alert("设置书籍保存位置", storageHelp) {
yesButton { yesButton {
localBookTreeSelect.launch { localBookTreeSelect.launch {
title = getString(R.string.select_book_folder) title = getString(R.string.select_book_folder)
mode = HandleFileContract.DIR_SYS mode = HandleFileContract.DIR_SYS
} }
} }
noButton() noButton()
} }

Loading…
Cancel
Save