优化
This commit is contained in:
@@ -4,6 +4,7 @@ import android.app.Application
|
||||
import android.net.Uri
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import io.legado.app.constant.AppLog
|
||||
import io.legado.app.constant.AppPattern
|
||||
import io.legado.app.constant.AppPattern.bookFileRegex
|
||||
import io.legado.app.model.localBook.LocalBook
|
||||
import io.legado.app.utils.*
|
||||
@@ -22,20 +23,13 @@ class FileAssociationViewModel(application: Application) : BaseAssociationViewMo
|
||||
if (uri.isContentScheme() || uri.isFileScheme()) {
|
||||
val fileDoc = FileDoc.fromUri(uri, false)
|
||||
fileName = fileDoc.name
|
||||
kotlin.runCatching {
|
||||
if (uri.inputStream(context).getOrNull().isJson()) {
|
||||
importJson(uri)
|
||||
return@execute
|
||||
if (fileName.matches(AppPattern.archiveFileRegex)) {
|
||||
ArchiveUtils.deCompress(fileDoc).forEach {
|
||||
dispatch(FileDoc.fromFile(it))
|
||||
}
|
||||
}.onFailure {
|
||||
it.printOnDebug()
|
||||
AppLog.put("尝试导入为JSON文件失败\n${it.localizedMessage}", it)
|
||||
} else {
|
||||
dispatch(fileDoc)
|
||||
}
|
||||
if (fileName.matches(bookFileRegex)) {
|
||||
importBookLiveData.postValue(uri)
|
||||
return@execute
|
||||
}
|
||||
notSupportedLiveData.postValue(Pair(uri, fileName))
|
||||
} else {
|
||||
onLineImportLive.postValue(uri)
|
||||
}
|
||||
@@ -46,6 +40,23 @@ class FileAssociationViewModel(application: Application) : BaseAssociationViewMo
|
||||
}
|
||||
}
|
||||
|
||||
fun dispatch(fileDoc: FileDoc) {
|
||||
kotlin.runCatching {
|
||||
if (fileDoc.openInputStream().getOrNull().isJson()) {
|
||||
importJson(fileDoc.uri)
|
||||
return
|
||||
}
|
||||
}.onFailure {
|
||||
it.printOnDebug()
|
||||
AppLog.put("尝试导入为JSON文件失败\n${it.localizedMessage}", it)
|
||||
}
|
||||
if (fileDoc.name.matches(bookFileRegex)) {
|
||||
importBookLiveData.postValue(fileDoc.uri)
|
||||
return
|
||||
}
|
||||
notSupportedLiveData.postValue(Pair(fileDoc.uri, fileDoc.name))
|
||||
}
|
||||
|
||||
fun importBook(uri: Uri) {
|
||||
val book = LocalBook.importFile(uri)
|
||||
openBookLiveData.postValue(book.bookUrl)
|
||||
|
||||
Reference in New Issue
Block a user