|
|
@ -17,7 +17,6 @@ import kotlinx.coroutines.Dispatchers.IO |
|
|
|
class BookInfoViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
class BookInfoViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
val bookData = MutableLiveData<Book>() |
|
|
|
val bookData = MutableLiveData<Book>() |
|
|
|
val chapterListData = MutableLiveData<List<BookChapter>>() |
|
|
|
val chapterListData = MutableLiveData<List<BookChapter>>() |
|
|
|
val isLoadingData = MutableLiveData<Boolean>() |
|
|
|
|
|
|
|
var durChapterIndex = 0 |
|
|
|
var durChapterIndex = 0 |
|
|
|
var inBookshelf = false |
|
|
|
var inBookshelf = false |
|
|
|
var groupData = MutableLiveData<BookGroup>() |
|
|
|
var groupData = MutableLiveData<BookGroup>() |
|
|
@ -45,7 +44,6 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
val chapterList = App.db.bookChapterDao().getChapterList(book.bookUrl) |
|
|
|
val chapterList = App.db.bookChapterDao().getChapterList(book.bookUrl) |
|
|
|
if (chapterList.isNotEmpty()) { |
|
|
|
if (chapterList.isNotEmpty()) { |
|
|
|
chapterListData.postValue(chapterList) |
|
|
|
chapterListData.postValue(chapterList) |
|
|
|
isLoadingData.postValue(false) |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
loadChapter(book) |
|
|
|
loadChapter(book) |
|
|
|
} |
|
|
|
} |
|
|
@ -60,7 +58,6 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
if (book.isLocalBook()) { |
|
|
|
if (book.isLocalBook()) { |
|
|
|
loadChapter(book, changeDruChapterIndex) |
|
|
|
loadChapter(book, changeDruChapterIndex) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
isLoadingData.postValue(true) |
|
|
|
|
|
|
|
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource -> |
|
|
|
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource -> |
|
|
|
WebBook(bookSource).getBookInfo(book, this) |
|
|
|
WebBook(bookSource).getBookInfo(book, this) |
|
|
|
.onSuccess(IO) { |
|
|
|
.onSuccess(IO) { |
|
|
@ -72,11 +69,10 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
loadChapter(it, changeDruChapterIndex) |
|
|
|
loadChapter(it, changeDruChapterIndex) |
|
|
|
} |
|
|
|
} |
|
|
|
}.onError { |
|
|
|
}.onError { |
|
|
|
isLoadingData.postValue(false) |
|
|
|
|
|
|
|
toast(R.string.error_get_book_info) |
|
|
|
toast(R.string.error_get_book_info) |
|
|
|
} |
|
|
|
} |
|
|
|
} ?: let { |
|
|
|
} ?: let { |
|
|
|
isLoadingData.postValue(false) |
|
|
|
chapterListData.postValue(null) |
|
|
|
toast(R.string.error_no_source) |
|
|
|
toast(R.string.error_no_source) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -88,17 +84,11 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null |
|
|
|
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null |
|
|
|
) { |
|
|
|
) { |
|
|
|
execute { |
|
|
|
execute { |
|
|
|
isLoadingData.postValue(true) |
|
|
|
|
|
|
|
if (book.isLocalBook()) { |
|
|
|
if (book.isLocalBook()) { |
|
|
|
AnalyzeTxtFile.analyze(context, book).let { |
|
|
|
AnalyzeTxtFile.analyze(context, book).let { |
|
|
|
App.db.bookDao().update(book) |
|
|
|
App.db.bookDao().update(book) |
|
|
|
App.db.bookChapterDao().insert(*it.toTypedArray()) |
|
|
|
App.db.bookChapterDao().insert(*it.toTypedArray()) |
|
|
|
if (changeDruChapterIndex == null) { |
|
|
|
chapterListData.postValue(it) |
|
|
|
chapterListData.postValue(it) |
|
|
|
|
|
|
|
isLoadingData.postValue(false) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
changeDruChapterIndex(it) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource -> |
|
|
|
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource -> |
|
|
@ -112,21 +102,19 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
} |
|
|
|
} |
|
|
|
if (changeDruChapterIndex == null) { |
|
|
|
if (changeDruChapterIndex == null) { |
|
|
|
chapterListData.postValue(it) |
|
|
|
chapterListData.postValue(it) |
|
|
|
isLoadingData.postValue(false) |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
changeDruChapterIndex(it) |
|
|
|
changeDruChapterIndex(it) |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
isLoadingData.postValue(false) |
|
|
|
|
|
|
|
toast(R.string.chapter_list_empty) |
|
|
|
toast(R.string.chapter_list_empty) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}.onError { |
|
|
|
}.onError { |
|
|
|
isLoadingData.postValue(false) |
|
|
|
chapterListData.postValue(null) |
|
|
|
toast(R.string.error_get_chapter_list) |
|
|
|
toast(R.string.error_get_chapter_list) |
|
|
|
} |
|
|
|
} |
|
|
|
} ?: let { |
|
|
|
} ?: let { |
|
|
|
isLoadingData.postValue(false) |
|
|
|
chapterListData.postValue(null) |
|
|
|
toast(R.string.error_no_source) |
|
|
|
toast(R.string.error_no_source) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|