Skip to content

Commit

Permalink
Introduce Article model
Browse files Browse the repository at this point in the history
  • Loading branch information
jocmp committed Dec 25, 2023
1 parent d035a1c commit 1c80a80
Show file tree
Hide file tree
Showing 22 changed files with 177 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import com.jocmp.basil.Account
import com.jocmp.basil.AccountManager
import com.jocmp.basil.Article
import com.jocmp.basil.Feed
import com.jocmp.basil.FeedFormEntry
import com.jocmp.basil.Folder
import com.jocmp.basil.db.Articles
import com.jocmp.basil.extensions.feedPagingSource
import com.jocmp.basil.feedPagingSource
import com.jocmp.basilreader.selectAccount
import com.jocmp.basilreader.selectedAccount
import kotlinx.coroutines.flow.first
Expand Down Expand Up @@ -52,17 +52,17 @@ class AccountViewModel(

private val feedID = mutableStateOf<String?>(null)

private val articleState = mutableStateOf<Articles?>(null)
private val articleState = mutableStateOf<Article?>(null)

private val pager = mutableStateOf<Pager<Int, Articles>?>(null)
private val pager = mutableStateOf<Pager<Int, Article>?>(null)

val feeds: List<Feed>
get() = account?.feeds?.toList() ?: emptyList()

val article: Articles?
val article: Article?
get() = articleState.value

fun articles(): Pager<Int, Articles>? = pager.value
fun articles(): Pager<Int, Article>? = pager.value

fun selectFeed(feedID: String, onComplete: () -> Unit) {
this.feedID.value = feedID
Expand All @@ -73,8 +73,8 @@ class AccountViewModel(
onComplete()
}

suspend fun selectArticle(articleID: Long) {
articleState.value = account?.findArticle(articleID, feedID.value?.toLongOrNull())
suspend fun selectArticle(articleID: String) {
articleState.value = account?.findArticle(articleID.toLong(), feedID.value?.toLongOrNull())
}

fun clearArticle() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.paging.Pager
import androidx.paging.compose.collectAsLazyPagingItems
import com.jocmp.basil.db.Articles
import com.jocmp.basil.Article
import kotlinx.coroutines.launch

@Composable
fun ArticleList(
pager: Pager<Int, Articles>,
onSelect: suspend (articleID: Long) -> Unit,
pager: Pager<Int, Article>,
onSelect: suspend (articleID: String) -> Unit,
) {
val composableScope = rememberCoroutineScope()
val lazyPagingItems = pager.flow.collectAsLazyPagingItems()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package com.jocmp.basilreader.ui.articles

import androidx.activity.compose.BackHandler
import androidx.compose.runtime.Composable
import com.jocmp.basil.db.Articles
import com.jocmp.basil.Article
import com.jocmp.basilreader.ui.components.WebView
import com.jocmp.basilreader.ui.components.rememberWebViewStateWithHTMLData

@Composable
fun ArticleView(
article: Articles?,
article: Article?,
onBackPressed: () -> Unit
) {
val state = rememberWebViewStateWithHTMLData(article?.content_html ?: "<div />")
val state = rememberWebViewStateWithHTMLData(article?.contentHTML ?: "<div />")

WebView(state)

Expand Down
31 changes: 18 additions & 13 deletions basil/src/main/java/com/jocmp/basil/Account.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package com.jocmp.basil

import com.jocmp.basil.accounts.AccountDelegate
import com.jocmp.basil.accounts.LocalAccountDelegate
import com.jocmp.basil.articles.articleMapper
import com.jocmp.basil.db.Articles
import com.jocmp.basil.db.Database
import com.jocmp.basil.extensions.asFeed
import com.jocmp.basil.extensions.asFolder
import com.jocmp.basil.extensions.feeds
import com.jocmp.basil.extensions.findOrCreate
import com.jocmp.basil.feeds.FeedRecords
import com.jocmp.basil.opml.Outline
import com.jocmp.basil.opml.asFeed
import com.jocmp.basil.opml.asFolder
import com.jocmp.feedfinder.FeedFinder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
Expand Down Expand Up @@ -46,6 +46,12 @@ data class Account(
loadOPML(opmlFile.load())
}

val flattenedFeeds: Set<Feed>
get() = mutableSetOf<Feed>().apply {
addAll(feeds)
addAll(folders.flatMap { it.feeds })
}

suspend fun addFolder(title: String): Folder {
val folder = Folder(title = title)

Expand All @@ -67,26 +73,24 @@ data class Account(

val externalFeed = delegate.createFeed(feedURL = found.feedURL)

val record = database.feeds.findOrCreate(
externalFeed = externalFeed,
feedURL = found.feedURL
)
val record = FeedRecords(database).findOrCreate(externalFeed)

val feed = Feed(
id = record.id.toString(),
externalID = externalFeed.externalID,
name = entryNameOrDefault(entry, found.name),
feedURL = found.feedURL.toString(),
feedURL = externalFeed.feedURL,
siteURL = entrySiteURL(found.siteURL)
)

coroutineScope {
launch {
val items = delegate.fetchAll(feed)
val feedID = feed.id.toLong()

items.forEach { item ->
database.articlesQueries.create(
feed_id = record.id,
feed_id = feedID,
external_id = item.externalID,
title = item.title,
content_html = item.contentHTML,
Expand Down Expand Up @@ -120,12 +124,13 @@ data class Account(
return feed
}

suspend fun findArticle(articleID: Long?, feedID: Long?): Articles? {
suspend fun findArticle(articleID: Long?, feedID: Long?): Article? {
articleID ?: return null

return database.articlesQueries.findBy(
articleID = articleID,
feedID = feedID
feedID = feedID,
mapper = ::articleMapper
).executeAsOneOrNull()
}

Expand Down Expand Up @@ -156,7 +161,7 @@ data class Account(
}

val dbFeeds = database
.feeds
.feedsQueries
.allByID(ids)
.executeAsList()
.associateBy { it.id }
Expand Down
14 changes: 14 additions & 0 deletions basil/src/main/java/com/jocmp/basil/Article.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.jocmp.basil

import java.net.URL

data class Article(
val id: String,
val externalID: String,
val feedID: String,
val title: String,
val contentHTML: String,
val url: URL?,
val summary: String,
val imageURL: URL?
)
28 changes: 28 additions & 0 deletions basil/src/main/java/com/jocmp/basil/ArticlesPagerExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.jocmp.basil

import androidx.paging.Pager
import androidx.paging.PagingConfig
import app.cash.sqldelight.paging3.QueryPagingSource
import com.jocmp.basil.articles.articleMapper
import kotlinx.coroutines.Dispatchers

fun Account.feedPagingSource(feedID: String?): Pager<Int, Article> {
return Pager(
config = PagingConfig(pageSize = 10),
pagingSourceFactory = {
QueryPagingSource(
countQuery = database.articlesQueries.countByFeed(feedID?.toLongOrNull()),
transacter = database.articlesQueries,
context = Dispatchers.IO,
queryProvider = { limit, offset ->
database.articlesQueries.allByFeed(
feedID = feedID?.toLongOrNull(),
limit = limit,
offset = offset,
mapper = ::articleMapper
)
}
)
}
)
}
2 changes: 1 addition & 1 deletion basil/src/main/java/com/jocmp/basil/Feed.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.jocmp.basil

import com.jocmp.basil.extensions.prepending
import com.jocmp.basil.shared.prepending

data class Feed(
val id: String,
Expand Down
4 changes: 2 additions & 2 deletions basil/src/main/java/com/jocmp/basil/Folder.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.jocmp.basil

import com.jocmp.basil.extensions.prepending
import com.jocmp.basil.extensions.repeatTab
import com.jocmp.basil.shared.prepending
import com.jocmp.basil.shared.repeatTab

data class Folder(
val title: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jocmp.basil.accounts

import com.jocmp.basil.Feed
import com.jocmp.basil.feeds.ExternalFeed
import java.net.URL

internal interface AccountDelegate {
Expand Down
3 changes: 0 additions & 3 deletions basil/src/main/java/com/jocmp/basil/accounts/ExternalFeed.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,26 @@ package com.jocmp.basil.accounts

import com.jocmp.basil.Account
import com.jocmp.basil.Feed
import com.jocmp.basil.feeds.ExternalFeed
import com.prof18.rssparser.RssParser
import com.prof18.rssparser.model.RssItem
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.withContext
import java.net.URL
import java.util.UUID

internal class LocalAccountDelegate(private val account: Account) : AccountDelegate {
override suspend fun createFeed(feedURL: URL): ExternalFeed {
val allFeeds = mutableSetOf<Feed>().apply {
addAll(account.feeds)
addAll(account.folders.flatMap { it.feeds })
}

val existingFeed = allFeeds.find { it.feedURL == feedURL.toString() }
val existingFeed = account.flattenedFeeds.find { it.feedURL == feedURL.toString() }

if (existingFeed != null) {
return ExternalFeed(externalID = existingFeed.externalID)
return ExternalFeed(
externalID = existingFeed.externalID,
feedURL = feedURL.toString()
)
}

return ExternalFeed(externalID = feedURL.toString())
return ExternalFeed(
externalID = feedURL.toString(),
feedURL = feedURL.toString()
)
}

override suspend fun fetchAll(feed: Feed): List<ParsedItem> {
Expand Down
27 changes: 27 additions & 0 deletions basil/src/main/java/com/jocmp/basil/articles/ArticleMapper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.jocmp.basil.articles

import com.jocmp.basil.Article
import com.jocmp.basil.shared.optionalURL

internal fun articleMapper(
id: Long,
externalID: String,
mapperFeedID: Long?,
title: String?,
contentHtml: String?,
url: String?,
summary: String?,
imageURL: String?,
datePublished: Long?
): Article {
return Article(
id = id.toString(),
externalID = externalID,
feedID = mapperFeedID.toString(),
title = title ?: "",
contentHTML = contentHtml ?: "",
url = optionalURL(url),
imageURL = optionalURL(imageURL),
summary = summary ?: ""
)
}
28 changes: 0 additions & 28 deletions basil/src/main/java/com/jocmp/basil/extensions/ArticlesPagerExt.kt

This file was deleted.

23 changes: 0 additions & 23 deletions basil/src/main/java/com/jocmp/basil/extensions/FeedsQueriesExt.kt

This file was deleted.

This file was deleted.

6 changes: 6 additions & 0 deletions basil/src/main/java/com/jocmp/basil/feeds/ExternalFeed.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.jocmp.basil.feeds

internal data class ExternalFeed(
val externalID: String,
val feedURL: String
)
Loading

0 comments on commit 1c80a80

Please sign in to comment.