mirror of https://git.sr.ht/~oppen/ariane
41 lines
1.3 KiB
Kotlin
41 lines
1.3 KiB
Kotlin
package oppen.gem.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(
|
|
bookmarkEntity.label ?: "Unknown",
|
|
URI.create(bookmarkEntity.uri))
|
|
)
|
|
}
|
|
onBookmarks(bookmarks)
|
|
}
|
|
}
|
|
|
|
override fun add(bookmark: Bookmark, onAdded: () -> Unit) {
|
|
GlobalScope.launch(Dispatchers.IO){
|
|
val bookmarkEntity = BookmarkEntity(bookmark.label, bookmark.uri.toString())
|
|
db.bookmarksDao().insertAll(bookmarkEntity)
|
|
onAdded()
|
|
}
|
|
}
|
|
|
|
override fun delete(bookmark: Bookmark, onDelete: () -> Unit) {
|
|
TODO("Not yet implemented")
|
|
}
|
|
} |