Skip to content

Commit

Permalink
[Connect SDK] UI for Connect SDK Example App (#9276)
Browse files Browse the repository at this point in the history
* Build UI of example app

# Conflicts:
#	gradle.properties

* Update build.gradle to use version catalog

* Update EmbeddedComponentService.kt

* Update PayoutsExampleActivity.kt

* Update AccountOnboardingExampleActivity.kt

* Fix dependencies

* Fix lint

* Fix fetch client secret

* Fix lint

* Fix lint

* Move to color/string res

* fix color values
  • Loading branch information
simond-stripe authored Oct 7, 2024
1 parent 8f35fe0 commit 5fce962
Show file tree
Hide file tree
Showing 13 changed files with 419 additions and 44 deletions.
2 changes: 2 additions & 0 deletions stripe-connect-example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ dependencies {

// Compose
implementation libs.compose.ui
implementation libs.compose.uiToolingPreview
implementation libs.compose.uiViewBinding
implementation libs.compose.foundation
implementation libs.compose.material
implementation libs.compose.materialIcons
implementation libs.compose.activity
implementation libs.compose.navigation
implementation libs.accompanist.systemUiController
debugImplementation libs.compose.uiTooling

// Test
testImplementation testLibs.androidx.archCore
Expand Down
19 changes: 19 additions & 0 deletions stripe-connect-example/dependencies/dependencies.txt
Original file line number Diff line number Diff line change
Expand Up @@ -376,12 +376,14 @@
| | | | +--- androidx.compose.ui:ui-geometry:1.5.4 (c)
| | | | +--- androidx.compose.ui:ui-graphics:1.5.4 (c)
| | | | +--- androidx.compose.ui:ui-text:1.5.4 (c)
| | | | +--- androidx.compose.ui:ui-tooling-preview:1.5.4 (c)
| | | | +--- androidx.compose.ui:ui-unit:1.5.4 (c)
| | | | \--- androidx.compose.ui:ui-viewbinding:1.5.4 (c)
| | | +--- org.jetbrains.kotlin:kotlin-stdlib:1.8.21 -> 1.9.24 (*)
| | | +--- androidx.compose.ui:ui:1.5.4 (c)
| | | +--- androidx.compose.ui:ui-graphics:1.5.4 (c)
| | | +--- androidx.compose.ui:ui-text:1.5.4 (c)
| | | +--- androidx.compose.ui:ui-tooling-preview:1.5.4 (c)
| | | +--- androidx.compose.ui:ui-unit:1.5.4 (c)
| | | +--- androidx.compose.ui:ui-util:1.5.4 (c)
| | | \--- androidx.compose.ui:ui-viewbinding:1.5.4 (c)
Expand All @@ -400,12 +402,14 @@
| | | | +--- androidx.compose.ui:ui-geometry:1.5.4 (c)
| | | | +--- androidx.compose.ui:ui-graphics:1.5.4 (c)
| | | | +--- androidx.compose.ui:ui-text:1.5.4 (c)
| | | | +--- androidx.compose.ui:ui-tooling-preview:1.5.4 (c)
| | | | +--- androidx.compose.ui:ui-util:1.5.4 (c)
| | | | \--- androidx.compose.ui:ui-viewbinding:1.5.4 (c)
| | | +--- androidx.compose.ui:ui-util:1.5.4 (*)
| | | +--- androidx.compose.ui:ui:1.5.4 (c)
| | | +--- androidx.compose.ui:ui-geometry:1.5.4 (c)
| | | +--- androidx.compose.ui:ui-text:1.5.4 (c)
| | | +--- androidx.compose.ui:ui-tooling-preview:1.5.4 (c)
| | | +--- androidx.compose.ui:ui-unit:1.5.4 (c)
| | | +--- androidx.compose.ui:ui-util:1.5.4 (c)
| | | \--- androidx.compose.ui:ui-viewbinding:1.5.4 (c)
Expand All @@ -425,6 +429,7 @@
| | | +--- androidx.compose.ui:ui:1.5.4 (c)
| | | +--- androidx.compose.ui:ui-geometry:1.5.4 (c)
| | | +--- androidx.compose.ui:ui-graphics:1.5.4 (c)
| | | +--- androidx.compose.ui:ui-tooling-preview:1.5.4 (c)
| | | +--- androidx.compose.ui:ui-unit:1.5.4 (c)
| | | +--- androidx.compose.ui:ui-util:1.5.4 (c)
| | | \--- androidx.compose.ui:ui-viewbinding:1.5.4 (c)
Expand All @@ -446,6 +451,7 @@
| | +--- androidx.compose.ui:ui-geometry:1.5.4 (c)
| | +--- androidx.compose.ui:ui-graphics:1.5.4 (c)
| | +--- androidx.compose.ui:ui-text:1.5.4 (c)
| | +--- androidx.compose.ui:ui-tooling-preview:1.5.4 (c)
| | +--- androidx.compose.ui:ui-unit:1.5.4 (c)
| | +--- androidx.compose.ui:ui-util:1.5.4 (c)
| | +--- androidx.compose.ui:ui-viewbinding:1.5.4 (c)
Expand All @@ -457,6 +463,7 @@
| | +--- androidx.fragment:fragment-ktx:1.3.2 -> 1.6.2 (*)
| | +--- org.jetbrains.kotlin:kotlin-stdlib:1.8.21 -> 1.9.24 (*)
| | +--- androidx.compose.ui:ui:1.5.4 (c)
| | +--- androidx.compose.ui:ui-tooling-preview:1.5.4 (c)
| | +--- androidx.compose.ui:ui-util:1.5.4 (c)
| | +--- androidx.compose.ui:ui-geometry:1.5.4 (c)
| | +--- androidx.compose.ui:ui-graphics:1.5.4 (c)
Expand Down Expand Up @@ -646,6 +653,18 @@
+--- androidx.lifecycle:lifecycle-viewmodel-savedstate:2.7.0 (*)
+--- androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0 (*)
+--- androidx.compose.ui:ui:1.5.4 (*)
+--- androidx.compose.ui:ui-tooling-preview:1.5.4
| \--- androidx.compose.ui:ui-tooling-preview-android:1.5.4
| +--- androidx.annotation:annotation:1.2.0 -> 1.7.1 (*)
| +--- androidx.compose.runtime:runtime:1.5.4 (*)
| +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.8.21 -> 1.9.24 (*)
| +--- androidx.compose.ui:ui:1.5.4 (c)
| +--- androidx.compose.ui:ui-geometry:1.5.4 (c)
| +--- androidx.compose.ui:ui-graphics:1.5.4 (c)
| +--- androidx.compose.ui:ui-text:1.5.4 (c)
| +--- androidx.compose.ui:ui-unit:1.5.4 (c)
| +--- androidx.compose.ui:ui-util:1.5.4 (c)
| \--- androidx.compose.ui:ui-viewbinding:1.5.4 (c)
+--- androidx.compose.ui:ui-viewbinding:1.5.4 (*)
+--- androidx.compose.foundation:foundation:1.5.4 (*)
+--- androidx.compose.material:material:1.5.4 (*)
Expand Down
13 changes: 11 additions & 2 deletions stripe-connect-example/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application>
<activity
android:name=".MainActivity"
android:exported="true">
android:exported="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".ui.accountonboarding.AccountOnboardingExampleActivity"
android:exported="false"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
<activity
android:name=".ui.payouts.PayoutsExampleActivity"
android:exported="false"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -1,69 +1,175 @@
package com.stripe.android.connectsdk.example

import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.border
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.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyItemScope
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Divider
import androidx.compose.material.Icon
import androidx.compose.material.Text
import com.stripe.android.connectsdk.EmbeddedComponentManager
import com.stripe.android.connectsdk.FetchClientSecretCallback
import com.stripe.android.connectsdk.FetchClientSecretCallback.ClientSecretResultCallback
import com.stripe.android.connectsdk.PrivateBetaConnectSDK
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.KeyboardArrowRight
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.LineHeightStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.stripe.android.connectsdk.example.ui.accountonboarding.AccountOnboardingExampleActivity
import com.stripe.android.connectsdk.example.ui.payouts.PayoutsExampleActivity

@OptIn(PrivateBetaConnectSDK::class)
class MainActivity : ComponentActivity() {

private lateinit var embeddedComponentManager: EmbeddedComponentManager
private data class MenuItem(
val title: String,
val subtitle: String,
val activity: Class<out ComponentActivity>,
val isBeta: Boolean = false,
)

private val menuItems = listOf(
MenuItem(
title = resources.getString(R.string.account_onboarding),
subtitle = resources.getString(R.string.account_onboarding_menu_subtitle),
activity = AccountOnboardingExampleActivity::class.java,
isBeta = true,
),
MenuItem(
title = resources.getString(R.string.payouts),
subtitle = resources.getString(R.string.payouts_menu_subtitle),
activity = PayoutsExampleActivity::class.java,
isBeta = true,
),
)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

embeddedComponentManager = EmbeddedComponentManager(
activity = this,
configuration = EmbeddedComponentManager.Configuration(
publishableKey = "<publishable key here>"
),
fetchClientSecret = object : FetchClientSecretCallback {
override fun fetchClientSecret(resultCallback: ClientSecretResultCallback) {
// Make a network call to fetch your client secret here
resultCallback.onResult("<client secret here>")
setContent {
ConnectSdkExampleTheme {
MainContent(title = stringResource(R.string.connect_sdk_example)) {
ComponentList(menuItems)
}
}
)

setContent {
Text("Not yet built...")
}
}

fun launchPayouts() {
// launch the payouts activity
embeddedComponentManager.presentPayouts()
@Composable
private fun ComponentList(components: List<MenuItem>) {
LazyColumn {
items(components) { menuItem ->
MenuRowItem(menuItem)
}
}
}

fun launchAccountOnboardingActivity() {
// launch the account onboarding activity
embeddedComponentManager.presentAccountOnboarding()
@Composable
private fun LazyItemScope.MenuRowItem(menuItem: MenuItem) {
val context = LocalContext.current
Row(
modifier = Modifier
.fillParentMaxWidth()
.clickable { context.startActivity(Intent(context, menuItem.activity)) },
verticalAlignment = Alignment.CenterVertically,
) {
Column(modifier = Modifier.weight(1f)) {
Column(modifier = Modifier.padding(8.dp)) {
Spacer(modifier = Modifier.height(8.dp))
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
Text(
text = menuItem.title,
fontSize = 16.sp,
fontWeight = FontWeight.SemiBold,
)
if (menuItem.isBeta) {
BetaBadge()
}
}
Spacer(modifier = Modifier.height(8.dp))
Text(

text = menuItem.subtitle,
fontSize = 16.sp,
)
}
Spacer(modifier = Modifier.height(8.dp))
Divider()
}

Icon(
modifier = Modifier
.size(36.dp)
.padding(start = 8.dp),
contentDescription = null,
imageVector = Icons.Default.KeyboardArrowRight,
)
}
}

fun updateAppearance() {
// update the appearance
embeddedComponentManager.update(
EmbeddedComponentManager.Appearance(
typography = EmbeddedComponentManager.Appearance.Typography(),
colors = EmbeddedComponentManager.Appearance.Colors(),
buttonPrimary = EmbeddedComponentManager.Appearance.Button(),
buttonSecondary = EmbeddedComponentManager.Appearance.Button(),
badgeNeutral = EmbeddedComponentManager.Appearance.Badge(),
badgeSuccess = EmbeddedComponentManager.Appearance.Badge(),
badgeWarning = EmbeddedComponentManager.Appearance.Badge(),
badgeDanger = EmbeddedComponentManager.Appearance.Badge(),
cornerRadius = EmbeddedComponentManager.Appearance.CornerRadius()
@Composable
private fun BetaBadge() {
val shape = RoundedCornerShape(4.dp)
val labelMediumEmphasized = TextStyle.Default.copy(
fontSize = 14.sp,
fontWeight = FontWeight.SemiBold,
lineHeight = 20.sp,
lineHeightStyle = LineHeightStyle(
alignment = LineHeightStyle.Alignment.Center,
trim = LineHeightStyle.Trim.None
)
)
Text(
modifier = Modifier
.border(1.dp, colorResource(R.color.default_border_color), shape)
.background(
color = colorResource(R.color.default_background_color),
shape = shape
)
.padding(horizontal = 6.dp, vertical = 1.dp),
color = colorResource(R.color.default_text_color),
fontSize = 12.sp,
lineHeight = 16.sp,
style = labelMediumEmphasized,
text = "BETA"
)
}

fun logout() {
// log out the user
embeddedComponentManager.logout()
@Composable
@Preview(showBackground = true)
fun ComponentListPreview() {
ConnectSdkExampleTheme {
ComponentList(menuItems)
}
}

@Composable
@Preview(showBackground = true)
fun BetaBadgePreview() {
ConnectSdkExampleTheme {
BetaBadge()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.stripe.android.connectsdk.example

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Divider
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material.darkColors
import androidx.compose.material.lightColors
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@Composable
fun ConnectSdkExampleTheme(
content: @Composable () -> Unit,
) {
MaterialTheme(
colors = if (isSystemInDarkTheme()) darkColors() else lightColors(),
content = content,
)
}

@Composable
fun MainContent(
title: String,
content: @Composable () -> Unit,
) {
Scaffold(
topBar = {
Column {
Text(
modifier = Modifier.padding(8.dp),
text = title,
fontSize = 24.sp,
fontWeight = FontWeight.SemiBold,
)
Divider()
}
}
) { contentPadding ->
Box(
modifier = Modifier
.fillMaxSize()
.padding(contentPadding),
) {
content()
}
}
}
Loading

0 comments on commit 5fce962

Please sign in to comment.