Skip to content

Commit 4151011

Browse files
committed
Extract queries (WIP)
1 parent 354dc97 commit 4151011

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

app/src/main/kotlin/api/standalone/StandaloneApi.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import db.Entry
1717
import db.EntryWithoutContent
1818
import db.Feed
1919
import db.Link
20+
import feeds.FeedQueries
2021
import http.await
2122
import kotlinx.coroutines.Deferred
2223
import kotlinx.coroutines.Dispatchers
@@ -125,7 +126,7 @@ class StandaloneNewsApi(
125126
}
126127

127128
override suspend fun getFeeds(): Result<List<Feed>> {
128-
return runCatching { db.feedQueries.selectAll().asFlow().mapToList().first() }
129+
return runCatching { FeedQueries(db).selectAll() }
129130
}
130131

131132
override suspend fun updateFeedTitle(feedId: String, newTitle: String): Result<Unit> {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package db
2+
3+
import android.database.Cursor
4+
5+
public fun Cursor.getNullableBoolean(columnIndex: Int): Boolean? {
6+
if (isNull(columnIndex)) {
7+
return null
8+
} else {
9+
return getInt(columnIndex) == 1
10+
}
11+
}

app/src/main/kotlin/db/Feed.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ data class Feed(
44
val id: String,
55
val links: List<Link>,
66
val title: String,
7-
val extOpenEntriesInBrowser: Boolean,
7+
val extOpenEntriesInBrowser: Boolean?,
88
val extBlockedWords: String,
99
val extShowPreviewImages: Boolean?,
1010
)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package feeds
2+
3+
import android.database.Cursor
4+
import android.database.sqlite.SQLiteDatabase
5+
import db.Feed
6+
import db.Link
7+
import db.getNullableBoolean
8+
9+
class FeedQueries(private val database: SQLiteDatabase) {
10+
companion object {
11+
private val PROJECTION_FULL = arrayOf(
12+
"id",
13+
"links",
14+
"title",
15+
"ext_open_entries_in_browser",
16+
"ext_blocked_words",
17+
"ext_show_preview_images",
18+
)
19+
}
20+
21+
fun selectAll(): List<Feed> {
22+
val res = mutableListOf<Feed>()
23+
val cursor = database.query("feed", PROJECTION_FULL, "", emptyArray(), "", "", "", "")
24+
while (cursor.moveToNext()) {
25+
res += Feed(
26+
id = cursor.getString(0),
27+
links = cursor.getLinks(1),
28+
title = cursor.getString(2),
29+
extOpenEntriesInBrowser = cursor.getNullableBoolean(3),
30+
extBlockedWords = cursor.getString(4),
31+
extShowPreviewImages = cursor.getNullableBoolean(5),
32+
)
33+
}
34+
return res
35+
}
36+
37+
private fun Cursor.getLinks(columnIndex: Int): List<Link> {
38+
return emptyList() // TODO
39+
}
40+
}

0 commit comments

Comments
 (0)