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 { .let {
if (it.updatePreferences.enabled().get()) { if (it.updatePreferences.enabled().get()) {
it.updateChecker it.updateChecker
.checkForUpdates() .checkForUpdates(false)
.singleOrNull() .singleOrNull()
} else null } else null
} }

View File

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

View File

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

View File

@@ -40,7 +40,7 @@ fun ApplicationScope.Tray(icon: Painter) {
trayState.sendNotification( trayState.sendNotification(
Notification( Notification(
MR.strings.new_update_title.localized(), 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 Notification.Type.Info
) )
) )

View File

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