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

Commit

Permalink
RSS: Add ArsTechnia feed
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 162aa7f commit bb9ef78
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
12 changes: 12 additions & 0 deletions app/src/main/java/io/aayush/relabs/rss/RSSNewsImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class RSSNewsImpl @Inject constructor(

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"
}
Expand All @@ -32,6 +33,17 @@ class RSSNewsImpl @Inject constructor(
}
}

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 {
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/io/aayush/relabs/rss/RSSNewsRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ class RSSNewsRepository @Inject constructor(
suspend fun getAndroidDevsFeed(): Result<List<RssItem>> {
return rssNewsImpl.getAndroidDevsFeed()
}

suspend fun getArsTechFeed(): Result<List<RssItem>> {
return rssNewsImpl.getArsTechFeed()
}
}
11 changes: 7 additions & 4 deletions app/src/main/java/io/aayush/relabs/ui/screens/news/NewsScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.ScrollableTabRow
import androidx.compose.material3.Tab
import androidx.compose.material3.TabRow
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -50,7 +50,7 @@ fun NewsScreen(navHostController: NavHostController, viewModel: NewsViewModel =
modifier = Modifier.fillMaxSize(),
topBar = { TopAppBar(title = { Text(text = stringResource(id = R.string.news)) }) }
) {
val tabData = listOf(R.string.android_devs, R.string.google_9to5, R.string.xda_portal)
val tabData = listOf(R.string.android_devs, R.string.google_9to5, R.string.arstech, R.string.xda_portal)
val pagerState = rememberPagerState(
initialPage = 0,
initialPageOffsetFraction = 0f,
Expand All @@ -60,6 +60,7 @@ fun NewsScreen(navHostController: NavHostController, viewModel: NewsViewModel =
val coroutineScope = rememberCoroutineScope()

val xdaPortalFeed: List<RssItem> by viewModel.xdaPortalFeed.collectAsStateWithLifecycle()
val arsTechFeed: List<RssItem> by viewModel.arsTechFeed.collectAsStateWithLifecycle()
val google9to5Feed: List<RssItem> by viewModel.google9to5Feed.collectAsStateWithLifecycle()
val androidDevsFeed: List<RssItem> by viewModel.androidDevsFeed.collectAsStateWithLifecycle()

Expand All @@ -70,13 +71,14 @@ fun NewsScreen(navHostController: NavHostController, viewModel: NewsViewModel =
when (page) {
0 -> viewModel.getAndroidDevelopersArticles()
1 -> viewModel.get9to5GoogleArticles()
2 -> viewModel.getXDAPortalArticles()
2 -> viewModel.getArsTechArticles()
else -> viewModel.getXDAPortalArticles()
}
}
}

Column(modifier = Modifier.padding(it)) {
TabRow(selectedTabIndex = tabIndex) {
ScrollableTabRow(selectedTabIndex = tabIndex, edgePadding = 0.dp) {
tabData.fastForEachIndexed { index, _ ->
Tab(
selected = tabIndex == index,
Expand All @@ -99,6 +101,7 @@ fun NewsScreen(navHostController: NavHostController, viewModel: NewsViewModel =
items = when (it) {
0 -> androidDevsFeed
1 -> google9to5Feed
2 -> arsTechFeed
else -> xdaPortalFeed
},
key = { a -> a.guid ?: UUID.randomUUID() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class NewsViewModel @Inject constructor(
private val _xdaPortalFeed = MutableStateFlow<List<RssItem>>(emptyList())
val xdaPortalFeed = _xdaPortalFeed.asStateFlow()

private val _arsTechFeed = MutableStateFlow<List<RssItem>>(emptyList())
val arsTechFeed = _arsTechFeed.asStateFlow()

private val _google9to5Feed = MutableStateFlow<List<RssItem>>(emptyList())
val google9to5Feed = _google9to5Feed.asStateFlow()

Expand All @@ -44,4 +47,11 @@ class NewsViewModel @Inject constructor(
rssNewsRepository.getAndroidDevsFeed().getOrDefault(emptyList())
}
}

fun getArsTechArticles() {
viewModelScope.launch {
_arsTechFeed.value =
rssNewsRepository.getArsTechFeed().getOrDefault(emptyList())
}
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<!-- NewsScreen -->
<string name="news">News</string>
<string name="xda_portal" translatable="false">XDA Portal</string>
<string name="arstech" translatable="false">ArsTechnica</string>
<string name="google_9to5" translatable="false">9to5Google</string>
<string name="android_devs" translatable="false">Android Devs</string>
<string name="posted_by">Posted by <xliff:g id="author">%1$s</xliff:g></string>
Expand Down

0 comments on commit bb9ef78

Please sign in to comment.