Search in sources :

Example 1 with Translation

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;
}
Also used : TranslationItem(com.quran.labs.androidquran.dao.translation.TranslationItem) Translation(com.quran.labs.androidquran.dao.translation.Translation) LocalTranslation(com.quran.labs.androidquran.common.LocalTranslation) LocalTranslation(com.quran.labs.androidquran.common.LocalTranslation) ArrayList(java.util.ArrayList) File(java.io.File)

Aggregations

LocalTranslation (com.quran.labs.androidquran.common.LocalTranslation)1 Translation (com.quran.labs.androidquran.dao.translation.Translation)1 TranslationItem (com.quran.labs.androidquran.dao.translation.TranslationItem)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1