|
|
|
@ -3,6 +3,7 @@ package io.legado.app.ui.rss.source.edit |
|
|
|
|
import android.app.Application |
|
|
|
|
import android.content.ClipboardManager |
|
|
|
|
import android.content.Context |
|
|
|
|
import android.content.Intent |
|
|
|
|
import androidx.lifecycle.MutableLiveData |
|
|
|
|
import io.legado.app.App |
|
|
|
|
import io.legado.app.base.BaseViewModel |
|
|
|
@ -13,26 +14,35 @@ import io.legado.app.utils.fromJsonObject |
|
|
|
|
class RssSourceEditViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
|
|
|
|
|
|
val sourceLiveData: MutableLiveData<RssSource> = MutableLiveData() |
|
|
|
|
var oldSourceUrl: String? = null |
|
|
|
|
|
|
|
|
|
fun setSource(key: String) { |
|
|
|
|
fun initData(intent: Intent) { |
|
|
|
|
execute { |
|
|
|
|
App.db.rssSourceDao().getByKey(key)?.let { |
|
|
|
|
val key = intent.getStringExtra("data") |
|
|
|
|
var source: RssSource? = null |
|
|
|
|
if (key != null) { |
|
|
|
|
source = App.db.rssSourceDao().getByKey(key) |
|
|
|
|
} |
|
|
|
|
source?.let { |
|
|
|
|
oldSourceUrl = it.sourceUrl |
|
|
|
|
sourceLiveData.postValue(it) |
|
|
|
|
} ?: let { |
|
|
|
|
sourceLiveData.postValue(RssSource().apply { |
|
|
|
|
customOrder = App.db.rssSourceDao().maxOrder + 1 |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun save(rssSource: RssSource, success: (() -> Unit)) { |
|
|
|
|
execute { |
|
|
|
|
sourceLiveData.value?.let { |
|
|
|
|
if (it.sourceUrl != rssSource.sourceUrl) { |
|
|
|
|
oldSourceUrl?.let { |
|
|
|
|
if (oldSourceUrl != rssSource.sourceUrl) { |
|
|
|
|
App.db.rssSourceDao().delete(it) |
|
|
|
|
} |
|
|
|
|
} ?: let { |
|
|
|
|
rssSource.customOrder = App.db.rssSourceDao().maxOrder + 1 |
|
|
|
|
} |
|
|
|
|
oldSourceUrl = rssSource.sourceUrl |
|
|
|
|
App.db.rssSourceDao().insert(rssSource) |
|
|
|
|
sourceLiveData.postValue(rssSource) |
|
|
|
|
}.onSuccess { |
|
|
|
|
success() |
|
|
|
|
}.onError { |
|
|
|
|