Skip to content

Commit

Permalink
[App] Enable nightly app updates, check every 12 hours
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba2k2 committed Jul 8, 2024
1 parent 095403c commit 58d9dec
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class BuildManager(val app: App) : CoroutineScope {
else -> Update.Type.RELEASE
}

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

fun fetchInstalledTime() {
if (app.config.appInstalledTime != 0L)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@ class UpdateManager(val app: App) : CoroutineScope {
override val coroutineContext: CoroutineContext
get() = job + Dispatchers.Default

fun isApplicable(update: Update): Boolean {
if (app.buildManager.isDebug)
return false // no updates on debug
if (update.versionCode > BuildConfig.VERSION_CODE)
return true
if (update.versionCode < BuildConfig.VERSION_CODE)
return false
if (update.versionName == BuildConfig.VERSION_NAME)
return false
if (app.buildManager.isNightly || app.buildManager.isDaily) {
val updateDate =
update.versionName
.replace("""\D""".toRegex(), "")
.padEnd(12, '9')
.toIntOrNull() ?: return false
val buildDate =
BuildConfig.VERSION_NAME
.replace("""\D""".toRegex(), "")
.padEnd(12, '9')
.toIntOrNull() ?: return false
return updateDate > buildDate
}
return false
}

/**
* Check for updates on the specified [maxChannel].
* If the running build is of "more-unstable" type,
Expand Down Expand Up @@ -75,7 +100,7 @@ class UpdateManager(val app: App) : CoroutineScope {
* @return [update] if it's a newer version, null otherwise
*/
fun process(update: Update?, notify: Boolean): Update? {
if (update == null || update.versionCode <= BuildConfig.VERSION_CODE) {
if (update == null || !isApplicable(update)) {
app.config.update = null
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kotlinx.coroutines.*
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.data.api.szkolny.response.Update
import pl.szczodrzynski.edziennik.ext.DAY
import pl.szczodrzynski.edziennik.ext.HOUR
import pl.szczodrzynski.edziennik.ext.formatDate
import pl.szczodrzynski.edziennik.utils.Utils
import timber.log.Timber
Expand Down Expand Up @@ -41,7 +42,11 @@ class UpdateWorker(val context: Context, val params: WorkerParameters) : Worker(
if (!app.config.sync.notifyAboutUpdates) {
return
}
val syncInterval = 4 * DAY;
val syncInterval =
if (app.buildManager.releaseType == Update.Type.NIGHTLY)
12 * HOUR
else
4 * DAY

val syncAt = System.currentTimeMillis() + syncInterval*1000
Timber.d("Scheduling work at ${syncAt.formatDate()}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ class SignatureInterceptor(val app: App) : Interceptor {
return chain.proceed(
request.newBuilder()
.header("X-ApiKey", app.config.apiKeyCustom?.takeValue() ?: API_KEY)
.header("X-AppBuild", BuildConfig.BUILD_TYPE)
.header("X-AppFlavor", BuildConfig.FLAVOR)
.header("X-AppBuild", app.buildManager.buildType)
.header("X-AppFlavor", app.buildManager.buildFlavor)
.header("X-AppVersion", BuildConfig.VERSION_CODE.toString())
.header("X-AppReleaseType", app.buildManager.releaseType.name.lowercase())
.header("X-DeviceId", app.deviceId)
.header("X-Signature", sign(timestamp, body, url))
.header("X-Timestamp", timestamp.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ class UpdateAvailableDialog(

override suspend fun onBeforeShow(): Boolean {
// show only if app is older than available
return update == null || update.versionCode > BuildConfig.VERSION_CODE
return update == null || app.updateManager.isApplicable(update)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class UpdateProgressDialog(
override fun getNegativeButtonText() = R.string.cancel

override suspend fun onShow() {
EventBus.getDefault().register(this)
b.update = update
b.progress.progress = 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class HomeFragment : BaseFragment<FragmentHomeBinding, MainActivity>(

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class HomeAvailabilityCard(
}
}
// show "update available" when available OR version too old for the register
else if (update != null && update.versionCode > BuildConfig.VERSION_CODE) {
else if (update != null && app.updateManager.isApplicable(update)) {
b.homeAvailabilityTitle.setText(R.string.home_availability_title)
b.homeAvailabilityText.setText(R.string.home_availability_text, update.versionName)
b.homeAvailabilityUpdate.isVisible = !app.buildManager.isPlayRelease
Expand All @@ -94,9 +94,7 @@ class HomeAvailabilityCard(
}

b.homeAvailabilityUpdate.onClick {
if (update == null)
return@onClick
if (update.isOnGooglePlay)
if (update == null || update.isOnGooglePlay)
Utils.openGooglePlay(activity)
else
activity.startService(Intent(app, UpdateDownloaderService::class.java))
Expand Down

0 comments on commit 58d9dec

Please sign in to comment.