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

Compose test #9190

Draft
wants to merge 2 commits into
base: master
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: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20.48.6
20.50.0
1 change: 1 addition & 0 deletions example/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<activity android:name=".activity.BillieActivity"/>
<activity android:name=".activity.SatispayActivity"/>
<activity android:name=".activity.CardInputWidgetComposeExampleActivity"/>
<activity android:name=".activity.TestComposeActivity"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.stripe.android.view.TestComposeView
import com.stripe.example.R
import com.stripe.example.databinding.LauncherActivityBinding

Expand Down Expand Up @@ -36,6 +37,7 @@ class LauncherActivity : AppCompatActivity() {
private val activity: Activity
) : RecyclerView.Adapter<ExamplesAdapter.ExamplesViewHolder>() {
private val items = listOf(
Item("Test Compose View", TestComposeActivity::class.java),
Item(
activity.getString(R.string.payment_auth_example),
PaymentAuthActivity::class.java
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.stripe.example.activity

import android.app.Activity
import android.app.Dialog
import android.os.Bundle
import com.stripe.android.view.TestComposeView

class TestComposeActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// val dialog = Dialog(this)
// dialog.setContentView(TestComposeView(this))
// dialog.show()
this.setContentView(TestComposeView(this))
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ android.useAndroidX=true
android.experimental.androidTest.numManagedDeviceShards=3

# Update StripeSdkVersion.VERSION_NAME when publishing a new release
VERSION_NAME=20.48.6
VERSION_NAME=20.50.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package com.stripe.android.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import androidx.compose.foundation.layout.Column
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MonotonicFrameClock
import androidx.compose.runtime.PausableMonotonicFrameClock
import androidx.compose.runtime.Recomposer
import androidx.compose.ui.platform.AbstractComposeView
import androidx.compose.ui.platform.AndroidUiDispatcher
import androidx.compose.ui.platform.ComposeView
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import androidx.lifecycle.findViewTreeLifecycleOwner
import androidx.lifecycle.setViewTreeLifecycleOwner
import androidx.savedstate.SavedStateRegistry
import androidx.savedstate.SavedStateRegistryController
import androidx.savedstate.SavedStateRegistryOwner
import androidx.savedstate.setViewTreeSavedStateRegistryOwner
import kotlin.coroutines.EmptyCoroutineContext

class TestComposeView(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
): LinearLayout(context, attrs, defStyleAttr) {

private val lifecycleDelegate = TestLifecycleOwnerDelegate()

init {
val view = MyComposeView(context = context) {
Column {
Text(text = "Hello")
Text(text = "Does this work?")
}
}
this.addView(view)
}

override fun onAttachedToWindow() {
super.onAttachedToWindow()
lifecycleDelegate.initLifecycle(this)
}

override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
lifecycleDelegate.destroyLifecycle(this)
}
}

class MyComposeView(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0,
private val content: @Composable () -> Unit
) : AbstractComposeView(context, attrs, defStyleAttr) {

private val lifecycleDelegate = TestLifecycleOwnerDelegate()

@Composable
override fun Content() {
content()
}

init {
val currentThreadContext = AndroidUiDispatcher.CurrentThread
val pausableClock = currentThreadContext[MonotonicFrameClock]?.let {
PausableMonotonicFrameClock(it).apply { pause() }
}
val contextWithClock = currentThreadContext + (pausableClock ?: EmptyCoroutineContext)
val recomposer = Recomposer(contextWithClock)
setParentCompositionContext(recomposer)
}

override fun onAttachedToWindow() {
lifecycleDelegate.initLifecycle(this)
super.onAttachedToWindow()
}

override fun onDetachedFromWindow() {
lifecycleDelegate.destroyLifecycle(this)
super.onDetachedFromWindow()
}

override val shouldCreateCompositionOnAttachedToWindow: Boolean
get() = false
}

internal class TestLifecycleOwnerDelegate : LifecycleOwner, SavedStateRegistryOwner {

fun initLifecycle(owner: View) {
owner.findViewTreeLifecycleOwner() ?: run {
savedStateRegistryController.performRestore(null)
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
attachToParent(owner)
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
}
}

fun destroyLifecycle(owner: View) {
owner.findViewTreeLifecycleOwner() ?: run {
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE)
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
}
}

private fun attachToParent(owner: View) {
owner.setViewTreeLifecycleOwner(this)
owner.setViewTreeSavedStateRegistryOwner(this)
}

// LifecycleOwner methods
private val lifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)

// SavedStateRegistry methods
private val savedStateRegistryController = SavedStateRegistryController.create(this)

override val lifecycle: Lifecycle
get() = lifecycleRegistry
override val savedStateRegistry: SavedStateRegistry
get() = savedStateRegistryController.savedStateRegistry
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ package com.stripe.android.core.version
*
*/
object StripeSdkVersion {
const val VERSION_NAME = "20.48.6"
const val VERSION_NAME = "20.50.0"
const val VERSION: String = "AndroidBindings/$VERSION_NAME"
}
Loading