11package conf
22
3+ import android.content.ContentValues
4+ import android.database.Cursor
5+ import android.database.sqlite.SQLiteDatabase
6+ import androidx.core.database.sqlite.transaction
37import db.Conf
4- import db.Db
58import kotlinx.coroutines.Dispatchers
69import kotlinx.coroutines.GlobalScope
710import kotlinx.coroutines.flow.MutableStateFlow
@@ -16,21 +19,20 @@ import org.koin.core.annotation.Single
1619
1720@Single
1821class ConfRepo (
19- private val db : Db ,
22+ private val db : SQLiteDatabase ,
2023) {
2124
2225 private val _conf : MutableStateFlow <Conf > = MutableStateFlow (
23- runBlocking { db.confQueries.select().executeAsOneOrNull() ? : DEFAULT_CONF }
24- )
26+ runBlocking { select() ? : DEFAULT_CONF })
2527
2628 val conf: StateFlow <Conf > = _conf .asStateFlow()
2729
2830 init {
2931 conf.onEach {
3032 withContext(Dispatchers .IO ) {
3133 db.transaction {
32- db.confQueries. delete()
33- db.confQueries. insert(it)
34+ delete()
35+ insert(it)
3436 }
3537 }
3638 }.launchIn(GlobalScope )
@@ -40,6 +42,101 @@ class ConfRepo(
4042 _conf .update { newConf(conf.value) }
4143 }
4244
45+ fun insert (conf : Conf ) {
46+ val values = ContentValues ().apply {
47+ put(" backend" , conf.backend)
48+ put(" miniflux_server_url" , conf.minifluxServerUrl)
49+ put(" miniflux_server_trust_self_signed_certs" , conf.minifluxServerTrustSelfSignedCerts)
50+ put(" miniflux_server_username" , conf.minifluxServerUsername)
51+ put(" miniflux_server_password" , conf.minifluxServerPassword)
52+ put(" nextcloud_server_url" , conf.nextcloudServerUrl)
53+ put(
54+ " nextcloud_server_trust_self_signed_certs" , conf.nextcloudServerTrustSelfSignedCerts
55+ )
56+ put(" nextcloud_server_username" , conf.nextcloudServerUsername)
57+ put(" nextcloud_server_password" , conf.nextcloudServerPassword)
58+ put(" initial_sync_completed" , conf.initialSyncCompleted)
59+ put(" last_entries_sync_datetime" , conf.lastEntriesSyncDatetime)
60+ put(" show_read_entries" , conf.showReadEntries)
61+ put(" sort_order" , conf.sortOrder)
62+ put(" show_preview_images" , conf.showPreviewImages)
63+ put(" crop_preview_images" , conf.cropPreviewImages)
64+ put(" mark_scrolled_entries_as_read" , conf.markScrolledEntriesAsRead)
65+ put(" sync_on_startup" , conf.syncOnStartup)
66+ put(" sync_in_background" , conf.syncInBackground)
67+ put(" background_sync_interval_millis" , conf.backgroundSyncIntervalMillis)
68+ put(" use_built_in_browser" , conf.useBuiltInBrowser)
69+ put(" show_preview_text" , conf.showPreviewText)
70+ put(" synced_on_startup" , conf.syncedOnStartup)
71+ }
72+ db.insert(" conf" , null , values)
73+ }
74+
75+ fun select (): Conf ? {
76+ val cursor = db.query(
77+ " conf" , arrayOf(
78+ " backend" ,
79+ " miniflux_server_url" ,
80+ " miniflux_server_trust_self_signed_certs" ,
81+ " miniflux_server_username" ,
82+ " miniflux_server_password" ,
83+ " nextcloud_server_url" ,
84+ " nextcloud_server_trust_self_signed_certs" ,
85+ " nextcloud_server_username" ,
86+ " nextcloud_server_password" ,
87+ " initial_sync_completed" ,
88+ " last_entries_sync_datetime" ,
89+ " show_read_entries" ,
90+ " sort_order" ,
91+ " show_preview_images" ,
92+ " crop_preview_images" ,
93+ " mark_scrolled_entries_as_read" ,
94+ " sync_on_startup" ,
95+ " sync_in_background" ,
96+ " background_sync_interval_millis" ,
97+ " use_built_in_browser" ,
98+ " show_preview_text" ,
99+ " synced_on_startup" ,
100+ ), " " , emptyArray(), " " , " " , " "
101+ )
102+ if (! cursor.moveToNext()) {
103+ return null
104+ } else {
105+ return Conf (
106+ backend = cursor.getString(0 ),
107+ minifluxServerUrl = cursor.getString(1 ),
108+ minifluxServerTrustSelfSignedCerts = cursor.getBoolean(2 ),
109+ minifluxServerUsername = cursor.getString(3 ),
110+ minifluxServerPassword = cursor.getString(4 ),
111+ nextcloudServerUrl = cursor.getString(5 ),
112+ nextcloudServerTrustSelfSignedCerts = cursor.getBoolean(6 ),
113+ nextcloudServerUsername = cursor.getString(7 ),
114+ nextcloudServerPassword = cursor.getString(8 ),
115+ initialSyncCompleted = cursor.getBoolean(9 ),
116+ lastEntriesSyncDatetime = cursor.getString(10 ),
117+ showReadEntries = cursor.getBoolean(11 ),
118+ sortOrder = cursor.getString(12 ),
119+ showPreviewImages = cursor.getBoolean(13 ),
120+ cropPreviewImages = cursor.getBoolean(14 ),
121+ markScrolledEntriesAsRead = cursor.getBoolean(15 ),
122+ syncOnStartup = cursor.getBoolean(16 ),
123+ syncInBackground = cursor.getBoolean(17 ),
124+ backgroundSyncIntervalMillis = cursor.getLong(18 ),
125+ useBuiltInBrowser = cursor.getBoolean(19 ),
126+ showPreviewText = cursor.getBoolean(20 ),
127+ syncedOnStartup = cursor.getBoolean(21 ),
128+ )
129+ }
130+ }
131+
132+ fun Cursor.getBoolean (columnIndex : Int ): Boolean {
133+ return getInt(columnIndex) == 1
134+ }
135+
136+ fun delete () {
137+ db.delete(" conf" , " " , emptyArray())
138+ }
139+
43140 companion object {
44141 const val BACKEND_STANDALONE = " standalone"
45142 const val BACKEND_MINIFLUX = " miniflux"
@@ -50,27 +147,27 @@ class ConfRepo(
50147
51148 val DEFAULT_CONF = Conf (
52149 backend = " " ,
53- miniflux_server_url = " " ,
54- miniflux_server_trust_self_signed_certs = false ,
55- miniflux_server_username = " " ,
56- miniflux_server_password = " " ,
57- nextcloud_server_url = " " ,
58- nextcloud_server_trust_self_signed_certs = false ,
59- nextcloud_server_username = " " ,
60- nextcloud_server_password = " " ,
61- initial_sync_completed = false ,
62- last_entries_sync_datetime = " " ,
63- show_read_entries = false ,
64- sort_order = SORT_ORDER_DESCENDING ,
65- show_preview_images = true ,
66- crop_preview_images = true ,
67- mark_scrolled_entries_as_read = false ,
68- sync_on_startup = true ,
69- sync_in_background = true ,
70- background_sync_interval_millis = 10800000 ,
71- use_built_in_browser = true ,
72- show_preview_text = true ,
73- synced_on_startup = false ,
150+ minifluxServerUrl = " " ,
151+ minifluxServerTrustSelfSignedCerts = false ,
152+ minifluxServerUsername = " " ,
153+ minifluxServerPassword = " " ,
154+ nextcloudServerUrl = " " ,
155+ nextcloudServerTrustSelfSignedCerts = false ,
156+ nextcloudServerUsername = " " ,
157+ nextcloudServerPassword = " " ,
158+ initialSyncCompleted = false ,
159+ lastEntriesSyncDatetime = " " ,
160+ showReadEntries = false ,
161+ sortOrder = SORT_ORDER_DESCENDING ,
162+ showPreviewImages = true ,
163+ cropPreviewImages = true ,
164+ markScrolledEntriesAsRead = false ,
165+ syncOnStartup = true ,
166+ syncInBackground = true ,
167+ backgroundSyncIntervalMillis = 10800000 ,
168+ useBuiltInBrowser = true ,
169+ showPreviewText = true ,
170+ syncedOnStartup = false ,
74171 )
75172 }
76173}
0 commit comments