diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt index af1231556..52b433a32 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt @@ -213,14 +213,6 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity }.show().applyTint() } - private fun selectFile() { - FileChooserDialog.show( - supportFragmentManager, importSource, - allowExtensions = arrayOf("txt", "json"), - menus = arrayOf(getString(R.string.sys_file_picker)) - ) - } - private fun selectFileSys() { try { val intent = Intent(Intent.ACTION_GET_CONTENT) @@ -229,15 +221,24 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity intent.type = "text/*"//设置类型 startActivityForResult(intent, importSource) } catch (e: Exception) { - selectFile() + PermissionsCompat.Builder(this) + .addPermissions( + Permissions.READ_EXTERNAL_STORAGE, + Permissions.WRITE_EXTERNAL_STORAGE + ) + .rationale(R.string.bg_image_per) + .onGranted { + selectFile() + } + .request() } } - override fun onMenuClick(menu: String) { - super.onMenuClick(menu) - when (menu) { - getString(R.string.sys_file_picker) -> selectFileSys() - } + private fun selectFile() { + FileChooserDialog.show( + supportFragmentManager, importSource, + allowExtensions = arrayOf("txt", "json") + ) } override fun onFilePicked(requestCode: Int, currentPath: String) { diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt index 00dfd3f34..5c53e91e1 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt @@ -30,17 +30,20 @@ import io.legado.app.lib.dialogs.okButton import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.primaryTextColor import io.legado.app.lib.theme.view.ATEAutoCompleteTextView +import io.legado.app.ui.filechooser.FileChooserDialog import io.legado.app.ui.replacerule.edit.ReplaceEditDialog import io.legado.app.utils.* import kotlinx.android.synthetic.main.activity_replace_rule.* import kotlinx.android.synthetic.main.dialog_edit_text.view.* import kotlinx.android.synthetic.main.view_search.* import org.jetbrains.anko.toast +import java.io.File import java.io.FileNotFoundException class ReplaceRuleActivity : VMBaseActivity(R.layout.activity_replace_rule), SearchView.OnQueryTextListener, + FileChooserDialog.CallBack, ReplaceRuleAdapter.CallBack { override val viewModel: ReplaceRuleViewModel get() = getViewModel(ReplaceRuleViewModel::class.java) @@ -191,7 +194,32 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi intent.type = "text/*"//设置类型 startActivityForResult(intent, importSource) } catch (e: Exception) { + PermissionsCompat.Builder(this) + .addPermissions( + Permissions.READ_EXTERNAL_STORAGE, + Permissions.WRITE_EXTERNAL_STORAGE + ) + .rationale(R.string.bg_image_per) + .onGranted { + selectFile() + } + .request() + } + } + private fun selectFile() { + FileChooserDialog.show( + supportFragmentManager, importSource, + allowExtensions = arrayOf("txt", "json") + ) + } + + override fun onFilePicked(requestCode: Int, currentPath: String) { + if (requestCode == importSource) { + Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show() + viewModel.importSource(File(currentPath).readText()) { msg -> + title_bar.snackbar(msg) + } } } diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt index 540900eec..ba6bc2a61 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt @@ -16,13 +16,13 @@ import java.io.File class ReplaceRuleViewModel(application: Application) : BaseViewModel(application) { - fun importSource(text: String, toast: (msg: String) -> Unit) { + fun importSource(text: String, showMsg: (msg: String) -> Unit) { execute { Restore.importOldReplaceRule(text) }.onError { - toast(it.localizedMessage ?: "ERROR") + showMsg(it.localizedMessage ?: "ERROR") }.onSuccess { - toast(context.getString(R.string.success)) + showMsg(context.getString(R.string.success)) } } diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt index 883acbb8c..b536a0b84 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt @@ -198,14 +198,6 @@ class RssSourceActivity : VMBaseActivity(R.layout.activity_r }.show().applyTint() } - private fun selectFile() { - FileChooserDialog.show( - supportFragmentManager, importSource, - allowExtensions = arrayOf("txt", "json"), - menus = arrayOf(getString(R.string.sys_file_picker)) - ) - } - private fun selectFileSys() { try { val intent = Intent(Intent.ACTION_GET_CONTENT) @@ -213,15 +205,24 @@ class RssSourceActivity : VMBaseActivity(R.layout.activity_r intent.type = "text/*"//设置类型 startActivityForResult(intent, importSource) } catch (e: Exception) { - selectFile() + PermissionsCompat.Builder(this) + .addPermissions( + Permissions.READ_EXTERNAL_STORAGE, + Permissions.WRITE_EXTERNAL_STORAGE + ) + .rationale(R.string.bg_image_per) + .onGranted { + selectFile() + } + .request() } } - override fun onMenuClick(menu: String) { - super.onMenuClick(menu) - when (menu) { - getString(R.string.sys_file_picker) -> selectFileSys() - } + private fun selectFile() { + FileChooserDialog.show( + supportFragmentManager, importSource, + allowExtensions = arrayOf("txt", "json") + ) } override fun onFilePicked(requestCode: Int, currentPath: String) {