Add lastUpdate timer feature to adventure to avoid constant refreshing.
This commit is contained in:
		
							parent
							
								
									2b03ffd1ac
								
							
						
					
					
						commit
						395f54d086
					
				
					 3 changed files with 25 additions and 16 deletions
				
			
		| 
						 | 
					@ -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.parser.adventure.AdventureStoryParser
 | 
				
			||||||
import com.pixelized.rplexicon.data.repository.Adventures
 | 
					import com.pixelized.rplexicon.data.repository.Adventures
 | 
				
			||||||
import com.pixelized.rplexicon.data.repository.GoogleSheetServiceRepository
 | 
					import com.pixelized.rplexicon.data.repository.GoogleSheetServiceRepository
 | 
				
			||||||
 | 
					import com.pixelized.rplexicon.utilitary.Update
 | 
				
			||||||
import com.pixelized.rplexicon.utilitary.exceptions.IncompatibleSheetStructure
 | 
					import com.pixelized.rplexicon.utilitary.exceptions.IncompatibleSheetStructure
 | 
				
			||||||
import kotlinx.coroutines.CoroutineScope
 | 
					import kotlinx.coroutines.CoroutineScope
 | 
				
			||||||
import kotlinx.coroutines.Dispatchers
 | 
					import kotlinx.coroutines.Dispatchers
 | 
				
			||||||
| 
						 | 
					@ -37,6 +38,9 @@ class AdventureRepository @Inject constructor(
 | 
				
			||||||
    private val database = companionDatabase.adventureDao()
 | 
					    private val database = companionDatabase.adventureDao()
 | 
				
			||||||
    private val service = Service()
 | 
					    private val service = Service()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    var lastSuccessFullUpdate: Update = Update.INITIAL
 | 
				
			||||||
 | 
					        private set
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private val adventures = database.let { database ->
 | 
					    private val adventures = database.let { database ->
 | 
				
			||||||
        combine(
 | 
					        combine(
 | 
				
			||||||
            database.getBooksFlow(),
 | 
					            database.getBooksFlow(),
 | 
				
			||||||
| 
						 | 
					@ -86,6 +90,8 @@ class AdventureRepository @Inject constructor(
 | 
				
			||||||
        (storiesToInsert + storiesToUpdate).forEach { story ->
 | 
					        (storiesToInsert + storiesToUpdate).forEach { story ->
 | 
				
			||||||
            fetchAndCompareLines(documentId = story.documentId, storyTitle = story.title)
 | 
					            fetchAndCompareLines(documentId = story.documentId, storyTitle = story.title)
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        lastSuccessFullUpdate = Update.currentTime()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Throws(IncompatibleSheetStructure::class, Exception::class)
 | 
					    @Throws(IncompatibleSheetStructure::class, Exception::class)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -52,7 +52,7 @@ fun AdventureBooksScreen(
 | 
				
			||||||
    val screen = LocalScreenNavHost.current
 | 
					    val screen = LocalScreenNavHost.current
 | 
				
			||||||
    val refresh = rememberPullRefreshState(
 | 
					    val refresh = rememberPullRefreshState(
 | 
				
			||||||
        refreshing = false,
 | 
					        refreshing = false,
 | 
				
			||||||
        onRefresh = { viewModel.update() },
 | 
					        onRefresh = { viewModel.update(force = true) },
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Surface(
 | 
					    Surface(
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -52,17 +52,19 @@ class AdventureBooksViewModel @Inject constructor(
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    init {
 | 
					    init {
 | 
				
			||||||
        viewModelScope.launch(Dispatchers.Default) {
 | 
					        viewModelScope.launch(Dispatchers.Default) {
 | 
				
			||||||
            update()
 | 
					            update(force = false)
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fun update() {
 | 
					    fun update(force: Boolean = false) {
 | 
				
			||||||
 | 
					        if (force || adventureRepository.lastSuccessFullUpdate.shouldUpdate()) {
 | 
				
			||||||
            viewModelScope.launch {
 | 
					            viewModelScope.launch {
 | 
				
			||||||
                try {
 | 
					                try {
 | 
				
			||||||
                    withContext(Dispatchers.Main) {
 | 
					                    withContext(Dispatchers.Main) {
 | 
				
			||||||
                        _isLoading.value = true
 | 
					                        _isLoading.value = true
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                    withContext(Dispatchers.Default) {
 | 
					                    withContext(Dispatchers.Default) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                        adventureRepository.fetchBooks()
 | 
					                        adventureRepository.fetchBooks()
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                } catch (_: Exception) {
 | 
					                } catch (_: Exception) {
 | 
				
			||||||
| 
						 | 
					@ -75,3 +77,4 @@ class AdventureBooksViewModel @Inject constructor(
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue