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

Avoid adding feed if it's not a valid rss feed #17

Merged
merged 2 commits into from
Aug 31, 2023
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
Expand Up @@ -36,6 +36,7 @@ fun AddFeedScreen(

val context = LocalContext.current
val isAddDone by viewModel.isAddDoneState.collectAsStateWithLifecycle()
val isInvalidUrl by viewModel.isInvalidRssFeed.collectAsStateWithLifecycle()

if (isAddDone) {
feedName = ""
Expand All @@ -48,6 +49,7 @@ fun AddFeedScreen(
AddFeedScreenContent(
feedName = feedName,
feedUrl = feedUrl,
isInvalidUrl = isInvalidUrl,
onFeedNameUpdated = { name ->
feedName = name
viewModel.updateFeedNameTextFieldValue(name)
Expand All @@ -68,6 +70,7 @@ fun AddFeedScreen(
private fun AddFeedScreenContent(
feedName: String,
feedUrl: String,
isInvalidUrl: Boolean,
onFeedNameUpdated: (String) -> Unit,
onFeedUrlUpdated: (String) -> Unit,
addFeed: () -> Unit,
Expand Down Expand Up @@ -99,6 +102,7 @@ private fun AddFeedScreenContent(
feedName = feedName,
onFeedNameUpdated = onFeedNameUpdated,
feedUrl = feedUrl,
isInvalidUrl = isInvalidUrl,
onFeedUrlUpdated = onFeedUrlUpdated,
addFeed = addFeed,
)
Expand All @@ -115,6 +119,23 @@ private fun AddScreenContentPreview() {
onFeedNameUpdated = {},
onFeedUrlUpdated = {},
addFeed = { },
isInvalidUrl = false,
navigateBack = {},
)
}
}

@FeedFlowPreview
@Composable
private fun AddScreenContentInvalidUrlPreview() {
FeedFlowTheme {
AddFeedScreenContent(
feedName = "My Feed",
feedUrl = "https://www.ablog.com/feed",
isInvalidUrl = true,
onFeedNameUpdated = {},
onFeedUrlUpdated = {},
addFeed = { },
navigateBack = {},
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ import kotlinx.coroutines.launch
import org.koin.androidx.compose.koinViewModel
import org.koin.compose.koinInject

@OptIn(ExperimentalMaterialApi::class)
@Suppress("LongMethod")
@Composable
internal fun HomeScreen(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.rememberScrollbarAdapter
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.prof18.feedflow.addfeed

import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
Expand All @@ -25,6 +24,7 @@ fun AddFeedScreen(
val viewModel = desktopViewModel { DI.koin.get<AddFeedViewModel>() }

val isAddDone by viewModel.isAddDoneState.collectAsState()
val isInvalidUrl by viewModel.isInvalidRssFeed.collectAsState()

if (isAddDone) {
feedName = ""
Expand All @@ -35,6 +35,7 @@ fun AddFeedScreen(
AddFeedScreenContent(
feedName = feedName,
feedUrl = feedUrl,
isInvalidUrl = isInvalidUrl,
onFeedNameUpdated = { name ->
feedName = name
viewModel.updateFeedNameTextFieldValue(name)
Expand All @@ -50,10 +51,10 @@ fun AddFeedScreen(
}

@Composable
@OptIn(ExperimentalMaterial3Api::class)
private fun AddFeedScreenContent(
feedName: String,
feedUrl: String,
isInvalidUrl: Boolean,
onFeedNameUpdated: (String) -> Unit,
onFeedUrlUpdated: (String) -> Unit,
addFeed: () -> Unit,
Expand All @@ -62,6 +63,7 @@ private fun AddFeedScreenContent(
AddFeedsContent(
paddingValues = paddingValues,
feedName = feedName,
isInvalidUrl = isInvalidUrl,
onFeedNameUpdated = onFeedNameUpdated,
feedUrl = feedUrl,
onFeedUrlUpdated = onFeedUrlUpdated,
Expand All @@ -77,6 +79,7 @@ private fun AddScreenContentPreview() {
AddFeedScreenContent(
feedName = "My Feed",
feedUrl = "https://www.ablog.com/feed",
isInvalidUrl = false,
onFeedNameUpdated = {},
onFeedUrlUpdated = {},
addFeed = { },
Expand All @@ -93,6 +96,22 @@ private fun AddScreenContentDarkPreview() {
AddFeedScreenContent(
feedName = "My Feed",
feedUrl = "https://www.ablog.com/feed",
isInvalidUrl = false,
onFeedNameUpdated = {},
onFeedUrlUpdated = {},
addFeed = { },
)
}
}

@Preview
@Composable
private fun AddScreenContentInvalidUrlPreview() {
FeedFlowTheme {
AddFeedScreenContent(
feedName = "My Feed",
feedUrl = "https://www.ablog.com/feed",
isInvalidUrl = true,
onFeedNameUpdated = {},
onFeedUrlUpdated = {},
addFeed = { },
Expand Down
1 change: 1 addition & 0 deletions i18n/src/commonMain/resources/MR/base/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@
<string name="open_website_button">Open website</string>
<string name="database_error">Something went wrong during the retrieval of the feeds. Please retry or restart the app :(</string>
<string name="force_feed_refresh">Force feeds refresh</string>
<string name="invalid_rss_url">The link you provided is not a valid RSS feed</string>
</resources>
77 changes: 53 additions & 24 deletions iosApp/Source/Settings/AddFeed/AddFeedScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,66 @@ import shared
import KMPNativeCoroutinesAsync

struct AddFeedScreen: View {

@EnvironmentObject var appState: AppState
@Environment(\.presentationMode) var presentationMode

@State private var feedName = ""
@State private var feedURL = ""

@State private var isInvalidUrl = false

@StateObject var addFeedViewModel: AddFeedViewModel = KotlinDependencies.shared.getAddFeedViewModel()

var body: some View {
VStack {
VStack(alignment: .leading) {
TextField(
MR.strings().feed_name.localized,
text: $feedName
)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding(.top, Spacing.regular)
.padding(.horizontal, Spacing.regular)

TextField(
MR.strings().feed_url.localized,
text: $feedURL
)
.keyboardType(.webSearch)
.border(isInvalidUrl ? Color.red : Color.clear)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding(.top, Spacing.regular)
.padding(.horizontal, Spacing.regular)

Button(
action: {
addFeedViewModel.addFeed()
},
label: {
Text(MR.strings().add_feed.localized)
}
)
.buttonStyle(.bordered)
.padding(.top, Spacing.regular)

Spacer()
}
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Text(MR.strings().add_feed.localized)
.font(.title2)

if isInvalidUrl {
Text(MR.strings().invalid_rss_url.localized)
.padding(.horizontal, Spacing.regular)
.frame(alignment: .leading)
.font(.caption)
.foregroundColor(.red)
}

HStack {
Spacer()

Button(
action: {
addFeedViewModel.addFeed()
},
label: {
Text(MR.strings().add_feed.localized)
}
)
.frame(alignment: .center)
.buttonStyle(.bordered)
.padding(.top, Spacing.regular)

Spacer()
}

Spacer()
}
.navigationTitle(MR.strings().add_feed.localized)
.navigationBarTitleDisplayMode(.inline)
.onChange(of: feedName) { value in
addFeedViewModel.updateFeedNameTextFieldValue(feedNameTextFieldValue: value)
}
Expand Down Expand Up @@ -88,6 +101,22 @@ struct AddFeedScreen: View {
)
}
}
.task {
do {
let stream = asyncSequence(for: addFeedViewModel.isInvalidRssFeedFlow)
for try await isInvalidRss in stream {
self.isInvalidUrl = isInvalidRss as? Bool ?? false
}
} catch {
self.appState.snackbarQueue.append(
SnackbarData(
title: MR.strings().generic_error_message.localized,
subtitle: nil,
showBanner: true
)
)
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import java.io.Reader
internal actual class OpmlFeedHandler(
private val dispatcherProvider: DispatcherProvider,
) {
actual suspend fun importFeed(opmlInput: OpmlInput): List<ParsedFeedSource> =
actual suspend fun generateFeedSources(opmlInput: OpmlInput): List<ParsedFeedSource> =
withContext(dispatcherProvider.default) {
val inputStream = opmlInput.inputStream
val feedSources = mutableListOf<ParsedFeedSource>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ class OPMLFeedParserTest {

@Test
fun `The number of feeds are correct`() = runTest {
val feedSources = parser.importFeed(opmlInput)
val feedSources = parser.generateFeedSources(opmlInput)
assertTrue(feedSources.size == 6)
}

@Test
fun `The number of feed in category are correct`() = runTest {
val feedSources = parser.importFeed(opmlInput)
val feedSources = parser.generateFeedSources(opmlInput)

val techFeeds = feedSources.filter { it.category == "Tech" }
val basketFeeds = feedSources.filter { it.category == "Basket" }
Expand All @@ -43,7 +43,7 @@ class OPMLFeedParserTest {

@Test
fun `The feeds are parsed correctly`() = runTest {
val feedSources = parser.importFeed(opmlInput)
val feedSources = parser.generateFeedSources(opmlInput)

assertEquals("Hacker News", feedSources[0].title)
assertEquals("https://news.ycombinator.com/rss", feedSources[0].url)
Expand Down Expand Up @@ -75,7 +75,7 @@ class OPMLFeedParserTest {
val opmlInput = OpmlInput(
inputStream = opmlWithText.byteInputStream(),
)
val feedSources = parser.importFeed(opmlInput)
val feedSources = parser.generateFeedSources(opmlInput)
assertTrue(feedSources.isNotEmpty())
}
}
4 changes: 2 additions & 2 deletions shared/src/commonMain/kotlin/com/prof18/feedflow/di/Koin.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.prof18.feedflow.di

import co.touchlab.kermit.ExperimentalKermitApi
import co.touchlab.kermit.Logger
import co.touchlab.kermit.StaticConfig
import co.touchlab.kermit.platformLogWriter
Expand Down Expand Up @@ -38,7 +37,6 @@ fun initKoin(
}
}

@OptIn(ExperimentalKermitApi::class)
private fun getLoggingModule(appEnvironment: AppEnvironment): Module =
module {
val loggers = mutableListOf(platformLogWriter())
Expand Down Expand Up @@ -79,6 +77,8 @@ private val coreModule = module {
databaseHelper = get(),
opmlFeedHandler = get(),
settingsHelper = get(),
rssParser = get(),
logger = getWith("FeedManagerRepositoryImpl"),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface FeedManagerRepository {
suspend fun addFeedsFromFile(opmlInput: OpmlInput)
suspend fun getFeeds(): Flow<List<FeedSource>>
suspend fun addFeed(url: String, name: String)
suspend fun checkIfValidRss(url: String): Boolean
suspend fun exportFeedsAsOpml(opmlOutput: OpmlOutput)
suspend fun deleteFeed(feedSource: FeedSource)
fun getFavouriteBrowserId(): String?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.prof18.feedflow.domain.feed.manager

import co.touchlab.kermit.Logger
import com.prof18.feedflow.core.model.FeedSource
import com.prof18.feedflow.data.DatabaseHelper
import com.prof18.feedflow.data.SettingsHelper
Expand All @@ -8,17 +9,22 @@ import com.prof18.feedflow.domain.model.ParsedFeedSource
import com.prof18.feedflow.domain.opml.OpmlFeedHandler
import com.prof18.feedflow.domain.opml.OpmlInput
import com.prof18.feedflow.domain.opml.OpmlOutput
import com.prof18.rssparser.RssParser
import kotlinx.coroutines.flow.Flow

internal class FeedManagerRepositoryImpl(
private val databaseHelper: DatabaseHelper,
private val opmlFeedHandler: OpmlFeedHandler,
private val settingsHelper: SettingsHelper,
private val rssParser: RssParser,
private val logger: Logger,
) : FeedManagerRepository {
override suspend fun addFeedsFromFile(opmlInput: OpmlInput) {
val feeds = opmlFeedHandler.importFeed(opmlInput)
val feeds = opmlFeedHandler.generateFeedSources(opmlInput)
val categories = feeds.mapNotNull { it.category }.distinct()

// TODO: check url and returns

databaseHelper.insertCategories(categories)
databaseHelper.insertFeedSource(feeds)
}
Expand Down Expand Up @@ -54,4 +60,15 @@ internal class FeedManagerRepositoryImpl(
override fun setFavouriteBrowser(browser: Browser) {
settingsHelper.saveFavouriteBrowserId(browser.id)
}

@Suppress("TooGenericExceptionCaught")
override suspend fun checkIfValidRss(url: String): Boolean {
return try {
rssParser.getRssChannel(url)
true
} catch (e: Throwable) {
logger.d { "Wrong url input: $e" }
false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import com.prof18.feedflow.core.model.FeedSource
import com.prof18.feedflow.domain.model.ParsedFeedSource

internal expect class OpmlFeedHandler {
suspend fun importFeed(opmlInput: OpmlInput): List<ParsedFeedSource>
suspend fun generateFeedSources(opmlInput: OpmlInput): List<ParsedFeedSource>
suspend fun exportFeed(opmlOutput: OpmlOutput, feedSources: List<FeedSource>)
}
Loading