Skip to content

Commit

Permalink
#88 Allow edit bookmark labels for selected verse
Browse files Browse the repository at this point in the history
  • Loading branch information
mjdenham committed May 6, 2017
1 parent 7fb86b4 commit 3838c9b
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public class BookmarkControl {
public static final String BOOKMARK_IDS_EXTRA = "bookmarkIds";
public static final String LABEL_NO_EXTRA = "labelNo";

private LabelDto LABEL_ALL;
private LabelDto LABEL_UNLABELLED;
private final LabelDto LABEL_ALL;
private final LabelDto LABEL_UNLABELLED;

private static final String BOOKMARK_SORT_ORDER = "BookmarkSortOrder";

Expand All @@ -61,18 +61,13 @@ public class BookmarkControl {
public BookmarkControl(SwordContentFacade swordContentFacade, ActiveWindowPageManagerProvider activeWindowPageManagerProvider, ResourceProvider resourceProvider) {
this.swordContentFacade = swordContentFacade;
this.activeWindowPageManagerProvider = activeWindowPageManagerProvider;
LABEL_ALL = new LabelDto();
LABEL_ALL.setName(resourceProvider.getString(R.string.all));
LABEL_ALL.setId(-999L);
LABEL_UNLABELLED = new LabelDto();
LABEL_UNLABELLED.setName(resourceProvider.getString(R.string.label_unlabelled));
LABEL_UNLABELLED.setId(-998L);
LABEL_ALL = new LabelDto(-999L, resourceProvider.getString(R.string.all), null);
LABEL_UNLABELLED = new LabelDto(-998L, resourceProvider.getString(R.string.label_unlabelled), null);
}

public boolean toggleBookmarkForVerseRange(VerseRange verseRange) {
boolean bOk = false;
CurrentPageManager currentPageControl = activeWindowPageManagerProvider.getActiveWindowPageManager();
if (currentPageControl.isBibleShown() || currentPageControl.isCommentaryShown()) {
if (isCurrentDocumentBookmarkable()) {

BookmarkDto bookmarkDto = getBookmarkByKey(verseRange);
final Activity currentActivity = CurrentActivityHolder.getInstance().getCurrentActivity();
Expand All @@ -88,17 +83,14 @@ public boolean toggleBookmarkForVerseRange(VerseRange verseRange) {
bookmarkDto = new BookmarkDto();
bookmarkDto.setVerseRange(verseRange);
final BookmarkDto newBookmark = addBookmark(bookmarkDto);

if (newBookmark!=null) {
// success
int actionTextColor = CommonUtils.getResourceColor(R.color.snackbar_action_text);
Snackbar.make(currentView, R.string.bookmark_added, Snackbar.LENGTH_LONG).setActionTextColor(actionTextColor).setAction(R.string.assign_labels, new View.OnClickListener() {
@Override
public void onClick(View v) {
// Show label view for new bookmark
final Intent intent = new Intent(currentActivity, BookmarkLabels.class);
intent.putExtra(BOOKMARK_IDS_EXTRA, new long[] {newBookmark.getId()});
currentActivity.startActivityForResult(intent, IntentHelper.REFRESH_DISPLAY_ON_FINISH);
showBookmarkLabelsActivity(currentActivity, newBookmark);
}
}).show();
bOk = true;
Expand All @@ -110,6 +102,18 @@ public void onClick(View v) {
return bOk;
}

// Label related methods
public void editBookmarkLabelsForVerseRange(VerseRange verseRange) {
if (isCurrentDocumentBookmarkable()) {
BookmarkDto bookmarkDto = getBookmarkByKey(verseRange);
final Activity currentActivity = CurrentActivityHolder.getInstance().getCurrentActivity();
if (bookmarkDto != null) {
// Show label view for new bookmark
showBookmarkLabelsActivity(currentActivity, bookmarkDto);
}
}
}

public String getBookmarkVerseKey(BookmarkDto bookmark) {
String keyText = "";
try {
Expand Down Expand Up @@ -212,11 +216,10 @@ public boolean deleteBookmark(BookmarkDto bookmark) {
} finally {
db.close();
}
}
}
return bOk;
}

// Label related methods
/** get bookmarks with the given label */
public List<BookmarkDto> getBookmarksWithLabel(LabelDto label) {
BookmarkDBAdapter db = new BookmarkDBAdapter();
Expand All @@ -240,10 +243,11 @@ public List<BookmarkDto> getBookmarksWithLabel(LabelDto label) {
return bookmarkList;
}


/** get bookmarks associated labels */
public List<LabelDto> getBookmarkLabels(BookmarkDto bookmark) {
List<LabelDto> labels;

BookmarkDBAdapter db = new BookmarkDBAdapter();
try {
db.open();
Expand All @@ -254,25 +258,24 @@ public List<LabelDto> getBookmarkLabels(BookmarkDto bookmark) {
return labels;
}


/** label the bookmark with these and only these labels */
public void setBookmarkLabels(BookmarkDto bookmark, List<LabelDto> labels) {
// never save LABEL_ALL
// never save LABEL_ALL
labels.remove(LABEL_ALL);
labels.remove(LABEL_UNLABELLED);

BookmarkDBAdapter db = new BookmarkDBAdapter();
try {
db.open();
List<LabelDto> prevLabels = db.getBookmarkLabels(bookmark);

//find those which have been deleted and remove them
Set<LabelDto> deleted = new HashSet<>(prevLabels);
deleted.removeAll(labels);
for (LabelDto label : deleted) {
db.removeBookmarkLabelJoin(bookmark, label);
}

//find those which are new and persist them
Set<LabelDto> added = new HashSet<>(labels);
added.removeAll(prevLabels);
Expand All @@ -284,7 +287,7 @@ public void setBookmarkLabels(BookmarkDto bookmark, List<LabelDto> labels) {
db.close();
}
}

public LabelDto saveOrUpdateLabel(LabelDto label) {
BookmarkDBAdapter db = new BookmarkDBAdapter();
LabelDto retLabel = null;
Expand All @@ -293,7 +296,7 @@ public LabelDto saveOrUpdateLabel(LabelDto label) {
if (label.getId()==null) {
retLabel = db.insertLabel(label);
} else {
retLabel = db.updateLabel(label);
retLabel = db.updateLabel(label);
}
} finally {
db.close();
Expand Down Expand Up @@ -340,6 +343,30 @@ public List<LabelDto> getAssignableLabels() {
return labelList;
}

public void changeBookmarkSortOrder() {
if (getBookmarkSortOrder().equals(BookmarkSortOrder.BIBLE_BOOK)) {
setBookmarkSortOrder(BookmarkSortOrder.DATE_CREATED);
} else {
setBookmarkSortOrder(BookmarkSortOrder.BIBLE_BOOK);
}
}

private BookmarkSortOrder getBookmarkSortOrder() {
String bookmarkSortOrderStr = CommonUtils.getSharedPreference(BOOKMARK_SORT_ORDER, BookmarkSortOrder.BIBLE_BOOK.toString());
return BookmarkSortOrder.valueOf(bookmarkSortOrderStr);
}
private void setBookmarkSortOrder(BookmarkSortOrder bookmarkSortOrder) {
CommonUtils.saveSharedPreference(BOOKMARK_SORT_ORDER, bookmarkSortOrder.toString());
}

public String getBookmarkSortOrderDescription() {
if (BookmarkSortOrder.BIBLE_BOOK.equals(getBookmarkSortOrder())) {
return CommonUtils.getResourceString(R.string.sort_by_bible_book);
} else {
return CommonUtils.getResourceString(R.string.sort_by_date);
}
}

private List<BookmarkDto> getSortedBookmarks(List<BookmarkDto> bookmarkList) {

Comparator<BookmarkDto> comparator;
Expand All @@ -363,28 +390,15 @@ private List<BookmarkDto> getSortedBookmarks(List<BookmarkDto> bookmarkList) {
return bookmarkList;
}

public void changeBookmarkSortOrder() {
if (getBookmarkSortOrder().equals(BookmarkSortOrder.BIBLE_BOOK)) {
setBookmarkSortOrder(BookmarkSortOrder.DATE_CREATED);
} else {
setBookmarkSortOrder(BookmarkSortOrder.BIBLE_BOOK);
}
}

public BookmarkSortOrder getBookmarkSortOrder() {
String bookmarkSortOrderStr = CommonUtils.getSharedPreference(BOOKMARK_SORT_ORDER, BookmarkSortOrder.BIBLE_BOOK.toString());
return BookmarkSortOrder.valueOf(bookmarkSortOrderStr);
}

public void setBookmarkSortOrder(BookmarkSortOrder bookmarkSortOrder) {
CommonUtils.saveSharedPreference(BOOKMARK_SORT_ORDER, bookmarkSortOrder.toString());
private boolean isCurrentDocumentBookmarkable() {
CurrentPageManager currentPageControl = activeWindowPageManagerProvider.getActiveWindowPageManager();
return currentPageControl.isBibleShown() || currentPageControl.isCommentaryShown();
}

public String getBookmarkSortOrderDescription() {
if (BookmarkSortOrder.BIBLE_BOOK.equals(getBookmarkSortOrder())) {
return CommonUtils.getResourceString(R.string.sort_by_bible_book);
} else {
return CommonUtils.getResourceString(R.string.sort_by_date);
}
private void showBookmarkLabelsActivity(Activity currentActivity, BookmarkDto bookmarkDto) {
// Show label view for new bookmark
final Intent intent = new Intent(currentActivity, BookmarkLabels.class);
intent.putExtra(BOOKMARK_IDS_EXTRA, new long[] {bookmarkDto.getId()});
currentActivity.startActivityForResult(intent, IntentHelper.REFRESH_DISPLAY_ON_FINISH);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
boolean isVerseBookmarked = startVerse!=null && bookmarkControl.isBookmarkForKey(getStartVerse());
menu.findItem(R.id.add_bookmark).setVisible(!isVerseBookmarked);
menu.findItem(R.id.delete_bookmark).setVisible(isVerseBookmarked);
menu.findItem(R.id.edit_bookmark_labels).setVisible(isVerseBookmarked);

// must return true if menu changed
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public boolean handleMenuRequest(int menuItemId, VerseRange verseRange) {
PassageChangeMediator.getInstance().forcePageUpdate();
isHandled = true;
break;
case R.id.edit_bookmark_labels:
bookmarkControl.editBookmarkLabelsForVerseRange(verseRange);
isHandled = true;
break;
case R.id.myNoteAddEdit:
myNoteControl.showMyNote(verseRange);
isHandled = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package net.bible.service.db.bookmark;


import android.support.annotation.NonNull;

import net.bible.android.control.bookmark.BookmarkStyle;

import org.apache.commons.lang3.StringUtils;
Expand All @@ -15,6 +17,15 @@ public class LabelDto implements Comparable<LabelDto> {
private String name;
private BookmarkStyle bookmarkStyle;

public LabelDto() {
}

public LabelDto(Long id, String name, BookmarkStyle bookmarkStyle) {
this.id = id;
this.name = name;
this.bookmarkStyle = bookmarkStyle;
}

@Override
public String toString() {
return getName();
Expand Down Expand Up @@ -89,8 +100,7 @@ public void setBookmarkStyleFromString(String bookmarkStyle) {
}

@Override
public int compareTo(LabelDto another) {
assert another!=null;
public int compareTo(@NonNull LabelDto another) {
return name.compareToIgnoreCase(another.name);
}
}
4 changes: 4 additions & 0 deletions and-bible/app/src/main/res/menu/verse_action_mode_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
android:icon="@drawable/ic_share_24dp"
appcompat:showAsAction="ifRoom"/>

<item android:id="@+id/edit_bookmark_labels"
android:title="@string/bookmark_labels"
android:icon="@drawable/ic_label_24dp"/>

<!-- Footnotes and references -->
<item android:id="@+id/notes"
android:title="@string/notes"/>
Expand Down

0 comments on commit 3838c9b

Please sign in to comment.