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
|
||||
|
||||
@OptIn(ExperimentalAtomicApi::class)
|
||||
class LibraryUpdateJob(private val context: Context, private val workerParams: WorkerParameters) :
|
||||
class LibraryUpdateJob(private val context: Context, workerParams: WorkerParameters) :
|
||||
CoroutineWorker(context, workerParams) {
|
||||
|
||||
private val sourceManager: SourceManager = Injekt.get()
|
||||
|
|
@ -659,11 +659,10 @@ class LibraryUpdateJob(private val context: Context, private val workerParams: W
|
|||
}
|
||||
|
||||
private suspend fun writeErrorToDB(error: Pair<Manga, String?>) {
|
||||
val errorMessage = error.second ?: "???"
|
||||
val errorMessageId = insertLibraryUpdateErrorMessages.get(errorMessage)
|
||||
?: insertLibraryUpdateErrorMessages.insert(
|
||||
libraryUpdateErrorMessage = LibraryUpdateErrorMessage(-1L, errorMessage),
|
||||
)
|
||||
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),
|
||||
|
|
|
|||
|
|
@ -128,6 +128,11 @@ import tachiyomi.domain.chapter.model.NoChaptersException
|
|||
import tachiyomi.domain.chapter.service.calculateChapterGap
|
||||
import tachiyomi.domain.chapter.service.getChapterSort
|
||||
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.GetDuplicateLibraryManga
|
||||
import tachiyomi.domain.manga.interactor.GetFlatMetadataById
|
||||
|
|
@ -221,6 +226,11 @@ class MangaScreenModel(
|
|||
private val mangaRepository: MangaRepository = Injekt.get(),
|
||||
private val filterChaptersForDownload: FilterChaptersForDownload = Injekt.get(),
|
||||
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) {
|
||||
|
||||
private val successState: State.Success?
|
||||
|
|
@ -612,6 +622,9 @@ class MangaScreenModel(
|
|||
withIOContext {
|
||||
val networkManga = state.source.getMangaDetails(state.manga.toSManga())
|
||||
updateManga.awaitUpdateFromSource(state.manga, networkManga, manualFetch)
|
||||
// KMK -->
|
||||
clearErrorFromDB(state.manga.id)
|
||||
// KMK <--
|
||||
}
|
||||
} catch (e: Throwable) {
|
||||
// Ignore early hints "errors" that aren't handled by OkHttp
|
||||
|
|
@ -621,9 +634,29 @@ class MangaScreenModel(
|
|||
screenModelScope.launch {
|
||||
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 -->
|
||||
private fun raiseMetadata(flatMetadata: FlatMetadata?, source: Source): RaisedSearchMetadata? {
|
||||
return if (flatMetadata != null) {
|
||||
|
|
@ -1124,6 +1157,9 @@ class MangaScreenModel(
|
|||
state.source.fetchChaptersForMergedManga(state.manga, manualFetch)
|
||||
}
|
||||
// SY <--
|
||||
// KMK -->
|
||||
clearErrorFromDB(state.manga.id)
|
||||
// KMK <--
|
||||
}
|
||||
} catch (e: Throwable) {
|
||||
val message = if (e is NoChaptersException) {
|
||||
|
|
@ -1138,6 +1174,9 @@ class MangaScreenModel(
|
|||
}
|
||||
val newManga = mangaRepository.getMangaById(mangaId)
|
||||
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 {
|
||||
return handler.awaitOneExecutable(inTransaction = true) {
|
||||
libraryUpdateErrorMessageQueries.insert(libraryUpdateErrorMessage.message)
|
||||
libraryUpdateErrorMessageQueries.selectLastInsertedRowId()
|
||||
libraryUpdateErrorMessageQueries.insertAndGet(libraryUpdateErrorMessage.message)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -47,8 +46,7 @@ class LibraryUpdateErrorMessageRepositoryImpl(
|
|||
): List<Pair<Long, String>> {
|
||||
return handler.await(inTransaction = true) {
|
||||
libraryUpdateErrorMessages.map {
|
||||
libraryUpdateErrorMessageQueries.insert(it.message)
|
||||
libraryUpdateErrorMessageQueries.selectLastInsertedRowId().executeAsOne() to it.message
|
||||
libraryUpdateErrorMessageQueries.insertAndGet(it.message).executeAsOne() to it.message
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,11 +11,17 @@ getErrorMessages:
|
|||
SELECT *
|
||||
FROM libraryUpdateErrorMessage WHERE message == :message;
|
||||
|
||||
insert:
|
||||
INSERT INTO libraryUpdateErrorMessage(message) VALUES (:message);
|
||||
insertAndGet {
|
||||
-- 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:
|
||||
DELETE FROM libraryUpdateErrorMessage;
|
||||
|
||||
selectLastInsertedRowId:
|
||||
SELECT last_insert_rowid();
|
||||
Loading…
Reference in a new issue