Check run updater preference in action

This commit is contained in:
Syer10
2022-06-29 22:57:59 -04:00
parent cf44c1ee20
commit c7e98581a6
5 changed files with 12 additions and 26 deletions

View File

@@ -31,7 +31,7 @@ class UpdateCheckWorker(private val context: Context, workerParams: WorkerParame
.let {
if (it.updatePreferences.enabled().get()) {
it.updateChecker
.checkForUpdates()
.checkForUpdates(false)
.singleOrNull()
} else null
}

View File

@@ -21,8 +21,8 @@ class UpdateChecker @Inject constructor(
private val updatePreferences: UpdatePreferences,
private val client: Http
) {
fun checkForUpdates() = flow {
// if (!updatePreferences.enabled().get()) return
fun checkForUpdates(manualFetch: Boolean) = flow {
if (!manualFetch && !updatePreferences.enabled().get()) return@flow
val latestRelease = client.get(
"https://api.github.com/repos/$GITHUB_REPO/releases/latest"
).body<GithubRelease>()

View File

@@ -62,7 +62,7 @@ class AboutViewModel @Inject constructor(
}
fun checkForUpdates() {
updateChecker.checkForUpdates()
updateChecker.checkForUpdates(true)
.filterIsInstance<Update.UpdateFound>()
.onEach {
_updates.emit(it)

View File

@@ -40,7 +40,7 @@ fun ApplicationScope.Tray(icon: Painter) {
trayState.sendNotification(
Notification(
MR.strings.new_update_title.localized(),
MR.strings.new_update_message.localized(Locale.getDefault(), it.version),
MR.strings.new_update_message.localized(Locale.getDefault(), it.release.version),
Notification.Type.Info
)
)

View File

@@ -7,39 +7,25 @@
package ca.gosyer.jui.ui.main.components
import ca.gosyer.jui.data.update.UpdateChecker
import ca.gosyer.jui.data.update.UpdatePreferences
import ca.gosyer.jui.data.update.model.GithubRelease
import ca.gosyer.jui.uicore.vm.ContextWrapper
import ca.gosyer.jui.uicore.vm.ViewModel
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.shareIn
import me.tatarka.inject.annotations.Inject
class TrayViewModel @Inject constructor(
updateChecker: UpdateChecker,
updatePreferences: UpdatePreferences,
contextWrapper: ContextWrapper
) : ViewModel(contextWrapper) {
override val scope = MainScope()
private val _updateFound = MutableSharedFlow<GithubRelease>()
val updateFound = _updateFound.asSharedFlow()
init {
if (updatePreferences.enabled().get()) {
updateChecker.checkForUpdates()
.onEach {
if (it is UpdateChecker.Update.UpdateFound) {
_updateFound.emit(it.release)
}
}
.launchIn(scope)
}
}
val updateFound = updateChecker
.checkForUpdates(false)
.filterIsInstance<UpdateChecker.Update.UpdateFound>()
.shareIn(scope, SharingStarted.Eagerly, 1)
override fun onDispose() {
super.onDispose()