-
Notifications
You must be signed in to change notification settings - Fork 645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new CustomerSheet
playground
#8362
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
da198f9
Add new `CustomerSheet` playground
samer-stripe 21b66a3
Fix bugs & add improvements to `CustomerSheet` playground.
samer-stripe 6e5ce07
Make `fetchOption` use `viewModelScope` instead of being suspendable.
samer-stripe 78dc8c4
Use public `rememberCustomerSheet`
samer-stripe fbdf84f
Move `CoroutineContext` into `launch` function in `fetchOption` & upd…
samer-stripe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
240 changes: 240 additions & 0 deletions
240
.../android/paymentsheet/example/playground/customersheet/CustomerSheetPlaygroundActivity.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,240 @@ | ||
package com.stripe.android.paymentsheet.example.playground.customersheet | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.widget.Toast | ||
import androidx.activity.compose.setContent | ||
import androidx.activity.viewModels | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.compose.animation.AnimatedContent | ||
import androidx.compose.animation.animateContentSize | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.isSystemInDarkTheme | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.ColumnScope | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.rememberScrollState | ||
import androidx.compose.foundation.verticalScroll | ||
import androidx.compose.material.Button | ||
import androidx.compose.material.Divider | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.Scaffold | ||
import androidx.compose.material.Surface | ||
import androidx.compose.material.Text | ||
import androidx.compose.material.darkColors | ||
import androidx.compose.material.lightColors | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.platform.testTag | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
import com.stripe.android.customersheet.CustomerSheetResultCallback | ||
import com.stripe.android.customersheet.ExperimentalCustomerSheetApi | ||
import com.stripe.android.customersheet.rememberCustomerSheet | ||
import com.stripe.android.paymentsheet.example.playground.activity.AppearanceBottomSheetDialogFragment | ||
import com.stripe.android.paymentsheet.example.playground.activity.AppearanceStore | ||
import com.stripe.android.paymentsheet.example.playground.activity.QrCodeActivity | ||
import com.stripe.android.paymentsheet.example.playground.customersheet.settings.CustomerSheetPlaygroundSettings | ||
import com.stripe.android.paymentsheet.example.playground.customersheet.settings.SettingsUi | ||
import com.stripe.android.paymentsheet.example.samples.ui.shared.PaymentMethodSelector | ||
|
||
internal class CustomerSheetPlaygroundActivity : AppCompatActivity() { | ||
companion object { | ||
fun createTestIntent(settingsJson: String): Intent { | ||
return Intent( | ||
Intent.ACTION_VIEW, | ||
CustomerSheetPlaygroundUrlHelper.createUri(settingsJson) | ||
) | ||
} | ||
} | ||
|
||
val viewModel: CustomerSheetPlaygroundViewModel by viewModels { | ||
CustomerSheetPlaygroundViewModel.Factory( | ||
applicationSupplier = { application }, | ||
uriSupplier = { intent.data }, | ||
) | ||
} | ||
|
||
@SuppressLint("UnusedContentLambdaTargetStateParameter") | ||
@OptIn(ExperimentalCustomerSheetApi::class) | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
setContent { | ||
val playgroundSettings by viewModel.playgroundSettingsFlow.collectAsState() | ||
val localPlaygroundSettings = playgroundSettings ?: return@setContent | ||
|
||
val playgroundState by viewModel.playgroundState.collectAsState() | ||
val context = LocalContext.current | ||
|
||
LaunchedEffect(viewModel.status) { | ||
viewModel.status.collect { status -> | ||
Toast.makeText(context, status.message, Toast.LENGTH_LONG).show() | ||
} | ||
} | ||
|
||
PlaygroundTheme( | ||
content = { | ||
SettingsUi(playgroundSettings = localPlaygroundSettings) | ||
|
||
AppearanceButton() | ||
|
||
QrCodeButton(playgroundSettings = localPlaygroundSettings) | ||
}, | ||
bottomBarContent = { | ||
ReloadButton() | ||
|
||
AnimatedContent( | ||
label = PLAYGROUND_BOTTOM_BAR_LABEL, | ||
targetState = playgroundState != null, | ||
) { | ||
Column { | ||
PlaygroundStateUi( | ||
playgroundState = playgroundState, | ||
callback = viewModel::onCustomerSheetCallback | ||
) | ||
} | ||
} | ||
}, | ||
) | ||
} | ||
} | ||
|
||
@Composable | ||
private fun AppearanceButton() { | ||
Button( | ||
onClick = { | ||
val bottomSheet = AppearanceBottomSheetDialogFragment.newInstance() | ||
bottomSheet.show(supportFragmentManager, bottomSheet.tag) | ||
}, | ||
modifier = Modifier.fillMaxWidth(), | ||
) { | ||
Text("Change Appearance") | ||
} | ||
} | ||
|
||
@Composable | ||
private fun QrCodeButton(playgroundSettings: CustomerSheetPlaygroundSettings) { | ||
val context = LocalContext.current | ||
Button( | ||
onClick = { | ||
context.startActivity( | ||
QrCodeActivity.create( | ||
context = context, | ||
settingsUri = CustomerSheetPlaygroundUrlHelper.createUri( | ||
playgroundSettings.snapshot().asJsonString() | ||
), | ||
) | ||
) | ||
}, | ||
modifier = Modifier.fillMaxWidth(), | ||
) { | ||
Text("QR code for current settings") | ||
} | ||
} | ||
|
||
@Composable | ||
private fun ReloadButton() { | ||
Button( | ||
onClick = viewModel::reset, | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.testTag(RELOAD_TEST_TAG), | ||
) { | ||
Text("Reload") | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalCustomerSheetApi::class) | ||
@Composable | ||
private fun PlaygroundStateUi( | ||
playgroundState: CustomerSheetPlaygroundState?, | ||
callback: CustomerSheetResultCallback, | ||
) { | ||
if (playgroundState == null) { | ||
return | ||
} | ||
|
||
val customerSheet = rememberCustomerSheet( | ||
configuration = playgroundState.customerSheetConfiguration(), | ||
customerAdapter = playgroundState.adapter, | ||
callback = callback, | ||
) | ||
|
||
LaunchedEffect(customerSheet) { | ||
viewModel.fetchOption(customerSheet) | ||
samer-stripe marked this conversation as resolved.
Show resolved
Hide resolved
samer-stripe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
val loaded = playgroundState.optionState as? CustomerSheetPlaygroundState.PaymentOptionState.Loaded | ||
val option = loaded?.paymentOption | ||
|
||
PaymentMethodSelector( | ||
isEnabled = playgroundState.optionState is CustomerSheetPlaygroundState.PaymentOptionState.Loaded, | ||
paymentMethodLabel = option?.label ?: "Select", | ||
paymentMethodPainter = option?.iconPainter, | ||
onClick = customerSheet::present | ||
) | ||
} | ||
} | ||
|
||
@Composable | ||
private fun PlaygroundTheme( | ||
content: @Composable ColumnScope.() -> Unit, | ||
bottomBarContent: @Composable ColumnScope.() -> Unit, | ||
) { | ||
val colors = if (isSystemInDarkTheme() || AppearanceStore.forceDarkMode) { | ||
darkColors() | ||
} else { | ||
lightColors() | ||
} | ||
MaterialTheme( | ||
typography = MaterialTheme.typography.copy( | ||
body1 = MaterialTheme.typography.body1.copy(fontSize = 14.sp) | ||
), | ||
colors = colors, | ||
) { | ||
Surface( | ||
color = MaterialTheme.colors.background, | ||
) { | ||
Scaffold( | ||
bottomBar = { | ||
Column( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.background(MaterialTheme.colors.surface) | ||
.animateContentSize() | ||
) { | ||
Divider() | ||
Column( | ||
content = bottomBarContent, | ||
modifier = Modifier | ||
.padding(horizontal = 12.dp, vertical = 8.dp) | ||
.fillMaxWidth() | ||
) | ||
} | ||
}, | ||
) { paddingValues -> | ||
Box(modifier = Modifier.padding(paddingValues)) { | ||
Column( | ||
modifier = Modifier | ||
.verticalScroll(rememberScrollState()) | ||
.fillMaxSize() | ||
.padding(16.dp), | ||
content = content, | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
const val RELOAD_TEST_TAG = "RELOAD" | ||
private const val PLAYGROUND_BOTTOM_BAR_LABEL = "CustomerSheetPlaygroundBottomBar" |
27 changes: 27 additions & 0 deletions
27
...ipe/android/paymentsheet/example/playground/customersheet/CustomerSheetPlaygroundState.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,27 @@ | ||
package com.stripe.android.paymentsheet.example.playground.customersheet | ||
|
||
import androidx.compose.runtime.Stable | ||
import com.stripe.android.customersheet.CustomerAdapter | ||
import com.stripe.android.customersheet.CustomerSheet | ||
import com.stripe.android.customersheet.ExperimentalCustomerSheetApi | ||
import com.stripe.android.paymentsheet.example.playground.customersheet.settings.CustomerSheetPlaygroundSettings | ||
import com.stripe.android.paymentsheet.model.PaymentOption | ||
|
||
@Stable | ||
@OptIn(ExperimentalCustomerSheetApi::class) | ||
internal data class CustomerSheetPlaygroundState( | ||
private val snapshot: CustomerSheetPlaygroundSettings.Snapshot, | ||
val adapter: CustomerAdapter, | ||
val optionState: PaymentOptionState, | ||
) { | ||
sealed interface PaymentOptionState { | ||
data object Unloaded : PaymentOptionState | ||
|
||
data class Loaded(val paymentOption: PaymentOption?) : PaymentOptionState | ||
} | ||
|
||
@OptIn(ExperimentalCustomerSheetApi::class) | ||
fun customerSheetConfiguration(): CustomerSheet.Configuration { | ||
return snapshot.customerSheetConfiguration(this) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...android/paymentsheet/example/playground/customersheet/CustomerSheetPlaygroundUrlHelper.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,23 @@ | ||
package com.stripe.android.paymentsheet.example.playground.customersheet | ||
|
||
import android.net.Uri | ||
import com.stripe.android.paymentsheet.example.playground.customersheet.settings.CustomerSheetPlaygroundSettings | ||
import okio.ByteString.Companion.decodeBase64 | ||
import okio.ByteString.Companion.toByteString | ||
|
||
internal object CustomerSheetPlaygroundUrlHelper { | ||
fun createUri(settingsJson: String): Uri { | ||
val base64Settings = settingsJson.encodeToByteArray().toByteString().base64Url() | ||
.trimEnd('=') | ||
return Uri.parse( | ||
"stripepaymentsheetexample://customersheetplayground?settings=" + | ||
base64Settings | ||
) | ||
} | ||
|
||
fun settingsFromUri(uri: Uri?): CustomerSheetPlaygroundSettings? { | ||
val settingsJson = uri?.getQueryParameter("settings") | ||
?.decodeBase64()?.utf8() ?: return null | ||
return CustomerSheetPlaygroundSettings.createFromJsonString(settingsJson) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like you replaced this menu item, but didn't delete the old playground.