Skip to content

Commit

Permalink
Consistently key by Long feed ID
Browse files Browse the repository at this point in the history
  • Loading branch information
jocmp committed Dec 19, 2023
1 parent 9f40c03 commit 029cd06
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.jocmp.basilreader.ui.fixtures

import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import com.jocmp.basil.Feed
import com.jocmp.basil.RandomUUID
import java.util.UUID
import kotlin.random.Random

Expand All @@ -11,12 +12,14 @@ class FeedPreviewFixture : PreviewParameterProvider<Feed> {
private fun feeds(): Sequence<Feed> {
return sequenceOf(
Feed(
id = UUID.randomUUID().toString(),
id = RandomUUID.generate(),
externalID = RandomUUID.generate(),
name = "GamersNexus",
feedURL = "https://gamersnexus.net/rss.xml"
),
Feed(
id = UUID.randomUUID().toString(),
id = RandomUUID.generate(),
externalID = RandomUUID.generate(),
name = "9to5Google",
feedURL = "https://9to5google.com/feed/"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import com.jocmp.basil.Account
import com.jocmp.basil.Feed
import com.jocmp.basil.Folder
import com.jocmp.basil.RandomUUID
import java.util.UUID
import kotlin.random.Random

Expand All @@ -16,12 +17,14 @@ class FolderPreviewFixture : PreviewParameterProvider<Folder> {
title = "Tech",
feeds = mutableListOf(
Feed(
id = UUID.randomUUID().toString(),
id = RandomUUID.generate(),
externalID = RandomUUID.generate(),
name = "The Verge",
feedURL = "https://www.theverge.com/rss/index.xml"
),
Feed(
id = UUID.randomUUID().toString(),
id = RandomUUID.generate(),
externalID = RandomUUID.generate(),
name = "Ars Technica",
feedURL = "https://arstechnica.com/feed/"
)
Expand All @@ -31,12 +34,14 @@ class FolderPreviewFixture : PreviewParameterProvider<Folder> {
title = "Programming",
feeds = mutableListOf(
Feed(
id = UUID.randomUUID().toString(),
id = RandomUUID.generate(),
externalID = RandomUUID.generate(),
name = "Android Weekly",
feedURL = ""
),
Feed(
id = UUID.randomUUID().toString(),
id = RandomUUID.generate(),
externalID = RandomUUID.generate(),
name = "Ruby Weekly",
feedURL = ""
),
Expand Down
21 changes: 10 additions & 11 deletions basil/src/main/java/com/jocmp/basil/Account.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.jocmp.basil

import com.jocmp.basil.accounts.AccountDelegate
import com.jocmp.basil.accounts.ExternalFeed
import com.jocmp.basil.accounts.LocalAccountDelegate
import com.jocmp.basil.accounts.ParsedItem
import com.jocmp.basil.db.Database
import com.jocmp.basil.extensions.asFeed
import com.jocmp.basil.extensions.asFolder
Expand All @@ -13,7 +11,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.jetbrains.annotations.VisibleForTesting
import java.net.URI
import java.net.URL

Expand Down Expand Up @@ -73,7 +70,8 @@ data class Account(
)

val feed = Feed(
id = externalFeed.externalID,
id = record.id.toString(),
externalID = externalFeed.externalID,
name = entryNameOrDefault(entry, found.name),
feedURL = found.feedURL.toString(),
siteURL = entrySiteURL(found.siteURL)
Expand Down Expand Up @@ -136,19 +134,20 @@ data class Account(
}

private fun loadOPML(items: List<Outline>) {
val externalIDs = mutableListOf<String>()
val ids = mutableListOf<Long>()

items.forEach {
when (it) {
is Outline.FeedOutline -> it.feed.externalID?.let { id -> externalIDs.add(id) }
is Outline.FolderOutline -> externalIDs.addAll(it.folder.feeds.mapNotNull { feed -> feed.externalID })
is Outline.FeedOutline -> it.feed.id?.toLongOrNull()?.let { id -> ids.add(id) }
is Outline.FolderOutline -> ids.addAll(it.folder.feeds.mapNotNull { feed -> feed.id?.toLongOrNull() })
}
}

val dbFeeds =
database.feedsQueries.allByExternalID(externalIDs).executeAsList().associateBy {
it.external_id
}
val dbFeeds = database
.feeds
.allByID(ids)
.executeAsList()
.associateBy { it.id }

items.forEach { item ->
when (item) {
Expand Down
1 change: 1 addition & 0 deletions basil/src/main/java/com/jocmp/basil/Feed.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.jocmp.basil.extensions.prepending

data class Feed(
val id: String,
val externalID: String,
val name: String,
val feedURL: String,
val siteURL: String = ""
Expand Down
4 changes: 2 additions & 2 deletions basil/src/main/java/com/jocmp/basil/FeedsQueriesExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal fun FeedsQueries.findOrCreate(externalFeed: ExternalFeed, feedURL: URL)
}

return create(
externalFeed.externalID,
feedURL.toString()
external_id = externalFeed.externalID,
feed_url = feedURL.toString()
).executeAsOne()
}
9 changes: 9 additions & 0 deletions basil/src/main/java/com/jocmp/basil/RandomUUID.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.jocmp.basil

import java.util.UUID as JavaUUID

object RandomUUID {
fun generate(): String {
return JavaUUID.randomUUID().toString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ internal class LocalAccountDelegate(private val account: Account) : AccountDeleg
val existingFeed = allFeeds.find { it.feedURL == feedURL.toString() }

if (existingFeed != null) {
return ExternalFeed(externalID = existingFeed.id)
return ExternalFeed(externalID = existingFeed.externalID)
}

return ExternalFeed(externalID = UUID.randomUUID().toString())
return ExternalFeed(externalID = feedURL.toString())
}

override suspend fun fetchAll(feed: Feed): List<ParsedItem> {
Expand Down
11 changes: 5 additions & 6 deletions basil/src/main/java/com/jocmp/basil/extensions/FeedOutlineExt.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package com.jocmp.basil.extensions

import com.jocmp.basil.Feed
import java.security.SecureRandom
import kotlin.random.Random
import com.jocmp.basil.db.Feeds as DBFeed
import com.jocmp.basil.opml.Feed as OPMLFeed

internal fun OPMLFeed.asFeed(feeds: Map<String, DBFeed>): Feed? {
externalID ?: return null
val feed = feeds[externalID]
internal fun OPMLFeed.asFeed(feeds: Map<Long, DBFeed>): Feed? {
val parsedID = id?.toLongOrNull() ?: return null
val feed = feeds[parsedID]

feed ?: return null

return Feed(
id = feed.external_id,
id = feed.id.toString(),
externalID = feed.external_id,
name = title ?: "",
feedURL = xmlUrl ?: ""
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.jocmp.basil.Folder
import com.jocmp.basil.opml.Outline
import com.jocmp.basil.db.Feeds as DBFeed

internal fun Outline.FolderOutline.asFolder(feeds: Map<String, DBFeed>): Folder {
internal fun Outline.FolderOutline.asFolder(feeds: Map<Long, DBFeed>): Folder {
return Folder(
title = folder.title ?: "",
feeds = folder.feeds.mapNotNull {
Expand Down
2 changes: 1 addition & 1 deletion basil/src/main/java/com/jocmp/basil/opml/Feed.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.jocmp.basil.opml

internal data class Feed(
val externalID: String? = null,
val id: String? = null,
val title: String? = null,
val text: String? = null,
val htmlUrl: String? = null,
Expand Down
4 changes: 2 additions & 2 deletions basil/src/main/java/com/jocmp/basil/opml/OPMLHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal class OPMLHandler : DefaultHandler() {
currentFolder?.also { folder ->
folder.feeds.add(
Feed(
externalID = attributes.getValue("basil_id"),
id = attributes.getValue("basil_id"),
title = attributes.getValue("title"),
text = attributes.getValue("text"),
htmlUrl = attributes.getValue("htmlUrl"),
Expand All @@ -50,7 +50,7 @@ internal class OPMLHandler : DefaultHandler() {
opmlDocument.outlines.add(
Outline.FeedOutline(
Feed(
externalID = attributes.getValue("basil_id"),
id = attributes.getValue("basil_id"),
title = attributes.getValue("title"),
text = attributes.getValue("text"),
htmlUrl = attributes.getValue("htmlUrl"),
Expand Down
8 changes: 6 additions & 2 deletions basil/src/main/sqldelight/com/jocmp/basil/db/feeds.sq
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ CREATE TABLE feeds (
feed_url TEXT NOT NULL UNIQUE
);

allByExternalID:
all:
SELECT *
FROM feeds;

allByID:
SELECT *
FROM feeds
WHERE external_id IN ?;
WHERE id IN ?;

findByExternalID:
SELECT *
Expand Down
6 changes: 6 additions & 0 deletions basil/src/test/java/com/jocmp/basil/OPMLFileTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ class OPMLFileTest {
feeds = mutableListOf(
Feed(
id = "1",
externalID = RandomUUID.generate(),
name = "The Verge",
feedURL = "https://www.theverge.com/rss/index.xml"
),
Feed(
id = "2",
externalID = RandomUUID.generate(),
name = "Ars Technica",
feedURL = "https://feeds.arstechnica.com/arstechnica/index"
)
Expand All @@ -43,11 +45,13 @@ class OPMLFileTest {
feeds = mutableListOf(
Feed(
id = "3",
externalID = RandomUUID.generate(),
name = "De Programmatica Ipsum",
feedURL = "https://deprogrammaticaipsum.com/feed"
),
Feed(
id = "4",
externalID = RandomUUID.generate(),
name = "Ruby Weekly",
feedURL = "https://cprss.s3.amazonaws.com/rubyweekly.com.xml"
),
Expand All @@ -60,11 +64,13 @@ class OPMLFileTest {
listOf(
Feed(
id = "5",
externalID = RandomUUID.generate(),
name = "GamersNexus",
feedURL = "https://gamersnexus.net/rss.xml"
),
Feed(
id = "6",
externalID = RandomUUID.generate(),
name = "9to5Google",
feedURL = "https://9to5google.com/feed"
)
Expand Down

0 comments on commit 029cd06

Please sign in to comment.