|
|
@ -12,26 +12,34 @@ import io.legado.app.help.storage.OldRule |
|
|
|
class BookSourceEditViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
class BookSourceEditViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
|
|
|
|
|
|
|
|
val sourceLiveData: MutableLiveData<BookSource> = MutableLiveData() |
|
|
|
val sourceLiveData: MutableLiveData<BookSource> = MutableLiveData() |
|
|
|
|
|
|
|
var oldSourceUrl: String? = null |
|
|
|
|
|
|
|
|
|
|
|
fun setBookSource(key: String) { |
|
|
|
fun setBookSource(key: String?) { |
|
|
|
execute { |
|
|
|
execute { |
|
|
|
App.db.bookSourceDao().getBookSource(key)?.let { |
|
|
|
var source: BookSource? = null |
|
|
|
|
|
|
|
if (key != null) { |
|
|
|
|
|
|
|
source = App.db.bookSourceDao().getBookSource(key) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
source?.let { |
|
|
|
|
|
|
|
oldSourceUrl = it.bookSourceUrl |
|
|
|
sourceLiveData.postValue(it) |
|
|
|
sourceLiveData.postValue(it) |
|
|
|
|
|
|
|
} ?: let { |
|
|
|
|
|
|
|
sourceLiveData.postValue(BookSource().apply { |
|
|
|
|
|
|
|
customOrder = App.db.bookSourceDao().maxOrder + 1 |
|
|
|
|
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun save(bookSource: BookSource, finally: (() -> Unit)? = null) { |
|
|
|
fun save(bookSource: BookSource, finally: (() -> Unit)? = null) { |
|
|
|
execute { |
|
|
|
execute { |
|
|
|
sourceLiveData.value?.let { |
|
|
|
oldSourceUrl?.let { |
|
|
|
if (it.bookSourceUrl != bookSource.bookSourceUrl) { |
|
|
|
if (oldSourceUrl != bookSource.bookSourceUrl) { |
|
|
|
App.db.bookSourceDao().delete(it) |
|
|
|
App.db.bookSourceDao().delete(it) |
|
|
|
} |
|
|
|
} |
|
|
|
} ?: let { |
|
|
|
|
|
|
|
bookSource.customOrder = App.db.bookSourceDao().maxOrder + 1 |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
oldSourceUrl = bookSource.bookSourceUrl |
|
|
|
App.db.bookSourceDao().insert(bookSource) |
|
|
|
App.db.bookSourceDao().insert(bookSource) |
|
|
|
sourceLiveData.postValue(bookSource) |
|
|
|
|
|
|
|
}.onFinally { |
|
|
|
}.onFinally { |
|
|
|
finally?.let { it() } |
|
|
|
finally?.let { it() } |
|
|
|
} |
|
|
|
} |
|
|
|