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.ui.readbook.Help
import io.legado.app.ui.readbook.ReadBookActivity
import io.legado.app.utils.getPrefInt
import io.legado.app.utils.postEvent
import io.legado.app.utils.putPrefInt
import io.legado.app.ui.widget.font.FontSelectDialog
import io.legado.app.utils.*
import kotlinx.android.synthetic.main.activity_read_book.*
import kotlinx.android.synthetic.main.dialog_read_book_style.*
import org.jetbrains.anko.sdk27.listeners.onCheckedChange
@ -87,6 +86,17 @@ class ReadStyleDialog : DialogFragment() {
tv_text_bold.isSelected = textBold
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 {
val activity = activity
dismiss()

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