🛠️ Fix issue with screen reset & cancellation

This commit is contained in:
Acclorite 2024-12-29 12:24:23 +02:00
parent b73c151b4d
commit 211e45058b
9 changed files with 26 additions and 1 deletions

View file

@ -14,6 +14,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import kotlinx.coroutines.yield
import ua.acclorite.book_story.R import ua.acclorite.book_story.R
import ua.acclorite.book_story.domain.use_case.remote.CheckForUpdates import ua.acclorite.book_story.domain.use_case.remote.CheckForUpdates
import ua.acclorite.book_story.presentation.core.util.launchActivity import ua.acclorite.book_story.presentation.core.util.launchActivity
@ -114,6 +115,7 @@ class AboutModel @Inject constructor(
private suspend inline fun <T> MutableStateFlow<T>.update(function: (T) -> T) { private suspend inline fun <T> MutableStateFlow<T>.update(function: (T) -> T) {
mutex.withLock { mutex.withLock {
yield()
this.value = function(this.value) this.value = function(this.value)
} }
} }

View file

@ -810,11 +810,14 @@ class BookInfoModel @Inject constructor(
viewModelScope.launch(Dispatchers.Main) { viewModelScope.launch(Dispatchers.Main) {
eventJob.cancel() eventJob.cancel()
eventJob = SupervisorJob() eventJob = SupervisorJob()
_state.update { BookInfoState() }
} }
} }
private suspend inline fun <T> MutableStateFlow<T>.update(function: (T) -> T) { private suspend inline fun <T> MutableStateFlow<T>.update(function: (T) -> T) {
mutex.withLock { mutex.withLock {
yield()
this.value = function(this.value) this.value = function(this.value)
} }
} }

View file

@ -732,6 +732,7 @@ class BrowseModel @Inject constructor(
private suspend inline fun <T> MutableStateFlow<T>.update(function: (T) -> T) { private suspend inline fun <T> MutableStateFlow<T>.update(function: (T) -> T) {
mutex.withLock { mutex.withLock {
yield()
this.value = function(this.value) this.value = function(this.value)
} }
} }

View file

@ -328,6 +328,7 @@ class HistoryModel @Inject constructor(
private suspend inline fun <T> MutableStateFlow<T>.update(function: (T) -> T) { private suspend inline fun <T> MutableStateFlow<T>.update(function: (T) -> T) {
mutex.withLock { mutex.withLock {
yield()
this.value = function(this.value) this.value = function(this.value)
} }
} }

View file

@ -289,6 +289,7 @@ class LibraryModel @Inject constructor(
private suspend inline fun <T> MutableStateFlow<T>.update(function: (T) -> T) { private suspend inline fun <T> MutableStateFlow<T>.update(function: (T) -> T) {
mutex.withLock { mutex.withLock {
yield()
this.value = function(this.value) this.value = function(this.value)
} }
} }

View file

@ -15,6 +15,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import kotlinx.coroutines.yield
import ua.acclorite.book_story.domain.browse.toBrowseFilesStructure import ua.acclorite.book_story.domain.browse.toBrowseFilesStructure
import ua.acclorite.book_story.domain.browse.toBrowseLayout import ua.acclorite.book_story.domain.browse.toBrowseLayout
import ua.acclorite.book_story.domain.browse.toBrowseSortOrder import ua.acclorite.book_story.domain.browse.toBrowseSortOrder
@ -512,6 +513,7 @@ class MainModel @Inject constructor(
private suspend inline fun <T> MutableStateFlow<T>.update(function: (T) -> T) { private suspend inline fun <T> MutableStateFlow<T>.update(function: (T) -> T) {
mutex.withLock { mutex.withLock {
yield()
this.value = function(this.value) this.value = function(this.value)
} }
} }

View file

@ -72,6 +72,8 @@ class ReaderModel @Inject constructor(
launch(Dispatchers.IO) { launch(Dispatchers.IO) {
val text = getText.execute(_state.value.book.textPath) val text = getText.execute(_state.value.book.textPath)
yield()
if (text.isEmpty()) { if (text.isEmpty()) {
_state.update { _state.update {
it.copy( it.copy(
@ -81,15 +83,23 @@ class ReaderModel @Inject constructor(
} }
} }
yield()
val lastOpened = getLatestHistory.execute(_state.value.book.id)?.time
yield()
_state.update { _state.update {
it.copy( it.copy(
book = it.book.copy( book = it.book.copy(
lastOpened = getLatestHistory.execute(_state.value.book.id)?.time lastOpened = lastOpened
), ),
text = text text = text
) )
} }
yield()
updateBook.execute(_state.value.book) updateBook.execute(_state.value.book)
LibraryScreen.refreshListChannel.trySend(0) LibraryScreen.refreshListChannel.trySend(0)
@ -680,11 +690,14 @@ class ReaderModel @Inject constructor(
viewModelScope.launch(Dispatchers.Main) { viewModelScope.launch(Dispatchers.Main) {
eventJob.cancel() eventJob.cancel()
eventJob = SupervisorJob() eventJob = SupervisorJob()
_state.update { ReaderState() }
} }
} }
private suspend inline fun <T> MutableStateFlow<T>.update(function: (T) -> T) { private suspend inline fun <T> MutableStateFlow<T>.update(function: (T) -> T) {
mutex.withLock { mutex.withLock {
yield()
this.value = function(this.value) this.value = function(this.value)
} }
} }

View file

@ -542,6 +542,7 @@ class SettingsModel @Inject constructor(
private suspend inline fun <T> MutableStateFlow<T>.update(function: (T) -> T) { private suspend inline fun <T> MutableStateFlow<T>.update(function: (T) -> T) {
mutex.withLock { mutex.withLock {
yield()
this.value = function(this.value) this.value = function(this.value)
} }
} }

View file

@ -189,6 +189,7 @@ class StartModel @Inject constructor(
private suspend inline fun <T> MutableStateFlow<T>.update(function: (T) -> T) { private suspend inline fun <T> MutableStateFlow<T>.update(function: (T) -> T) {
mutex.withLock { mutex.withLock {
yield()
this.value = function(this.value) this.value = function(this.value)
} }
} }