From 6da72a6ba7f42465d1a2b4ae6a978964f073a44f Mon Sep 17 00:00:00 2001 From: Larry Ng Date: Fri, 13 Dec 2024 15:35:32 -0500 Subject: [PATCH] add light/dark theme example --- .../example/ui/appearance/AppearanceInfo.kt | 50 ++++++++++++ .../common/BasicExampleComponentActivity.kt | 19 ++++- .../src/main/res/values-night/colors.xml | 81 +++++++++++++++++++ .../src/main/res/values/colors.xml | 28 ++++++- .../src/main/res/values/strings.xml | 1 + 5 files changed, 176 insertions(+), 3 deletions(-) create mode 100644 connect-example/src/main/res/values-night/colors.xml diff --git a/connect-example/src/main/java/com/stripe/android/connect/example/ui/appearance/AppearanceInfo.kt b/connect-example/src/main/java/com/stripe/android/connect/example/ui/appearance/AppearanceInfo.kt index 7a2e4d57b4c..980153ad3dd 100644 --- a/connect-example/src/main/java/com/stripe/android/connect/example/ui/appearance/AppearanceInfo.kt +++ b/connect-example/src/main/java/com/stripe/android/connect/example/ui/appearance/AppearanceInfo.kt @@ -27,6 +27,7 @@ data class AppearanceInfo( HotDog(R.string.appearance_hot_dog), OceanBreeze(R.string.appearance_ocean_breeze), Link(R.string.appearance_link), + Dynamic(R.string.appearance_dynamic), CustomFont(R.string.custom_font) } @@ -38,6 +39,7 @@ data class AppearanceInfo( AppearanceId.HotDog -> hotDogAppearance(context) AppearanceId.OceanBreeze -> oceanBreezeAppearance(context) AppearanceId.Link -> linkAppearance(context) + AppearanceId.Dynamic -> dynamicAppearance(context) AppearanceId.CustomFont -> customFont() } } @@ -148,6 +150,54 @@ data class AppearanceInfo( ) ) + private fun dynamicAppearance(context: Context) = AppearanceInfo( + appearanceId = AppearanceId.Dynamic, + appearance = Appearance( + colors = Colors( + primary = ContextCompat.getColor(context, R.color.dynamic_colors_primary), + text = ContextCompat.getColor(context, R.color.dynamic_colors_text), + background = ContextCompat.getColor(context, R.color.dynamic_colors_background), + border = ContextCompat.getColor(context, R.color.dynamic_colors_border), + secondaryText = ContextCompat.getColor(context, R.color.dynamic_colors_secondary_text), + actionPrimaryText = ContextCompat.getColor(context, R.color.dynamic_colors_action_primary_text), + actionSecondaryText = ContextCompat.getColor(context, R.color.dynamic_colors_action_secondary_text), + formAccent = ContextCompat.getColor(context, R.color.dynamic_colors_form_accent), + formHighlightBorder = ContextCompat.getColor(context, R.color.dynamic_colors_form_highlight_border), + danger = ContextCompat.getColor(context, R.color.dynamic_colors_danger), + offsetBackground = ContextCompat.getColor(context, R.color.dynamic_colors_offset_background), + ), + buttonPrimary = Button( + colorBackground = ContextCompat.getColor(context, R.color.dynamic_colors_button_primary_background), + colorBorder = ContextCompat.getColor(context, R.color.dynamic_colors_button_primary_border), + colorText = ContextCompat.getColor(context, R.color.dynamic_colors_button_primary_text) + ), + buttonSecondary = Button( + colorBackground = ContextCompat.getColor( + context, + R.color.dynamic_colors_button_secondary_background + ), + colorBorder = ContextCompat.getColor(context, R.color.dynamic_colors_button_secondary_border), + colorText = ContextCompat.getColor(context, R.color.dynamic_colors_button_secondary_text) + ), + badgeSuccess = Badge( + colorBorder = ContextCompat.getColor(context, R.color.dynamic_colors_badge_success_border), + colorText = ContextCompat.getColor(context, R.color.dynamic_colors_badge_success_text) + ), + badgeNeutral = Badge( + colorBorder = ContextCompat.getColor(context, R.color.dynamic_colors_badge_neutral_border), + colorText = ContextCompat.getColor(context, R.color.dynamic_colors_badge_neutral_text), + ), + badgeWarning = Badge( + colorBorder = ContextCompat.getColor(context, R.color.dynamic_colors_badge_warning_border), + colorText = ContextCompat.getColor(context, R.color.dynamic_colors_badge_warning_text) + ), + badgeDanger = Badge( + colorBorder = ContextCompat.getColor(context, R.color.dynamic_colors_badge_danger_border), + colorText = ContextCompat.getColor(context, R.color.dynamic_colors_badge_danger_text) + ), + ) + ) + private fun customFont() = AppearanceInfo( appearanceId = AppearanceId.Link, appearance = Appearance( diff --git a/connect-example/src/main/java/com/stripe/android/connect/example/ui/common/BasicExampleComponentActivity.kt b/connect-example/src/main/java/com/stripe/android/connect/example/ui/common/BasicExampleComponentActivity.kt index bfc21324b52..09bb80acfc4 100644 --- a/connect-example/src/main/java/com/stripe/android/connect/example/ui/common/BasicExampleComponentActivity.kt +++ b/connect-example/src/main/java/com/stripe/android/connect/example/ui/common/BasicExampleComponentActivity.kt @@ -12,10 +12,12 @@ import androidx.compose.material.ModalBottomSheetLayout import androidx.compose.material.ModalBottomSheetValue import androidx.compose.material.rememberModalBottomSheetState import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.viewinterop.AndroidView import androidx.fragment.app.FragmentActivity @@ -27,6 +29,8 @@ import com.stripe.android.connect.EmbeddedComponentManager import com.stripe.android.connect.PrivateBetaConnectSDK import com.stripe.android.connect.example.core.Success import com.stripe.android.connect.example.core.then +import com.stripe.android.connect.example.data.SettingsService +import com.stripe.android.connect.example.ui.appearance.AppearanceInfo import com.stripe.android.connect.example.ui.appearance.AppearanceView import com.stripe.android.connect.example.ui.appearance.AppearanceViewModel import com.stripe.android.connect.example.ui.embeddedcomponentmanagerloader.EmbeddedComponentLoaderViewModel @@ -34,6 +38,7 @@ import com.stripe.android.connect.example.ui.embeddedcomponentmanagerloader.Embe import com.stripe.android.connect.example.ui.settings.settingsComposables import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.launch +import javax.inject.Inject @Suppress("ConstPropertyName") private object BasicComponentExampleDestination { @@ -50,6 +55,9 @@ abstract class BasicExampleComponentActivity : FragmentActivity() { abstract fun createComponentView(context: Context, embeddedComponentManager: EmbeddedComponentManager): View + @Inject + lateinit var settingsService: SettingsService + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -117,8 +125,15 @@ abstract class BasicExampleComponentActivity : FragmentActivity() { openSettings = openSettings, reload = viewModel::reload, ) { embeddedComponentManager -> - AndroidView(modifier = Modifier.fillMaxSize(), factory = { context -> - createComponentView(context, embeddedComponentManager) + val context = LocalContext.current + LaunchedEffect(context) { + val appearanceInfo = settingsService.getAppearanceId() + ?.let { AppearanceInfo.getAppearance(it, context).appearance } + ?: return@LaunchedEffect + embeddedComponentManager.update(appearanceInfo) + } + AndroidView(modifier = Modifier.fillMaxSize(), factory = { + createComponentView(it, embeddedComponentManager) }) } } diff --git a/connect-example/src/main/res/values-night/colors.xml b/connect-example/src/main/res/values-night/colors.xml new file mode 100644 index 00000000000..105ad808372 --- /dev/null +++ b/connect-example/src/main/res/values-night/colors.xml @@ -0,0 +1,81 @@ + + + #FBD992 + #FCEEB5 + #B13600 + + + #5AE92B + #837411 + #DAB9B9 + #F00000 + #000000 + #025F08 + #554125 + #28D72A + #638863 + #000000 + + + #FF2200 + #FFFF00 + #C6C6C6 + #1F1F1F + #1F1F1F + #1F1F1F + #C6C6C6 + #FF2200 + #000000 + #000000 + #991400 + #F9A443 + + + #EAF6FB + #15609E + #2A6093 + #5A621D + #2C93E8 + #2C93E8 + + + #1C3944 + #33DCB3 + #33DCB3 + #1C3944 + #485B61 + #1C3944 + #33DCB3 + #B4FEE1 + #C0D7CD + #1C3944 + #DEFECC + #1C3944 + + + #EBF0F4 + #FFFFFF + #272626 + #077EDF + #077EDF + #FFFFFF + #3D4042 + #89969F + #FFFFFF + #3D3D3D + #F4F3F3 + #EBF0F4 + #F7F7F7 + #EBF0F4 + #363636 + #DF1B41 + #7D7F87 + #D1D3DC + #7A8C7B + #CFE3D0 + #794A00 + #E7A288 + #6F2341 + #EC93AF + #171717 + diff --git a/connect-example/src/main/res/values/colors.xml b/connect-example/src/main/res/values/colors.xml index c41c17c64a7..8cc259888d7 100644 --- a/connect-example/src/main/res/values/colors.xml +++ b/connect-example/src/main/res/values/colors.xml @@ -52,4 +52,30 @@ #DEFECC #1C3944 - \ No newline at end of file + + #0969DA + #24292F + #FFFFFF + #0969DA + #261B1F24 + #FFFFFF + #F6F8FA + #261B1F24 + #24292F + #D7D7D7 + #57606A + #0969DA + #6E7781 + #0969DA + #0969DA + #B35900 + #8C959F + #6E7781 + #218BFF + #0969DA + #D4A72C + #BF8700 + #DD7815 + #B35900 + #F8F8F8 + diff --git a/connect-example/src/main/res/values/strings.xml b/connect-example/src/main/res/values/strings.xml index 3185af36cc5..f637110537c 100644 --- a/connect-example/src/main/res/values/strings.xml +++ b/connect-example/src/main/res/values/strings.xml @@ -44,5 +44,6 @@ Hot Dog Stand Ocean Breeze Link + Dynamic Custom Font