diff --git a/app/src/main/java/oppen/gem/io/bookmarks/Bookmark.kt b/app/src/main/java/oppen/gem/io/bookmarks/Bookmark.kt index eb64fb1..d4a2f53 100644 --- a/app/src/main/java/oppen/gem/io/bookmarks/Bookmark.kt +++ b/app/src/main/java/oppen/gem/io/bookmarks/Bookmark.kt @@ -6,4 +6,6 @@ class Bookmark( val label: String, val uri: URI, val index: Int -) \ No newline at end of file +){ + var visible = true +} \ No newline at end of file diff --git a/app/src/main/java/oppen/gem/io/bookmarks/RoomBookmarks.kt b/app/src/main/java/oppen/gem/io/bookmarks/RoomBookmarks.kt index 0d3c4a7..66e2c2f 100644 --- a/app/src/main/java/oppen/gem/io/bookmarks/RoomBookmarks.kt +++ b/app/src/main/java/oppen/gem/io/bookmarks/RoomBookmarks.kt @@ -65,6 +65,10 @@ class RoomBookmarks(applicationContext: Context): BookmarksDatasource { } override fun delete(bookmark: Bookmark, onDelete: () -> Unit) { - TODO("Not yet implemented") + GlobalScope.launch(Dispatchers.IO){ + val entity = db.bookmarksDao().getBookmark(bookmark.index) + db.bookmarksDao().delete(entity) + onDelete() + } } } \ No newline at end of file diff --git a/app/src/main/java/oppen/gem/ui/bookmarks/BookmarkDialog.kt b/app/src/main/java/oppen/gem/ui/bookmarks/BookmarkDialog.kt index 3d7748e..8f25a08 100644 --- a/app/src/main/java/oppen/gem/ui/bookmarks/BookmarkDialog.kt +++ b/app/src/main/java/oppen/gem/ui/bookmarks/BookmarkDialog.kt @@ -42,7 +42,7 @@ class BookmarkDialog( bookmarkDatasource.add(Bookmark( label = view.bookmark_name.text.toString(), uri = URI.create(view.bookmark_uri.text.toString()), - index = allBookmarks.size + index = allBookmarks.last().index + 1 )){ Handler(Looper.getMainLooper()).post { onDismiss() diff --git a/app/src/main/java/oppen/gem/ui/bookmarks/BookmarksAdapter.kt b/app/src/main/java/oppen/gem/ui/bookmarks/BookmarksAdapter.kt index cab62b6..771b648 100644 --- a/app/src/main/java/oppen/gem/ui/bookmarks/BookmarksAdapter.kt +++ b/app/src/main/java/oppen/gem/ui/bookmarks/BookmarksAdapter.kt @@ -7,6 +7,7 @@ import androidx.recyclerview.widget.RecyclerView import kotlinx.android.synthetic.main.bookmark.view.* import oppen.gem.R import oppen.gem.io.bookmarks.Bookmark +import oppen.visible class BookmarksAdapter(val onBookmark: (bookmark: Bookmark) -> Unit, val onOverflow: (view: View, bookmark: Bookmark, isFirst: Boolean, isLast: Boolean) -> Unit): RecyclerView.Adapter() { @@ -29,19 +30,40 @@ class BookmarksAdapter(val onBookmark: (bookmark: Bookmark) -> Unit, val onOverf override fun onBindViewHolder(holder: ViewHolder, position: Int) { val bookmark = bookmarks[position] - holder.itemView.bookmark_name.text = bookmark.label - holder.itemView.bookmark_uri.text = bookmark.uri.toString() + if(bookmark.visible) { + holder.itemView.visible(true) + holder.itemView.bookmark_name.text = bookmark.label + holder.itemView.bookmark_uri.text = bookmark.uri.toString() - holder.itemView.bookmark_layout.setOnClickListener { - onBookmark(bookmarks[holder.adapterPosition]) - } + holder.itemView.bookmark_layout.setOnClickListener { + onBookmark(bookmarks[holder.adapterPosition]) + } - holder.itemView.bookmark_overflow.setOnClickListener {view -> - val isFirst = (holder.adapterPosition == 0) - val isLast = (holder.adapterPosition == bookmarks.size -1) - onOverflow(view, bookmarks[holder.adapterPosition], isFirst, isLast) + holder.itemView.bookmark_overflow.setOnClickListener { view -> + val isFirst = (holder.adapterPosition == 0) + val isLast = (holder.adapterPosition == bookmarks.size - 1) + onOverflow(view, bookmarks[holder.adapterPosition], isFirst, isLast) + } + }else{ + holder.itemView.visible(false) } } override fun getItemCount(): Int = bookmarks.size + + fun hide(bookmark: Bookmark) { + bookmark.visible = false + notifyItemChanged(bookmarks.indexOf(bookmark)) + } + + fun show(bookmark: Bookmark) { + bookmark.visible = true + notifyItemChanged(bookmarks.indexOf(bookmark)) + } + + fun remove(bookmark: Bookmark){ + val index = bookmarks.indexOf(bookmark) + bookmarks.remove(bookmark) + notifyItemRemoved(index) + } } \ No newline at end of file diff --git a/app/src/main/java/oppen/gem/ui/bookmarks/BookmarksDialog.kt b/app/src/main/java/oppen/gem/ui/bookmarks/BookmarksDialog.kt index bf1c7f4..ee42225 100644 --- a/app/src/main/java/oppen/gem/ui/bookmarks/BookmarksDialog.kt +++ b/app/src/main/java/oppen/gem/ui/bookmarks/BookmarksDialog.kt @@ -7,6 +7,8 @@ import android.view.View import androidx.appcompat.app.AppCompatDialog import androidx.appcompat.widget.PopupMenu import androidx.recyclerview.widget.LinearLayoutManager +import com.google.android.material.snackbar.BaseTransientBottomBar +import com.google.android.material.snackbar.Snackbar import kotlinx.android.synthetic.main.dialog_bookmarks.view.* import oppen.gem.R import oppen.gem.io.bookmarks.Bookmark @@ -20,8 +22,9 @@ class BookmarksDialog( var bookmarksAdapter: BookmarksAdapter + var view: View = View.inflate(context, R.layout.dialog_bookmarks, null) + init { - val view = View.inflate(context, R.layout.dialog_bookmarks, null) setContentView(view) @@ -54,8 +57,8 @@ class BookmarksDialog( bookmarkOverflow.setOnMenuItemClickListener { menuItem -> when(menuItem.itemId){ - R.id.menu_bookmark_edit -> {} - R.id.menu_bookmark_delete -> {} + R.id.menu_bookmark_edit -> edit(bookmark) + R.id.menu_bookmark_delete -> delete(bookmark) R.id.menu_bookmark_move_up -> moveUp(bookmark) R.id.menu_bookmark_move_down-> moveDown(bookmark) } @@ -75,6 +78,30 @@ class BookmarksDialog( } } + private fun edit(bookmark: Bookmark){ + + } + + /** + * + * Bookmark isn't actually deleted from the DB until the Snackbar disappears. Which is nice. + * + */ + private fun delete(bookmark: Bookmark){ + //OnDelete + bookmarksAdapter.hide(bookmark) + Snackbar.make(view, "Deleted ${bookmark.label}", Snackbar.LENGTH_SHORT).addCallback(object: Snackbar.Callback() { + override fun onDismissed(transientBottomBar: Snackbar?, event: Int) = when (event) { + BaseTransientBottomBar.BaseCallback.DISMISS_EVENT_ACTION -> bookmarksAdapter.show(bookmark) + else -> bookmarkDatasource.delete(bookmark){ + bookmarksAdapter.remove(bookmark) + } + } + }).setAction("Undo"){ + //Action listener unused + }.show() + } + private fun moveUp(bookmark: Bookmark){ bookmarkDatasource.moveUp(bookmark){ bookmarkDatasource.get { bookmarks ->