mirror of https://git.sr.ht/~oppen/ariane
83 lines
2.9 KiB
Kotlin
83 lines
2.9 KiB
Kotlin
package oppen.ariane.io.bookmarks
|
|
|
|
import android.content.Context
|
|
import androidx.room.Room
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.GlobalScope
|
|
import kotlinx.coroutines.launch
|
|
import java.net.URI
|
|
|
|
class RoomBookmarks(applicationContext: Context): BookmarksDatasource {
|
|
|
|
private val db: BookmarksDB = Room.databaseBuilder(applicationContext, BookmarksDB::class.java, "tva_bookmarks").build()
|
|
|
|
override fun get(onBookmarks: (List<Bookmark>) -> Unit) {
|
|
GlobalScope.launch(Dispatchers.IO){
|
|
val dbBookmarks = db.bookmarksDao().getAll()
|
|
val bookmarks = mutableListOf<Bookmark>()
|
|
|
|
dbBookmarks.forEach { bookmarkEntity ->
|
|
bookmarks.add(
|
|
Bookmark(
|
|
uid = bookmarkEntity.uid,
|
|
label = bookmarkEntity.label ?: "Unknown",
|
|
uri = URI.create(bookmarkEntity.uri),
|
|
index = bookmarkEntity.uiIndex ?: 0)
|
|
)
|
|
}
|
|
onBookmarks(bookmarks)
|
|
}
|
|
}
|
|
|
|
override fun add(bookmark: Bookmark, onAdded: () -> Unit) {
|
|
GlobalScope.launch(Dispatchers.IO){
|
|
val bookmarkEntity = BookmarkEntity(
|
|
label = bookmark.label,
|
|
uri = bookmark.uri.toString(),
|
|
uiIndex = bookmark.index,
|
|
folder = "~/")
|
|
|
|
db.bookmarksDao().insertAll(bookmarkEntity)
|
|
onAdded()
|
|
}
|
|
}
|
|
|
|
override fun moveUp(bookmark: Bookmark, onMoved: () -> Unit) {
|
|
GlobalScope.launch(Dispatchers.IO){
|
|
|
|
//todo - this method is broken:
|
|
val prev = db.bookmarksDao().getBookmark(bookmark.index -1)
|
|
val target = db.bookmarksDao().getBookmark(bookmark.index)
|
|
|
|
db.bookmarksDao().updateUIIndex(prev.uid, bookmark.index)
|
|
db.bookmarksDao().updateUIIndex(target.uid, bookmark.index - 1)
|
|
onMoved()
|
|
}
|
|
}
|
|
|
|
override fun moveDown(bookmark: Bookmark, onMoved: () -> Unit) {
|
|
GlobalScope.launch(Dispatchers.IO){
|
|
val next = db.bookmarksDao().getBookmark(bookmark.index + 1)
|
|
val target = db.bookmarksDao().getBookmark(bookmark.index)
|
|
|
|
db.bookmarksDao().updateUIIndex(next.uid, bookmark.index)
|
|
db.bookmarksDao().updateUIIndex(target.uid, bookmark.index + 1)
|
|
onMoved()
|
|
}
|
|
}
|
|
|
|
override fun update(bookmark: Bookmark, label: String?, uri: String?, onUpdate: () -> Unit) {
|
|
GlobalScope.launch(Dispatchers.IO){
|
|
db.bookmarksDao().updateContent(bookmark.uid, label ?: "", uri ?: "")
|
|
onUpdate()
|
|
}
|
|
}
|
|
|
|
override fun delete(bookmark: Bookmark, onDelete: () -> Unit) {
|
|
GlobalScope.launch(Dispatchers.IO){
|
|
val entity = db.bookmarksDao().getBookmark(bookmark.index)
|
|
db.bookmarksDao().delete(entity)
|
|
onDelete()
|
|
}
|
|
}
|
|
} |