|
|
|
@ -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 ?: "" |
|
|
|
|
} |
|
|
|
|
} |