Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feed groups table to DB #442

Merged
merged 7 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2024 Sasikanth Miriyampalli
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dev.sasikanth.rss.reader.core.model.local

import kotlinx.datetime.Instant

data class FeedGroup(
val id: String,
val name: String,
val feedIds: Set<String>,
val feedIcons: Set<String>,
val createdAt: Instant,
val updatedAt: Instant,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2024 Sasikanth Miriyampalli
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dev.sasikanth.rss.reader.database

import app.cash.sqldelight.ColumnAdapter

internal object ListToStringAdapter : ColumnAdapter<List<String>, String> {

override fun decode(databaseValue: String): List<String> {
return databaseValue.split(", ")
}

override fun encode(value: List<String>): String {
return value.joinToString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import app.cash.sqldelight.db.SqlDriver
import dev.sasikanth.rss.reader.database.Bookmark
import dev.sasikanth.rss.reader.database.DateAdapter
import dev.sasikanth.rss.reader.database.Feed
import dev.sasikanth.rss.reader.database.FeedGroup
import dev.sasikanth.rss.reader.database.ListToStringAdapter
import dev.sasikanth.rss.reader.database.Post
import dev.sasikanth.rss.reader.database.ReaderDatabase
import dev.sasikanth.rss.reader.database.migrations.SQLCodeMigrations
Expand All @@ -45,6 +47,12 @@ internal interface DataComponent : SqlDriverPlatformComponent, DataStorePlatform
lastCleanUpAtAdapter = DateAdapter
),
bookmarkAdapter = Bookmark.Adapter(dateAdapter = DateAdapter),
feedGroupAdapter =
FeedGroup.Adapter(
feedIdsAdapter = ListToStringAdapter,
createdAtAdapter = DateAdapter,
updatedAtAdapter = DateAdapter
)
)
}

Expand All @@ -61,4 +69,6 @@ internal interface DataComponent : SqlDriverPlatformComponent, DataStorePlatform

@Provides
fun providesFeedSearchFTSQueries(database: ReaderDatabase) = database.feedSearchFTSQueries

@Provides fun providesFeedGroupQueries(database: ReaderDatabase) = database.feedGroupQueries
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ import app.cash.sqldelight.coroutines.asFlow
import app.cash.sqldelight.coroutines.mapToList
import app.cash.sqldelight.coroutines.mapToOne
import app.cash.sqldelight.paging3.QueryPagingSource
import com.benasher44.uuid.uuid4
import dev.sasikanth.rss.reader.core.model.local.Feed
import dev.sasikanth.rss.reader.core.model.local.FeedGroup
import dev.sasikanth.rss.reader.core.model.local.Post
import dev.sasikanth.rss.reader.core.model.local.PostWithMetadata
import dev.sasikanth.rss.reader.core.network.fetcher.FeedFetchResult
import dev.sasikanth.rss.reader.core.network.fetcher.FeedFetcher
import dev.sasikanth.rss.reader.database.BookmarkQueries
import dev.sasikanth.rss.reader.database.FeedGroupQueries
import dev.sasikanth.rss.reader.database.FeedQueries
import dev.sasikanth.rss.reader.database.FeedSearchFTSQueries
import dev.sasikanth.rss.reader.database.PostQueries
Expand All @@ -52,6 +55,7 @@ class RssRepository(
private val postSearchFTSQueries: PostSearchFTSQueries,
private val bookmarkQueries: BookmarkQueries,
private val feedSearchFTSQueries: FeedSearchFTSQueries,
private val feedGroupQueries: FeedGroupQueries,
dispatchersProvider: DispatchersProvider
) {

Expand Down Expand Up @@ -496,6 +500,60 @@ class RssRepository(
}
}

fun feedGroups(): PagingSource<Int, FeedGroup> {
return QueryPagingSource(
countQuery = feedGroupQueries.count(),
transacter = feedGroupQueries,
context = ioDispatcher,
queryProvider = { limit, offset ->
feedGroupQueries.groups(
limit = limit,
offset = offset,
mapper = {
id: String,
name: String,
feedIds: List<String>,
feedIcons: String,
createdAt: Instant,
updatedAt: Instant ->
FeedGroup(
id = id,
name = name,
feedIds = feedIds.toSet(),
feedIcons = feedIcons.split(",").toSet(),
createdAt = createdAt,
updatedAt = updatedAt
)
}
)
}
)
}

suspend fun createGroup(name: String) {
withContext(ioDispatcher) {
feedGroupQueries.createGroup(
id = uuid4().toString(),
name = name,
feedIds = emptyList(),
createdAt = Clock.System.now(),
updatedAt = Clock.System.now()
)
}
}

suspend fun updateGroupName(groupId: String, name: String) {
withContext(ioDispatcher) { feedGroupQueries.updateGroupName(name, groupId) }
}

suspend fun updateFeedIds(groupId: String, feedIds: Set<String>) {
withContext(ioDispatcher) { feedGroupQueries.updateFeedIds(feedIds.toList(), groupId) }
}

suspend fun deleteGroup(groupId: String) {
withContext(ioDispatcher) { feedGroupQueries.deleteGroup(groupId) }
}

private fun sanitizeSearchQuery(searchQuery: String): String {
return searchQuery.replace(Regex.fromLiteral("\""), "\"\"").run { "\"$this\"" }
}
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import kotlin.String;
import kotlin.collections.List;
import kotlinx.datetime.Instant;

CREATE TABLE IF NOT EXISTS feedGroup(
id TEXT NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
feedIds TEXT AS List<String> NOT NULL,
createdAt INTEGER AS Instant NOT NULL,
updatedAt INTEGER AS Instant NOT NULL
);

count:
SELECT COUNT(*) FROM feedGroup;

groups:
SELECT
id,
name,
feedIds,
(SELECT GROUP_CONCAT(feed.icon)
FROM feed
WHERE feed.id IN (feedGroup.feedIds)
LIMIT 4) AS feedIcons,
createdAt,
updatedAt
FROM feedGroup
LIMIT :limit OFFSET :offset;

createGroup:
INSERT INTO feedGroup(id, name, feedIds, createdAt, updatedAt)
VALUES (:id, :name, :feedIds, :createdAt, :updatedAt)
ON CONFLICT(name) DO NOTHING;

updateGroupName:
UPDATE feedGroup SET name = :name WHERE id = :id;

updateFeedIds:
UPDATE feedGroup SET feedIds = :feedIds WHERE id = :id;

deleteGroup:
DELETE FROM feedGroup WHERE id = :id;
11 changes: 11 additions & 0 deletions shared/src/commonMain/sqldelight/migrations/14.sqm
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import kotlin.String;
import kotlin.collections.List;
import kotlinx.datetime.Instant;

CREATE TABLE IF NOT EXISTS feedGroup(
id TEXT NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
feedIds TEXT AS List<String> NOT NULL,
createdAt INTEGER AS Instant NOT NULL,
updatedAt INTEGER AS Instant NOT NULL
);