|
|
@ -6,15 +6,14 @@ import android.net.Uri |
|
|
|
import androidx.documentfile.provider.DocumentFile |
|
|
|
import androidx.documentfile.provider.DocumentFile |
|
|
|
import androidx.lifecycle.MutableLiveData |
|
|
|
import androidx.lifecycle.MutableLiveData |
|
|
|
import io.legado.app.base.BaseViewModel |
|
|
|
import io.legado.app.base.BaseViewModel |
|
|
|
import io.legado.app.help.IntentDataHelp |
|
|
|
|
|
|
|
import io.legado.app.model.localBook.LocalBook |
|
|
|
import io.legado.app.model.localBook.LocalBook |
|
|
|
import io.legado.app.ui.book.read.ReadBookActivity |
|
|
|
import io.legado.app.ui.book.read.ReadBookActivity |
|
|
|
import io.legado.app.utils.isJsonArray |
|
|
|
import io.legado.app.utils.isJson |
|
|
|
import io.legado.app.utils.isJsonObject |
|
|
|
|
|
|
|
import io.legado.app.utils.readText |
|
|
|
import io.legado.app.utils.readText |
|
|
|
import java.io.File |
|
|
|
import java.io.File |
|
|
|
|
|
|
|
|
|
|
|
class FileAssociationViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
class FileAssociationViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
|
|
|
|
val onLineImportLive = MutableLiveData<Uri>() |
|
|
|
val importBookSourceLive = MutableLiveData<String>() |
|
|
|
val importBookSourceLive = MutableLiveData<String>() |
|
|
|
val importRssSourceLive = MutableLiveData<String>() |
|
|
|
val importRssSourceLive = MutableLiveData<String>() |
|
|
|
val importReplaceRuleLive = MutableLiveData<String>() |
|
|
|
val importReplaceRuleLive = MutableLiveData<String>() |
|
|
@ -31,20 +30,20 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
DocumentFile.fromSingleUri(context, uri)?.readText(context) |
|
|
|
DocumentFile.fromSingleUri(context, uri)?.readText(context) |
|
|
|
} |
|
|
|
} |
|
|
|
if (content != null) { |
|
|
|
content?.let { |
|
|
|
if (content.isJsonObject() || content.isJsonArray()) { |
|
|
|
if (it.isJson()) { |
|
|
|
//暂时根据文件内容判断属于什么 |
|
|
|
//暂时根据文件内容判断属于什么 |
|
|
|
when { |
|
|
|
when { |
|
|
|
content.contains("bookSourceUrl") -> { |
|
|
|
content.contains("bookSourceUrl") -> { |
|
|
|
importBookSourceLive.postValue(IntentDataHelp.putData(content)) |
|
|
|
importBookSourceLive.postValue(it) |
|
|
|
return@execute |
|
|
|
return@execute |
|
|
|
} |
|
|
|
} |
|
|
|
content.contains("sourceUrl") -> { |
|
|
|
content.contains("sourceUrl") -> { |
|
|
|
importRssSourceLive.postValue(IntentDataHelp.putData(content)) |
|
|
|
importRssSourceLive.postValue(it) |
|
|
|
return@execute |
|
|
|
return@execute |
|
|
|
} |
|
|
|
} |
|
|
|
content.contains("pattern") -> { |
|
|
|
content.contains("pattern") -> { |
|
|
|
importReplaceRuleLive.postValue(IntentDataHelp.putData(content)) |
|
|
|
importReplaceRuleLive.postValue(it) |
|
|
|
return@execute |
|
|
|
return@execute |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -57,9 +56,9 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica |
|
|
|
val intent = Intent(context, ReadBookActivity::class.java) |
|
|
|
val intent = Intent(context, ReadBookActivity::class.java) |
|
|
|
intent.putExtra("bookUrl", book.bookUrl) |
|
|
|
intent.putExtra("bookUrl", book.bookUrl) |
|
|
|
successLiveData.postValue(intent) |
|
|
|
successLiveData.postValue(intent) |
|
|
|
} else { |
|
|
|
} ?: throw Exception("文件不存在") |
|
|
|
throw Exception("文件不存在") |
|
|
|
} else { |
|
|
|
} |
|
|
|
onLineImportLive.postValue(uri) |
|
|
|
} |
|
|
|
} |
|
|
|
}.onError { |
|
|
|
}.onError { |
|
|
|
it.printStackTrace() |
|
|
|
it.printStackTrace() |
|
|
|