feat(library-update-errors): add/clear error when updating manually from entry's screen (#922)
* add/clear error when updating manually from entry's screen * Optimize redundant code * Replace default error message with localized string
This commit is contained in:
parent
f8ef5d4cf2
commit
262c6315fd
4 changed files with 57 additions and 15 deletions
|
|
@ -105,7 +105,7 @@ import kotlin.concurrent.atomics.ExperimentalAtomicApi
|
||||||
import kotlin.concurrent.atomics.incrementAndFetch
|
import kotlin.concurrent.atomics.incrementAndFetch
|
||||||
|
|
||||||
@OptIn(ExperimentalAtomicApi::class)
|
@OptIn(ExperimentalAtomicApi::class)
|
||||||
class LibraryUpdateJob(private val context: Context, private val workerParams: WorkerParameters) :
|
class LibraryUpdateJob(private val context: Context, workerParams: WorkerParameters) :
|
||||||
CoroutineWorker(context, workerParams) {
|
CoroutineWorker(context, workerParams) {
|
||||||
|
|
||||||
private val sourceManager: SourceManager = Injekt.get()
|
private val sourceManager: SourceManager = Injekt.get()
|
||||||
|
|
@ -659,9 +659,8 @@ class LibraryUpdateJob(private val context: Context, private val workerParams: W
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun writeErrorToDB(error: Pair<Manga, String?>) {
|
private suspend fun writeErrorToDB(error: Pair<Manga, String?>) {
|
||||||
val errorMessage = error.second ?: "???"
|
val errorMessage = error.second ?: context.stringResource(MR.strings.unknown_error)
|
||||||
val errorMessageId = insertLibraryUpdateErrorMessages.get(errorMessage)
|
val errorMessageId = insertLibraryUpdateErrorMessages.insert(
|
||||||
?: insertLibraryUpdateErrorMessages.insert(
|
|
||||||
libraryUpdateErrorMessage = LibraryUpdateErrorMessage(-1L, errorMessage),
|
libraryUpdateErrorMessage = LibraryUpdateErrorMessage(-1L, errorMessage),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -128,6 +128,11 @@ import tachiyomi.domain.chapter.model.NoChaptersException
|
||||||
import tachiyomi.domain.chapter.service.calculateChapterGap
|
import tachiyomi.domain.chapter.service.calculateChapterGap
|
||||||
import tachiyomi.domain.chapter.service.getChapterSort
|
import tachiyomi.domain.chapter.service.getChapterSort
|
||||||
import tachiyomi.domain.library.service.LibraryPreferences
|
import tachiyomi.domain.library.service.LibraryPreferences
|
||||||
|
import tachiyomi.domain.libraryUpdateError.interactor.DeleteLibraryUpdateErrors
|
||||||
|
import tachiyomi.domain.libraryUpdateError.interactor.InsertLibraryUpdateErrors
|
||||||
|
import tachiyomi.domain.libraryUpdateError.model.LibraryUpdateError
|
||||||
|
import tachiyomi.domain.libraryUpdateErrorMessage.interactor.InsertLibraryUpdateErrorMessages
|
||||||
|
import tachiyomi.domain.libraryUpdateErrorMessage.model.LibraryUpdateErrorMessage
|
||||||
import tachiyomi.domain.manga.interactor.DeleteMergeById
|
import tachiyomi.domain.manga.interactor.DeleteMergeById
|
||||||
import tachiyomi.domain.manga.interactor.GetDuplicateLibraryManga
|
import tachiyomi.domain.manga.interactor.GetDuplicateLibraryManga
|
||||||
import tachiyomi.domain.manga.interactor.GetFlatMetadataById
|
import tachiyomi.domain.manga.interactor.GetFlatMetadataById
|
||||||
|
|
@ -221,6 +226,11 @@ class MangaScreenModel(
|
||||||
private val mangaRepository: MangaRepository = Injekt.get(),
|
private val mangaRepository: MangaRepository = Injekt.get(),
|
||||||
private val filterChaptersForDownload: FilterChaptersForDownload = Injekt.get(),
|
private val filterChaptersForDownload: FilterChaptersForDownload = Injekt.get(),
|
||||||
val snackbarHostState: SnackbarHostState = SnackbarHostState(),
|
val snackbarHostState: SnackbarHostState = SnackbarHostState(),
|
||||||
|
// KMK -->
|
||||||
|
private val deleteLibraryUpdateErrors: DeleteLibraryUpdateErrors = Injekt.get(),
|
||||||
|
private val insertLibraryUpdateErrors: InsertLibraryUpdateErrors = Injekt.get(),
|
||||||
|
private val insertLibraryUpdateErrorMessages: InsertLibraryUpdateErrorMessages = Injekt.get(),
|
||||||
|
// KMK <--
|
||||||
) : StateScreenModel<MangaScreenModel.State>(State.Loading) {
|
) : StateScreenModel<MangaScreenModel.State>(State.Loading) {
|
||||||
|
|
||||||
private val successState: State.Success?
|
private val successState: State.Success?
|
||||||
|
|
@ -612,6 +622,9 @@ class MangaScreenModel(
|
||||||
withIOContext {
|
withIOContext {
|
||||||
val networkManga = state.source.getMangaDetails(state.manga.toSManga())
|
val networkManga = state.source.getMangaDetails(state.manga.toSManga())
|
||||||
updateManga.awaitUpdateFromSource(state.manga, networkManga, manualFetch)
|
updateManga.awaitUpdateFromSource(state.manga, networkManga, manualFetch)
|
||||||
|
// KMK -->
|
||||||
|
clearErrorFromDB(state.manga.id)
|
||||||
|
// KMK <--
|
||||||
}
|
}
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
// Ignore early hints "errors" that aren't handled by OkHttp
|
// Ignore early hints "errors" that aren't handled by OkHttp
|
||||||
|
|
@ -621,9 +634,29 @@ class MangaScreenModel(
|
||||||
screenModelScope.launch {
|
screenModelScope.launch {
|
||||||
snackbarHostState.showSnackbar(message = with(context) { e.formattedMessage })
|
snackbarHostState.showSnackbar(message = with(context) { e.formattedMessage })
|
||||||
}
|
}
|
||||||
|
// KMK -->
|
||||||
|
writeErrorToDB(state.manga to with(context) { e.formattedMessage })
|
||||||
|
// KMK <--
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// KMK -->
|
||||||
|
private suspend fun clearErrorFromDB(mangaId: Long) {
|
||||||
|
deleteLibraryUpdateErrors.deleteMangaError(mangaIds = listOf(mangaId))
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun writeErrorToDB(error: Pair<Manga, String?>) {
|
||||||
|
val errorMessage = error.second ?: context.stringResource(MR.strings.unknown_error)
|
||||||
|
val errorMessageId = insertLibraryUpdateErrorMessages.insert(
|
||||||
|
libraryUpdateErrorMessage = LibraryUpdateErrorMessage(-1L, errorMessage),
|
||||||
|
)
|
||||||
|
|
||||||
|
insertLibraryUpdateErrors.upsert(
|
||||||
|
LibraryUpdateError(id = -1L, mangaId = error.first.id, messageId = errorMessageId),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
// KMK <--
|
||||||
|
|
||||||
// SY -->
|
// SY -->
|
||||||
private fun raiseMetadata(flatMetadata: FlatMetadata?, source: Source): RaisedSearchMetadata? {
|
private fun raiseMetadata(flatMetadata: FlatMetadata?, source: Source): RaisedSearchMetadata? {
|
||||||
return if (flatMetadata != null) {
|
return if (flatMetadata != null) {
|
||||||
|
|
@ -1124,6 +1157,9 @@ class MangaScreenModel(
|
||||||
state.source.fetchChaptersForMergedManga(state.manga, manualFetch)
|
state.source.fetchChaptersForMergedManga(state.manga, manualFetch)
|
||||||
}
|
}
|
||||||
// SY <--
|
// SY <--
|
||||||
|
// KMK -->
|
||||||
|
clearErrorFromDB(state.manga.id)
|
||||||
|
// KMK <--
|
||||||
}
|
}
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
val message = if (e is NoChaptersException) {
|
val message = if (e is NoChaptersException) {
|
||||||
|
|
@ -1138,6 +1174,9 @@ class MangaScreenModel(
|
||||||
}
|
}
|
||||||
val newManga = mangaRepository.getMangaById(mangaId)
|
val newManga = mangaRepository.getMangaById(mangaId)
|
||||||
updateSuccessState { it.copy(manga = newManga, isRefreshingData = false) }
|
updateSuccessState { it.copy(manga = newManga, isRefreshingData = false) }
|
||||||
|
// KMK -->
|
||||||
|
writeErrorToDB(state.manga to message)
|
||||||
|
// KMK <--
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -37,8 +37,7 @@ class LibraryUpdateErrorMessageRepositoryImpl(
|
||||||
|
|
||||||
override suspend fun insert(libraryUpdateErrorMessage: LibraryUpdateErrorMessage): Long {
|
override suspend fun insert(libraryUpdateErrorMessage: LibraryUpdateErrorMessage): Long {
|
||||||
return handler.awaitOneExecutable(inTransaction = true) {
|
return handler.awaitOneExecutable(inTransaction = true) {
|
||||||
libraryUpdateErrorMessageQueries.insert(libraryUpdateErrorMessage.message)
|
libraryUpdateErrorMessageQueries.insertAndGet(libraryUpdateErrorMessage.message)
|
||||||
libraryUpdateErrorMessageQueries.selectLastInsertedRowId()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -47,8 +46,7 @@ class LibraryUpdateErrorMessageRepositoryImpl(
|
||||||
): List<Pair<Long, String>> {
|
): List<Pair<Long, String>> {
|
||||||
return handler.await(inTransaction = true) {
|
return handler.await(inTransaction = true) {
|
||||||
libraryUpdateErrorMessages.map {
|
libraryUpdateErrorMessages.map {
|
||||||
libraryUpdateErrorMessageQueries.insert(it.message)
|
libraryUpdateErrorMessageQueries.insertAndGet(it.message).executeAsOne() to it.message
|
||||||
libraryUpdateErrorMessageQueries.selectLastInsertedRowId().executeAsOne() to it.message
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,11 +11,17 @@ getErrorMessages:
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM libraryUpdateErrorMessage WHERE message == :message;
|
FROM libraryUpdateErrorMessage WHERE message == :message;
|
||||||
|
|
||||||
insert:
|
insertAndGet {
|
||||||
INSERT INTO libraryUpdateErrorMessage(message) VALUES (:message);
|
-- Insert the message if it doesn't exist already
|
||||||
|
INSERT OR IGNORE INTO libraryUpdateErrorMessage(message)
|
||||||
|
VALUES (:message);
|
||||||
|
|
||||||
|
-- Finally return the message ID
|
||||||
|
SELECT _id
|
||||||
|
FROM libraryUpdateErrorMessage
|
||||||
|
WHERE message = :message
|
||||||
|
LIMIT 1;
|
||||||
|
}
|
||||||
|
|
||||||
deleteAllErrorMessages:
|
deleteAllErrorMessages:
|
||||||
DELETE FROM libraryUpdateErrorMessage;
|
DELETE FROM libraryUpdateErrorMessage;
|
||||||
|
|
||||||
selectLastInsertedRowId:
|
|
||||||
SELECT last_insert_rowid();
|
|
||||||
Loading…
Reference in a new issue