pull/66/head
kunfei 5 years ago
parent d3541b7e53
commit b972679f6a
  1. 27
      app/src/main/java/io/legado/app/base/BasePreferenceFragment.kt
  2. 1
      app/src/main/java/io/legado/app/constant/PreferKey.kt
  3. 17
      app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt
  4. 22
      app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt
  5. 108
      app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt
  6. 4
      app/src/main/java/io/legado/app/utils/DocumentUtils.kt
  7. 16
      app/src/main/res/layout/dialog_font_select.xml

@ -1,27 +0,0 @@
package io.legado.app.base
import android.os.Bundle
import androidx.preference.PreferenceFragmentCompat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlin.coroutines.CoroutineContext
abstract class BasePreferenceFragment : PreferenceFragmentCompat(), CoroutineScope {
lateinit var job: Job
override val coroutineContext: CoroutineContext
get() = job + Dispatchers.Main
override fun onCreate(savedInstanceState: Bundle?) {
job = Job()
super.onCreate(savedInstanceState)
}
override fun onDestroy() {
super.onDestroy()
job.cancel()
}
}

@ -16,4 +16,5 @@ object PreferKey {
const val lastGroup = "lastGroup"
const val pageAnim = "pageAnim"
const val readBookFont = "readBookFont"
const val fontFolder = "fontFolder"
}

@ -20,7 +20,10 @@ import io.legado.app.lib.theme.primaryColor
import io.legado.app.ui.book.read.Help
import io.legado.app.ui.book.read.ReadBookActivity
import io.legado.app.ui.widget.font.FontSelectDialog
import io.legado.app.utils.*
import io.legado.app.utils.getPrefInt
import io.legado.app.utils.postEvent
import io.legado.app.utils.progressAdd
import io.legado.app.utils.putPrefInt
import kotlinx.android.synthetic.main.activity_book_read.*
import kotlinx.android.synthetic.main.dialog_read_book_style.*
import org.jetbrains.anko.sdk27.listeners.onCheckedChange
@ -86,17 +89,7 @@ class ReadStyleDialog : DialogFragment() {
postEvent(Bus.UP_CONFIG, false)
}
tv_text_font.onClick {
FontSelectDialog(requireContext()).apply {
curPath = requireContext().getPrefString(PreferKey.readBookFont)
defaultFont = {
requireContext().putPrefString(PreferKey.readBookFont, "")
postEvent(Bus.UP_CONFIG, true)
}
selectFile = {
requireContext().putPrefString(PreferKey.readBookFont, it)
postEvent(Bus.UP_CONFIG, true)
}
}.show()
FontSelectDialog().show(childFragmentManager, "fontSelectDialog")
}
tv_text_indent.onClick {
selector(

@ -11,8 +11,8 @@ import androidx.documentfile.provider.DocumentFile
import androidx.preference.EditTextPreference
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import io.legado.app.R
import io.legado.app.base.BasePreferenceFragment
import io.legado.app.help.IntentHelp
import io.legado.app.help.permission.Permissions
import io.legado.app.help.permission.PermissionsCompat
@ -28,17 +28,22 @@ import io.legado.app.utils.DocumentUtils
import io.legado.app.utils.LogUtils
import io.legado.app.utils.applyTint
import io.legado.app.utils.getPrefString
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.jetbrains.anko.toast
import kotlin.coroutines.CoroutineContext
class WebDavConfigFragment : BasePreferenceFragment(), Preference.OnPreferenceChangeListener {
class WebDavConfigFragment : PreferenceFragmentCompat(),
Preference.OnPreferenceChangeListener,
CoroutineScope {
lateinit var job: Job
private val oldDataRequestCode = 23156
override val coroutineContext: CoroutineContext
get() = job + Dispatchers.Main
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
job = Job()
fun bindPreferenceSummaryToValue(preference: Preference?) {
preference?.apply {
onPreferenceChangeListener = this@WebDavConfigFragment
@ -76,6 +81,11 @@ class WebDavConfigFragment : BasePreferenceFragment(), Preference.OnPreferenceCh
ATH.applyEdgeEffectColor(listView)
}
override fun onDestroy() {
super.onDestroy()
job.cancel()
}
override fun onPreferenceChange(preference: Preference?, newValue: Any?): Boolean {
when {
preference?.key == "web_dav_password" -> if (newValue == null) {

@ -1,68 +1,83 @@
package io.legado.app.ui.widget.font
import android.annotation.SuppressLint
import android.content.Context
import android.os.Environment
import android.app.Activity.RESULT_OK
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.app.AlertDialog
import android.view.ViewGroup
import androidx.documentfile.provider.DocumentFile
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.App
import io.legado.app.R
import io.legado.app.lib.dialogs.AlertBuilder
import io.legado.app.lib.dialogs.alert
import io.legado.app.utils.applyTint
import io.legado.app.utils.invisible
import io.legado.app.utils.visible
import kotlinx.android.synthetic.main.dialog_font_select.view.*
import io.legado.app.constant.PreferKey
import io.legado.app.help.FileHelp
import io.legado.app.utils.DocumentUtils
import io.legado.app.utils.getPrefString
import kotlinx.android.synthetic.main.dialog_font_select.*
import java.io.File
class FontSelectDialog(context: Context) : FontAdapter.CallBack {
private val defaultFolder =
Environment.getExternalStorageDirectory().absolutePath + File.separator + "Fonts"
class FontSelectDialog : DialogFragment(), FontAdapter.CallBack {
private val fontFolderRequestCode = 35485
private lateinit var adapter: FontAdapter
private var builder: AlertBuilder<AlertDialog>
private var dialog: AlertDialog? = null
@SuppressLint("InflateParams")
private var view: View = LayoutInflater.from(context).inflate(R.layout.dialog_font_select, null)
var curPath: String? = null
var fontFolder: String? = null
val fontFolder = App.INSTANCE.filesDir.absolutePath + File.separator + "Fonts" + File.separator
var defaultFont: (() -> Unit)? = null
var selectFile: ((path: String) -> Unit)? = null
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 onStart() {
super.onStart()
val dm = DisplayMetrics()
activity?.windowManager?.defaultDisplay?.getMetrics(dm)
dialog?.window?.setLayout((dm.widthPixels * 0.9).toInt(), (dm.heightPixels * 0.9).toInt())
}
fun show() {
dialog = builder.show().applyTint()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.dialog_font_select, container)
}
private fun initData() = with(view) {
adapter = FontAdapter(context, this@FontSelectDialog)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
tool_bar.setTitle(R.string.select_font)
adapter = FontAdapter(requireContext(), this)
recycler_view.layoutManager = LinearLayoutManager(context)
recycler_view.adapter = adapter
val files = getFontFiles()
if (files.isNullOrEmpty()) {
tv_no_data.visible()
} else {
tv_no_data.invisible()
adapter.setItems(files.toList())
val fontPath = getPrefString(PreferKey.fontFolder)
if (fontPath.isNullOrEmpty()) {
try {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivityForResult(intent, fontFolderRequestCode)
} catch (e: java.lang.Exception) {
}
}
}
@SuppressLint("DefaultLocale")
private fun getFontFiles(uri: Uri) {
DocumentFile.fromTreeUri(requireContext(), uri)?.listFiles()?.forEach { file ->
if (file.name?.toLowerCase()?.matches(".*\\.[ot]tf".toRegex()) == true) {
DocumentUtils.readBytes(App.INSTANCE, file.uri)?.let {
FileHelp.getFile(fontFolder + file.name).writeBytes(it)
}
}
}
}
@SuppressLint("DefaultLocale")
private fun getFontFiles(): Array<File>? {
val path = if (fontFolder.isNullOrEmpty()) {
defaultFolder
} else fontFolder
return try {
val file = File(path)
val file = File(fontFolder)
file.listFiles { pathName ->
pathName.name.toLowerCase().matches(".*\\.[ot]tf".toRegex())
}
@ -83,4 +98,19 @@ class FontSelectDialog(context: Context) : FontAdapter.CallBack {
override fun curFilePath(): String {
return curPath ?: ""
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
fontFolderRequestCode -> if (resultCode == RESULT_OK) {
data?.data?.let { uri ->
getFontFiles(uri)
}
}
}
}
interface CallBack {
fun selectFile(uri: Uri)
}
}

@ -5,10 +5,12 @@ import android.net.Uri
object DocumentUtils {
@JvmStatic
fun writeText(context: Context, data: String, fileUri: Uri): Boolean {
return writeBytes(context, data.toByteArray(), fileUri)
}
@JvmStatic
fun writeBytes(context: Context, data: ByteArray, fileUri: Uri): Boolean {
try {
context.contentResolver.openOutputStream(fileUri)?.let {
@ -22,6 +24,7 @@ object DocumentUtils {
return false
}
@JvmStatic
fun readText(context: Context, uri: Uri): String? {
readBytes(context, uri)?.let {
return String(it)
@ -29,6 +32,7 @@ object DocumentUtils {
return null
}
@JvmStatic
fun readBytes(context: Context, uri: Uri): ByteArray? {
try {
context.contentResolver.openInputStream(uri)?.let {

@ -1,20 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
<androidx.appcompat.widget.Toolbar
android:id="@+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_no_data"
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/fonts_folder" />
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
Loading…
Cancel
Save