mirror of
https://git.sr.ht/~oppen/ariane
synced 2024-12-26 01:38:40 +00:00
bookmark deleting implemented
This commit is contained in:
parent
bddb27d43b
commit
d41b5fbe99
5 changed files with 70 additions and 15 deletions
|
@ -6,4 +6,6 @@ class Bookmark(
|
|||
val label: String,
|
||||
val uri: URI,
|
||||
val index: Int
|
||||
)
|
||||
){
|
||||
var visible = true
|
||||
}
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
|
|
|
@ -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<BookmarksAdapter.ViewHolder>() {
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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 ->
|
||||
|
|
Loading…
Reference in a new issue