1
+ package eu.darken.octi.main.core
2
+
3
+ import dagger.Reusable
4
+ import eu.darken.octi.common.WebpageTool
5
+ import eu.darken.octi.common.datastore.value
6
+ import eu.darken.octi.common.debug.logging.Logging.Priority.ERROR
7
+ import eu.darken.octi.common.debug.logging.Logging.Priority.INFO
8
+ import eu.darken.octi.common.debug.logging.Logging.Priority.WARN
9
+ import eu.darken.octi.common.debug.logging.asLog
10
+ import eu.darken.octi.common.debug.logging.log
11
+ import eu.darken.octi.common.debug.logging.logTag
12
+ import eu.darken.octi.main.core.updater.UpdateChecker
13
+ import java.time.Duration
14
+ import java.time.Instant
15
+ import javax.inject.Inject
16
+
17
+ @Reusable
18
+ class FossUpdateChecker @Inject constructor(
19
+ private val checker : GithubReleaseCheck ,
20
+ private val webpageTool : WebpageTool ,
21
+ private val settings : FossUpdateSettings ,
22
+ ) : UpdateChecker {
23
+
24
+ override suspend fun getLatest (channel : UpdateChecker .Channel ): UpdateChecker .Update ? {
25
+ log(TAG ) { " getLatest($channel ) checking..." }
26
+
27
+ val release: GithubApi .ReleaseInfo ? = try {
28
+ if (Duration .between(settings.lastReleaseCheck.value(), Instant .now()) < UPDATE_CHECK_INTERVAL ) {
29
+ log(TAG ) { " Using cached release data" }
30
+ when (channel) {
31
+ UpdateChecker .Channel .BETA -> settings.lastReleaseBeta.value()
32
+ UpdateChecker .Channel .PROD -> settings.lastReleaseProd.value()
33
+ }
34
+ } else {
35
+ log(TAG ) { " Fetching new release data" }
36
+ when (channel) {
37
+ UpdateChecker .Channel .BETA -> checker.allReleases(OWNER , REPO ).first()
38
+ UpdateChecker .Channel .PROD -> checker.latestRelease(OWNER , REPO )
39
+ }.also {
40
+ log(TAG , INFO ) { " getLatest($channel ) new data is $it " }
41
+ settings.lastReleaseCheck.value(Instant .now())
42
+ when (channel) {
43
+ UpdateChecker .Channel .BETA -> settings.lastReleaseBeta.value(it)
44
+ UpdateChecker .Channel .PROD -> settings.lastReleaseProd.value(it)
45
+ }
46
+ }
47
+ }
48
+ } catch (e: Exception ) {
49
+ log(TAG , ERROR ) { " getLatest($channel ) failed: ${e.asLog()} " }
50
+ null
51
+ }
52
+
53
+ log(TAG , INFO ) { " getLatest($channel ) is ${release?.tagName} " }
54
+
55
+ val update = release?.let { rel ->
56
+ Update (
57
+ channel = channel,
58
+ versionName = rel.tagName,
59
+ changelogLink = rel.htmlUrl,
60
+ downloadLink = rel.assets.singleOrNull { it.name.endsWith(" .apk" ) }?.downloadUrl,
61
+ )
62
+ }
63
+
64
+ return update
65
+ }
66
+
67
+ override suspend fun startUpdate (update : UpdateChecker .Update ) {
68
+ log(TAG , INFO ) { " startUpdate($update )" }
69
+ update as Update
70
+ if (update.downloadLink != null ) {
71
+ webpageTool.open(update.downloadLink)
72
+ } else {
73
+ log(TAG , WARN ) { " No download link available for $update " }
74
+ }
75
+ }
76
+
77
+ override suspend fun viewUpdate (update : UpdateChecker .Update ) {
78
+ log(TAG , INFO ) { " viewUpdate($update )" }
79
+ update as Update
80
+ webpageTool.open(update.changelogLink)
81
+ }
82
+
83
+ override suspend fun dismissUpdate (update : UpdateChecker .Update ) {
84
+ log(TAG , INFO ) { " dismissUpdate($update )" }
85
+ update as Update
86
+ settings.dismiss(update)
87
+ }
88
+
89
+ override suspend fun isDismissed (update : UpdateChecker .Update ): Boolean {
90
+ update as Update
91
+ return settings.isDismissed(update)
92
+ }
93
+
94
+ override fun isEnabledByDefault (): Boolean {
95
+ val isEnabled = false
96
+ log(TAG , INFO ) { " Update check default isEnabled=$isEnabled " }
97
+ return isEnabled
98
+ }
99
+
100
+ override suspend fun isCheckSupported (): Boolean {
101
+ return true
102
+ }
103
+
104
+ data class Update (
105
+ override val channel : UpdateChecker .Channel ,
106
+ override val versionName : String ,
107
+ val changelogLink : String ,
108
+ val downloadLink : String? ,
109
+ ) : UpdateChecker.Update
110
+
111
+ companion object {
112
+ private val UPDATE_CHECK_INTERVAL = Duration .ofHours(6 )
113
+ private const val OWNER = " d4rken-org"
114
+ private const val REPO = " octi"
115
+ private val TAG = logTag(" Updater" , " Checker" , " FOSS" )
116
+ }
117
+ }
0 commit comments