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

Commit

Permalink
NewsScreen: Use LaunchedEffect to load article
Browse files Browse the repository at this point in the history
Signed-off-by: Aayush Gupta <[email protected]>
  • Loading branch information
theimpulson committed Aug 19, 2023
1 parent 2527064 commit ee543a7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
22 changes: 10 additions & 12 deletions app/src/main/java/io/aayush/relabs/ui/screens/news/NewsScreen.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package io.aayush.relabs.ui.screens.news

import android.net.Uri
import android.util.Log
import androidx.browser.customtabs.CustomTabsIntent
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
Expand All @@ -16,7 +13,11 @@ import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.rememberTopAppBarState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
Expand Down Expand Up @@ -56,6 +57,11 @@ fun NewsScreen(
) {
val articles: List<Article> by viewModel.feed.collectAsStateWithLifecycle()
val context = LocalContext.current
var clicked by remember { mutableStateOf("") }

LaunchedEffect(key1 = clicked) {
if (clicked.isNotBlank()) viewModel.loadArticle(context, clicked)
}

LazyColumn(modifier = Modifier.padding(it)) {
items(articles) { article ->
Expand All @@ -79,15 +85,7 @@ fun NewsScreen(
).toString(),
author = stringResource(id = R.string.posted_by, article.author ?: ""),
date = article.pubDate ?: "",
onClicked = {
try {
CustomTabsIntent.Builder()
.build()
.launchUrl(context, Uri.parse(article.link))
} catch (exception: Exception) {
Log.e(TAG, "Failed to open profile", exception)
}
}
onClicked = { clicked = article.link ?: "" }
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package io.aayush.relabs.ui.screens.news

import android.content.Context
import android.net.Uri
import android.util.Log
import androidx.browser.customtabs.CustomTabsIntent
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.prof.rssparser.Article
Expand All @@ -12,9 +16,12 @@ import javax.inject.Inject

@HiltViewModel
class NewsViewModel @Inject constructor(
private val rssNewsRepository: RSSNewsRepository
private val rssNewsRepository: RSSNewsRepository,
private val customTabIntent: CustomTabsIntent
) : ViewModel() {

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

private val _feed = MutableStateFlow<List<Article>>(emptyList())
val feed = _feed.asStateFlow()

Expand All @@ -27,4 +34,12 @@ class NewsViewModel @Inject constructor(
_feed.value = rssNewsRepository.getMobileFeed().getOrDefault(emptyList())
}
}

fun loadArticle(context: Context, link: String) {
try {
customTabIntent.launchUrl(context, Uri.parse(link))
} catch (exception: Exception) {
Log.e(TAG, "Failed to open profile", exception)
}
}
}
7 changes: 7 additions & 0 deletions app/src/main/java/io/aayush/relabs/utils/CommonModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.aayush.relabs.utils

import android.content.Context
import android.content.SharedPreferences
import androidx.browser.customtabs.CustomTabsIntent
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand All @@ -23,4 +24,10 @@ object CommonModule {
fun provideSharedPrefInstance(@ApplicationContext context: Context): SharedPreferences {
return context.getSharedPreferences(context.packageName, Context.MODE_PRIVATE)
}

@Singleton
@Provides
fun provideCustomTabIntent(): CustomTabsIntent {
return CustomTabsIntent.Builder().build()
}
}

0 comments on commit ee543a7

Please sign in to comment.