From 365026e1a04df4e53f8b342aea7e080471dbed26 Mon Sep 17 00:00:00 2001 From: William Brawner Date: Fri, 11 Nov 2022 21:20:58 -0700 Subject: [PATCH] Handle Entry network operations in the background Previously the UI had to wait on the network request to complete or fail before updating. --- .../nanoflux/network/repository/EntryRepository.kt | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/network/src/main/java/com/wbrawner/nanoflux/network/repository/EntryRepository.kt b/network/src/main/java/com/wbrawner/nanoflux/network/repository/EntryRepository.kt index 0251576..a91c39e 100644 --- a/network/src/main/java/com/wbrawner/nanoflux/network/repository/EntryRepository.kt +++ b/network/src/main/java/com/wbrawner/nanoflux/network/repository/EntryRepository.kt @@ -23,6 +23,7 @@ class EntryRepository @Inject constructor( private val entryDao: EntryDao, private val logger: Timber.Tree ) { + private val repositoryScope = CoroutineScope(Dispatchers.IO) fun observeRead() = entryDao.observeRead() fun observeStarred() = entryDao.observeStarred() fun observeUnread() = entryDao.observeUnread() @@ -62,15 +63,19 @@ class EntryRepository @Inject constructor( suspend fun markEntryRead(entry: Entry) { entryDao.update(entry.copy(status = Entry.Status.READ)) - retryOnFailure { - apiService.updateEntries(listOf(entry.id), Entry.Status.READ) + repositoryScope.launch { + retryOnFailure { + apiService.updateEntries(listOf(entry.id), Entry.Status.READ) + } } } suspend fun markEntryUnread(entry: Entry) { entryDao.update(entry.copy(status = Entry.Status.UNREAD)) - retryOnFailure { - apiService.updateEntries(listOf(entry.id), Entry.Status.UNREAD) + repositoryScope.launch { + retryOnFailure { + apiService.updateEntries(listOf(entry.id), Entry.Status.UNREAD) + } } }