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 lastGroup = "lastGroup"
const val pageAnim = "pageAnim" const val pageAnim = "pageAnim"
const val readBookFont = "readBookFont" 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.Help
import io.legado.app.ui.book.read.ReadBookActivity import io.legado.app.ui.book.read.ReadBookActivity
import io.legado.app.ui.widget.font.FontSelectDialog 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.activity_book_read.*
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
@ -86,17 +89,7 @@ class ReadStyleDialog : DialogFragment() {
postEvent(Bus.UP_CONFIG, false) postEvent(Bus.UP_CONFIG, false)
} }
tv_text_font.onClick { tv_text_font.onClick {
FontSelectDialog(requireContext()).apply { FontSelectDialog().show(childFragmentManager, "fontSelectDialog")
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()
} }
tv_text_indent.onClick { tv_text_indent.onClick {
selector( selector(

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

@ -1,68 +1,83 @@
package io.legado.app.ui.widget.font package io.legado.app.ui.widget.font
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Context import android.app.Activity.RESULT_OK
import android.os.Environment import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View 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 androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.lib.dialogs.AlertBuilder import io.legado.app.constant.PreferKey
import io.legado.app.lib.dialogs.alert import io.legado.app.help.FileHelp
import io.legado.app.utils.applyTint import io.legado.app.utils.DocumentUtils
import io.legado.app.utils.invisible import io.legado.app.utils.getPrefString
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(context: Context) : FontAdapter.CallBack { class FontSelectDialog : DialogFragment(), FontAdapter.CallBack {
private val fontFolderRequestCode = 35485
private val defaultFolder =
Environment.getExternalStorageDirectory().absolutePath + File.separator + "Fonts"
private lateinit var adapter: FontAdapter 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 curPath: String? = null
var fontFolder: String? = null val fontFolder = App.INSTANCE.filesDir.absolutePath + File.separator + "Fonts" + File.separator
var defaultFont: (() -> Unit)? = null var defaultFont: (() -> Unit)? = null
var selectFile: ((path: String) -> Unit)? = null var selectFile: ((path: String) -> Unit)? = null
init { override fun onStart() {
builder = context.alert(title = context.getString(R.string.select_font)) { super.onStart()
customView = view val dm = DisplayMetrics()
positiveButton(R.string.default_font) { defaultFont?.invoke() } activity?.windowManager?.defaultDisplay?.getMetrics(dm)
negativeButton(R.string.cancel) dialog?.window?.setLayout((dm.widthPixels * 0.9).toInt(), (dm.heightPixels * 0.9).toInt())
}
initData()
} }
fun show() { override fun onCreateView(
dialog = builder.show().applyTint() inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.dialog_font_select, container)
} }
private fun initData() = with(view) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
adapter = FontAdapter(context, this@FontSelectDialog) super.onViewCreated(view, savedInstanceState)
tool_bar.setTitle(R.string.select_font)
adapter = FontAdapter(requireContext(), this)
recycler_view.layoutManager = LinearLayoutManager(context) recycler_view.layoutManager = LinearLayoutManager(context)
recycler_view.adapter = adapter recycler_view.adapter = adapter
val files = getFontFiles()
if (files.isNullOrEmpty()) { val fontPath = getPrefString(PreferKey.fontFolder)
tv_no_data.visible() if (fontPath.isNullOrEmpty()) {
} else { try {
tv_no_data.invisible() val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
adapter.setItems(files.toList()) 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") @SuppressLint("DefaultLocale")
private fun getFontFiles(): Array<File>? { private fun getFontFiles(): Array<File>? {
val path = if (fontFolder.isNullOrEmpty()) {
defaultFolder
} else fontFolder
return try { return try {
val file = File(path) val file = File(fontFolder)
file.listFiles { pathName -> file.listFiles { pathName ->
pathName.name.toLowerCase().matches(".*\\.[ot]tf".toRegex()) pathName.name.toLowerCase().matches(".*\\.[ot]tf".toRegex())
} }
@ -83,4 +98,19 @@ class FontSelectDialog(context: Context) : FontAdapter.CallBack {
override fun curFilePath(): String { override fun curFilePath(): String {
return curPath ?: "" 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 { object DocumentUtils {
@JvmStatic
fun writeText(context: Context, data: String, fileUri: Uri): Boolean { fun writeText(context: Context, data: String, fileUri: Uri): Boolean {
return writeBytes(context, data.toByteArray(), fileUri) return writeBytes(context, data.toByteArray(), fileUri)
} }
@JvmStatic
fun writeBytes(context: Context, data: ByteArray, fileUri: Uri): Boolean { fun writeBytes(context: Context, data: ByteArray, fileUri: Uri): Boolean {
try { try {
context.contentResolver.openOutputStream(fileUri)?.let { context.contentResolver.openOutputStream(fileUri)?.let {
@ -22,6 +24,7 @@ object DocumentUtils {
return false return false
} }
@JvmStatic
fun readText(context: Context, uri: Uri): String? { fun readText(context: Context, uri: Uri): String? {
readBytes(context, uri)?.let { readBytes(context, uri)?.let {
return String(it) return String(it)
@ -29,6 +32,7 @@ object DocumentUtils {
return null return null
} }
@JvmStatic
fun readBytes(context: Context, uri: Uri): ByteArray? { fun readBytes(context: Context, uri: Uri): ByteArray? {
try { try {
context.contentResolver.openInputStream(uri)?.let { context.contentResolver.openInputStream(uri)?.let {

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