From da1a3c1f546d86aa1d85e90910c229c4cc516d30 Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 6 Nov 2019 10:21:08 +0800 Subject: [PATCH] up --- .../io/legado/app/data/entities/BookSource.kt | 10 ++--- .../io/legado/app/data/entities/RssSource.kt | 27 +++++++++++- .../io/legado/app/ui/main/MainActivity.kt | 25 ++++++----- .../rss/source/edit/RssSourceEditActivity.kt | 44 +++++++++++++++---- 4 files changed, 80 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/entities/BookSource.kt b/app/src/main/java/io/legado/app/data/entities/BookSource.kt index b9419c805..20965dbf7 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookSource.kt @@ -191,9 +191,9 @@ data class BookSource( && ruleToc == source.ruleToc && ruleContent == source.ruleContent } -} -data class ExploreKind( - var title: String, - var url: String? = null -) \ No newline at end of file + data class ExploreKind( + var title: String, + var url: String? = null + ) +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/entities/RssSource.kt b/app/src/main/java/io/legado/app/data/entities/RssSource.kt index c295753b1..128410f99 100644 --- a/app/src/main/java/io/legado/app/data/entities/RssSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/RssSource.kt @@ -30,4 +30,29 @@ data class RssSource( var enableJs: Boolean = false, var loadWithBaseUrl: Boolean = false, var customOrder: Int = 0 -) : Parcelable \ No newline at end of file +) : Parcelable { + + fun equal(source: RssSource?): Boolean { + if (source == null) { + return false + } else { + return sourceUrl == source.sourceUrl + && sourceName == source.sourceName + && sourceIcon == source.sourceIcon + && enabled == source.enabled + && sourceGroup == source.sourceGroup + && ruleArticles == source.ruleArticles + && ruleNextPage == source.ruleNextPage + && ruleTitle == source.ruleTitle + && rulePubDate == source.rulePubDate + && ruleCategories == source.ruleCategories + && ruleDescription == source.ruleDescription + && ruleLink == source.ruleLink + && ruleContent == source.ruleContent + && enableJs == source.enableJs + && loadWithBaseUrl == source.loadWithBaseUrl + && customOrder == source.customOrder + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt index ebd4b73a7..1c9e0a18b 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt @@ -117,21 +117,22 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), recreate() } } -} -private class TabFragmentPageAdapter internal constructor(fm: FragmentManager) : - FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) { + private class TabFragmentPageAdapter internal constructor(fm: FragmentManager) : + FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) { - override fun getItem(position: Int): Fragment { - return when (position) { - 0 -> BookshelfFragment() - 1 -> ExploreFragment() - 2 -> RssFragment() - else -> MyFragment() + override fun getItem(position: Int): Fragment { + return when (position) { + 0 -> BookshelfFragment() + 1 -> ExploreFragment() + 2 -> RssFragment() + else -> MyFragment() + } } - } - override fun getCount(): Int { - return 4 + override fun getCount(): Int { + return 4 + } } } + diff --git a/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt index 35ef24d50..9d415e860 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt @@ -18,10 +18,14 @@ import io.legado.app.base.VMBaseActivity import io.legado.app.constant.AppConst import io.legado.app.data.entities.EditEntity import io.legado.app.data.entities.RssSource +import io.legado.app.lib.dialogs.alert +import io.legado.app.lib.dialogs.noButton +import io.legado.app.lib.dialogs.yesButton import io.legado.app.lib.theme.ATH import io.legado.app.ui.rss.source.debug.RssSourceDebugActivity import io.legado.app.ui.widget.KeyboardToolPop import io.legado.app.utils.GSON +import io.legado.app.utils.applyTint import io.legado.app.utils.getViewModel import kotlinx.android.synthetic.main.activity_rss_source_edit.* import org.jetbrains.anko.displayMetrics @@ -50,6 +54,24 @@ class RssSourceEditActivity : } } + override fun finish() { + val source = getRssSource() + if (!source.equal(viewModel.rssSource)) { + alert(R.string.exit_no_save) { + yesButton { + if (checkSource(source)) { + viewModel.save(source) { + super.finish() + } + } + } + noButton { } + }.show().applyTint() + } else { + super.finish() + } + } + override fun onDestroy() { super.onDestroy() mSoftKeyboardTool?.dismiss() @@ -63,17 +85,19 @@ class RssSourceEditActivity : override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.menu_save -> { - getRssSource()?.let { - viewModel.save(it) { + val source = getRssSource() + if (checkSource(source)) { + viewModel.save(source) { setResult(Activity.RESULT_OK) finish() } } } R.id.menu_debug_source -> { - getRssSource()?.let { - viewModel.save(it) { - startActivity(Pair("key", it.sourceUrl)) + val source = getRssSource() + if (checkSource(source)) { + viewModel.save(source) { + startActivity(Pair("key", source.sourceUrl)) } } } @@ -134,7 +158,7 @@ class RssSourceEditActivity : adapter.editEntities = sourceEntities } - private fun getRssSource(): RssSource? { + private fun getRssSource(): RssSource { val source = viewModel.rssSource?.copy() ?: RssSource() source.enabled = cb_is_enable.isChecked source.enableJs = cb_enable_js.isChecked @@ -156,11 +180,15 @@ class RssSourceEditActivity : "ruleContent" -> source.ruleContent = it.value } } + return source + } + + private fun checkSource(source: RssSource): Boolean { if (source.sourceName.isBlank() || source.sourceName.isBlank()) { toast("名称或url不能为空") - return null + return false } - return source + return true } override fun sendText(text: String) {