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

Pay Pal Messaging #194

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*.ap_
*.aab

!Demo/static-libs/*.aar

# Files for the ART/Dalvik VM
*.dex

Expand Down
5 changes: 4 additions & 1 deletion Demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ android {

defaultConfig {
applicationId "com.paypal.android"
minSdkVersion 21
minSdkVersion 23
targetSdkVersion 33
versionCode 1
versionName "1.0"
Expand Down Expand Up @@ -91,6 +91,9 @@ dependencies {
implementation deps.androidxLifecycleRuntimeKtx
implementation deps.fragmentKtx

// Ref: https://stackoverflow.com/a/49663101
implementation fileTree(include: ['*.aar'], dir: 'static-libs')

// TODO: remove dependency when we don't relay on nxo models anymore
implementation (deps.nativeCheckout) {
exclude module: 'data-collector'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ class FeaturesFragment : Fragment() {
CARD_VAULT(R.string.feature_vault),
PAYPAL_WEB(R.string.feature_paypal_web),
PAYPAL_BUTTONS(R.string.feature_paypal_buttons),
PAYPAL_NATIVE(R.string.feature_paypal_native)
PAYPAL_NATIVE(R.string.feature_paypal_native),
PAYPAL_MESSAGING(R.string.feature_paypal_messaging)
}

private val cardFeatures = listOf(
Expand All @@ -68,6 +69,10 @@ class FeaturesFragment : Fragment() {
Feature.PAYPAL_NATIVE
)

private val payPalMessagingFeatures = listOf(
Feature.PAYPAL_MESSAGING
)

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand Down Expand Up @@ -111,6 +116,10 @@ class FeaturesFragment : Fragment() {
Feature.CARD_VAULT -> {
FeaturesFragmentDirections.actionPaymentMethodsFragmentToVaultFragment()
}

Feature.PAYPAL_MESSAGING -> {
FeaturesFragmentDirections.actionPaymentMethodsFragmentToPayPalMessagingFragment()
}
}
findNavController().navigate(action)
}
Expand Down Expand Up @@ -146,6 +155,12 @@ class FeaturesFragment : Fragment() {
item {
FeatureOptions(payPalNativeFeatures, onFeatureSelected = onFeatureSelected)
}
stickyHeader {
FeatureGroupHeader("PayPal Messaging")
}
item {
FeatureOptions(payPalMessagingFeatures, onFeatureSelected = onFeatureSelected)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.paypal.android.ui.paypalmessaging

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.paypal.messages.PayPalMessageView
import com.paypal.messages.config.message.PayPalMessageConfig
import com.paypal.messages.config.message.PayPalMessageData
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class PayPalMessagingFragment : Fragment() {

private val viewModel by viewModels<PayPalMessagingViewModel>()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
) = ComposeView(requireContext()).apply {
viewModel.fetchClientId()
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setContent {
MaterialTheme {
Surface(modifier = Modifier.fillMaxSize()) {
val clientId by viewModel.clientId.collectAsStateWithLifecycle()
PayPalMessagingView(clientId)
}
}
}
}

@Composable
fun PayPalMessagingView(clientId: String) {
Column(
modifier = Modifier
.padding(16.dp)
.fillMaxWidth()
) {
Box(
modifier = Modifier.fillMaxWidth()
) {
if (clientId.isNotEmpty()) {
val config = PayPalMessageConfig()
config.setGlobalAnalytics("", "")
config.data = PayPalMessageData(clientId = clientId)

// Ref: https://developer.android.com/jetpack/compose/migrate/interoperability-apis/views-in-compose#androidview_in_lazy_lists
AndroidView(
factory = { context ->
// Demo: Intrinsic Height
// val messageView = PayPalMessagingPlaceholderView(context)
// messageView
// End: Intrinsic Height

// Demo: PayPal Messaging Integration
val messageView = PayPalMessageView(context, config = config)
messageView.layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
messageView
// End: PayPal Messaging Integration

// // NOTE: Kotlin getters / setters would be preferable to Java style getters / setters
// messageView.setLogoType(PayPalMessageLogoType.ALTERNATIVE)
// messageView.setColor(PayPalMessageColor.MONOCHROME)
// messageView.setViewStates(
// PayPalMessageViewState(
// onLoading = {
// Log.d("TAG", "onLoading")
// },
// onError = {
// Log.d("TAG", "onError")
// },
// onSuccess = {
// Log.d("TAG", "onSuccess")
// }
// )
// )
}
)
}
}
}
}

@Preview
@Composable
fun PayPalMessagingViewPreview() {
MaterialTheme {
Surface(modifier = Modifier.fillMaxSize()) {
PayPalMessagingView("")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.paypal.android.ui.paypalmessaging

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import com.paypal.android.R

class PayPalMessagingPlaceholderView @JvmOverloads constructor(
context: Context,
attributeSet: AttributeSet? = null,
defStyleAttr: Int = 0,
) : FrameLayout(context, attributeSet, defStyleAttr) {

init {
LayoutInflater.from(context)
.inflate(R.layout.pay_pal_messaging_placeholder_view, this, true)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.paypal.android.ui.paypalmessaging

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.paypal.android.api.services.SDKSampleServerAPI
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class PayPalMessagingViewModel @Inject constructor(
val sdkSampleServerAPI: SDKSampleServerAPI
) : ViewModel() {

private val _clientId = MutableStateFlow("")
val clientId = _clientId.asStateFlow()

fun fetchClientId() {
viewModelScope.launch {
_clientId.update { sdkSampleServerAPI.fetchClientId() }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/pay_pal_message_text" />
7 changes: 7 additions & 0 deletions Demo/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<action
android:id="@+id/action_paymentMethodsFragment_to_payPalNativeFragment"
app:destination="@id/payPalNativeFragment" />
<action
android:id="@+id/action_paymentMethodsFragment_to_payPalMessagingFragment"
app:destination="@id/payPalMessagingFragment" />
</fragment>
<fragment
android:id="@+id/cardFragment"
Expand Down Expand Up @@ -56,4 +59,8 @@
android:id="@+id/payPalWebFragment"
android:name="com.paypal.android.ui.paypalweb.PayPalWebFragment"
android:label="PayPalWebFragment" />
<fragment
android:id="@+id/payPalMessagingFragment"
android:name="com.paypal.android.ui.paypalmessaging.PayPalMessagingFragment"
android:label="PayPalMessagingFragment" />
</navigation>
3 changes: 3 additions & 0 deletions Demo/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<string name="feature_paypal_web">PayPal Web</string>
<string name="feature_paypal_native">PayPal Native</string>
<string name="feature_paypal_buttons">PayPal Buttons</string>
<string name="feature_paypal_messaging">PayPal Messaging</string>

<!-- Card Fields -->
<string name="card_field_card_number">CARD NUMBER</string>
Expand Down Expand Up @@ -46,4 +47,6 @@
<string name="intent_title">Intent</string>
<string name="intent_authorize">AUTHORIZE</string>
<string name="intent_capture">CAPTURE</string>

<string name="pay_pal_message_text">Hello from TextView</string>
</resources>
Binary file added Demo/static-libs/library-release_2023-09-06.aar
Binary file not shown.
Loading