-
-
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 #183 from Devendra34/feature/account_compose
Migrate account feature from XML to Jetpack Compose
- Loading branch information
Showing
11 changed files
with
392 additions
and
248 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
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
102 changes: 102 additions & 0 deletions
102
app/src/main/java/com/hieuwu/groceriesstore/presentation/account/AccountScreen.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,102 @@ | ||
package com.hieuwu.groceriesstore.presentation.account | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.dimensionResource | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
import com.hieuwu.groceriesstore.R | ||
import com.hieuwu.groceriesstore.domain.models.UserModel | ||
import com.hieuwu.groceriesstore.presentation.account.widgets.AccountContentView | ||
import com.hieuwu.groceriesstore.presentation.account.widgets.AccountHeaderView | ||
|
||
@Composable | ||
fun AccountScreen( | ||
modifier: Modifier = Modifier, | ||
viewModel: AccountViewModel = hiltViewModel(), | ||
onSignInClick: (() -> Unit), | ||
onProfileSettingsClick: (() -> Unit), | ||
onNotificationSettingsClick: (() -> Unit), | ||
onOrderHistorySettingsClick: (() -> Unit), | ||
) { | ||
val user = viewModel.user.collectAsState() | ||
|
||
AccountScreenView( | ||
modifier = modifier, | ||
user = user.value, | ||
onSignInClick = onSignInClick, | ||
onProfileSettingsClick = onProfileSettingsClick, | ||
onNotificationSettingsClick = onNotificationSettingsClick, | ||
onOrderHistorySettingsClick = onOrderHistorySettingsClick, | ||
onSignOutClick = { viewModel.signOut() }, | ||
) | ||
} | ||
|
||
@Composable | ||
private fun AccountScreenView( | ||
modifier: Modifier = Modifier, | ||
user: UserModel? = null, | ||
onSignInClick: () -> Unit, | ||
onProfileSettingsClick: () -> Unit, | ||
onNotificationSettingsClick: () -> Unit, | ||
onOrderHistorySettingsClick: () -> Unit, | ||
onSignOutClick: () -> Unit, | ||
) { | ||
Scaffold { contentPadding -> | ||
Column( | ||
modifier = modifier.padding(contentPadding), | ||
verticalArrangement = Arrangement.Top | ||
) { | ||
|
||
AccountHeaderView(user = user) | ||
AccountContentView( | ||
modifier = Modifier.padding(horizontal = dimensionResource(id = R.dimen.margin_medium)), | ||
user = user, | ||
onSignInClick = onSignInClick, | ||
onProfileSettingsClick = onProfileSettingsClick, | ||
onNotificationSettingsClick = onNotificationSettingsClick, | ||
onOrderHistorySettingsClick = onOrderHistorySettingsClick, | ||
onSignOutClick = onSignOutClick, | ||
) | ||
} | ||
} | ||
} | ||
|
||
internal val DemoUser = UserModel( | ||
"0", "J.K. Rowling", "[email protected]", | ||
phone = "+1234567890", | ||
address = "Earth", | ||
isOrderCreatedNotiEnabled = false, | ||
isPromotionNotiEnabled = false, | ||
isDataRefreshedNotiEnabled = false | ||
) | ||
|
||
@Preview(showSystemUi = true) | ||
@Composable | ||
private fun SignedInAccountScreen() { | ||
AccountScreenView( | ||
user = DemoUser, | ||
onSignInClick = { /*TODO*/ }, | ||
onProfileSettingsClick = { /*TODO*/ }, | ||
onNotificationSettingsClick = { /*TODO*/ }, | ||
onOrderHistorySettingsClick = { /*TODO*/ }, | ||
onSignOutClick = { /*TODO*/ }, | ||
) | ||
} | ||
|
||
@Preview(showSystemUi = true) | ||
@Composable | ||
private fun SignedOutAccountScreen() { | ||
AccountScreenView( | ||
onSignInClick = { /*TODO*/ }, | ||
onProfileSettingsClick = { /*TODO*/ }, | ||
onNotificationSettingsClick = { /*TODO*/ }, | ||
onOrderHistorySettingsClick = { /*TODO*/ }, | ||
onSignOutClick = { /*TODO*/ }, | ||
) | ||
} |
139 changes: 139 additions & 0 deletions
139
...rc/main/java/com/hieuwu/groceriesstore/presentation/account/widgets/AccountContentView.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,139 @@ | ||
package com.hieuwu.groceriesstore.presentation.account.widgets | ||
|
||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.res.dimensionResource | ||
import androidx.compose.ui.res.painterResource | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import com.hieuwu.groceriesstore.R | ||
import com.hieuwu.groceriesstore.domain.models.UserModel | ||
import com.hieuwu.groceriesstore.presentation.account.DemoUser | ||
import com.hieuwu.groceriesstore.presentation.core.widgets.PrimaryButton | ||
import com.hieuwu.groceriesstore.presentation.core.widgets.SecondaryButton | ||
|
||
@Composable | ||
fun AccountContentView( | ||
modifier: Modifier = Modifier, | ||
user: UserModel? = null, | ||
onSignInClick: () -> Unit, | ||
onProfileSettingsClick: () -> Unit, | ||
onNotificationSettingsClick: () -> Unit, | ||
onOrderHistorySettingsClick: () -> Unit, | ||
onSignOutClick: () -> Unit, | ||
) { | ||
if (user != null) { | ||
AccountContentSignedInView( | ||
modifier = modifier, | ||
onProfileSettingsClick = onProfileSettingsClick, | ||
onNotificationSettingsClick = onNotificationSettingsClick, | ||
onOrderHistorySettingsClick = onOrderHistorySettingsClick, | ||
onSignOutClick = onSignOutClick, | ||
) | ||
} else { | ||
AccountContentSignedOutView( | ||
modifier = modifier.padding(vertical = dimensionResource(id = R.dimen.margin_medium)), | ||
onSignInClick = onSignInClick | ||
) | ||
} | ||
} | ||
|
||
@Composable | ||
fun AccountContentSignedInView( | ||
modifier: Modifier = Modifier, | ||
onProfileSettingsClick: (() -> Unit), | ||
onNotificationSettingsClick: (() -> Unit), | ||
onOrderHistorySettingsClick: (() -> Unit), | ||
onSignOutClick: (() -> Unit), | ||
) { | ||
val actions = listOf( | ||
R.string.profile_information to onProfileSettingsClick, | ||
R.string.notification_settings to onNotificationSettingsClick, | ||
R.string.order_history_settings to onOrderHistorySettingsClick, | ||
) | ||
for (action in actions) { | ||
Row( | ||
modifier = modifier | ||
.fillMaxWidth() | ||
.padding(top = dimensionResource(id = R.dimen.margin_small)) | ||
.clickable(onClick = action.second), | ||
horizontalArrangement = Arrangement.SpaceBetween, | ||
verticalAlignment = Alignment.CenterVertically, | ||
) { | ||
Text( | ||
text = stringResource(id = action.first), | ||
fontWeight = FontWeight.Bold, | ||
color = Color(0xFF808080), | ||
maxLines = 1 | ||
) | ||
Icon( | ||
modifier = Modifier.size(dimensionResource(id = R.dimen.icon_small)), | ||
painter = painterResource(id = R.drawable.ic_baseline_chevron_right_24), | ||
contentDescription = null, | ||
) | ||
} | ||
} | ||
|
||
SecondaryButton( | ||
modifier = modifier | ||
.fillMaxWidth() | ||
.padding(vertical = dimensionResource(id = R.dimen.margin_medium)), | ||
onClick = onSignOutClick | ||
) { | ||
Text(text = stringResource(id = R.string.sign_out)) | ||
} | ||
} | ||
|
||
@Composable | ||
fun AccountContentSignedOutView( | ||
modifier: Modifier = Modifier, | ||
onSignInClick: (() -> Unit), | ||
) { | ||
PrimaryButton( | ||
modifier = modifier.fillMaxWidth(), | ||
onClick = onSignInClick | ||
) { | ||
Text(text = stringResource(id = R.string.sign_in)) | ||
} | ||
} | ||
|
||
@Preview(showBackground = true) | ||
@Composable | ||
private fun AccountContentViewSignedInStatePreview() { | ||
Column { | ||
AccountContentView( | ||
modifier = Modifier.padding(horizontal = dimensionResource(id = R.dimen.margin_medium)), | ||
user = DemoUser, | ||
onSignInClick = {}, | ||
onProfileSettingsClick = {}, | ||
onNotificationSettingsClick = {}, | ||
onOrderHistorySettingsClick = {}, | ||
) {} | ||
} | ||
} | ||
|
||
@Preview | ||
@Composable | ||
private fun AccountContentViewSignedOutStatePreview() { | ||
Column { | ||
AccountContentView( | ||
modifier = Modifier.padding(horizontal = dimensionResource(id = R.dimen.margin_medium)), | ||
onSignInClick = {}, | ||
onProfileSettingsClick = {}, | ||
onNotificationSettingsClick = {}, | ||
onOrderHistorySettingsClick = {}, | ||
) {} | ||
} | ||
} |
Oops, something went wrong.