Skip to content
This repository has been archived by the owner on Aug 20, 2024. It is now read-only.

Commit

Permalink
RSS: Strip out common method to handle fetching feeds
Browse files Browse the repository at this point in the history
Signed-off-by: Aayush Gupta <[email protected]>
  • Loading branch information
theimpulson committed Oct 15, 2023
1 parent bb9ef78 commit cd089f9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 47 deletions.
47 changes: 4 additions & 43 deletions app/src/main/java/io/aayush/relabs/rss/RSSNewsImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.aayush.relabs.rss
import android.util.Log
import com.prof18.rssparser.RssParser
import com.prof18.rssparser.model.RssItem
import io.aayush.relabs.rss.data.RssFeed
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import javax.inject.Inject
Expand All @@ -15,52 +16,12 @@ class RSSNewsImpl @Inject constructor(

private val TAG = RSSNewsImpl::class.java.simpleName

companion object {
private const val FEED_XDA_PORTAL = "https://www.xda-developers.com/feed/category/mobile/"
private const val FEED_ARS_TECH = "https://feeds.arstechnica.com/arstechnica/index"
private const val FEED_9TO5GOOGLE = "https://9to5google.com/feed/"
private const val FEED_ANDROID_DEVS = "https://feeds.feedburner.com/blogspot/hsDu"
}

suspend fun getXDAPortalFeed(): Result<List<RssItem>> {
return withContext(Dispatchers.IO) {
try {
return@withContext Result.success(parser.getRssChannel(FEED_XDA_PORTAL).items)
} catch (exception: Exception) {
Log.e(TAG, "Failed to fetch xda portal rss feed!", exception)
return@withContext Result.failure(exception)
}
}
}

suspend fun getArsTechFeed(): Result<List<RssItem>> {
return withContext(Dispatchers.IO) {
try {
return@withContext Result.success(parser.getRssChannel(FEED_ARS_TECH).items)
} catch (exception: Exception) {
Log.e(TAG, "Failed to fetch arstechnica rss feed!", exception)
return@withContext Result.failure(exception)
}
}
}

suspend fun get9to5GoogleFeed(): Result<List<RssItem>> {
return withContext(Dispatchers.IO) {
try {
return@withContext Result.success(parser.getRssChannel(FEED_9TO5GOOGLE).items)
} catch (exception: Exception) {
Log.e(TAG, "Failed to fetch 9to5Google rss feed!", exception)
return@withContext Result.failure(exception)
}
}
}

suspend fun getAndroidDevsFeed(): Result<List<RssItem>> {
suspend fun getFeed(rssFeed: RssFeed): Result<List<RssItem>> {
return withContext(Dispatchers.IO) {
try {
return@withContext Result.success(parser.getRssChannel(FEED_ANDROID_DEVS).items)
return@withContext Result.success(parser.getRssChannel(rssFeed.source).items)
} catch (exception: Exception) {
Log.e(TAG, "Failed to fetch android developers rss feed!", exception)
Log.e(TAG, "Failed to fetch rss feed!", exception)
return@withContext Result.failure(exception)
}
}
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/java/io/aayush/relabs/rss/RSSNewsRepository.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package io.aayush.relabs.rss

import com.prof18.rssparser.model.RssItem
import io.aayush.relabs.rss.data.RssFeed
import javax.inject.Inject

class RSSNewsRepository @Inject constructor(
private val rssNewsImpl: RSSNewsImpl
) {

suspend fun getXDAPortalFeed(): Result<List<RssItem>> {
return rssNewsImpl.getXDAPortalFeed()
return rssNewsImpl.getFeed(RssFeed.XDA)
}

suspend fun get9to5GoogleFeed(): Result<List<RssItem>> {
return rssNewsImpl.get9to5GoogleFeed()
return rssNewsImpl.getFeed(RssFeed.Google9To5)
}

suspend fun getAndroidDevsFeed(): Result<List<RssItem>> {
return rssNewsImpl.getAndroidDevsFeed()
return rssNewsImpl.getFeed(RssFeed.AndroidDevs)
}

suspend fun getArsTechFeed(): Result<List<RssItem>> {
return rssNewsImpl.getArsTechFeed()
return rssNewsImpl.getFeed(RssFeed.ArsTech)
}
}
8 changes: 8 additions & 0 deletions app/src/main/java/io/aayush/relabs/rss/data/RssFeed.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.aayush.relabs.rss.data

sealed class RssFeed(val source: String) {
data object XDA : RssFeed("https://www.xda-developers.com/feed/category/mobile/")
data object ArsTech : RssFeed("https://feeds.arstechnica.com/arstechnica/index")
data object Google9To5 : RssFeed("https://9to5google.com/feed/")
data object AndroidDevs : RssFeed("https://feeds.feedburner.com/blogspot/hsDu")
}

0 comments on commit cd089f9

Please sign in to comment.