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