Skip to content
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

Use Cash App Pay SDK button #1256

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ package com.adyen.checkout.cashapppay.internal.ui

import android.content.Context
import android.util.AttributeSet
import com.adyen.checkout.cashapppay.internal.ui.view.CashAppPayButtonView
import com.adyen.checkout.cashapppay.internal.ui.view.CashAppPayView
import com.adyen.checkout.cashapppay.internal.ui.view.CashAppPayWaitingView
import com.adyen.checkout.ui.core.internal.ui.ButtonComponentViewType
import com.adyen.checkout.ui.core.internal.ui.ButtonViewProvider
import com.adyen.checkout.ui.core.internal.ui.ComponentView
import com.adyen.checkout.ui.core.internal.ui.ComponentViewType
import com.adyen.checkout.ui.core.internal.ui.ViewProvider
import com.adyen.checkout.ui.core.internal.ui.view.PayButton

internal object CashAppPayViewProvider : ViewProvider {

Expand All @@ -31,7 +34,15 @@ internal object CashAppPayViewProvider : ViewProvider {
}
}

internal class CashAppPayButtonViewProvider : ButtonViewProvider {
override fun getButton(context: Context, attrs: AttributeSet?, defStyleAttr: Int): PayButton =
CashAppPayButtonView(context, attrs, defStyleAttr)
}

internal object CashAppPayComponentViewType : ButtonComponentViewType {

override val buttonViewProvider: ButtonViewProvider get() = CashAppPayButtonViewProvider()

override val viewProvider: ViewProvider = CashAppPayViewProvider

override val buttonTextResId: Int = ButtonComponentViewType.DEFAULT_BUTTON_TEXT_RES_ID
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2023 Adyen N.V.
*
* This file is open source and available under the MIT license. See the LICENSE file for more info.
*
* Created by oscars on 30/6/2023.
*/

package com.adyen.checkout.cashapppay.internal.ui.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import com.adyen.checkout.cashapppay.databinding.CashAppPayButtonViewBinding
import com.adyen.checkout.ui.core.internal.ui.view.PayButton

internal class CashAppPayButtonView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : PayButton(context, attrs, defStyleAttr) {

private val binding = CashAppPayButtonViewBinding.inflate(LayoutInflater.from(context), this)

override fun setOnClickListener(listener: OnClickListener?) {
binding.payButton.setOnClickListener(listener)
}

override fun setText(text: String?) = Unit
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (c) 2023 Adyen N.V.
~
~ This file is open source and available under the MIT license. See the LICENSE file for more info.
~
~ Created by oscars on 30/6/2023.
-->

<merge xmlns:android="http://schemas.android.com/apk/res/android">

<app.cash.paykit.core.ui.CashAppPayDarkButton
android:id="@+id/payButton"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp" />

</merge>
20 changes: 20 additions & 0 deletions cashapppay/src/main/res/layout/cash_app_pay_button_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (c) 2023 Adyen N.V.
~
~ This file is open source and available under the MIT license. See the LICENSE file for more info.
~
~ Created by oscars on 30/6/2023.
-->

<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<app.cash.paykit.core.ui.CashAppPayLightButton
android:id="@+id/payButton"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp" />

</merge>
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.MotionEvent
import android.widget.Button
import android.widget.LinearLayout
import androidx.core.view.children
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.lifecycle.LifecycleOwner
Expand All @@ -32,6 +32,7 @@ import com.adyen.checkout.ui.core.internal.ui.PaymentComponentUIEvent
import com.adyen.checkout.ui.core.internal.ui.UIStateDelegate
import com.adyen.checkout.ui.core.internal.ui.ViewProvidingDelegate
import com.adyen.checkout.ui.core.internal.ui.ViewableComponent
import com.adyen.checkout.ui.core.internal.ui.view.PayButton
import com.adyen.checkout.ui.core.internal.util.PayButtonFormatter
import com.adyen.checkout.ui.core.internal.util.createLocalizedContext
import com.adyen.checkout.ui.core.internal.util.hideKeyboard
Expand Down Expand Up @@ -123,8 +124,6 @@ class AdyenComponentView @JvmOverloads constructor(

val localizedContext = context.createLocalizedContext(componentParams.shopperLocale)

binding.payButton.setText(viewType, componentParams, localizedContext)

val view = componentView.getView()
binding.frameLayoutComponentContainer.addView(view)
view.updateLayoutParams { width = LayoutParams.MATCH_PARENT }
Expand All @@ -144,42 +143,54 @@ class AdyenComponentView @JvmOverloads constructor(
}
}?.launchIn(coroutineScope)

binding.payButton.isVisible = buttonDelegate.shouldShowSubmitButton()
binding.payButton.setOnClickListener {
binding.frameLayoutButtonContainer.isVisible = buttonDelegate.shouldShowSubmitButton()
val buttonView = (viewType as ButtonComponentViewType)
.buttonViewProvider.getButton(context, attrs, defStyleAttr)
buttonView.setText(viewType, componentParams, localizedContext)
buttonView.setOnClickListener {
buttonDelegate.onSubmit()
}
binding.frameLayoutButtonContainer.addView(buttonView)
} else {
binding.payButton.isVisible = false
binding.payButton.setOnClickListener(null)
binding.frameLayoutButtonContainer.isVisible = false
}
}

private fun setInteractionBlocked(isInteractionBlocked: Boolean) {
this.isInteractionBlocked = isInteractionBlocked

binding.payButton.isEnabled = !isInteractionBlocked
binding.frameLayoutButtonContainer.children.forEach { it.isEnabled = !isInteractionBlocked }

if (isInteractionBlocked) {
resetFocus()
hideKeyboard()
}
}

private fun Button.setText(
private fun PayButton.setText(
viewType: ComponentViewType,
componentParams: ComponentParams,
localizedContext: Context
) {
if (viewType is AmountButtonComponentViewType) {
text = PayButtonFormatter.getPayButtonText(
amount = componentParams.amount,
locale = componentParams.shopperLocale,
localizedContext = localizedContext,
emptyAmountStringResId = viewType.buttonTextResId
)
} else if (viewType is ButtonComponentViewType) {
text = localizedContext.getString(viewType.buttonTextResId)
val text = when (viewType) {
is AmountButtonComponentViewType -> {
PayButtonFormatter.getPayButtonText(
amount = componentParams.amount,
locale = componentParams.shopperLocale,
localizedContext = localizedContext,
emptyAmountStringResId = viewType.buttonTextResId
)
}

is ButtonComponentViewType -> {
localizedContext.getString(viewType.buttonTextResId)
}

else -> {
null
}
}
setText(text)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2023 Adyen N.V.
*
* This file is open source and available under the MIT license. See the LICENSE file for more info.
*
* Created by oscars on 30/6/2023.
*/

package com.adyen.checkout.ui.core.internal.ui

import android.content.Context
import android.util.AttributeSet
import androidx.annotation.RestrictTo
import com.adyen.checkout.ui.core.internal.ui.view.DefaultPayButton
import com.adyen.checkout.ui.core.internal.ui.view.PayButton

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
interface ButtonViewProvider {
fun getButton(
context: Context,
attrs: AttributeSet?,
defStyleAttr: Int,
): PayButton
}

internal class DefaultButtonViewProvider : ButtonViewProvider {

override fun getButton(context: Context, attrs: AttributeSet?, defStyleAttr: Int): PayButton =
DefaultPayButton(context, attrs, defStyleAttr)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ interface ComponentViewType {

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
interface ButtonComponentViewType : ComponentViewType {

val buttonViewProvider: ButtonViewProvider get() = DefaultButtonViewProvider()

val buttonTextResId: Int
@StringRes get

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2023 Adyen N.V.
*
* This file is open source and available under the MIT license. See the LICENSE file for more info.
*
* Created by oscars on 30/6/2023.
*/

package com.adyen.checkout.ui.core.internal.ui.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import com.adyen.checkout.ui.core.databinding.DefaultPayButtonViewBinding

internal class DefaultPayButton @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : PayButton(context, attrs, defStyleAttr) {

private val binding = DefaultPayButtonViewBinding.inflate(LayoutInflater.from(context), this)

override fun setOnClickListener(listener: OnClickListener?) {
binding.payButton.setOnClickListener(listener)
}

override fun setText(text: String?) {
binding.payButton.text = text
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2023 Adyen N.V.
*
* This file is open source and available under the MIT license. See the LICENSE file for more info.
*
* Created by oscars on 30/6/2023.
*/

package com.adyen.checkout.ui.core.internal.ui.view

import android.content.Context
import android.util.AttributeSet
import android.widget.FrameLayout
import androidx.annotation.RestrictTo

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
abstract class PayButton(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

context: Context,
attrs: AttributeSet?,
defStyleAttr: Int,
) : FrameLayout(context, attrs, defStyleAttr) {

abstract override fun setOnClickListener(listener: OnClickListener?)

abstract fun setText(text: String?)
}
11 changes: 6 additions & 5 deletions ui-core/src/main/res/layout/adyen_component_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
android:layout_height="0dp"
android:layout_weight="1" />

<com.google.android.material.button.MaterialButton
android:id="@+id/payButton"
style="@style/AdyenCheckout.Button.Colored"
android:visibility="gone"
tools:text="Pay" />
<FrameLayout
android:id="@+id/frameLayout_buttonContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/standard_margin"
android:visibility="gone" />

</merge>
18 changes: 18 additions & 0 deletions ui-core/src/main/res/layout/default_pay_button_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (c) 2023 Adyen N.V.
~
~ This file is open source and available under the MIT license. See the LICENSE file for more info.
~
~ Created by oscars on 30/6/2023.
-->
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:parentTag="android.widget.FrameLayout">

<com.google.android.material.button.MaterialButton
android:id="@+id/payButton"
style="@style/AdyenCheckout.Button.Colored"
tools:text="Pay" />
</merge>