Skip to content

Commit 8ea6014

Browse files
authored
Merge pull request #202 from google-pay/kmp-migration
feat: migrate compose-pay-button library to Kotlin Multiplatform (KMP)
2 parents 942ab72 + a6381a2 commit 8ea6014

14 files changed

Lines changed: 841 additions & 69 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ build/
99
*.apk
1010

1111
*.pem
12-
.kotlin
12+
.kotlin
13+
kotlin-js-store/

build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616

1717
plugins {
1818
alias(libs.plugins.nexusPublishingPlugin)
19+
alias(libs.plugins.kotlin.multiplatform) apply false
20+
alias(libs.plugins.androidLibrary) apply false
21+
alias(libs.plugins.compose.compiler) apply false
22+
alias(libs.plugins.compose.multiplatform) apply false
23+
alias(libs.plugins.kotlin.android) apply false
24+
alias(libs.plugins.androidApplication) apply false
1925
}
2026

2127
apply from: "${rootDir}/scripts/publish-root.gradle"

compose-pay-button/build.gradle

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
*/
1616

1717
plugins {
18+
alias(libs.plugins.kotlin.multiplatform)
1819
alias(libs.plugins.androidLibrary)
19-
alias(libs.plugins.kotlin.android)
2020
alias(libs.plugins.compose.compiler)
21+
alias(libs.plugins.compose.multiplatform)
2122
}
2223

2324
ext {
@@ -50,9 +51,6 @@ android {
5051
sourceCompatibility JavaVersion.VERSION_1_8
5152
targetCompatibility JavaVersion.VERSION_1_8
5253
}
53-
kotlinOptions {
54-
jvmTarget = '1.8'
55-
}
5654

5755
buildFeatures {
5856
compose true
@@ -71,10 +69,50 @@ android {
7169
targetSdk 37
7270
}
7371
}
72+
kotlin {
73+
androidTarget {
74+
publishLibraryVariants("release")
75+
}
76+
js(IR) {
77+
browser()
78+
}
79+
wasmJs {
80+
browser()
81+
}
7482

75-
dependencies {
76-
implementation libs.playServices.wallet
77-
implementation libs.compose.ui
78-
implementation libs.compose.material
79-
implementation libs.androidx.core
83+
sourceSets {
84+
commonMain {
85+
dependencies {
86+
implementation compose.runtime
87+
implementation compose.ui
88+
implementation compose.foundation
89+
implementation compose.material
90+
implementation compose.components.resources
91+
}
92+
}
93+
androidMain {
94+
dependencies {
95+
implementation libs.playServices.wallet
96+
implementation libs.androidx.core
97+
}
98+
}
99+
jsMain {
100+
dependencies {
101+
implementation compose.ui
102+
}
103+
}
104+
wasmJsMain {
105+
dependencies {
106+
implementation compose.ui
107+
}
108+
}
109+
}
110+
}
111+
112+
tasks.register("printWasmJsClasspath") {
113+
doLast {
114+
configurations.getByName("wasmJsCompileClasspath").files.forEach {
115+
println(it.absolutePath)
116+
}
117+
}
80118
}

compose-pay-button/src/main/java/com/google/pay/button/PayButton.kt renamed to compose-pay-button/src/androidMain/kotlin/com/google/pay/button/PayButton.kt

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,46 +24,44 @@ import androidx.compose.runtime.setValue
2424
import androidx.compose.ui.Modifier
2525
import androidx.compose.ui.platform.LocalDensity
2626
import androidx.compose.ui.unit.Dp
27-
import androidx.compose.ui.unit.dp
2827
import androidx.compose.ui.viewinterop.AndroidView
2928
import com.google.android.gms.wallet.button.ButtonConstants
3029
import com.google.android.gms.wallet.button.ButtonOptions
3130
import com.google.android.gms.wallet.button.PayButton as GmsPayButton
3231

33-
enum class ButtonTheme(val value: Int) {
34-
Dark(ButtonConstants.ButtonTheme.DARK),
35-
Light(ButtonConstants.ButtonTheme.LIGHT),
36-
}
32+
private const val FULL_ALPHA = 1f
33+
private const val HALF_ALPHA = 0.5f
3734

38-
enum class ButtonType(val value: Int) {
39-
Book(ButtonConstants.ButtonType.BOOK),
40-
Buy(ButtonConstants.ButtonType.BUY),
41-
Checkout(ButtonConstants.ButtonType.CHECKOUT),
42-
Donate(ButtonConstants.ButtonType.DONATE),
43-
Order(ButtonConstants.ButtonType.ORDER),
44-
Pay(ButtonConstants.ButtonType.PAY),
45-
Plain(ButtonConstants.ButtonType.PLAIN),
46-
Subscribe(ButtonConstants.ButtonType.SUBSCRIBE),
47-
PIX(ButtonConstants.ButtonType.PIX),
48-
EWALLET(ButtonConstants.ButtonType.EWALLET)
35+
private fun ButtonTheme.toAndroidValue(): Int = when (this) {
36+
ButtonTheme.Dark -> ButtonConstants.ButtonTheme.DARK
37+
ButtonTheme.Light -> ButtonConstants.ButtonTheme.LIGHT
4938
}
5039

51-
private const val FULL_ALPHA = 1f
52-
private const val HALF_ALPHA = 0.5f
40+
private fun ButtonType.toAndroidValue(): Int = when (this) {
41+
ButtonType.Book -> ButtonConstants.ButtonType.BOOK
42+
ButtonType.Buy -> ButtonConstants.ButtonType.BUY
43+
ButtonType.Checkout -> ButtonConstants.ButtonType.CHECKOUT
44+
ButtonType.Donate -> ButtonConstants.ButtonType.DONATE
45+
ButtonType.Order -> ButtonConstants.ButtonType.ORDER
46+
ButtonType.Pay -> ButtonConstants.ButtonType.PAY
47+
ButtonType.Plain -> ButtonConstants.ButtonType.PLAIN
48+
ButtonType.Subscribe -> ButtonConstants.ButtonType.SUBSCRIBE
49+
ButtonType.PIX -> ButtonConstants.ButtonType.PIX
50+
ButtonType.EWALLET -> ButtonConstants.ButtonType.EWALLET
51+
}
5352

5453
@Composable
55-
fun PayButton(
54+
actual fun PayButton(
5655
onClick: () -> Unit,
5756
allowedPaymentMethods: String,
58-
modifier: Modifier = Modifier,
59-
theme: ButtonTheme = ButtonTheme.Dark,
60-
type: ButtonType = ButtonType.Buy,
61-
radius: Dp = 100.dp,
62-
enabled: Boolean = true,
63-
onError: (Throwable) -> Unit = {},
64-
fallbackUi: @Composable (() -> Unit)? = null,
57+
modifier: Modifier,
58+
theme: ButtonTheme,
59+
type: ButtonType,
60+
radius: Dp,
61+
enabled: Boolean,
62+
onError: (Throwable) -> Unit,
63+
fallbackUi: @Composable (() -> Unit)?,
6564
) {
66-
6765
var showFallback by remember { mutableStateOf(false) }
6866

6967
val radiusPixelValue = with(LocalDensity.current) { radius.toPx().toInt() }
@@ -76,8 +74,8 @@ fun PayButton(
7674
kotlin.runCatching {
7775
this.initialize(
7876
ButtonOptions.newBuilder()
79-
.setButtonTheme(theme.value)
80-
.setButtonType(type.value)
77+
.setButtonTheme(theme.toAndroidValue())
78+
.setButtonType(type.toAndroidValue())
8179
.setCornerRadius(radiusPixelValue)
8280
.setAllowedPaymentMethods(allowedPaymentMethods)
8381
.build()
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2023 Google LLC.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.pay.button
18+
19+
import androidx.compose.runtime.Composable
20+
import androidx.compose.ui.Modifier
21+
import androidx.compose.ui.unit.Dp
22+
import androidx.compose.ui.unit.dp
23+
24+
enum class ButtonTheme {
25+
Dark,
26+
Light,
27+
}
28+
29+
enum class ButtonType {
30+
Book,
31+
Buy,
32+
Checkout,
33+
Donate,
34+
Order,
35+
Pay,
36+
Plain,
37+
Subscribe,
38+
PIX,
39+
EWALLET
40+
}
41+
42+
@Composable
43+
expect fun PayButton(
44+
onClick: () -> Unit,
45+
allowedPaymentMethods: String,
46+
modifier: Modifier = Modifier,
47+
theme: ButtonTheme = ButtonTheme.Dark,
48+
type: ButtonType = ButtonType.Buy,
49+
radius: Dp = 100.dp,
50+
enabled: Boolean = true,
51+
onError: (Throwable) -> Unit = {},
52+
fallbackUi: @Composable (() -> Unit)? = null,
53+
)

0 commit comments

Comments
 (0)