Skip to content

Commit

Permalink
Add infra for per flavor preference upgrades (quran#1570)
Browse files Browse the repository at this point in the history
Add the ability for each flavor to have their own set of preference
upgrades. This is important because the version codes of the flavors are
often different, and so a single answer may not work for all flavors of
the app.
  • Loading branch information
ahmedre authored Mar 23, 2021
1 parent a59137c commit 6c4d2ae
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 8 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ dependencies {
implementation project(path: ':common:networking')
implementation project(path: ':common:pages')
implementation project(path: ':common:search')
implementation project(path: ':common:upgrade')

implementation project(path: ':feature:audio')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.core.app.ActivityCompat.OnRequestPermissionsResultCallback
import androidx.core.content.ContextCompat
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.work.WorkManager
import com.quran.common.upgrade.PreferencesUpgrade
import com.quran.data.model.QuranDataStatus
import com.quran.data.source.PageProvider
import com.quran.labs.androidquran.presenter.data.QuranDataPresenter
Expand Down Expand Up @@ -75,6 +76,9 @@ class QuranDataActivity : Activity(), SimpleDownloadListener, OnRequestPermissio
@Inject
lateinit var quranDataPresenter: QuranDataPresenter

@Inject
lateinit var preferencesUpgrade: PreferencesUpgrade

private lateinit var quranSettings: QuranSettings

private var errorDialog: AlertDialog? = null
Expand All @@ -92,7 +96,7 @@ class QuranDataActivity : Activity(), SimpleDownloadListener, OnRequestPermissio
val quranApp = application as QuranApplication
quranApp.applicationComponent.inject(this)
quranSettings = QuranSettings.getInstance(this)
quranSettings.upgradePreferences()
quranSettings.upgradePreferences(preferencesUpgrade)

// replace null app locations (especially those set to null due to failures
// of finding a suitable data directory) with the default value to allow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import androidx.work.workDataOf
import com.quran.data.core.QuranInfo
import com.quran.data.model.QuranDataStatus
import com.quran.data.source.PageProvider
import com.quran.data.upgrade.LocalDataUpgrade
import com.quran.common.upgrade.LocalDataUpgrade
import com.quran.labs.androidquran.QuranDataActivity
import com.quran.labs.androidquran.data.Constants
import com.quran.labs.androidquran.presenter.Presenter
Expand Down Expand Up @@ -42,7 +42,8 @@ class QuranDataPresenter @Inject internal constructor(
val quranScreenInfo: QuranScreenInfo,
private val quranPageProvider: PageProvider,
val quranFileUtils: QuranFileUtils,
private val localDataUpgrade: LocalDataUpgrade) : Presenter<QuranDataActivity> {
private val localDataUpgrade: LocalDataUpgrade
) : Presenter<QuranDataActivity> {

private var activity: QuranDataActivity? = null
private var checkPagesDisposable: Disposable? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.os.Build;
import android.preference.PreferenceManager;

import com.quran.common.upgrade.PreferencesUpgrade;
import com.quran.labs.androidquran.BuildConfig;
import com.quran.labs.androidquran.R;
import com.quran.labs.androidquran.data.Constants;
Expand Down Expand Up @@ -178,7 +179,7 @@ public boolean isShowSuraTranslatedName() {
}

// probably should eventually move this to Application.onCreate..
public void upgradePreferences() {
public void upgradePreferences(PreferencesUpgrade preferencesUpgrade) {
int version = getVersion();
if (version != BuildConfig.VERSION_CODE) {
if (version == 0) {
Expand All @@ -190,8 +191,14 @@ public void upgradePreferences() {
setAppCustomLocation(getAppCustomLocation());
}

// make sure that the version code now says that we're up to date.
setVersion(BuildConfig.VERSION_CODE);
// allow specific flavors of the app to handle their own upgrade logic.
// this is important because different flavors have different version codes, so
// common code here would likely be wrong for other flavors (unless it depends on
// relative offsets to the version code instead of the actual version code).
if (preferencesUpgrade.upgrade(appContext, version, BuildConfig.VERSION_CODE)) {
// make sure that the version code now says that we're up to date.
setVersion(BuildConfig.VERSION_CODE);
}
}
}

Expand Down
20 changes: 20 additions & 0 deletions common/upgrade/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion deps.android.build.compileSdkVersion
defaultConfig {
minSdkVersion deps.android.build.minSdkVersion
targetSdkVersion deps.android.build.targetSdkVersion
}
}

dependencies {
implementation project(":common:data")

kapt deps.dagger.apt
implementation deps.dagger.runtime

implementation "androidx.annotation:annotation:${androidxAnnotationVersion}"
}
1 change: 1 addition & 0 deletions common/upgrade/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="com.quran.labs.androidquran.common.upgrade"/>
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.quran.data.upgrade
package com.quran.common.upgrade

import androidx.annotation.WorkerThread
import com.quran.data.model.QuranDataStatus

/**
* Interface for handling data upgrades (pages, databases, etc) between
* upgrades of versions of Quran for Android.
*/
fun interface LocalDataUpgrade {
@WorkerThread
fun processData(quranDataStatus: QuranDataStatus): QuranDataStatus
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.quran.common.upgrade

import android.content.Context

/**
* Upgrade preferences between one version of the app and another.
*/
fun interface PreferencesUpgrade {

/**
* Upgrades from one given version to another. [to] is expected to be
* the current version code of the app.
*
* @return a boolean as to whether the upgrade succeeded or not.
*/
fun upgrade(context: Context, from: Int, to: Int): Boolean
}
1 change: 1 addition & 0 deletions pages/madani/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies {
implementation project(path: ':common:data')
implementation project(path: ':common:pages')
implementation project(path: ':common:audio')
implementation project(path: ':common:upgrade')

kapt deps.dagger.apt
implementation deps.dagger.runtime
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.quran.data.page.provider

import com.quran.common.upgrade.LocalDataUpgrade
import com.quran.common.upgrade.PreferencesUpgrade
import com.quran.data.page.provider.madani.MadaniPageProvider
import com.quran.data.pageinfo.mapper.AyahMapper
import com.quran.data.pageinfo.mapper.IdentityAyahMapper
import com.quran.data.source.PageProvider
import com.quran.data.upgrade.LocalDataUpgrade
import com.quran.page.common.draw.ImageDrawHelper
import dagger.Module
import dagger.Provides
Expand All @@ -31,9 +32,14 @@ object QuranPageModule {
return emptySet()
}

@JvmStatic
@Provides
fun provideLocalDataUpgrade(): LocalDataUpgrade = LocalDataUpgrade { it }

@JvmStatic
@Provides
fun providePreferencesUpgrade(): PreferencesUpgrade = PreferencesUpgrade { _, _, _ -> true }

@JvmStatic
@Reusable
@Provides
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ include ':common:data'
include ':common:networking'
include ':common:pages'
include ':common:search'
include ':common:upgrade'
include ':feature:analytics-noop'
include ':feature:audio'
include ':pages:madani'
Expand Down

0 comments on commit 6c4d2ae

Please sign in to comment.