-
Notifications
You must be signed in to change notification settings - Fork 226
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 #413 from Automattic/task/410-add-modal-structure
End of Year: Add modal structure
- Loading branch information
Showing
7 changed files
with
248 additions
and
3 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
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
142 changes: 142 additions & 0 deletions
142
...se/src/main/java/au/com/shiftyjelly/pocketcasts/compose/bottomsheet/BottomSheetContent.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,142 @@ | ||
package au.com.shiftyjelly.pocketcasts.compose.bottomsheet | ||
|
||
import androidx.compose.foundation.BorderStroke | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.wrapContentHeight | ||
import androidx.compose.foundation.layout.wrapContentWidth | ||
import androidx.compose.material.Button | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.OutlinedButton | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.text.style.TextAlign | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.tooling.preview.PreviewParameter | ||
import androidx.compose.ui.unit.dp | ||
import au.com.shiftyjelly.pocketcasts.compose.AppThemeWithBackground | ||
import au.com.shiftyjelly.pocketcasts.compose.components.TextH40 | ||
import au.com.shiftyjelly.pocketcasts.compose.components.TextP50 | ||
import au.com.shiftyjelly.pocketcasts.compose.preview.ThemePreviewParameterProvider | ||
import au.com.shiftyjelly.pocketcasts.compose.theme | ||
import au.com.shiftyjelly.pocketcasts.ui.theme.Theme | ||
|
||
private val outlinedBorder: BorderStroke | ||
@Composable | ||
get() = BorderStroke(2.dp, MaterialTheme.colors.primary) | ||
|
||
class BottomSheetContentState( | ||
val content: Content, | ||
) { | ||
data class Content( | ||
val titleText: String, | ||
val summaryText: String, | ||
val primaryButton: Button.Primary, | ||
val secondaryButton: Button.Secondary? = null, | ||
) { | ||
sealed interface Button { | ||
val label: String | ||
val onClick: (() -> Unit)? | ||
|
||
data class Primary( | ||
override val label: String, | ||
override val onClick: () -> Unit, | ||
) : Button | ||
|
||
data class Secondary( | ||
override val label: String, | ||
override val onClick: (() -> Unit)? = null, | ||
) : Button | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun BottomSheetContent( | ||
state: BottomSheetContentState, | ||
onDismiss: () -> Unit, | ||
modifier: Modifier = Modifier | ||
) { | ||
Box( | ||
modifier = modifier | ||
.fillMaxWidth() | ||
.wrapContentWidth(unbounded = false) | ||
.wrapContentHeight(unbounded = true) | ||
.padding(24.dp) | ||
) { | ||
Column( | ||
modifier = modifier.fillMaxSize(), | ||
horizontalAlignment = Alignment.CenterHorizontally | ||
) { | ||
val content = state.content | ||
|
||
Spacer(modifier = modifier.height(16.dp)) | ||
|
||
TextH40( | ||
text = content.titleText, | ||
color = MaterialTheme.theme.colors.primaryText01, | ||
) | ||
|
||
Spacer(modifier = modifier.height(16.dp)) | ||
|
||
TextP50( | ||
text = content.summaryText, | ||
style = MaterialTheme.typography.body1, | ||
color = MaterialTheme.theme.colors.primaryText02, | ||
textAlign = TextAlign.Center | ||
) | ||
|
||
Spacer(modifier = modifier.height(16.dp)) | ||
|
||
Button(onClick = content.primaryButton.onClick) { | ||
Text(text = content.primaryButton.label) | ||
} | ||
|
||
Spacer(modifier = modifier.height(8.dp)) | ||
|
||
if (content.secondaryButton != null) { | ||
OutlinedButton( | ||
border = outlinedBorder, | ||
onClick = { | ||
onDismiss.invoke() | ||
content.secondaryButton.onClick?.invoke() | ||
} | ||
) { | ||
Text(text = content.secondaryButton.label) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Preview(showBackground = true) | ||
@Composable | ||
private fun BottomSheetContentPreview( | ||
@PreviewParameter(ThemePreviewParameterProvider::class) themeType: Theme.ThemeType, | ||
) { | ||
AppThemeWithBackground(themeType) { | ||
BottomSheetContent( | ||
state = BottomSheetContentState( | ||
content = BottomSheetContentState.Content( | ||
titleText = "Heading", | ||
summaryText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.", | ||
primaryButton = BottomSheetContentState.Content.Button.Primary( | ||
label = "Confirm", | ||
onClick = {} | ||
), | ||
secondaryButton = BottomSheetContentState.Content.Button.Secondary( | ||
label = "Not now", | ||
), | ||
) | ||
), | ||
onDismiss = {} | ||
) | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...pose/src/main/java/au/com/shiftyjelly/pocketcasts/compose/bottomsheet/ModalBottomSheet.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,53 @@ | ||
package au.com.shiftyjelly.pocketcasts.compose.bottomsheet | ||
|
||
import androidx.activity.compose.BackHandler | ||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.material.ExperimentalMaterialApi | ||
import androidx.compose.material.ModalBottomSheetLayout | ||
import androidx.compose.material.ModalBottomSheetState | ||
import androidx.compose.material.ModalBottomSheetValue | ||
import androidx.compose.material.rememberModalBottomSheetState | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.rememberCoroutineScope | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.unit.dp | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
|
||
@OptIn(ExperimentalMaterialApi::class) | ||
@Composable | ||
fun ModalBottomSheet( | ||
showOnLoad: Boolean = false, | ||
content: BottomSheetContentState.Content, | ||
) { | ||
val sheetState = rememberModalBottomSheetState( | ||
initialValue = if (showOnLoad) ModalBottomSheetValue.Expanded else ModalBottomSheetValue.Hidden, | ||
) | ||
val coroutineScope = rememberCoroutineScope() | ||
|
||
BackHandler(sheetState.isVisible) { | ||
hideBottomSheet(coroutineScope, sheetState) | ||
} | ||
|
||
ModalBottomSheetLayout( | ||
sheetState = sheetState, | ||
sheetContent = { | ||
BottomSheetContent( | ||
state = BottomSheetContentState(content), | ||
onDismiss = { | ||
hideBottomSheet(coroutineScope, sheetState) | ||
} | ||
) | ||
}, | ||
sheetShape = RoundedCornerShape(topStart = 16.dp, topEnd = 16.dp), | ||
scrimColor = Color.Black.copy(alpha = .25f), | ||
content = {} | ||
) | ||
} | ||
|
||
@OptIn(ExperimentalMaterialApi::class) | ||
fun hideBottomSheet(coroutineScope: CoroutineScope, sheetState: ModalBottomSheetState) { | ||
coroutineScope.launch { | ||
sheetState.hide() | ||
} | ||
} |
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