优化
This commit is contained in:
@@ -24,6 +24,9 @@ import kotlinx.coroutines.Dispatchers.Main
|
|||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字体选择对话框
|
||||||
|
*/
|
||||||
class FontSelectDialog : BaseDialogFragment(R.layout.dialog_font_select),
|
class FontSelectDialog : BaseDialogFragment(R.layout.dialog_font_select),
|
||||||
Toolbar.OnMenuItemClickListener,
|
Toolbar.OnMenuItemClickListener,
|
||||||
FontAdapter.CallBack {
|
FontAdapter.CallBack {
|
||||||
@@ -35,11 +38,11 @@ class FontSelectDialog : BaseDialogFragment(R.layout.dialog_font_select),
|
|||||||
}
|
}
|
||||||
private val selectFontDir = registerForActivityResult(HandleFileContract()) {
|
private val selectFontDir = registerForActivityResult(HandleFileContract()) {
|
||||||
it.uri?.let { uri ->
|
it.uri?.let { uri ->
|
||||||
if (uri.toString().isContentScheme()) {
|
if (uri.isContentScheme()) {
|
||||||
putPrefString(PreferKey.fontFolder, uri.toString())
|
putPrefString(PreferKey.fontFolder, uri.toString())
|
||||||
val doc = DocumentFile.fromTreeUri(requireContext(), uri)
|
val doc = DocumentFile.fromTreeUri(requireContext(), uri)
|
||||||
if (doc != null) {
|
if (doc != null) {
|
||||||
loadFontFiles(doc)
|
loadFontFiles(FileDoc.fromDocumentFile(doc))
|
||||||
} else {
|
} else {
|
||||||
RealPathUtil.getPath(requireContext(), uri)?.let { path ->
|
RealPathUtil.getPath(requireContext(), uri)?.let { path ->
|
||||||
loadFontFilesByPermission(path)
|
loadFontFilesByPermission(path)
|
||||||
@@ -75,7 +78,7 @@ class FontSelectDialog : BaseDialogFragment(R.layout.dialog_font_select),
|
|||||||
if (fontPath.isContentScheme()) {
|
if (fontPath.isContentScheme()) {
|
||||||
val doc = DocumentFile.fromTreeUri(requireContext(), Uri.parse(fontPath))
|
val doc = DocumentFile.fromTreeUri(requireContext(), Uri.parse(fontPath))
|
||||||
if (doc?.canRead() == true) {
|
if (doc?.canRead() == true) {
|
||||||
loadFontFiles(doc)
|
loadFontFiles(FileDoc.fromDocumentFile(doc))
|
||||||
} else {
|
} else {
|
||||||
openFolder()
|
openFolder()
|
||||||
}
|
}
|
||||||
@@ -122,35 +125,24 @@ class FontSelectDialog : BaseDialogFragment(R.layout.dialog_font_select),
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun loadFontFiles(doc: DocumentFile) {
|
|
||||||
execute {
|
|
||||||
val fontItems = doc.listFileDocs {
|
|
||||||
it.name.matches(fontRegex)
|
|
||||||
}!!
|
|
||||||
mergeFontItems(fontItems, getLocalFonts())
|
|
||||||
}.onSuccess {
|
|
||||||
adapter.setItems(it)
|
|
||||||
}.onError {
|
|
||||||
toastOnUi("getFontFiles:${it.localizedMessage}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun loadFontFilesByPermission(path: String) {
|
private fun loadFontFilesByPermission(path: String) {
|
||||||
PermissionsCompat.Builder(this@FontSelectDialog)
|
PermissionsCompat.Builder(this@FontSelectDialog)
|
||||||
.addPermissions(*Permissions.Group.STORAGE)
|
.addPermissions(*Permissions.Group.STORAGE)
|
||||||
.rationale(R.string.tip_perm_request_storage)
|
.rationale(R.string.tip_perm_request_storage)
|
||||||
.onGranted {
|
.onGranted {
|
||||||
loadFontFiles(path)
|
loadFontFiles(
|
||||||
|
FileDoc.fromFile(File(path))
|
||||||
|
)
|
||||||
}
|
}
|
||||||
.request()
|
.request()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun loadFontFiles(path: String) {
|
private fun loadFontFiles(fileDoc: FileDoc) {
|
||||||
execute {
|
execute {
|
||||||
val fontItems = File(path).listFileDocs {
|
val fontItems = fileDoc.list {
|
||||||
it.name.matches(fontRegex)
|
it.name.matches(fontRegex)
|
||||||
}
|
}
|
||||||
mergeFontItems(fontItems, getLocalFonts())
|
mergeFontItems(fontItems!!, getLocalFonts())
|
||||||
}.onSuccess {
|
}.onSuccess {
|
||||||
adapter.setItems(it)
|
adapter.setItems(it)
|
||||||
}.onError {
|
}.onError {
|
||||||
|
|||||||
Reference in New Issue
Block a user