Replacing room with sqldelight. Big cleaning.
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
@ -1,6 +1,14 @@
|
||||
object SqlDelight {
|
||||
const val runtime = "com.squareup.sqldelight:runtime:1.5.3"
|
||||
const val android = "com.squareup.sqldelight:android-driver:1.5.3"
|
||||
const val native = "com.squareup.sqldelight:native-driver:1.5.3"
|
||||
|
||||
}
|
||||
|
||||
plugins {
|
||||
kotlin("multiplatform")
|
||||
id("com.android.library")
|
||||
id("com.squareup.sqldelight")
|
||||
kotlin("plugin.serialization") version "1.4.10"
|
||||
}
|
||||
|
||||
@ -39,6 +47,9 @@ kotlin {
|
||||
|
||||
// Network information
|
||||
implementation("com.github.ln-12:multiplatform-connectivity-status:1.3.0")
|
||||
|
||||
// Sql
|
||||
implementation(SqlDelight.runtime)
|
||||
}
|
||||
}
|
||||
val commonTest by getting {
|
||||
@ -50,6 +61,9 @@ kotlin {
|
||||
val androidMain by getting {
|
||||
dependencies {
|
||||
implementation("io.ktor:ktor-client-android:2.0.1")
|
||||
|
||||
// Sql
|
||||
implementation(SqlDelight.android)
|
||||
}
|
||||
}
|
||||
val androidTest by getting {
|
||||
@ -66,6 +80,11 @@ kotlin {
|
||||
iosX64Main.dependsOn(this)
|
||||
iosArm64Main.dependsOn(this)
|
||||
//iosSimulatorArm64Main.dependsOn(this)
|
||||
|
||||
// Sql
|
||||
dependencies {
|
||||
implementation(SqlDelight.native)
|
||||
}
|
||||
}
|
||||
val iosX64Test by getting
|
||||
val iosArm64Test by getting
|
||||
@ -93,4 +112,12 @@ android {
|
||||
sourceCompatibility = JavaVersion.VERSION_1_8
|
||||
targetCompatibility = JavaVersion.VERSION_1_8
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sqldelight {
|
||||
database("ReaderForSelfossDB") {
|
||||
packageName = "bou.amine.apps.readerforselfossv2.dao"
|
||||
sourceFolders = listOf("sqldelight")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,10 @@
|
||||
package bou.amine.apps.readerforselfossv2.dao
|
||||
import android.content.Context
|
||||
import com.squareup.sqldelight.android.AndroidSqliteDriver
|
||||
import com.squareup.sqldelight.db.SqlDriver
|
||||
|
||||
actual class DriverFactory(private val context: Context) {
|
||||
actual fun createDriver(): SqlDriver {
|
||||
return AndroidSqliteDriver(ReaderForSelfossDB.Schema, context, "ReaderForSelfossV2-android.db")
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package bou.amine.apps.readerforselfossv2.utils
|
||||
|
||||
import android.text.Html
|
||||
import bou.amine.apps.readerforselfossv2.rest.SelfossModel
|
||||
|
||||
actual fun SelfossModel.Item.getTitleDecoded(): String {
|
||||
return Html.fromHtml(title).toString()
|
||||
}
|
||||
|
||||
actual fun SelfossModel.Item.getSourceTitle(): String {
|
||||
return Html.fromHtml(sourcetitle).toString()
|
||||
}
|
||||
|
||||
actual fun SelfossModel.Source.getTitleDecoded(): String {
|
||||
return Html.fromHtml(title).toString()
|
||||
}
|
@ -1,9 +1,7 @@
|
||||
package bou.amine.apps.readerforselfossv2.dao
|
||||
|
||||
interface DeviceDatabase<ItemEntity> {
|
||||
suspend fun items(): List<ItemEntity>
|
||||
suspend fun insertAllItems(vararg items: ItemEntity)
|
||||
suspend fun deleteAllItems()
|
||||
suspend fun delete(item: ItemEntity)
|
||||
suspend fun updateItem(item: ItemEntity)
|
||||
}
|
||||
import com.squareup.sqldelight.db.SqlDriver
|
||||
|
||||
expect class DriverFactory {
|
||||
fun createDriver(): SqlDriver
|
||||
}
|
@ -1,10 +1,15 @@
|
||||
package bou.amine.apps.readerforselfossv2.repository
|
||||
|
||||
import bou.amine.apps.readerforselfossv2.dao.ACTION
|
||||
import bou.amine.apps.readerforselfossv2.dao.ReaderForSelfossDB
|
||||
import bou.amine.apps.readerforselfossv2.dao.SOURCE
|
||||
import bou.amine.apps.readerforselfossv2.dao.TAG
|
||||
import bou.amine.apps.readerforselfossv2.rest.SelfossApi
|
||||
import bou.amine.apps.readerforselfossv2.rest.SelfossModel
|
||||
import bou.amine.apps.readerforselfossv2.service.ApiDetailsService
|
||||
import bou.amine.apps.readerforselfossv2.utils.DateUtils
|
||||
import bou.amine.apps.readerforselfossv2.utils.ItemType
|
||||
import bou.amine.apps.readerforselfossv2.utils.toEntity
|
||||
import com.github.ln_12.library.ConnectivityStatus
|
||||
import com.russhwolf.settings.Settings
|
||||
import io.github.aakira.napier.Napier
|
||||
@ -12,7 +17,7 @@ import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class Repository(private val api: SelfossApi, private val apiDetails: ApiDetailsService, val connectivityStatus: ConnectivityStatus) {
|
||||
class Repository(private val api: SelfossApi, private val apiDetails: ApiDetailsService, val connectivityStatus: ConnectivityStatus, private val db: ReaderForSelfossDB) {
|
||||
val settings = Settings()
|
||||
|
||||
var items = ArrayList<SelfossModel.Item>()
|
||||
@ -342,5 +347,33 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
|
||||
|
||||
fun isNetworkAvailable() = isConnectionAvailable.value && !offlineOverride
|
||||
|
||||
// TODO: Handle offline actions
|
||||
fun getDBActions(): List<ACTION> =
|
||||
db.actionsQueries.actions().executeAsList()
|
||||
|
||||
fun deleteDBAction(action: ACTION) =
|
||||
db.actionsQueries.deleteAction(action.id)
|
||||
|
||||
fun getDBTags(): List<TAG> = db.tagsQueries.tags().executeAsList()
|
||||
|
||||
fun getDBSources(): List<SOURCE> = db.sourcesQueries.sources().executeAsList()
|
||||
|
||||
fun resetDBTagsWithData(tagEntities: List<SelfossModel.Tag>) {
|
||||
db.tagsQueries.deleteAllTags()
|
||||
|
||||
db.tagsQueries.transaction {
|
||||
tagEntities.forEach { tag ->
|
||||
db.tagsQueries.insertTag(tag.toEntity())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun resetDBSourcesWithData(sources: List<SelfossModel.Source>) {
|
||||
db.sourcesQueries.deleteAllSources()
|
||||
|
||||
db.sourcesQueries.transaction {
|
||||
sources.forEach { source ->
|
||||
db.sourcesQueries.insertSource(source.toEntity())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
package bou.amine.apps.readerforselfossv2.service
|
||||
|
||||
import bou.amine.apps.readerforselfossv2.dao.DeviceDatabase
|
||||
import bou.amine.apps.readerforselfossv2.utils.parseDate
|
||||
import bou.amine.apps.readerforselfossv2.rest.SelfossModel
|
||||
|
||||
abstract class DeviceDataBaseService<ItemEntity>(val db: DeviceDatabase<ItemEntity>, private val searchService: SearchService) {
|
||||
var itemsCaching = false
|
||||
var items: ArrayList<SelfossModel.Item> = arrayListOf()
|
||||
get() {
|
||||
return ArrayList(field)
|
||||
}
|
||||
set(value) {
|
||||
field = ArrayList(value)
|
||||
}
|
||||
|
||||
abstract suspend fun updateDatabase()
|
||||
abstract suspend fun clearDBItems()
|
||||
abstract fun appendNewItems(items: List<SelfossModel.Item>)
|
||||
abstract fun getFromDB()
|
||||
|
||||
fun sortItems() {
|
||||
val tmpItems = ArrayList(items.sortedByDescending { it.parseDate(searchService.dateUtils) })
|
||||
items = tmpItems
|
||||
}
|
||||
|
||||
// This filtered items from items val. Do not use
|
||||
fun getFocusedItems() {}
|
||||
fun computeBadges() {
|
||||
searchService.badgeUnread = items.filter { item -> item.unread }.size
|
||||
searchService.badgeStarred = items.filter { item -> item.starred }.size
|
||||
searchService.badgeAll = items.size
|
||||
}
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
package bou.amine.apps.readerforselfossv2.utils
|
||||
|
||||
import bou.amine.apps.readerforselfossv2.dao.ITEM
|
||||
import bou.amine.apps.readerforselfossv2.dao.SOURCE
|
||||
import bou.amine.apps.readerforselfossv2.dao.TAG
|
||||
import bou.amine.apps.readerforselfossv2.rest.SelfossModel
|
||||
|
||||
expect fun SelfossModel.Source.getTitleDecoded(): String
|
||||
expect fun SelfossModel.Item.getTitleDecoded(): String
|
||||
expect fun SelfossModel.Item.getSourceTitle(): String
|
||||
|
||||
fun TAG.toView(): SelfossModel.Tag =
|
||||
SelfossModel.Tag(
|
||||
this.name,
|
||||
this.color,
|
||||
this.unread.toInt()
|
||||
)
|
||||
|
||||
fun SOURCE.toView(): SelfossModel.Source =
|
||||
SelfossModel.Source(
|
||||
this.id.toInt(),
|
||||
this.title,
|
||||
this.tags.split(","),
|
||||
this.spout,
|
||||
this.error,
|
||||
this.icon
|
||||
)
|
||||
|
||||
fun SelfossModel.Source.toEntity(): SOURCE =
|
||||
SOURCE(
|
||||
this.id.toString(),
|
||||
this.getTitleDecoded(),
|
||||
this.tags.joinToString(","),
|
||||
this.spout,
|
||||
this.error,
|
||||
this.icon.orEmpty()
|
||||
)
|
||||
|
||||
fun SelfossModel.Tag.toEntity(): TAG =
|
||||
TAG(
|
||||
this.tag,
|
||||
this.color,
|
||||
this.unread.toLong()
|
||||
)
|
||||
|
||||
fun ITEM.toView(): SelfossModel.Item =
|
||||
SelfossModel.Item(
|
||||
this.id.toInt(),
|
||||
this.datetime,
|
||||
this.title,
|
||||
this.content,
|
||||
this.unread,
|
||||
this.starred,
|
||||
this.thumbnail,
|
||||
this.icon,
|
||||
this.link,
|
||||
this.sourcetitle,
|
||||
this.tags.split(",")
|
||||
)
|
||||
|
||||
fun SelfossModel.Item.toEntity(): ITEM =
|
||||
ITEM(
|
||||
this.id.toString(),
|
||||
this.datetime,
|
||||
this.getTitleDecoded(),
|
||||
this.content,
|
||||
this.unread,
|
||||
this.starred,
|
||||
this.thumbnail,
|
||||
this.icon,
|
||||
this.link,
|
||||
this.getSourceTitle(),
|
||||
this.tags.joinToString(",")
|
||||
)
|
@ -0,0 +1,18 @@
|
||||
CREATE TABLE `ACTION` (
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`articleid` TEXT NOT NULL,
|
||||
`read` INTEGER AS Boolean DEFAULT 0 NOT NULL,
|
||||
`unread` INTEGER AS Boolean DEFAULT 0 NOT NULL,
|
||||
`starred` INTEGER AS Boolean DEFAULT 0 NOT NULL,
|
||||
`unstarred` INTEGER AS Boolean DEFAULT 0 NOT NULL
|
||||
);
|
||||
|
||||
actions:
|
||||
SELECT *
|
||||
FROM `ACTION`;
|
||||
|
||||
insertAction:
|
||||
INSERT OR REPLACE INTO `ACTION` (`articleid`, `read`, `unread`, `starred`, `unstarred`) VALUES (?, ?, ?, ?, ?);
|
||||
|
||||
deleteAction:
|
||||
DELETE FROM `ACTION` WHERE id = ?;
|
@ -0,0 +1,30 @@
|
||||
CREATE TABLE ITEM (
|
||||
`id` TEXT NOT NULL,
|
||||
`datetime` TEXT NOT NULL,
|
||||
`title` TEXT NOT NULL,
|
||||
`content` TEXT NOT NULL,
|
||||
`unread` INTEGER AS Boolean DEFAULT 0 NOT NULL,
|
||||
`starred` INTEGER AS Boolean DEFAULT 0 NOT NULL,
|
||||
`thumbnail` TEXT,
|
||||
`icon` TEXT,
|
||||
`link` TEXT NOT NULL,
|
||||
`sourcetitle` TEXT NOT NULL,
|
||||
`tags` TEXT NOT NULL,
|
||||
PRIMARY KEY(`id`)
|
||||
);
|
||||
|
||||
CREATE INDEX item_title ON ITEM(`title`);
|
||||
CREATE INDEX item_source ON ITEM(`sourcetitle`);
|
||||
|
||||
|
||||
items:
|
||||
SELECT * FROM ITEM ORDER BY `id` DESC;
|
||||
|
||||
insertItem:
|
||||
INSERT OR REPLACE INTO ITEM VALUES ?;
|
||||
|
||||
deleteItem:
|
||||
DELETE FROM ITEM WHERE `id` = ?;
|
||||
|
||||
updateItem:
|
||||
UPDATE ITEM SET `datetime` = ?, `title` = ?, `content` = ?, `unread` = ?, `starred` = ?, `thumbnail` = ?, `icon` = ?, `link` = ?, `sourcetitle` = ?, `tags` = ? WHERE `id` = ?;
|
@ -0,0 +1,21 @@
|
||||
CREATE TABLE SOURCE (
|
||||
`id` TEXT NOT NULL,
|
||||
`title` TEXT NOT NULL,
|
||||
`tags` TEXT NOT NULL,
|
||||
`spout` TEXT NOT NULL,
|
||||
`error` TEXT NOT NULL,
|
||||
`icon` TEXT NOT NULL,
|
||||
PRIMARY KEY(`id`)
|
||||
);
|
||||
|
||||
sources:
|
||||
SELECT * FROM SOURCE;
|
||||
|
||||
insertSource:
|
||||
INSERT OR REPLACE INTO SOURCE VALUES ?;
|
||||
|
||||
deleteAllSources:
|
||||
DELETE FROM SOURCE;
|
||||
|
||||
deleteSource:
|
||||
DELETE FROM SOURCE WHERE `id` = ?;
|
@ -0,0 +1,20 @@
|
||||
CREATE TABLE TAG (
|
||||
`name` TEXT NOT NULL,
|
||||
`color` TEXT NOT NULL,
|
||||
`unread` INTEGER NOT NULL,
|
||||
PRIMARY KEY(`name`)
|
||||
);
|
||||
|
||||
CREATE INDEX tag_name ON TAG(`name`);
|
||||
|
||||
tags:
|
||||
SELECT * FROM TAG;
|
||||
|
||||
insertTag:
|
||||
INSERT OR REPLACE INTO TAG VALUES ?;
|
||||
|
||||
deleteAllTags:
|
||||
DELETE FROM TAG;
|
||||
|
||||
deleteTag:
|
||||
DELETE FROM TAG WHERE `name` = ? AND `color` = ?;
|
@ -0,0 +1,10 @@
|
||||
package bou.amine.apps.readerforselfossv2.dao
|
||||
|
||||
import com.squareup.sqldelight.db.SqlDriver
|
||||
import com.squareup.sqldelight.drivers.native.NativeSqliteDriver
|
||||
|
||||
actual class DriverFactory {
|
||||
actual fun createDriver(): SqlDriver {
|
||||
return NativeSqliteDriver(ReaderForSelfossDB.Schema, "ReaderForSelfossV2-IOS.db")
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package bou.amine.apps.readerforselfossv2.utils
|
||||
|
||||
actual class DateUtils actual constructor(apiMajorVersion: Int) {
|
||||
actual fun parseDate(dateString: String): Long {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
actual fun parseRelativeDate(dateString: String): String {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package bou.amine.apps.readerforselfossv2.utils
|
||||
|
||||
import bou.amine.apps.readerforselfossv2.rest.SelfossModel
|
||||
|
||||
actual fun SelfossModel.Item.getTitleDecoded(): String {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
actual fun SelfossModel.Item.getSourceTitle(): String {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
actual fun SelfossModel.Source.getTitleDecoded(): String {
|
||||
TODO("Not yet implemented")
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package bou.amine.apps.readerforselfossv2.dao
|
||||
|
||||
import com.squareup.sqldelight.db.SqlDriver
|
||||
import com.squareup.sqldelight.drivers.native.NativeSqliteDriver
|
||||
|
||||
actual class DriverFactory {
|
||||
actual fun createDriver(): SqlDriver {
|
||||
return NativeSqliteDriver(ReaderForSelfossDB.Schema, "ReaderForSelfossV2-IOS.db")
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package bou.amine.apps.readerforselfossv2.utils
|
||||
|
||||
actual class DateUtils actual constructor(apiMajorVersion: Int) {
|
||||
actual fun parseDate(dateString: String): Long {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
actual fun parseRelativeDate(dateString: String): String {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package bou.amine.apps.readerforselfossv2.utils
|
||||
|
||||
import bou.amine.apps.readerforselfossv2.rest.SelfossModel
|
||||
|
||||
actual fun SelfossModel.Item.getTitleDecoded(): String {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
actual fun SelfossModel.Item.getSourceTitle(): String {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
actual fun SelfossModel.Source.getTitleDecoded(): String {
|
||||
TODO("Not yet implemented")
|
||||
}
|
Reference in New Issue
Block a user