This commit is contained in:
kunfei
2023-02-22 08:36:24 +08:00
parent cfbea3ead2
commit 00efb16d72
6 changed files with 18 additions and 27 deletions

View File

@@ -192,8 +192,9 @@ data class BookSource(
}
}
fun equal(source: BookSource) =
equal(bookSourceName, source.bookSourceName)
fun equal(source: BookSource?): Boolean {
source ?: return false
return equal(bookSourceName, source.bookSourceName)
&& equal(bookSourceUrl, source.bookSourceUrl)
&& equal(bookSourceGroup, source.bookSourceGroup)
&& bookSourceType == source.bookSourceType
@@ -216,6 +217,7 @@ data class BookSource(
&& getBookInfoRule() == source.getBookInfoRule()
&& getTocRule() == source.getTocRule()
&& getContentRule() == source.getContentRule()
}
private fun equal(a: String?, b: String?) = a == b || (a.isNullOrEmpty() && b.isNullOrEmpty())

View File

@@ -101,7 +101,8 @@ data class RssSource(
override fun hashCode() = sourceUrl.hashCode()
fun equal(source: RssSource): Boolean {
fun equal(source: RssSource?): Boolean {
source ?: return false
return equal(sourceUrl, source.sourceUrl)
&& equal(sourceIcon, source.sourceIcon)
&& enabled == source.enabled

View File

@@ -103,7 +103,7 @@ class BookSourceEditActivity :
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.menu_save -> getSource().let { source ->
if (!source.equal(viewModel.bookSource ?: BookSource())) {
if (!source.equal(viewModel.bookSource)) {
source.lastUpdateTime = System.currentTimeMillis()
}
if (checkSource(source)) {
@@ -192,12 +192,7 @@ class BookSourceEditActivity :
override fun finish() {
val source = getSource()
val source2 = viewModel.bookSource ?: BookSource().apply {
enabledExplore = true
enabledCookieJar = true
enabledReview = true
}
if (!source.equal(source2)) {
if (!source.equal(viewModel.bookSource)) {
alert(R.string.exit) {
setMessage(R.string.exit_no_save)
positiveButton(R.string.yes)
@@ -599,7 +594,7 @@ class BookSourceEditActivity :
launch {
val source = viewModel.bookSource
if (source == null) {
toastOnUi("书源不存在")
toastOnUi("先保存书源")
return@launch
}
val variable = withContext(IO) { source.getVariable() }

View File

@@ -18,7 +18,6 @@ import kotlinx.coroutines.Dispatchers
class BookSourceEditViewModel(application: Application) : BaseViewModel(application) {
var autoComplete = false
var bookSource: BookSource? = null
private var oldSourceUrl: String? = null
fun initData(intent: Intent, onFinally: () -> Unit) {
execute {
@@ -28,7 +27,6 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat
source = appDb.bookSourceDao.getBookSource(sourceUrl)
}
source?.let {
oldSourceUrl = it.bookSourceUrl
bookSource = it
}
}.onFinally {
@@ -38,13 +36,10 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat
fun save(source: BookSource, success: (() -> Unit)? = null) {
execute {
oldSourceUrl?.let {
if (oldSourceUrl != source.bookSourceUrl) {
appDb.bookSourceDao.delete(it)
SourceConfig.removeSource(it)
}
bookSource?.let {
appDb.bookSourceDao.delete(it)
SourceConfig.removeSource(it.bookSourceUrl)
}
oldSourceUrl = source.bookSourceUrl
source.lastUpdateTime = System.currentTimeMillis()
appDb.bookSourceDao.insert(source)
bookSource = source

View File

@@ -100,7 +100,7 @@ class RssSourceEditActivity :
}
override fun onMenuOpened(featureId: Int, menu: Menu): Boolean {
menu.findItem(R.id.menu_login)?.isVisible = !viewModel.rssSource.loginUrl.isNullOrBlank()
menu.findItem(R.id.menu_login)?.isVisible = !viewModel.rssSource?.loginUrl.isNullOrBlank()
menu.findItem(R.id.menu_auto_complete)?.isChecked = viewModel.autoComplete
return super.onMenuOpened(featureId, menu)
}
@@ -251,7 +251,7 @@ class RssSourceEditActivity :
}
private fun getRssSource(): RssSource {
val source = viewModel.rssSource
val source = viewModel.rssSource?.copy() ?: RssSource()
source.enabled = binding.cbIsEnable.isChecked
source.singleUrl = binding.cbSingleUrl.isChecked
source.enabledCookieJar = binding.cbIsEnableCookie.isChecked

View File

@@ -17,8 +17,7 @@ import kotlinx.coroutines.Dispatchers
class RssSourceEditViewModel(application: Application) : BaseViewModel(application) {
var autoComplete = false
var rssSource: RssSource = RssSource()
private var oldSourceUrl: String = ""
var rssSource: RssSource? = null
fun initData(intent: Intent, onFinally: () -> Unit) {
execute {
@@ -28,7 +27,6 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati
rssSource = it
}
}
oldSourceUrl = rssSource.sourceUrl
}.onFinally {
onFinally()
}
@@ -36,11 +34,11 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati
fun save(source: RssSource, success: (() -> Unit)) {
execute {
if (oldSourceUrl != source.sourceUrl) {
appDb.rssSourceDao.delete(oldSourceUrl)
oldSourceUrl = source.sourceUrl
rssSource?.let {
appDb.rssSourceDao.delete(it)
}
appDb.rssSourceDao.insert(source)
rssSource = source
}.onSuccess {
success()
}.onError {