Skip to content

Commit

Permalink
Merge branch 'replace-topbars-constraintlayout-with-row-droid-340'
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Sep 25, 2023
2 parents bdac804 + 67d3484 commit f0bb6f1
Showing 1 changed file with 51 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ package net.mullvad.mullvadvpn.compose.component
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
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.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.constraintlayout.compose.ConstraintLayout
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.lib.theme.AppTheme
import net.mullvad.mullvadvpn.lib.theme.Dimens
Expand All @@ -34,6 +35,34 @@ private fun PreviewTopBar() {
}
}

@Preview
@Composable
private fun PreviewNoIconAndLogoTopBar() {
AppTheme {
TopBar(
backgroundColor = MaterialTheme.colorScheme.inversePrimary,
iconTintColor = MaterialTheme.colorScheme.onPrimary,
isIconAndLogoVisible = false,
onSettingsClicked = {},
onAccountClicked = null,
)
}
}

@Preview
@Composable
private fun PreviewNothingTopBar() {
AppTheme {
TopBar(
backgroundColor = MaterialTheme.colorScheme.inversePrimary,
iconTintColor = MaterialTheme.colorScheme.onPrimary,
isIconAndLogoVisible = false,
onSettingsClicked = null,
onAccountClicked = null
)
}
}

@Composable
fun TopBar(
backgroundColor: Color,
Expand All @@ -43,41 +72,32 @@ fun TopBar(
iconTintColor: Color,
isIconAndLogoVisible: Boolean = true
) {
ConstraintLayout(
Row(
modifier =
Modifier.fillMaxWidth()
.height(Dimens.topBarHeight)
.background(backgroundColor)
.then(modifier),
verticalAlignment = Alignment.CenterVertically
) {
val (logo, appName, accountIcon, settingsIcon) = createRefs()

if (isIconAndLogoVisible) {
Image(
painter = painterResource(id = R.drawable.logo_icon),
contentDescription = null, // No meaningful user info or action.
modifier =
Modifier.padding(start = Dimens.mediumPadding)
.width(Dimens.buttonHeight)
.height(Dimens.buttonHeight)
.constrainAs(logo) {
centerVerticallyTo(parent)
start.linkTo(parent.start)
}
)

Icon(
painter = painterResource(id = R.drawable.logo_text),
tint = iconTintColor,
contentDescription = null, // No meaningful user info or action.
modifier =
Modifier.padding(start = Dimens.smallPadding)
.height(Dimens.mediumPadding)
.constrainAs(appName) {
centerVerticallyTo(parent)
start.linkTo(logo.end)
}
)
Row(
Modifier.height(Dimens.topBarHeight).weight(1f).padding(start = Dimens.mediumPadding),
verticalAlignment = Alignment.CenterVertically
) {
if (isIconAndLogoVisible) {
Image(
painter = painterResource(id = R.drawable.logo_icon),
contentDescription = null, // No meaningful user info or action.
modifier = Modifier.size(Dimens.buttonHeight)
)
Icon(
painter = painterResource(id = R.drawable.logo_text),
tint = iconTintColor,
contentDescription = null, // No meaningful user info or action.
modifier =
Modifier.padding(start = Dimens.smallPadding).height(Dimens.mediumPadding)
)
}
}

if (onAccountClicked != null) {
Expand All @@ -88,13 +108,6 @@ fun TopBar(
Modifier.clickable { onAccountClicked() }
.fillMaxHeight()
.padding(horizontal = Dimens.mediumPadding)
.constrainAs(accountIcon) {
if (onSettingsClicked != null) {
end.linkTo(settingsIcon.start)
} else {
end.linkTo(parent.end)
}
}
)
}

Expand All @@ -106,7 +119,6 @@ fun TopBar(
Modifier.clickable { onSettingsClicked() }
.fillMaxHeight()
.padding(horizontal = Dimens.mediumPadding)
.constrainAs(settingsIcon) { end.linkTo(parent.end) }
)
}
}
Expand Down

0 comments on commit f0bb6f1

Please sign in to comment.