Skip to content

Commit

Permalink
Invalidate translation caches after update (#1074)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedre authored Feb 1, 2019
1 parent 4562f22 commit 15c9221
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class TranslationsDBAdapter {
private final SQLiteDatabase db;
private final QuranFileUtils quranFileUtils;
private volatile List<LocalTranslation> cachedTranslations;
private long lastWriteTime = 0;

@Inject
public TranslationsDBAdapter(Context context,
Expand All @@ -52,6 +53,10 @@ public SparseArray<LocalTranslation> getTranslationsHash() {
return result;
}

public long getLastWriteTime() {
return this.lastWriteTime;
}

@WorkerThread
@NonNull
public List<LocalTranslation> getTranslations() {
Expand Down Expand Up @@ -125,6 +130,7 @@ public boolean writeTranslationUpdates(List<TranslationItem> updates) {
}
db.setTransactionSuccessful();

this.lastWriteTime = System.currentTimeMillis();
// clear the cached translations
this.cachedTranslations = null;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ internal open class BaseTranslationPresenter<T> internal constructor(
private val translationsAdapter: TranslationsDBAdapter,
private val translationUtil: TranslationUtil,
private val quranInfo: QuranInfo) : Presenter<T> {
private var lastCacheTime: Long = 0
private val translationMap: MutableMap<String, LocalTranslation> = HashMap()

var translationScreen: T? = null
Expand Down Expand Up @@ -168,14 +169,16 @@ internal open class BaseTranslationPresenter<T> internal constructor(
}

private fun getTranslationMapSingle(): Single<Map<String, LocalTranslation>> {
return if (this.translationMap.isEmpty()) {
return if (this.translationMap.isEmpty() ||
this.lastCacheTime != translationsAdapter.lastWriteTime) {
Single.fromCallable<List<LocalTranslation>> { translationsAdapter.translations }
.map<Map<String, LocalTranslation>> { translations ->
translations.associateBy { it.filename }
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSuccess { map ->
this.lastCacheTime = translationsAdapter.lastWriteTime
this.translationMap.clear()
this.translationMap.putAll(map)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,15 @@ private List<TranslationItem> mergeWithServerTranslations(List<Translation> serv
boolean exists = dbFile.exists();

TranslationItem item;
TranslationItem override = null;
if (exists) {
if (local == null) {
final Pair<Integer, Integer> versions = getVersionFromDatabase(translation.getFileName());
item = new TranslationItem(translation.withSchema(versions.second), versions.first);
item = new TranslationItem(translation, versions.first);
if (versions.second != translation.getMinimumVersion()) {
// schema change, write downloaded schema version to the db and return server item
override = new TranslationItem(translation.withSchema(versions.second), versions.first);
}
} else {
item = new TranslationItem(translation, local.getVersion());
}
Expand All @@ -242,7 +247,13 @@ private List<TranslationItem> mergeWithServerTranslations(List<Translation> serv
}

if ((local == null && exists) || (local != null && !exists)) {
updates.add(item);
if (override != null && item.getTranslation().getMinimumVersion() >= 5) {
// certain schema changes, especially those going to v5, keep the same filename while
// changing the database entry id. this could cause duplicate entries in the database.
// work around it by removing the existing entries before doing the updates.
translationsDBAdapter.deleteTranslationByFile(override.getTranslation().getFileName());
}
updates.add(override == null ? item : override);
} else if (local != null && local.getLanguageCode() == null) {
// older items don't have a language code
updates.add(item);
Expand Down

0 comments on commit 15c9221

Please sign in to comment.