mirror of https://git.sr.ht/~oppen/ariane
70 lines
2.2 KiB
Kotlin
70 lines
2.2 KiB
Kotlin
package oppen.ariane.io.database.history
|
|
|
|
import android.net.Uri
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.GlobalScope
|
|
import kotlinx.coroutines.launch
|
|
import oppen.ariane.io.database.ArianeAbstractDatabase
|
|
|
|
class ArianeHistory(private val db: ArianeAbstractDatabase): HistoryDatasource {
|
|
|
|
override fun get(onHistory: (List<HistoryEntry>) -> Unit) {
|
|
GlobalScope.launch(Dispatchers.IO){
|
|
val dbBookmarks = db.history().getAll()
|
|
val history = mutableListOf<HistoryEntry>()
|
|
|
|
dbBookmarks.forEach { entity ->
|
|
history.add(HistoryEntry(entity.uid, entity.timestamp ?: 0L, Uri.parse(entity.uri)))
|
|
}
|
|
onHistory(history)
|
|
}
|
|
}
|
|
|
|
override fun add(entry: HistoryEntry, onAdded: () -> Unit) {
|
|
GlobalScope.launch(Dispatchers.IO){
|
|
val lastAdded = db.history().getLastAdded()
|
|
val entity = HistoryEntity(entry.uri.toString(), System.currentTimeMillis())
|
|
|
|
when (lastAdded) {
|
|
null -> db.history().insert(entity)
|
|
else -> {
|
|
when {
|
|
lastAdded.uri.toString() != entry.uri.toString() -> db.history().insert(entity)
|
|
}
|
|
}
|
|
}
|
|
|
|
onAdded()
|
|
}
|
|
}
|
|
|
|
override fun add(uri: Uri, onAdded: () -> Unit) {
|
|
GlobalScope.launch(Dispatchers.IO){
|
|
val lastAdded = db.history().getLastAdded()
|
|
val entity = HistoryEntity(uri.toString(), System.currentTimeMillis())
|
|
|
|
when (lastAdded) {
|
|
null -> db.history().insert(entity)
|
|
else -> {
|
|
when {
|
|
lastAdded.uri.toString() != uri.toString() -> db.history().insert(entity)
|
|
}
|
|
}
|
|
}
|
|
|
|
onAdded()
|
|
}
|
|
}
|
|
|
|
override fun clear(onClear: () -> Unit) {
|
|
//todo
|
|
}
|
|
|
|
override fun delete(entry: HistoryEntry, onDelete: () -> Unit) {
|
|
GlobalScope.launch(Dispatchers.IO){
|
|
val entity = db.history().getEntry(entry.uid)
|
|
db.history().delete(entity)
|
|
onDelete()
|
|
}
|
|
}
|
|
} |