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

Bookmark icon change features added. #1

Merged
merged 1 commit into from
May 18, 2024
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 @@ -14,23 +14,27 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.PathEffect
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import com.loc.newsapp.R
import com.loc.newsapp.domain.model.Article
import com.loc.newsapp.domain.model.Source
import com.loc.newsapp.ui.theme.NewsAppTheme

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DetailsTopBar(
sideEffect: Boolean?,
onBrowsingClick: () -> Unit,
onShareClick: () -> Unit,
onBookmarkClick: () -> Unit,
onBackClick: () -> Unit
) {

TopAppBar(
title = { Text(text = "") },
modifier = Modifier.fillMaxWidth(),
Expand All @@ -49,10 +53,17 @@ fun DetailsTopBar(
},
actions = {
IconButton(onClick = onBookmarkClick) {
Icon(
painter = painterResource(id = R.drawable.ic_bookmark),
contentDescription = null
)
if (sideEffect == true) {
Icon(
painter = painterResource(id = R.drawable.ic_bookmark_remove),
contentDescription = null
)
} else {
Icon(
painter = painterResource(id = R.drawable.ic_bookmark_add),
contentDescription = null
)
}
}
IconButton(onClick = onShareClick) {
Icon(
Expand All @@ -77,6 +88,7 @@ fun DetailsTopBarPreivew() {
NewsAppTheme {
Box(modifier = Modifier.background(MaterialTheme.colorScheme.background)) {
DetailsTopBar(
sideEffect = true,
onBrowsingClick = { /*TODO*/ },
onShareClick = { /*TODO*/ },
onBookmarkClick = { /*TODO*/ }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,16 @@ fun NewsNavigator() {

composable(route = Route.DetailsScreen.route) {
val viewModel: DetailsViewModel = hiltViewModel()
if (viewModel.sideEffect != null) {
Toast.makeText(LocalContext.current, viewModel.sideEffect, Toast.LENGTH_SHORT).show()
viewModel.onEvent(DetailsEvent.RemoveSideEffect)
}

navController.previousBackStackEntry?.savedStateHandle?.get<Article?>("article")
?.let { article ->
DetailsScreen(
article = article,
sideEffect = viewModel.sideEffect,
event = viewModel::onEvent,
controlBookmark = {
viewModel.controlBookmarkedArticle(article)
},
navigateUp = { navController.navigateUp() })
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
Expand All @@ -32,18 +34,24 @@ import com.loc.newsapp.ui.theme.NewsAppTheme
@Composable
fun DetailsScreen(
article: Article,
sideEffect: Boolean?,
event: (DetailsEvent) -> Unit,
controlBookmark: () -> Unit,
navigateUp: () -> Unit
) {

val context = LocalContext.current

LaunchedEffect(key1 = article) {
controlBookmark.invoke()
}

Column(
modifier = Modifier
.fillMaxSize()
.statusBarsPadding()
) {
DetailsTopBar(
sideEffect = sideEffect,
onBrowsingClick = {
Intent(Intent.ACTION_VIEW).also {
it.data = Uri.parse(article.url)
Expand Down Expand Up @@ -126,10 +134,10 @@ fun DetailsScreenPreview() {
url = "https://consent.google.com/ml?continue=https://news.google.com/rss/articles/CBMiaWh0dHBzOi8vY3J5cHRvc2F1cnVzLnRlY2gvY29pbmJhc2Utc2F5cy1hcHBsZS1ibG9ja2VkLWl0cy1sYXN0LWFwcC1yZWxlYXNlLW9uLW5mdHMtaW4td2FsbGV0LXJldXRlcnMtY29tL9IBAA?oc%3D5&gl=FR&hl=en-US&cm=2&pc=n&src=1",
urlToImage = "https://media.wired.com/photos/6495d5e893ba5cd8bbdc95af/191:100/w_1280,c_limit/The-EU-Rules-Phone-Batteries-Must-Be-Replaceable-Gear-2BE6PRN.jpg"
),
event = {}
) {

}
sideEffect = false,
event = {},
controlBookmark = {}
) {}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import androidx.lifecycle.viewModelScope
import com.loc.newsapp.domain.model.Article
import com.loc.newsapp.domain.usecases.news.NewsUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.forEach
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import javax.inject.Inject

Expand All @@ -16,8 +21,7 @@ class DetailsViewModel @Inject constructor(
private val newsUseCase: NewsUseCase
) : ViewModel() {

var sideEffect by mutableStateOf<String?>(null)
private set
var sideEffect by mutableStateOf<Boolean?>(null)

fun onEvent(event: DetailsEvent) {
when (event) {
Expand All @@ -40,12 +44,19 @@ class DetailsViewModel @Inject constructor(

private suspend fun deleteArticle(article: Article) {
newsUseCase.deleteArticleDatabase(article = article)
sideEffect = "Article Deleted"
sideEffect = false
}

private suspend fun upsertArticle(article: Article) {
newsUseCase.upsertArticleDatabase(article = article)
sideEffect = "Article Saved"
sideEffect = true
}

fun controlBookmarkedArticle(article: Article) {
viewModelScope.launch {
newsUseCase.selectBookmarkAllArticleDatabase().collect {
sideEffect = it.contains(article)
}
}
}
}
1 change: 1 addition & 0 deletions app/src/main/java/com/loc/newsapp/util/Dimens.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.compose.ui.unit.dp
object Dimens {

val MediumPadding1 = 24.dp

val MediumPadding2 = 30.dp

val IndicatorSize = 14.dp
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_bookmark_add.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:pathData="M200,840v-640q0,-33 23.5,-56.5T280,120h240v80L280,200v518l200,-86 200,86v-278h80v400L480,720 200,840ZM280,200h240,-240ZM680,360v-80h-80v-80h80v-80h80v80h80v80h-80v80h-80Z"
android:fillColor="#e8eaed"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_bookmark_remove.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:pathData="M840,280L600,280v-80h240v80ZM200,840v-640q0,-33 23.5,-56.5T280,120h240v80L280,200v518l200,-86 200,86v-278h80v400L480,720 200,840ZM280,200h240,-240Z"
android:fillColor="#e8eaed"/>
</vector>