moves mark as read to the AccountViewModel scope.
This commit is contained in:
@@ -831,34 +831,17 @@ private fun CheckNewAndRenderNote(
|
|||||||
val backgroundColor = remember { mutableStateOf<Color>(defaultBackgroundColor) }
|
val backgroundColor = remember { mutableStateOf<Color>(defaultBackgroundColor) }
|
||||||
|
|
||||||
LaunchedEffect(key1 = routeForLastRead, key2 = parentBackgroundColor?.value) {
|
LaunchedEffect(key1 = routeForLastRead, key2 = parentBackgroundColor?.value) {
|
||||||
launch(Dispatchers.IO) {
|
routeForLastRead?.let {
|
||||||
routeForLastRead?.let {
|
accountViewModel.loadAndMarkAsRead(it, baseNote.createdAt()) { isNew ->
|
||||||
val lastTime = accountViewModel.account.loadLastRead(it)
|
val newBackgroundColor = if (isNew) {
|
||||||
|
if (parentBackgroundColor != null) {
|
||||||
val createdAt = baseNote.createdAt()
|
newItemColor.compositeOver(parentBackgroundColor.value)
|
||||||
if (createdAt != null) {
|
|
||||||
accountViewModel.account.markAsRead(it, createdAt)
|
|
||||||
|
|
||||||
val isNew = createdAt > lastTime
|
|
||||||
|
|
||||||
val newBackgroundColor = if (isNew) {
|
|
||||||
if (parentBackgroundColor != null) {
|
|
||||||
newItemColor.compositeOver(parentBackgroundColor.value)
|
|
||||||
} else {
|
|
||||||
newItemColor.compositeOver(defaultBackgroundColor)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
parentBackgroundColor?.value ?: defaultBackgroundColor
|
newItemColor.compositeOver(defaultBackgroundColor)
|
||||||
}
|
|
||||||
|
|
||||||
if (newBackgroundColor != backgroundColor.value) {
|
|
||||||
launch(Dispatchers.Main) {
|
|
||||||
backgroundColor.value = newBackgroundColor
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
parentBackgroundColor?.value ?: defaultBackgroundColor
|
||||||
}
|
}
|
||||||
} ?: run {
|
|
||||||
val newBackgroundColor = parentBackgroundColor?.value ?: defaultBackgroundColor
|
|
||||||
|
|
||||||
if (newBackgroundColor != backgroundColor.value) {
|
if (newBackgroundColor != backgroundColor.value) {
|
||||||
launch(Dispatchers.Main) {
|
launch(Dispatchers.Main) {
|
||||||
@@ -866,6 +849,14 @@ private fun CheckNewAndRenderNote(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} ?: run {
|
||||||
|
val newBackgroundColor = parentBackgroundColor?.value ?: defaultBackgroundColor
|
||||||
|
|
||||||
|
if (newBackgroundColor != backgroundColor.value) {
|
||||||
|
launch(Dispatchers.Main) {
|
||||||
|
backgroundColor.value = newBackgroundColor
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -720,6 +720,19 @@ class AccountViewModel(val account: Account) : ViewModel(), Dao {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun loadAndMarkAsRead(routeForLastRead: String, baseNoteCreatedAt: Long?, onIsNew: (Boolean) -> Unit) {
|
||||||
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
|
val lastTime = account.loadLastRead(routeForLastRead)
|
||||||
|
|
||||||
|
if (baseNoteCreatedAt != null) {
|
||||||
|
account.markAsRead(routeForLastRead, baseNoteCreatedAt)
|
||||||
|
onIsNew(baseNoteCreatedAt > lastTime)
|
||||||
|
} else {
|
||||||
|
onIsNew(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class Factory(val account: Account) : ViewModelProvider.Factory {
|
class Factory(val account: Account) : ViewModelProvider.Factory {
|
||||||
override fun <AccountViewModel : ViewModel> create(modelClass: Class<AccountViewModel>): AccountViewModel {
|
override fun <AccountViewModel : ViewModel> create(modelClass: Class<AccountViewModel>): AccountViewModel {
|
||||||
return AccountViewModel(account) as AccountViewModel
|
return AccountViewModel(account) as AccountViewModel
|
||||||
|
|||||||
Reference in New Issue
Block a user