forked from ReVanced/revanced-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(NotificationCard): rewrite & consistent usage (ReVanced#1426)
- Loading branch information
Showing
6 changed files
with
163 additions
and
120 deletions.
There are no files selected for viewing
156 changes: 134 additions & 22 deletions
156
app/src/main/java/app/revanced/manager/ui/component/NotificationCard.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 |
---|---|---|
@@ -1,59 +1,171 @@ | ||
package app.revanced.manager.ui.component | ||
|
||
import androidx.compose.foundation.background | ||
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.width | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.outlined.Close | ||
import androidx.compose.material3.Card | ||
import androidx.compose.material3.CardColors | ||
import androidx.compose.material3.CardDefaults | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.IconButton | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.clip | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.vector.ImageVector | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.unit.dp | ||
import app.revanced.manager.R | ||
|
||
@Composable | ||
fun NotificationCard( | ||
color: Color, | ||
isWarning: Boolean = false, | ||
title: String? = null, | ||
text: String, | ||
icon: ImageVector, | ||
actions: (@Composable () -> Unit)? | ||
) { | ||
NotificationCardInstance(isWarning = isWarning) { | ||
Column( | ||
modifier = Modifier.padding(if (title != null) 20.dp else 16.dp), | ||
verticalArrangement = Arrangement.spacedBy(16.dp) | ||
) { | ||
if (title != null) { | ||
Icon( | ||
modifier = Modifier.size(36.dp), | ||
imageVector = icon, | ||
contentDescription = null, | ||
tint = if (isWarning) MaterialTheme.colorScheme.onError else MaterialTheme.colorScheme.onPrimaryContainer | ||
) | ||
Column( | ||
verticalArrangement = Arrangement.spacedBy(6.dp) | ||
) { | ||
Text( | ||
text = title, | ||
style = MaterialTheme.typography.titleLarge, | ||
color = if (isWarning) MaterialTheme.colorScheme.onError else MaterialTheme.colorScheme.onPrimaryContainer | ||
) | ||
Text( | ||
text = text, | ||
style = MaterialTheme.typography.bodyMedium, | ||
color = if (isWarning) MaterialTheme.colorScheme.onError else MaterialTheme.colorScheme.onPrimaryContainer | ||
) | ||
} | ||
} else { | ||
Row(horizontalArrangement = Arrangement.spacedBy(16.dp)) { | ||
Icon( | ||
modifier = Modifier.size(24.dp), | ||
imageVector = icon, | ||
contentDescription = null, | ||
tint = if (isWarning) MaterialTheme.colorScheme.onError else MaterialTheme.colorScheme.onPrimaryContainer | ||
) | ||
Text( | ||
text = text, | ||
style = MaterialTheme.typography.bodyMedium, | ||
color = if (isWarning) MaterialTheme.colorScheme.onError else MaterialTheme.colorScheme.onPrimaryContainer | ||
) | ||
} | ||
} | ||
actions?.invoke() | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun NotificationCard( | ||
isWarning: Boolean = false, | ||
title: String? = null, | ||
text: String, | ||
content: (@Composable () -> Unit)? = null, | ||
icon: ImageVector, | ||
onDismiss: (() -> Unit)? = null, | ||
primaryAction: (() -> Unit)? = null | ||
) { | ||
Card( | ||
colors = CardDefaults.cardColors(containerColor = color), | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.clip(RoundedCornerShape(28.dp)) | ||
) { | ||
NotificationCardInstance(isWarning = isWarning, onClick = primaryAction) { | ||
Row( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(16.dp), | ||
verticalAlignment = Alignment.CenterVertically, | ||
horizontalArrangement = Arrangement.spacedBy( | ||
16.dp, | ||
Alignment.Start | ||
) | ||
horizontalArrangement = Arrangement.spacedBy(16.dp), | ||
) { | ||
Icon( | ||
modifier = Modifier.size(if (title != null) 36.dp else 24.dp), | ||
imageVector = icon, | ||
contentDescription = null, | ||
tint = if (isWarning) MaterialTheme.colorScheme.onError else MaterialTheme.colorScheme.onPrimaryContainer | ||
) | ||
Text( | ||
modifier = if (content != null) Modifier.width(220.dp) else Modifier, | ||
text = text, | ||
style = MaterialTheme.typography.bodyMedium | ||
) | ||
content?.invoke() | ||
if (title != null) { | ||
Column( | ||
modifier = Modifier.weight(1f), | ||
verticalArrangement = Arrangement.spacedBy(6.dp) | ||
) { | ||
Text( | ||
text = title, | ||
style = MaterialTheme.typography.titleLarge, | ||
color = if (isWarning) MaterialTheme.colorScheme.onError else MaterialTheme.colorScheme.onPrimaryContainer | ||
) | ||
Text( | ||
text = text, | ||
style = MaterialTheme.typography.bodyMedium, | ||
color = if (isWarning) MaterialTheme.colorScheme.onError else MaterialTheme.colorScheme.onPrimaryContainer | ||
) | ||
} | ||
} else { | ||
Text( | ||
modifier = Modifier.weight(1f), | ||
text = text, | ||
style = MaterialTheme.typography.bodyMedium, | ||
color = if (isWarning) MaterialTheme.colorScheme.onError else MaterialTheme.colorScheme.onPrimaryContainer | ||
) | ||
} | ||
if (onDismiss != null) { | ||
IconButton(onClick = onDismiss) { | ||
Icon( | ||
imageVector = Icons.Outlined.Close, | ||
contentDescription = stringResource(R.string.close), | ||
tint = if (isWarning) MaterialTheme.colorScheme.onError else MaterialTheme.colorScheme.onPrimaryContainer | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@Composable | ||
private fun NotificationCardInstance( | ||
isWarning: Boolean = false, | ||
onClick: (() -> Unit)? = null, | ||
content: (@Composable () -> Unit), | ||
) { | ||
if (onClick != null) { | ||
Card( | ||
onClick = onClick, | ||
colors = CardDefaults.cardColors(containerColor = (if (isWarning) MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.primaryContainer)), | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(16.dp) | ||
.clip(RoundedCornerShape(24.dp)) | ||
) { | ||
content.invoke() | ||
} | ||
} else { | ||
Card( | ||
colors = CardDefaults.cardColors(containerColor = (if (isWarning) MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.primaryContainer)), | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(16.dp) | ||
.clip(RoundedCornerShape(24.dp)) | ||
) { | ||
content.invoke() | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.