Skip to content

Commit

Permalink
Remove local feed
Browse files Browse the repository at this point in the history
  • Loading branch information
jocmp committed Jan 3, 2024
1 parent aefc053 commit bfedbbe
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,30 @@ class AccountViewModel(
pager.value = account.buildPager(nextFilter)
}

fun selectFeed(feedID: String, onComplete: () -> Unit) {
fun selectFeed(feedID: String) {
val feed = account.findFeed(feedID) ?: return
val feedFilter = ArticleFilter.Feeds(feed = feed, status = filter.value.status)

selectFilter(feedFilter, onComplete)
selectFilter(feedFilter)
}

fun selectFolder(title: String, onComplete: () -> Unit) {
fun selectFolder(title: String) {
val folder = account.findFolder(title) ?: return
val feedFilter = ArticleFilter.Folders(folder = folder, status = filter.value.status)

selectFilter(feedFilter, onComplete)
selectFilter(feedFilter)
}

fun removeFeed() {
viewModelScope.launch {
val currentFilter = filter.value

if (currentFilter is ArticleFilter.Feeds) {
account.removeFeed(feedID = currentFilter.feed.id)
selectFilter(ArticleFilter.default().copy(currentFilter.status))
accountState.value = account
}
}
}

suspend fun refreshFeed() {
Expand Down Expand Up @@ -130,13 +142,11 @@ class AccountViewModel(
articleState.value = null
}

private fun selectFilter(nextFilter: ArticleFilter, onComplete: () -> Unit) {
private fun selectFilter(nextFilter: ArticleFilter) {
filter.value = nextFilter
pager.value = account.buildPager(nextFilter)

clearArticle()

onComplete()
}

// private fun selectSettingsAccount(accountID: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fun ArticleFilterNavigationBar(
)
},
label = { Text(stringResource(id = R.string.article_filters_starred)) },
selected = selected === ArticleStatus.STARRED,
selected = selected == ArticleStatus.STARRED,
onClick = { checkedSelect(ArticleStatus.STARRED) },
alwaysShowLabel = false
)
Expand All @@ -45,7 +45,7 @@ fun ArticleFilterNavigationBar(
)
},
label = { Text(stringResource(R.string.article_filters_unread)) },
selected = selected === ArticleStatus.UNREAD,
selected = selected == ArticleStatus.UNREAD,
onClick = { checkedSelect(ArticleStatus.UNREAD) },
alwaysShowLabel = false
)
Expand All @@ -57,7 +57,7 @@ fun ArticleFilterNavigationBar(
)
},
label = { Text(stringResource(R.string.article_filters_all)) },
selected = selected === ArticleStatus.ALL,
selected = selected == ArticleStatus.ALL,
onClick = { checkedSelect(ArticleStatus.ALL) },
alwaysShowLabel = false
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package com.jocmp.basilreader.ui.articles

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Button
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.IconButton
import androidx.compose.material.TopAppBar
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
Expand All @@ -24,7 +25,6 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.jocmp.basilreader.ui.accounts.AccountViewModel
import kotlinx.coroutines.launch
import org.koin.androidx.compose.koinViewModel
Expand Down Expand Up @@ -74,18 +74,38 @@ fun ArticleScreen(
folders = viewModel.folders,
feeds = viewModel.feeds,
onFeedAdd = onFeedAdd,
onFolderSelect = { viewModel.selectFolder(it) { onComplete() } },
onFeedSelect = { viewModel.selectFeed(it) { onComplete() } }
onSelectFolder = {
viewModel.selectFolder(it)
onComplete()
},
onSelectFeed = {
viewModel.selectFeed(it)
onComplete()
},
)
},
listPane = {
Scaffold(
topBar = {
TopAppBar {
Button(onClick = { coroutineScope.launch { drawerState.open() } }) {
Icon(imageVector = Icons.Filled.Menu, contentDescription = null)
TopAppBar(
title = {},
navigationIcon = {
IconButton(onClick = { coroutineScope.launch { drawerState.open() } }) {
Icon(
imageVector = Icons.Filled.Menu,
contentDescription = null
)
}
},
actions = {
IconButton(onClick = { viewModel.removeFeed() }) {
Icon(
imageVector = Icons.Filled.Delete,
contentDescription = null
)
}
}
}
)
},
bottomBar = {
ArticleFilterNavigationBar(
Expand Down
14 changes: 7 additions & 7 deletions app/src/main/java/com/jocmp/basilreader/ui/articles/FeedList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ fun FeedList(
folders: List<Folder> = emptyList(),
feeds: List<Feed> = emptyList(),
onFeedAdd: () -> Unit,
onFolderSelect: (folderTitle: String) -> Unit,
onFeedSelect: (feedID: String) -> Unit
onSelectFolder: (folderTitle: String) -> Unit,
onSelectFeed: (feedID: String) -> Unit,
) {
Column {
Row {
Expand All @@ -33,8 +33,8 @@ fun FeedList(
folders.forEach {
FolderRow(
folder = it,
onFolderSelect = onFolderSelect,
onFeedSelect = onFeedSelect
onFolderSelect = onSelectFolder,
onFeedSelect = onSelectFeed,
)
}
if (feeds.isNotEmpty()) {
Expand All @@ -44,7 +44,7 @@ fun FeedList(
feeds.forEach {
FeedRow(
feed = it,
onSelect = onFeedSelect
onSelect = onSelectFeed
)
}
}
Expand All @@ -61,7 +61,7 @@ fun FeedListPreview() {
folders = folders,
feeds = feeds,
onFeedAdd = {},
onFolderSelect = {},
onFeedSelect = {}
onSelectFolder = {},
onSelectFeed = {}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.jocmp.basilreader.ui.articles

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand All @@ -25,13 +26,14 @@ import com.jocmp.basilreader.ui.theme.BasilReaderTheme
@Composable
fun FeedRow(
feed: Feed,
onSelect: (id: String) -> Unit
onSelect: (id: String) -> Unit,
) {
NavigationDrawerItem(
label = {
Row(
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier.fillMaxWidth()
modifier = Modifier
.fillMaxWidth()
) {
Text(feed.name)
Text(feed.unreadCount.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import com.jocmp.basilreader.ui.theme.BasilReaderTheme
fun FolderRow(
folder: Folder,
onFolderSelect: (folderTitle: String) -> Unit,
onFeedSelect: (feedID: String) -> Unit
onFeedSelect: (feedID: String) -> Unit,
) {
Column {
Row(
Expand All @@ -39,7 +39,10 @@ fun FolderRow(
}
folder.feeds.forEach { feed ->
Row {
FeedRow(feed = feed, onSelect = onFeedSelect)
FeedRow(
feed = feed,
onSelect = onFeedSelect,
)
}
}
}
Expand All @@ -54,7 +57,7 @@ fun FolderRowPreview() {
FolderRow(
folder = folder,
onFolderSelect = {},
onFeedSelect = {}
onFeedSelect = {},
)
}
}
30 changes: 22 additions & 8 deletions basil/src/main/java/com/jocmp/basil/Account.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.jocmp.basil.opml.asFeed
import com.jocmp.basil.opml.asFolder
import com.jocmp.basil.persistence.ArticleRecords
import com.jocmp.basil.persistence.FeedRecords
import com.jocmp.basil.persistence.articleMapper
import com.jocmp.basil.shared.nowUTCInSeconds
import com.jocmp.feedfinder.FeedFinder
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -45,7 +44,8 @@ data class Account(
account = this,
)

internal val articles: ArticleRecords = ArticleRecords(database)
internal val articleRecords: ArticleRecords = ArticleRecords(database)
private val feedRecords: FeedRecords = FeedRecords(database)

val displayName = "Local"

Expand All @@ -69,6 +69,20 @@ data class Account(
return folder
}

suspend fun removeFeed(feedID: String) {
feedRecords.deleteFeed(feedID = feedID)

feeds.removeIf { it.id == feedID }

folders.forEach { folder ->
folder.feeds.removeIf { it.id == feedID }
}

folders.removeIf { it.feeds.isEmpty() }

saveOPMLFile()
}

suspend fun addFeed(entry: FeedFormEntry): Result<Feed> {
val result = FeedFinder.find(feedURL = entry.url)

Expand All @@ -80,7 +94,7 @@ data class Account(

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

val record = FeedRecords(database).findOrCreate(externalFeed)
val record = feedRecords.findOrCreate(externalFeed)

val feed = Feed(
id = record.id.toString(),
Expand Down Expand Up @@ -143,23 +157,23 @@ data class Account(
}

fun findArticle(articleID: String): Article? {
return articles.fetch(articleID = articleID)
return articleRecords.fetch(articleID = articleID)
}

fun addStar(articleID: String) {
articles.addStar(articleID = articleID)
articleRecords.addStar(articleID = articleID)
}

fun removeStar(articleID: String) {
articles.removeStar(articleID = articleID)
articleRecords.removeStar(articleID = articleID)
}

fun markRead(articleID: String) {
articles.markRead(articleID)
articleRecords.markRead(articleID)
}

fun markUnread(articleID: String) {
articles.markUnread(articleID = articleID)
articleRecords.markUnread(articleID = articleID)
}

private fun updateArticles(feed: Feed, items: List<ParsedItem>) {
Expand Down
4 changes: 1 addition & 3 deletions basil/src/main/java/com/jocmp/basil/AccountUnreadExt.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package com.jocmp.basil

import com.jocmp.basil.persistence.ArticleRecords

val Account.unreadCounts: Map<String, Long>
get() = articles.countUnread()
get() = articleRecords.countUnread()
2 changes: 1 addition & 1 deletion basil/src/main/java/com/jocmp/basil/opml/OPMLHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ internal class OPMLHandler : DefaultHandler() {
}
currentFolder = null
currentType = null
} else if (currentType === OutlineType.FEED && currentFolder != null) {
} else if (currentType == OutlineType.FEED && currentFolder != null) {
currentType = OutlineType.FOLDER
} else {
currentType = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ internal class FeedRecords(val database: Database) {
feed_url = externalFeed.feedURL
).executeAsOne()
}

internal fun deleteFeed(feedID: String) {
database.feedsQueries.delete(feedID = feedID.toLong())
}
}
8 changes: 8 additions & 0 deletions basil/src/main/sqldelight/com/jocmp/basil/db/feeds.sq
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ SELECT *
FROM feeds
WHERE id = last_insert_rowid();
}

delete {
DELETE FROM article_statuses WHERE article_statuses.feed_id = :feedID;

DELETE FROM articles WHERE articles.feed_id = :feedID;

DELETE FROM feeds WHERE id = :feedID;
}
24 changes: 23 additions & 1 deletion basil/src/test/java/com/jocmp/basil/AccountTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class AccountTest {
database = InMemoryDatabaseProvider.forAccount("777")
}

private fun buildAccount(id: String, path: File): Account {
private fun buildAccount(id: String = "777", path: File = folder.newFile()): Account {
return Account(
id = id,
path = path.toURI(),
Expand Down Expand Up @@ -166,6 +166,28 @@ class AccountTest {
assertEquals(techFeed, cultureFeed)
}

@Test
fun removeFeed_topLevelFeed() {
val account = buildAccount()
runBlocking {
account.addFeed(
FeedFormEntry(
url = "https://theverge.com/rss/index.xml",
name = "The Verge",
folderTitles = listOf(),
)
)
}

val feed = account.feeds.find { it.name == "The Verge" }!!

assertEquals(expected = 1, account.feeds.size)

runBlocking { account.removeFeed(feedID = feed.id) }

assertEquals(expected = 0, account.feeds.size)
}

@Test
fun findFeed_topLevelFeed() {
val account = buildAccount(id = "777", path = folder.newFile())
Expand Down

0 comments on commit bfedbbe

Please sign in to comment.