pull/32/head
kunfei 5 years ago
parent 0e5a9c1294
commit 189df7a5de
  1. 16
      app/src/main/java/io/legado/app/ui/readbook/config/ReadStyleDialog.kt
  2. 55
      app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt

@ -17,9 +17,8 @@ import io.legado.app.lib.theme.accentColor
import io.legado.app.lib.theme.primaryColor import io.legado.app.lib.theme.primaryColor
import io.legado.app.ui.readbook.Help import io.legado.app.ui.readbook.Help
import io.legado.app.ui.readbook.ReadBookActivity import io.legado.app.ui.readbook.ReadBookActivity
import io.legado.app.utils.getPrefInt import io.legado.app.ui.widget.font.FontSelectDialog
import io.legado.app.utils.postEvent import io.legado.app.utils.*
import io.legado.app.utils.putPrefInt
import kotlinx.android.synthetic.main.activity_read_book.* import kotlinx.android.synthetic.main.activity_read_book.*
import kotlinx.android.synthetic.main.dialog_read_book_style.* import kotlinx.android.synthetic.main.dialog_read_book_style.*
import org.jetbrains.anko.sdk27.listeners.onCheckedChange import org.jetbrains.anko.sdk27.listeners.onCheckedChange
@ -87,6 +86,17 @@ class ReadStyleDialog : DialogFragment() {
tv_text_bold.isSelected = textBold tv_text_bold.isSelected = textBold
postEvent(Bus.UP_CONFIG, false) postEvent(Bus.UP_CONFIG, false)
} }
tv_text_font.onClick {
FontSelectDialog(requireContext()).apply {
curPath = requireContext().getPrefString("readBookFont")
defaultFont = {
requireContext().putPrefString("readBookFont", "")
}
selectFile = {
requireContext().putPrefString("readBookFont", it)
}
}.show()
}
tv_padding.onClick { tv_padding.onClick {
val activity = activity val activity = activity
dismiss() dismiss()

@ -1,41 +1,49 @@
package io.legado.app.ui.widget.font package io.legado.app.ui.widget.font
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.os.Bundle import android.content.Context
import android.content.DialogInterface
import android.os.Environment import android.os.Environment
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.R import io.legado.app.R
import io.legado.app.lib.dialogs.AlertBuilder
import io.legado.app.lib.dialogs.alert
import io.legado.app.utils.invisible import io.legado.app.utils.invisible
import io.legado.app.utils.visible import io.legado.app.utils.visible
import kotlinx.android.synthetic.main.dialog_font_select.* import kotlinx.android.synthetic.main.dialog_font_select.view.*
import java.io.File import java.io.File
class FontSelectDialog : DialogFragment(), FontAdapter.CallBack { class FontSelectDialog(context: Context) : FontAdapter.CallBack {
private val defaultFolder = private val defaultFolder =
Environment.getExternalStorageDirectory().absolutePath + File.separator + "Fonts" Environment.getExternalStorageDirectory().absolutePath + File.separator + "Fonts"
lateinit var adapter: FontAdapter private lateinit var adapter: FontAdapter
private var builder: AlertBuilder<DialogInterface>
@SuppressLint("InflateParams")
private var view: View = LayoutInflater.from(context).inflate(R.layout.dialog_font_select, null)
var curPath: String? = null
var fontFolder: String? = null
var defaultFont: (() -> Unit)? = null
var selectFile: ((path: String) -> Unit)? = null
override fun onCreateView( init {
inflater: LayoutInflater, builder = context.alert(title = context.getString(R.string.select_font)) {
container: ViewGroup?, customView = view
savedInstanceState: Bundle? positiveButton(R.string.default_font) { defaultFont?.invoke() }
): View? { negativeButton(R.string.cancel)
return inflater.inflate(R.layout.dialog_font_select, container)
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initData() initData()
} }
private fun initData() { fun show() {
adapter = FontAdapter(requireContext(), this) builder.show()
recycler_view.layoutManager = LinearLayoutManager(requireContext()) }
private fun initData() = with(view) {
adapter = FontAdapter(context, this@FontSelectDialog)
recycler_view.layoutManager = LinearLayoutManager(context)
recycler_view.adapter = adapter recycler_view.adapter = adapter
val files = getFontFiles() val files = getFontFiles()
if (files == null) { if (files == null) {
@ -47,9 +55,12 @@ class FontSelectDialog : DialogFragment(), FontAdapter.CallBack {
} }
@SuppressLint("DefaultLocale") @SuppressLint("DefaultLocale")
private fun getFontFiles(fontFolder: String = defaultFolder): Array<File>? { private fun getFontFiles(): Array<File>? {
val path = if (fontFolder.isNullOrEmpty()) {
defaultFolder
} else fontFolder
return try { return try {
val file = File(fontFolder) val file = File(path)
file.listFiles { pathName -> file.listFiles { pathName ->
pathName.name.toLowerCase().matches(".*\\.[ot]tf".toRegex()) pathName.name.toLowerCase().matches(".*\\.[ot]tf".toRegex())
} }
@ -59,10 +70,10 @@ class FontSelectDialog : DialogFragment(), FontAdapter.CallBack {
} }
override fun onClick(file: File) { override fun onClick(file: File) {
selectFile?.invoke(file.absolutePath)
} }
override fun curFilePath(): String { override fun curFilePath(): String {
return "" return curPath ?: ""
} }
} }
Loading…
Cancel
Save