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);
}
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;
}
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;
}
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;
}
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;
}
Aggregations