feat: 优化代码

pull/120/head
kunfei 5 years ago
parent 02d42edaf7
commit f876dbce20
  1. 39
      app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt

@ -89,17 +89,14 @@ class FontSelectDialog : BaseDialogFragment(),
override fun onMenuItemClick(item: MenuItem?): Boolean { override fun onMenuItemClick(item: MenuItem?): Boolean {
when (item?.itemId) { when (item?.itemId) {
R.id.menu_default -> { R.id.menu_default -> {
val cb = (parentFragment as? CallBack) ?: (activity as? CallBack) val requireContext = requireContext()
cb?.let { requireContext.alert(titleResource = R.string.system_typeface) {
val requireContext = requireContext() items(requireContext.resources.getStringArray(R.array.system_typefaces).toList()) { _, i ->
requireContext.alert(titleResource = R.string.system_typeface) { AppConfig.systemTypefaces = i
items(requireContext.resources.getStringArray(R.array.system_typefaces).toList()) { _, i -> onDefaultFontChange()
AppConfig.systemTypefaces = i dismiss()
onDefaultFontChange(it) }
} }.show()
}.show()
}
dismiss()
} }
R.id.menu_other -> { R.id.menu_other -> {
openFolder() openFolder()
@ -212,12 +209,9 @@ class FontSelectDialog : BaseDialogFragment(),
launch(IO) { launch(IO) {
file.copyTo(FileUtils.createFileIfNotExist(fontFolder, file.name), true) file.copyTo(FileUtils.createFileIfNotExist(fontFolder, file.name), true)
.absolutePath.let { path -> .absolutePath.let { path ->
val cb = (parentFragment as? CallBack) ?: (activity as? CallBack) if (callBack?.curFontPath != path) {
cb?.let { withContext(Main) {
if (it.curFontPath != path) { callBack?.selectFile(path)
withContext(Main) {
it.selectFile(path)
}
} }
} }
} }
@ -226,9 +220,7 @@ class FontSelectDialog : BaseDialogFragment(),
} }
override fun curFilePath(): String { override fun curFilePath(): String {
return (parentFragment as? CallBack)?.curFontPath return callBack?.curFontPath ?: ""
?: (activity as? CallBack)?.curFontPath
?: ""
} }
override fun onFilePicked(requestCode: Int, currentPath: String) { override fun onFilePicked(requestCode: Int, currentPath: String) {
@ -263,14 +255,17 @@ class FontSelectDialog : BaseDialogFragment(),
} }
} }
private fun onDefaultFontChange(callBack: CallBack) { private fun onDefaultFontChange() {
if (curFilePath() == "") { if (curFilePath() == "") {
postEvent(EventBus.UP_CONFIG, true) postEvent(EventBus.UP_CONFIG, true)
} else { } else {
callBack.selectFile("") callBack?.selectFile("")
} }
} }
private val callBack: CallBack?
get() = (parentFragment as? CallBack) ?: (activity as? CallBack)
interface CallBack { interface CallBack {
fun selectFile(path: String) fun selectFile(path: String)
val curFontPath: String val curFontPath: String

Loading…
Cancel
Save