Search in sources :

Example 6 with LocalTranslation

use of com.quran.labs.androidquran.common.LocalTranslation in project quran_android by quran.

the class BaseTranslationPresenterTest method testGetTranslationNames.

@Test
public void testGetTranslationNames() {
    final List<String> databases = Arrays.asList("one.db", "two.db");
    Map<String, LocalTranslation> map = new HashMap<String, LocalTranslation>() {

        {
            put("one.db", new LocalTranslation(1, "one.db", "One", "First", null, null, null, 1));
            put("two.db", new LocalTranslation(2, "two.db", "Two", "Second", null, null, null, 1));
            put("three.db", new LocalTranslation(2, "three.db", "Three", "Third", null, null, null, 1));
        }
    };
    String[] translations = presenter.getTranslationNames(databases, map);
    assertThat(translations).hasLength(2);
    assertThat(translations[0]).isEqualTo("First");
    assertThat(translations[1]).isEqualTo("Second");
}
Also used : HashMap(java.util.HashMap) LocalTranslation(com.quran.labs.androidquran.common.LocalTranslation) Test(org.junit.Test)

Example 7 with LocalTranslation

use of com.quran.labs.androidquran.common.LocalTranslation in project quran_android by quran.

the class QuranDataProvider method search.

private Cursor search(String query, List<LocalTranslation> translations) {
    Timber.d("query: %s", query);
    final Context context = getContext();
    final boolean queryIsArabic = QuranUtils.doesStringContainArabic(query);
    final boolean haveArabic = queryIsArabic && quranFileUtils.hasTranslation(context, QURAN_ARABIC_DATABASE);
    if (translations.size() == 0 && (queryIsArabic && !haveArabic)) {
        return null;
    }
    int start = haveArabic ? -1 : 0;
    int total = translations.size();
    for (int i = start; i < total; i++) {
        String databaseName;
        if (i < 0) {
            databaseName = QURAN_ARABIC_DATABASE;
        } else {
            LocalTranslation translation = translations.get(i);
            // skip non-arabic databases if the query is in arabic
            if (queryIsArabic && translation.languageCode != null && !"ar".equals(translation.languageCode)) {
                continue;
            } else if (!queryIsArabic && "ar".equals(translation.languageCode)) {
                // skip arabic databases when the query isn't arabic
                continue;
            }
            databaseName = translation.filename;
        }
        Cursor cursor = search(query, databaseName, true);
        if (cursor != null && cursor.getCount() > 0) {
            return cursor;
        }
    }
    return null;
}
Also used : Context(android.content.Context) LocalTranslation(com.quran.labs.androidquran.common.LocalTranslation) MatrixCursor(android.database.MatrixCursor) Cursor(android.database.Cursor)

Example 8 with LocalTranslation

use of com.quran.labs.androidquran.common.LocalTranslation in project quran_android by quran.

the class TranslationsDBAdapter method getTranslations.

@WorkerThread
@NonNull
public List<LocalTranslation> getTranslations() {
    // intentional, since cachedTranslations can be replaced by another thread, causing the check
    // to be true, but the cached object returned to be null (or to change).
    List<LocalTranslation> cached = cachedTranslations;
    if (cached != null && cached.size() > 0) {
        return cached;
    }
    List<LocalTranslation> items = new ArrayList<>();
    Cursor cursor = db.query(TranslationsTable.TABLE_NAME, null, null, null, null, null, TranslationsTable.ID + " ASC");
    if (cursor != null) {
        while (cursor.moveToNext()) {
            int id = cursor.getInt(0);
            String name = cursor.getString(1);
            String translator = cursor.getString(2);
            String translatorForeign = cursor.getString(3);
            String filename = cursor.getString(4);
            String url = cursor.getString(5);
            String languageCode = cursor.getString(6);
            int version = cursor.getInt(7);
            if (quranFileUtils.hasTranslation(context, filename)) {
                items.add(new LocalTranslation(id, filename, name, translator, translatorForeign, url, languageCode, version));
            }
        }
        cursor.close();
    }
    items = Collections.unmodifiableList(items);
    if (items.size() > 0) {
        cachedTranslations = items;
    }
    return items;
}
Also used : LocalTranslation(com.quran.labs.androidquran.common.LocalTranslation) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

Aggregations

LocalTranslation (com.quran.labs.androidquran.common.LocalTranslation)8 Cursor (android.database.Cursor)3 Context (android.content.Context)2 MatrixCursor (android.database.MatrixCursor)2 ArrayList (java.util.ArrayList)2 NonNull (android.support.annotation.NonNull)1 WorkerThread (android.support.annotation.WorkerThread)1 SparseArray (android.util.SparseArray)1 Translation (com.quran.labs.androidquran.dao.translation.Translation)1 TranslationItem (com.quran.labs.androidquran.dao.translation.TranslationItem)1 DisposableSingleObserver (io.reactivex.observers.DisposableSingleObserver)1 File (java.io.File)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Test (org.junit.Test)1