From 395f54d0861c21ec280045500eb314203a401855 Mon Sep 17 00:00:00 2001 From: "Andres Gomez, Thomas (ITDV RL)" Date: Mon, 24 Jun 2024 13:43:04 +0200 Subject: [PATCH] Add lastUpdate timer feature to adventure to avoid constant refreshing. --- .../adventure/AdventureRepository.kt | 6 ++++ .../adventure/book/AdventureBooksScreen.kt | 2 +- .../adventure/book/AdventureBooksViewModel.kt | 33 ++++++++++--------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/pixelized/rplexicon/data/repository/adventure/AdventureRepository.kt b/app/src/main/java/com/pixelized/rplexicon/data/repository/adventure/AdventureRepository.kt index a85a33b..50993e3 100644 --- a/app/src/main/java/com/pixelized/rplexicon/data/repository/adventure/AdventureRepository.kt +++ b/app/src/main/java/com/pixelized/rplexicon/data/repository/adventure/AdventureRepository.kt @@ -10,6 +10,7 @@ import com.pixelized.rplexicon.data.parser.adventure.AdventureStoryLineParser import com.pixelized.rplexicon.data.parser.adventure.AdventureStoryParser import com.pixelized.rplexicon.data.repository.Adventures import com.pixelized.rplexicon.data.repository.GoogleSheetServiceRepository +import com.pixelized.rplexicon.utilitary.Update import com.pixelized.rplexicon.utilitary.exceptions.IncompatibleSheetStructure import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -37,6 +38,9 @@ class AdventureRepository @Inject constructor( private val database = companionDatabase.adventureDao() private val service = Service() + var lastSuccessFullUpdate: Update = Update.INITIAL + private set + private val adventures = database.let { database -> combine( database.getBooksFlow(), @@ -86,6 +90,8 @@ class AdventureRepository @Inject constructor( (storiesToInsert + storiesToUpdate).forEach { story -> fetchAndCompareLines(documentId = story.documentId, storyTitle = story.title) } + + lastSuccessFullUpdate = Update.currentTime() } @Throws(IncompatibleSheetStructure::class, Exception::class) diff --git a/app/src/main/java/com/pixelized/rplexicon/ui/screens/adventure/book/AdventureBooksScreen.kt b/app/src/main/java/com/pixelized/rplexicon/ui/screens/adventure/book/AdventureBooksScreen.kt index ec976cd..52e8767 100644 --- a/app/src/main/java/com/pixelized/rplexicon/ui/screens/adventure/book/AdventureBooksScreen.kt +++ b/app/src/main/java/com/pixelized/rplexicon/ui/screens/adventure/book/AdventureBooksScreen.kt @@ -52,7 +52,7 @@ fun AdventureBooksScreen( val screen = LocalScreenNavHost.current val refresh = rememberPullRefreshState( refreshing = false, - onRefresh = { viewModel.update() }, + onRefresh = { viewModel.update(force = true) }, ) Surface( diff --git a/app/src/main/java/com/pixelized/rplexicon/ui/screens/adventure/book/AdventureBooksViewModel.kt b/app/src/main/java/com/pixelized/rplexicon/ui/screens/adventure/book/AdventureBooksViewModel.kt index 1cc5130..5e5fe0d 100644 --- a/app/src/main/java/com/pixelized/rplexicon/ui/screens/adventure/book/AdventureBooksViewModel.kt +++ b/app/src/main/java/com/pixelized/rplexicon/ui/screens/adventure/book/AdventureBooksViewModel.kt @@ -52,24 +52,27 @@ class AdventureBooksViewModel @Inject constructor( init { viewModelScope.launch(Dispatchers.Default) { - update() + update(force = false) } } - fun update() { - viewModelScope.launch { - try { - withContext(Dispatchers.Main) { - _isLoading.value = true - } - withContext(Dispatchers.Default) { - adventureRepository.fetchBooks() - } - } catch (_: Exception) { - _error.emit(value = Structure(ADVENTURE)) - } finally { - withContext(Dispatchers.Main) { - _isLoading.value = false + fun update(force: Boolean = false) { + if (force || adventureRepository.lastSuccessFullUpdate.shouldUpdate()) { + viewModelScope.launch { + try { + withContext(Dispatchers.Main) { + _isLoading.value = true + } + withContext(Dispatchers.Default) { + + adventureRepository.fetchBooks() + } + } catch (_: Exception) { + _error.emit(value = Structure(ADVENTURE)) + } finally { + withContext(Dispatchers.Main) { + _isLoading.value = false + } } } }