From ba69ae317bce04dbc7f612d9e997c48ff57064ea Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 19 Nov 2021 23:35:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../book/source/manage/BookSourceActivity.kt | 16 +-------------- .../ui/rss/source/manage/RssSourceActivity.kt | 5 ++--- .../rss/source/manage/RssSourceViewModel.kt | 20 ++++++------------- .../io/legado/app/utils/ContextExtensions.kt | 15 ++++++++++++++ 4 files changed, 24 insertions(+), 32 deletions(-) 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 333d1908b..6533035bb 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 @@ -1,7 +1,6 @@ package io.legado.app.ui.book.source.manage import android.annotation.SuppressLint -import android.content.Intent import android.os.Bundle import android.view.Menu import android.view.MenuItem @@ -9,13 +8,11 @@ import android.view.SubMenu import androidx.activity.viewModels import androidx.appcompat.widget.PopupMenu import androidx.appcompat.widget.SearchView -import androidx.core.content.FileProvider import androidx.core.os.bundleOf import androidx.recyclerview.widget.ItemTouchHelper import com.google.android.material.snackbar.Snackbar import io.legado.app.R import io.legado.app.base.VMBaseActivity -import io.legado.app.constant.AppConst import io.legado.app.constant.AppPattern import io.legado.app.constant.EventBus import io.legado.app.data.appDb @@ -124,18 +121,7 @@ class BookSourceActivity : VMBaseActivity startActivity() R.id.menu_import_qr -> qrResult.launch(null) R.id.menu_share_source -> viewModel.saveToFile(adapter.selection) { - val fileUri = FileProvider.getUriForFile(this, AppConst.authority, it) - val intent = Intent(Intent.ACTION_SEND) - intent.type = "text/*" - intent.putExtra(Intent.EXTRA_STREAM, fileUri) - intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - startActivity( - Intent.createChooser( - intent, - getString(R.string.share_selected_source) - ) - ) + share(it) } R.id.menu_group_manage -> showDialogFragment() R.id.menu_import_local -> importDoc.launch { 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 6a7d970ae..82339958f 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 @@ -1,7 +1,6 @@ package io.legado.app.ui.rss.source.manage import android.annotation.SuppressLint -import android.content.Intent import android.os.Bundle import android.view.Menu import android.view.MenuItem @@ -117,8 +116,8 @@ class RssSourceActivity : VMBaseActivity showImportDialog() R.id.menu_import_qr -> qrCodeResult.launch(null) R.id.menu_group_manage -> showDialogFragment() - R.id.menu_share_source -> viewModel.shareSelection(adapter.selection) { - startActivity(Intent.createChooser(it, getString(R.string.share_selected_source))) + R.id.menu_share_source -> viewModel.saveToFile(adapter.selection) { + share(it) } R.id.menu_import_default -> viewModel.importDefault() R.id.menu_help -> showHelp() diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt index b0a80eec2..e80380762 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt @@ -1,15 +1,13 @@ package io.legado.app.ui.rss.source.manage import android.app.Application -import android.content.Intent import android.text.TextUtils -import androidx.core.content.FileProvider import io.legado.app.base.BaseViewModel -import io.legado.app.constant.AppConst import io.legado.app.data.appDb import io.legado.app.data.entities.RssSource import io.legado.app.help.DefaultData import io.legado.app.utils.* +import java.io.File class RssSourceViewModel(application: Application) : BaseViewModel(application) { @@ -77,19 +75,13 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application) } } - fun shareSelection(sources: List, success: ((intent: Intent) -> Unit)) { + fun saveToFile(sources: List, success: (file: File) -> Unit) { execute { - val tmpSharePath = "${context.filesDir}/shareRssSource.json" - FileUtils.delete(tmpSharePath) - val intent = Intent(Intent.ACTION_SEND) - val file = FileUtils.createFileWithReplace(tmpSharePath) + val path = "${context.filesDir}/shareRssSource.json" + FileUtils.delete(path) + val file = FileUtils.createFileWithReplace(path) file.writeText(GSON.toJson(sources)) - val fileUri = FileProvider.getUriForFile(context, AppConst.authority, file) - intent.type = "text/*" - intent.putExtra(Intent.EXTRA_STREAM, fileUri) - intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - intent + file }.onSuccess { success.invoke(it) }.onError { diff --git a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt index d181da779..6562c98e9 100644 --- a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt @@ -177,6 +177,21 @@ fun Context.share(text: String, title: String = getString(R.string.share)) { } } +fun Context.share(file: File, type: String = "text/*") { + val fileUri = FileProvider.getUriForFile(this, AppConst.authority, file) + val intent = Intent(Intent.ACTION_SEND) + intent.type = type + intent.putExtra(Intent.EXTRA_STREAM, fileUri) + intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + startActivity( + Intent.createChooser( + intent, + getString(R.string.share_selected_source) + ) + ) +} + @SuppressLint("SetWorldReadable") fun Context.shareWithQr( text: String,