Search in sources :

Example 1 with Bookmark

use of com.quran.labs.androidquran.dao.Bookmark in project quran_android by quran.

the class ArabicDatabaseUtilsTest method testHydrateAyahTextEmpty.

@Test
public void testHydrateAyahTextEmpty() {
    ArabicDatabaseUtils arabicDatabaseUtils = new ArabicDatabaseUtils(context, new QuranInfo(new MadaniPageProvider()), mock(QuranFileUtils.class)) {

        @Override
        DatabaseHandler getArabicDatabaseHandler() {
            return arabicHandler;
        }

        @Override
        Map<Integer, String> getAyahTextForAyat(List<Integer> ayat) {
            Map<Integer, String> result = new HashMap<>();
            for (Integer ayahId : ayat) {
                result.put(ayahId, "verse " + ayahId);
            }
            return result;
        }
    };
    List<Bookmark> bookmarks = new ArrayList<>(1);
    bookmarks.add(new Bookmark(1, null, null, 3));
    List<Bookmark> result = arabicDatabaseUtils.hydrateAyahText(bookmarks);
    assertThat(result).hasSize(1);
    assertThat(result.get(0)).isNotInstanceOf(BookmarkWithAyahText.class);
    assertThat(result.get(0).getAyahText()).isNull();
    assertThat(result).isSameAs(bookmarks);
}
Also used : MadaniPageProvider(com.quran.data.page.provider.madani.MadaniPageProvider) Bookmark(com.quran.labs.androidquran.dao.Bookmark) HashMap(java.util.HashMap) QuranInfo(com.quran.labs.androidquran.data.QuranInfo) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) QuranFileUtils(com.quran.labs.androidquran.util.QuranFileUtils) Test(org.junit.Test)

Example 2 with Bookmark

use of com.quran.labs.androidquran.dao.Bookmark in project quran_android by quran.

the class BookmarksDBAdapter method getBookmarks.

@NonNull
private List<Bookmark> getBookmarks(int sortOrder, Integer pageFilter) {
    String orderBy;
    switch(sortOrder) {
        case SORT_LOCATION:
            orderBy = BookmarksTable.PAGE + " ASC, " + BookmarksTable.SURA + " ASC, " + BookmarksTable.AYAH + " ASC";
            break;
        case SORT_DATE_ADDED:
        default:
            orderBy = BookmarksTable.TABLE_NAME + "." + BookmarksTable.ADDED_DATE + " DESC";
    }
    List<Bookmark> bookmarks = new ArrayList<>();
    StringBuilder queryBuilder = new StringBuilder(BookmarksDBHelper.QUERY_BOOKMARKS);
    if (pageFilter != null) {
        queryBuilder.append(" WHERE ").append(BookmarksTable.PAGE).append(" = ").append(pageFilter).append(" AND ").append(BookmarksTable.SURA).append(" IS NOT NULL").append(" AND ").append(BookmarksTable.AYAH).append(" IS NOT NULL");
    }
    queryBuilder.append(" ORDER BY ").append(orderBy);
    Cursor cursor = null;
    try {
        cursor = mDb.rawQuery(queryBuilder.toString(), null);
        if (cursor != null) {
            long lastId = -1;
            Bookmark lastBookmark = null;
            List<Long> tagIds = new ArrayList<>();
            while (cursor.moveToNext()) {
                long id = cursor.getLong(0);
                Integer sura = cursor.getInt(1);
                Integer ayah = cursor.getInt(2);
                int page = cursor.getInt(3);
                long time = cursor.getLong(4);
                long tagId = cursor.getLong(5);
                if (sura == 0 || ayah == 0) {
                    sura = null;
                    ayah = null;
                }
                if (lastId != id) {
                    if (lastBookmark != null) {
                        bookmarks.add(lastBookmark.withTags(tagIds));
                    }
                    tagIds.clear();
                    lastBookmark = new Bookmark(id, sura, ayah, page, time);
                    lastId = id;
                }
                if (tagId > 0) {
                    tagIds.add(tagId);
                }
            }
            if (lastBookmark != null) {
                bookmarks.add(lastBookmark.withTags(tagIds));
            }
        }
    } finally {
        DatabaseUtils.closeCursor(cursor);
    }
    return bookmarks;
}
Also used : Bookmark(com.quran.labs.androidquran.dao.Bookmark) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) NonNull(android.support.annotation.NonNull)

Example 3 with Bookmark

use of com.quran.labs.androidquran.dao.Bookmark in project quran_android by quran.

the class ArabicDatabaseUtils method mergeBookmarksWithAyahText.

private List<Bookmark> mergeBookmarksWithAyahText(List<Bookmark> bookmarks, Map<Integer, String> ayahMap) {
    List<Bookmark> result;
    if (ayahMap.isEmpty()) {
        result = bookmarks;
    } else {
        result = new ArrayList<>(bookmarks.size());
        for (int i = 0, bookmarksSize = bookmarks.size(); i < bookmarksSize; i++) {
            Bookmark bookmark = bookmarks.get(i);
            Bookmark toAdd;
            if (bookmark.isPageBookmark()) {
                toAdd = bookmark;
            } else {
                String ayahText = ayahMap.get(quranInfo.getAyahId(bookmark.sura, bookmark.ayah));
                toAdd = ayahText == null ? bookmark : bookmark.withAyahText(ayahText);
            }
            result.add(toAdd);
        }
    }
    return result;
}
Also used : Bookmark(com.quran.labs.androidquran.dao.Bookmark)

Example 4 with Bookmark

use of com.quran.labs.androidquran.dao.Bookmark in project quran_android by quran.

the class BookmarkPresenter method getSortedRows.

private List<QuranRow> getSortedRows(List<Bookmark> bookmarks) {
    List<QuranRow> rows = new ArrayList<>(bookmarks.size());
    List<Bookmark> ayahBookmarks = new ArrayList<>();
    // add the page bookmarks directly, save ayah bookmarks for later
    for (int i = 0, bookmarksSize = bookmarks.size(); i < bookmarksSize; i++) {
        Bookmark bookmark = bookmarks.get(i);
        if (bookmark.isPageBookmark()) {
            rows.add(quranRowFactory.fromBookmark(appContext, bookmark));
        } else {
            ayahBookmarks.add(bookmark);
        }
    }
    // add page bookmarks header if needed
    if (rows.size() > 0) {
        rows.add(0, quranRowFactory.fromPageBookmarksHeader(appContext));
    }
    // add ayah bookmarks if any
    if (ayahBookmarks.size() > 0) {
        rows.add(quranRowFactory.fromAyahBookmarksHeader(appContext));
        for (int i = 0, ayahBookmarksSize = ayahBookmarks.size(); i < ayahBookmarksSize; i++) {
            rows.add(quranRowFactory.fromBookmark(appContext, ayahBookmarks.get(i)));
        }
    }
    return rows;
}
Also used : Bookmark(com.quran.labs.androidquran.dao.Bookmark) QuranRow(com.quran.labs.androidquran.ui.helpers.QuranRow) ArrayList(java.util.ArrayList) SuppressLint(android.annotation.SuppressLint)

Example 5 with Bookmark

use of com.quran.labs.androidquran.dao.Bookmark in project quran_android by quran.

the class BookmarkPresenter method generateTagsMapping.

private Map<Long, List<Bookmark>> generateTagsMapping(List<Tag> tags, List<Bookmark> bookmarks) {
    Set<Long> seenBookmarks = new HashSet<>();
    Map<Long, List<Bookmark>> tagMappings = new HashMap<>();
    for (int i = 0, tagSize = tags.size(); i < tagSize; i++) {
        long id = tags.get(i).getId();
        List<Bookmark> matchingBookmarks = new ArrayList<>();
        for (int j = 0, bookmarkSize = bookmarks.size(); j < bookmarkSize; j++) {
            Bookmark bookmark = bookmarks.get(j);
            if (bookmark.tags.contains(id)) {
                matchingBookmarks.add(bookmark);
                seenBookmarks.add(bookmark.id);
            }
        }
        tagMappings.put(id, matchingBookmarks);
    }
    List<Bookmark> untaggedBookmarks = new ArrayList<>();
    for (int i = 0, bookmarksSize = bookmarks.size(); i < bookmarksSize; i++) {
        Bookmark bookmark = bookmarks.get(i);
        if (!seenBookmarks.contains(bookmark.id)) {
            untaggedBookmarks.add(bookmark);
        }
    }
    tagMappings.put(BOOKMARKS_WITHOUT_TAGS_ID, untaggedBookmarks);
    return tagMappings;
}
Also used : Bookmark(com.quran.labs.androidquran.dao.Bookmark) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SuppressLint(android.annotation.SuppressLint) HashSet(java.util.HashSet)

Aggregations

Bookmark (com.quran.labs.androidquran.dao.Bookmark)12 ArrayList (java.util.ArrayList)8 List (java.util.List)6 SuppressLint (android.annotation.SuppressLint)4 Tag (com.quran.labs.androidquran.dao.Tag)4 QuranRow (com.quran.labs.androidquran.ui.helpers.QuranRow)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 MadaniPageProvider (com.quran.data.page.provider.madani.MadaniPageProvider)2 QuranInfo (com.quran.labs.androidquran.data.QuranInfo)2 QuranFileUtils (com.quran.labs.androidquran.util.QuranFileUtils)2 ContentValues (android.content.ContentValues)1 Cursor (android.database.Cursor)1 NonNull (android.support.annotation.NonNull)1 RecentPage (com.quran.labs.androidquran.dao.RecentPage)1 JuzView (com.quran.labs.androidquran.widgets.JuzView)1 TestObserver (io.reactivex.observers.TestObserver)1 HashSet (java.util.HashSet)1