Skip to content

Commit 58d9dec

Browse files
committed
[App] Enable nightly app updates, check every 12 hours
1 parent 095403c commit 58d9dec

File tree

8 files changed

+40
-12
lines changed

8 files changed

+40
-12
lines changed

Diff for: app/src/main/java/pl/szczodrzynski/edziennik/core/manager/BuildManager.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class BuildManager(val app: App) : CoroutineScope {
103103
else -> Update.Type.RELEASE
104104
}
105105

106-
val devModeEasy = (isDaily || isNightly || isDebug) && !App.devMode
106+
val devModeEasy = (releaseType == Update.Type.NIGHTLY || isDebug) && !App.devMode
107107

108108
fun fetchInstalledTime() {
109109
if (app.config.appInstalledTime != 0L)

Diff for: app/src/main/java/pl/szczodrzynski/edziennik/core/manager/UpdateManager.kt

+26-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,31 @@ class UpdateManager(val app: App) : CoroutineScope {
3030
override val coroutineContext: CoroutineContext
3131
get() = job + Dispatchers.Default
3232

33+
fun isApplicable(update: Update): Boolean {
34+
if (app.buildManager.isDebug)
35+
return false // no updates on debug
36+
if (update.versionCode > BuildConfig.VERSION_CODE)
37+
return true
38+
if (update.versionCode < BuildConfig.VERSION_CODE)
39+
return false
40+
if (update.versionName == BuildConfig.VERSION_NAME)
41+
return false
42+
if (app.buildManager.isNightly || app.buildManager.isDaily) {
43+
val updateDate =
44+
update.versionName
45+
.replace("""\D""".toRegex(), "")
46+
.padEnd(12, '9')
47+
.toIntOrNull() ?: return false
48+
val buildDate =
49+
BuildConfig.VERSION_NAME
50+
.replace("""\D""".toRegex(), "")
51+
.padEnd(12, '9')
52+
.toIntOrNull() ?: return false
53+
return updateDate > buildDate
54+
}
55+
return false
56+
}
57+
3358
/**
3459
* Check for updates on the specified [maxChannel].
3560
* If the running build is of "more-unstable" type,
@@ -75,7 +100,7 @@ class UpdateManager(val app: App) : CoroutineScope {
75100
* @return [update] if it's a newer version, null otherwise
76101
*/
77102
fun process(update: Update?, notify: Boolean): Update? {
78-
if (update == null || update.versionCode <= BuildConfig.VERSION_CODE) {
103+
if (update == null || !isApplicable(update)) {
79104
app.config.update = null
80105
return null
81106
}

Diff for: app/src/main/java/pl/szczodrzynski/edziennik/core/work/UpdateWorker.kt

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import kotlinx.coroutines.*
1111
import pl.szczodrzynski.edziennik.*
1212
import pl.szczodrzynski.edziennik.data.api.szkolny.response.Update
1313
import pl.szczodrzynski.edziennik.ext.DAY
14+
import pl.szczodrzynski.edziennik.ext.HOUR
1415
import pl.szczodrzynski.edziennik.ext.formatDate
1516
import pl.szczodrzynski.edziennik.utils.Utils
1617
import timber.log.Timber
@@ -41,7 +42,11 @@ class UpdateWorker(val context: Context, val params: WorkerParameters) : Worker(
4142
if (!app.config.sync.notifyAboutUpdates) {
4243
return
4344
}
44-
val syncInterval = 4 * DAY;
45+
val syncInterval =
46+
if (app.buildManager.releaseType == Update.Type.NIGHTLY)
47+
12 * HOUR
48+
else
49+
4 * DAY
4550

4651
val syncAt = System.currentTimeMillis() + syncInterval*1000
4752
Timber.d("Scheduling work at ${syncAt.formatDate()}")

Diff for: app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/interceptor/SignatureInterceptor.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ class SignatureInterceptor(val app: App) : Interceptor {
2929
return chain.proceed(
3030
request.newBuilder()
3131
.header("X-ApiKey", app.config.apiKeyCustom?.takeValue() ?: API_KEY)
32-
.header("X-AppBuild", BuildConfig.BUILD_TYPE)
33-
.header("X-AppFlavor", BuildConfig.FLAVOR)
32+
.header("X-AppBuild", app.buildManager.buildType)
33+
.header("X-AppFlavor", app.buildManager.buildFlavor)
3434
.header("X-AppVersion", BuildConfig.VERSION_CODE.toString())
35+
.header("X-AppReleaseType", app.buildManager.releaseType.name.lowercase())
3536
.header("X-DeviceId", app.deviceId)
3637
.header("X-Signature", sign(timestamp, body, url))
3738
.header("X-Timestamp", timestamp.toString())

Diff for: app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/sync/UpdateAvailableDialog.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ class UpdateAvailableDialog(
4646

4747
override suspend fun onBeforeShow(): Boolean {
4848
// show only if app is older than available
49-
return update == null || update.versionCode > BuildConfig.VERSION_CODE
49+
return update == null || app.updateManager.isApplicable(update)
5050
}
5151
}

Diff for: app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/sync/UpdateProgressDialog.kt

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class UpdateProgressDialog(
3535
override fun getNegativeButtonText() = R.string.cancel
3636

3737
override suspend fun onShow() {
38-
EventBus.getDefault().register(this)
3938
b.update = update
4039
b.progress.progress = 0
4140

Diff for: app/src/main/java/pl/szczodrzynski/edziennik/ui/home/HomeFragment.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class HomeFragment : BaseFragment<FragmentHomeBinding, MainActivity>(
164164

165165
val status = app.availabilityManager.check(app.profile, cacheOnly = true)?.status
166166
val update = app.config.update
167-
if (update != null && update.versionCode > BuildConfig.VERSION_CODE || status?.userMessage != null) {
167+
if (update != null && app.updateManager.isApplicable(update) || status?.userMessage != null) {
168168
items.add(0, HomeAvailabilityCard(102, app, activity, this, app.profile))
169169
}
170170

Diff for: app/src/main/java/pl/szczodrzynski/edziennik/ui/home/cards/HomeAvailabilityCard.kt

+2-4
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class HomeAvailabilityCard(
8080
}
8181
}
8282
// show "update available" when available OR version too old for the register
83-
else if (update != null && update.versionCode > BuildConfig.VERSION_CODE) {
83+
else if (update != null && app.updateManager.isApplicable(update)) {
8484
b.homeAvailabilityTitle.setText(R.string.home_availability_title)
8585
b.homeAvailabilityText.setText(R.string.home_availability_text, update.versionName)
8686
b.homeAvailabilityUpdate.isVisible = !app.buildManager.isPlayRelease
@@ -94,9 +94,7 @@ class HomeAvailabilityCard(
9494
}
9595

9696
b.homeAvailabilityUpdate.onClick {
97-
if (update == null)
98-
return@onClick
99-
if (update.isOnGooglePlay)
97+
if (update == null || update.isOnGooglePlay)
10098
Utils.openGooglePlay(activity)
10199
else
102100
activity.startService(Intent(app, UpdateDownloaderService::class.java))

0 commit comments

Comments
 (0)