use of com.quran.labs.androidquran.dao.translation.Translation in project quran_android by quran.
the class TranslationManagerPresenter method mergeWithServerTranslations.
private List<TranslationItem> mergeWithServerTranslations(List<Translation> serverTranslations) {
List<TranslationItem> results = new ArrayList<>(serverTranslations.size());
SparseArray<LocalTranslation> localTranslations = translationsDBAdapter.getTranslationsHash();
String databaseDir = quranFileUtils.getQuranDatabaseDirectory(appContext);
List<TranslationItem> updates = new ArrayList<>();
for (int i = 0, count = serverTranslations.size(); i < count; i++) {
Translation translation = serverTranslations.get(i);
LocalTranslation local = localTranslations.get(translation.getId());
File dbFile = new File(databaseDir, translation.getFileName());
boolean exists = dbFile.exists();
TranslationItem item;
if (exists) {
int version = local == null ? getVersionFromDatabase(translation.getFileName()) : local.version;
item = new TranslationItem(translation, version);
} else {
item = new TranslationItem(translation);
}
if (exists && !item.exists()) {
// delete the file, it has been corrupted
if (dbFile.delete()) {
exists = false;
}
}
if ((local == null && exists) || (local != null && !exists)) {
updates.add(item);
} else if (local != null && local.languageCode == null) {
// older items don't have a language code
updates.add(item);
}
results.add(item);
}
if (!updates.isEmpty()) {
translationsDBAdapter.writeTranslationUpdates(updates);
}
return results;
}
Aggregations