-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #188 from Yashraj254/migrating_notification_settin…
…gs_to_compose migrated NotificationSettings fragment to NotificationSettingsScreen Composable
- Loading branch information
Showing
4 changed files
with
186 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
...com/hieuwu/groceriesstore/presentation/notificationsettings/NotificationSettingsScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package com.hieuwu.groceriesstore.presentation.notificationsettings | ||
|
||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.foundation.text.ClickableText | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.ArrowBack | ||
import androidx.compose.material3.CenterAlignedTopAppBar | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.IconButton | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TopAppBarDefaults | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
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.graphics.Color | ||
import androidx.compose.ui.res.colorResource | ||
import androidx.compose.ui.text.AnnotatedString | ||
import androidx.compose.ui.text.TextStyle | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
import com.hieuwu.groceriesstore.R | ||
import com.hieuwu.groceriesstore.presentation.notificationsettings.composables.NotificationSettingsItem | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@Composable | ||
fun NotificationSettingsScreen( | ||
onNavigateUp: () -> Unit, | ||
viewModel: NotificationSettingsViewModel = hiltViewModel() | ||
) { | ||
Scaffold( | ||
topBar = { | ||
CenterAlignedTopAppBar( | ||
title = { Text(text = "Notifications", color = Color.White, fontWeight = FontWeight.Medium) }, | ||
actions = { | ||
ClickableText( | ||
text = AnnotatedString("SAVE"), | ||
style = TextStyle(color = Color.White, fontWeight = FontWeight.Medium), | ||
onClick = { | ||
viewModel.updateNotificationSettings() | ||
}, | ||
modifier = Modifier.padding(horizontal = 8.dp) | ||
) | ||
}, | ||
navigationIcon = { | ||
IconButton(onClick = { onNavigateUp() }) { | ||
Icon( | ||
imageVector = Icons.Filled.ArrowBack, | ||
contentDescription = null, | ||
tint = Color.White | ||
) | ||
} | ||
}, | ||
colors = TopAppBarDefaults.centerAlignedTopAppBarColors( | ||
containerColor = colorResource(id = R.color.colorPrimary) | ||
) | ||
) | ||
} | ||
) { | ||
val user = viewModel.user.collectAsState().value | ||
|
||
if (user != null) | ||
Box( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(it) | ||
) { | ||
var isOrderCreatedNotiEnabled by remember { mutableStateOf(user.isOrderCreatedNotiEnabled) } | ||
var isDataRefreshedNotiEnabled by remember { mutableStateOf(user.isDataRefreshedNotiEnabled) } | ||
var isPromotionNotiEnabled by remember { mutableStateOf(user.isPromotionNotiEnabled) } | ||
viewModel.initializeSwitchValue( | ||
user.copy( | ||
isOrderCreatedNotiEnabled = isOrderCreatedNotiEnabled, | ||
isDataRefreshedNotiEnabled = isDataRefreshedNotiEnabled, | ||
isPromotionNotiEnabled = isPromotionNotiEnabled | ||
) | ||
) | ||
|
||
LazyColumn( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
) { | ||
item { | ||
NotificationSettingsItem( | ||
text = R.string.show_when_order_created, | ||
isChecked = isOrderCreatedNotiEnabled, | ||
onClick = { | ||
isOrderCreatedNotiEnabled = !isOrderCreatedNotiEnabled | ||
} | ||
) | ||
} | ||
item { | ||
NotificationSettingsItem( | ||
text = R.string.show_when_promotion_sent, | ||
isChecked = isPromotionNotiEnabled, | ||
onClick = { | ||
isPromotionNotiEnabled = !isPromotionNotiEnabled | ||
} | ||
) | ||
} | ||
item { | ||
NotificationSettingsItem( | ||
text = R.string.show_when_app_data_is_refreshed, | ||
isChecked = isDataRefreshedNotiEnabled, | ||
onClick = { | ||
isDataRefreshedNotiEnabled = !isDataRefreshedNotiEnabled | ||
} | ||
) | ||
} | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
@Preview | ||
@Composable | ||
fun NotificationSettingsScreenPreview() { | ||
NotificationSettingsScreen(onNavigateUp = { }) | ||
} |
50 changes: 50 additions & 0 deletions
50
...groceriesstore/presentation/notificationsettings/composables/NotificationSettingsItems.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.hieuwu.groceriesstore.presentation.notificationsettings.composables | ||
|
||
import androidx.annotation.StringRes | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.Switch | ||
import androidx.compose.material3.SwitchDefaults | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.colorResource | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import com.hieuwu.groceriesstore.R | ||
|
||
@Composable | ||
fun NotificationSettingsItem(@StringRes text: Int, isChecked: Boolean, onClick: () -> Unit) { | ||
Row( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(vertical = 8.dp, horizontal = 16.dp), | ||
horizontalArrangement = Arrangement.SpaceBetween, | ||
verticalAlignment = Alignment.CenterVertically | ||
) { | ||
Text(text = stringResource(text)) | ||
Switch( | ||
checked = isChecked, onCheckedChange = { onClick() }, colors = SwitchDefaults.colors( | ||
checkedTrackColor = colorResource(id = R.color.colorPrimary) | ||
) | ||
) | ||
} | ||
} | ||
|
||
@Preview | ||
@Composable | ||
fun NotificationSettingsItemPreview() { | ||
NotificationSettingsItem( | ||
text = R.string.show_when_order_created, | ||
isChecked = true, | ||
onClick = { } | ||
) | ||
} |
75 changes: 0 additions & 75 deletions
75
app/src/main/res/layout/fragment_notification_settings.xml
This file was deleted.
Oops, something went wrong.