Skip to content

Commit

Permalink
Show generic bookmarks in bookmarks activity too
Browse files Browse the repository at this point in the history
  • Loading branch information
tuomas2 committed Jul 26, 2023
1 parent de07180 commit e56057b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ open class BookmarkControl @Inject constructor(

fun firstBookmarkStartingAtVerse(key: Verse): BookmarkWithNotes? = dao.bookmarksStartingAtVerse(key).firstOrNull()

fun deleteBookmark(bookmark: BookmarkWithNotes) {
fun deleteBookmark(bookmark: BaseBookmarkWithNotes) {
dao.delete(bookmark)
sanitizeStudyPadOrder(bookmark)
ABEventBus.post(BookmarksDeletedEvent(listOf(bookmark.id)))
Expand Down Expand Up @@ -233,7 +233,7 @@ open class BookmarkControl @Inject constructor(
return bookmarks
}

fun getGenericBookmarksWithLabel(label: Label, orderBy: BookmarkSortOrder = BookmarkSortOrder.BIBLE_ORDER, addData: Boolean = false): List<GenericBookmarkWithNotes> {
fun getGenericBookmarksWithLabel(label: Label, addData: Boolean = false): List<GenericBookmarkWithNotes> {
val bookmarks = when {
labelAll == label -> dao.allGenericBookmarks()
labelUnlabelled == label -> dao.unlabelledGenericBookmarks()
Expand Down Expand Up @@ -297,12 +297,15 @@ open class BookmarkControl @Inject constructor(

fun reset() {}

fun isSpeakBookmark(bookmark: BookmarkWithNotes): Boolean = labelsForBookmark(bookmark).contains(speakLabel)
fun isSpeakBookmark(bookmark: BaseBookmarkWithNotes): Boolean = labelsForBookmark(bookmark).contains(speakLabel)
fun speakBookmarkForVerse(verse: Verse) = dao.bookmarksForVerseStartWithLabel(verse, speakLabel).firstOrNull()

fun changeLabelsForBookmark(bookmark: BookmarkWithNotes, labelIds: List<IdType>) {
fun changeLabelsForBookmark(bookmark: BaseBookmarkWithNotes, labelIds: List<IdType>) {
dao.clearLabels(bookmark)
dao.insertBookmarkToLabels(labelIds.map { BookmarkToLabel(bookmark.id, it)})
when(bookmark) {
is BookmarkWithNotes -> dao.insertBookmarkToLabels(labelIds.map { BookmarkToLabel(bookmark.id, it)})
is GenericBookmarkWithNotes -> dao.insertGenericBookmarkToLabels(labelIds.map { GenericBookmarkToLabel(bookmark.id, it)})
}
}

fun saveBookmarkNote(bookmarkId: IdType, note: String?) {
Expand Down Expand Up @@ -584,7 +587,7 @@ open class BookmarkControl @Inject constructor(
)
}

private fun sanitizeStudyPadOrder(bookmark: BookmarkWithNotes) {
private fun sanitizeStudyPadOrder(bookmark: BaseBookmarkWithNotes) {
for (it in labelsForBookmark(bookmark)) {
sanitizeStudyPadOrder(it.id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import net.bible.android.activity.databinding.BookmarkListItemBinding
import net.bible.android.common.toV11n
import net.bible.android.control.bookmark.BookmarkControl
import net.bible.android.control.page.window.WindowControl
import net.bible.android.database.bookmarks.BookmarkEntities
import net.bible.android.database.bookmarks.BookmarkEntities.BookmarkWithNotes
import net.bible.service.common.htmlToSpan

Expand All @@ -40,10 +41,10 @@ import net.bible.service.common.htmlToSpan
*/
class BookmarkItemAdapter(
context: Context,
items: List<BookmarkWithNotes>,
items: List<BookmarkEntities.BaseBookmarkWithNotes>,
private val bookmarkControl: BookmarkControl,
private val windowControl: WindowControl,
) : ArrayAdapter<BookmarkWithNotes>(context, R.layout.bookmark_list_item, items) {
) : ArrayAdapter<BookmarkEntities.BaseBookmarkWithNotes>(context, R.layout.bookmark_list_item, items) {
private lateinit var bindings: BookmarkListItemBinding

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
Expand Down Expand Up @@ -79,13 +80,22 @@ class BookmarkItemAdapter(

// Set value for the first text field
val versification = windowControl.activeWindowPageManager.currentBible.versification
val verseName = item.verseRange.toV11n(versification).name
val book = item.speakBook
if (isSpeak && book != null) {
bindings.verseText.text = context.getString(R.string.something_with_parenthesis, verseName, book.abbreviation)
} else {
bindings.verseText.text = verseName
when(item) {
is BookmarkWithNotes -> {
val verseName = item.verseRange.toV11n(versification).name
val book = item.speakBook
if (isSpeak && book != null) {
bindings.verseText.text = context.getString(R.string.something_with_parenthesis, verseName, book.abbreviation)
} else {
bindings.verseText.text = verseName
}
}
is BookmarkEntities.GenericBookmarkWithNotes -> {
val keyName = "${item.book?.abbreviation}: ${item.bookKey.name}"
bindings.verseText.text = keyName
}
}

if(item.notes != null) {
bindings.notesText.visibility = View.VISIBLE
try {
Expand All @@ -103,7 +113,11 @@ class BookmarkItemAdapter(
val sDt = DateFormat.format("yyyy-MM-dd HH:mm", item.createdAt).toString()
bindings.dateText.text = sDt

val spanned = htmlToSpan(item.highlightedText)
val spanned = when(item) {
is BookmarkWithNotes -> htmlToSpan(item.highlightedText)
is BookmarkEntities.GenericBookmarkWithNotes -> htmlToSpan(item.text)
else -> throw RuntimeException("Illegal type")
}

bindings.verseContentText.text = spanned
return convertView ?: bindings.root
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ import net.bible.android.control.event.ABEventBus
import net.bible.android.control.page.window.WindowControl
import net.bible.android.control.speak.SpeakControl
import net.bible.android.database.IdType
import net.bible.android.database.bookmarks.BookmarkEntities
import net.bible.android.view.activity.base.ListActionModeHelper
import net.bible.android.view.activity.base.ListActionModeHelper.ActionModeActivity
import net.bible.android.view.activity.base.ListActivityBase
import net.bible.service.common.CommonUtils.settings
import net.bible.android.database.bookmarks.BookmarkEntities.BookmarkWithNotes
import net.bible.android.database.bookmarks.BookmarkEntities.BaseBookmarkWithNotes
import net.bible.android.database.bookmarks.BookmarkEntities.Label
import net.bible.android.database.bookmarks.BookmarkSortOrder
import net.bible.android.view.activity.base.Dialogs
Expand Down Expand Up @@ -86,7 +87,7 @@ class Bookmarks : ListActivityBase(), ActionModeActivity {
private var selectedLabelNo = 0

// the document list
private val bookmarkList: MutableList<BookmarkWithNotes> = ArrayList()
private val bookmarkList: MutableList<BaseBookmarkWithNotes> = ArrayList()
private var listActionModeHelper: ListActionModeHelper? = null
override val integrateWithHistoryManager: Boolean = true

Expand Down Expand Up @@ -145,7 +146,7 @@ class Bookmarks : ListActivityBase(), ActionModeActivity {
}

// prepare the document list view
val bookmarkArrayAdapter: ArrayAdapter<BookmarkWithNotes> = BookmarkItemAdapter(
val bookmarkArrayAdapter: ArrayAdapter<BaseBookmarkWithNotes> = BookmarkItemAdapter(
this, bookmarkList, bookmarkControl, windowControl
)
listAdapter = bookmarkArrayAdapter
Expand Down Expand Up @@ -173,7 +174,7 @@ class Bookmarks : ListActivityBase(), ActionModeActivity {
return intent
}

private fun assignLabels(bookmarks: List<BookmarkWithNotes>) = lifecycleScope.launch(Dispatchers.IO) {
private fun assignLabels(bookmarks: List<BaseBookmarkWithNotes>) = lifecycleScope.launch(Dispatchers.IO) {
val labels = mutableSetOf<IdType>()
for (b in bookmarks) {
labels.addAll(bookmarkControl.labelsForBookmark(b).map { it.id })
Expand All @@ -198,7 +199,7 @@ class Bookmarks : ListActivityBase(), ActionModeActivity {
}
}

private fun delete(bookmarks: List<BookmarkWithNotes>) {
private fun delete(bookmarks: List<BaseBookmarkWithNotes>) {
AlertDialog.Builder(this)
.setMessage(getString(R.string.confirm_delete_bookmarks, bookmarks.size))
.setPositiveButton(R.string.yes) { _, _ ->
Expand Down Expand Up @@ -235,6 +236,7 @@ class Bookmarks : ListActivityBase(), ActionModeActivity {
withContext(Dispatchers.Main) {
bookmarkList.clear()
bookmarkList.addAll(bookmarkControl.getBookmarksWithLabel(selectedLabel, bookmarkSortOrder))
bookmarkList.addAll(bookmarkControl.getGenericBookmarksWithLabel(selectedLabel))
notifyDataSetChanged()

// if in action mode then must exit because the data has changed, invalidating selections
Expand All @@ -255,14 +257,22 @@ class Bookmarks : ListActivityBase(), ActionModeActivity {
}
}

private fun bookmarkSelected(bookmark: BookmarkWithNotes) {
Log.i(TAG, "Bookmark selected:" + bookmark.verseRange)
private fun bookmarkSelected(bookmark: BaseBookmarkWithNotes) {
Log.i(TAG, "Bookmark selected:$bookmark")
try {
if (bookmarkControl.isSpeakBookmark(bookmark)) {
if (bookmark is BookmarkEntities.BookmarkWithNotes && bookmarkControl.isSpeakBookmark(bookmark)) {
speakControl.speakFromBookmark(bookmark)
}
val resultIntent = Intent(this, Bookmarks::class.java)
resultIntent.putExtra("verse", bookmark.verseRange.start.osisID)
when(bookmark) {
is BookmarkEntities.BookmarkWithNotes -> {
resultIntent.putExtra("verse", bookmark.verseRange.start.osisID)
}
is BookmarkEntities.GenericBookmarkWithNotes -> {
resultIntent.putExtra("key", bookmark.key)
resultIntent.putExtra("book", bookmark.book?.name)
}
}
setResult(Activity.RESULT_OK, resultIntent)
finish()
} catch (e: Exception) {
Expand Down Expand Up @@ -362,8 +372,8 @@ class Bookmarks : ListActivityBase(), ActionModeActivity {
return listView.isItemChecked(position)
}

private fun getSelectedBookmarks(selectedItemPositions: List<Int>): List<BookmarkWithNotes> {
val selectedBookmarks: MutableList<BookmarkWithNotes> = ArrayList()
private fun getSelectedBookmarks(selectedItemPositions: List<Int>): List<BaseBookmarkWithNotes> {
val selectedBookmarks: MutableList<BaseBookmarkWithNotes> = ArrayList()
for (position in selectedItemPositions) {
selectedBookmarks.add(bookmarkList[position])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,11 @@ interface BookmarkDao {
else -> throw RuntimeException("Illegal type")
}

@Query("DELETE FROM Bookmark WHERE id=:id")
fun deleteBookmarkById(id: IdType)

fun delete(b: BookmarkWithNotes) = deleteBookmarkById(b.id)
fun delete(bookmark: BaseBookmarkWithNotes) = when (bookmark) {
is BookmarkWithNotes -> deleteBookmarksById(listOf(bookmark.id))
is GenericBookmarkWithNotes -> deleteGenericBookmarksById(listOf(bookmark.id))
else -> throw RuntimeException("Illegal type")
}

@Query("DELETE FROM Bookmark WHERE id IN (:bs)")
fun deleteBookmarksById(bs: List<IdType>)
Expand Down Expand Up @@ -366,6 +367,14 @@ interface BookmarkDao {

@Query("DELETE FROM BookmarkToLabel WHERE bookmarkId=:bookmarkId")
fun clearLabels(bookmarkId: IdType)

@Query("DELETE FROM GenericBookmarkToLabel WHERE bookmarkId=:bookmarkId")
fun clearLabelsGeneric(bookmarkId: IdType)
fun clearLabels(bookmark: BaseBookmarkWithNotes) = when(bookmark) {
is BookmarkWithNotes -> clearLabels(bookmark.id)
is GenericBookmarkWithNotes -> clearLabelsGeneric(bookmark.id)
else -> throw RuntimeException("Illegal type")
}
fun clearLabels(bookmark: BookmarkWithNotes) = clearLabels(bookmark.id)

@Delete fun delete(entities: List<BookmarkToLabel>): Int
Expand Down

0 comments on commit e56057b

Please sign in to comment.