Skip to content

Commit

Permalink
Remove rememberDrawablePainter from example app
Browse files Browse the repository at this point in the history
  • Loading branch information
tillh-stripe committed Dec 12, 2023
1 parent c2bfae2 commit bc0b7b2
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import com.stripe.android.paymentsheet.example.R
import com.stripe.android.paymentsheet.example.samples.ui.customersheet.playground.CustomerSheetPlaygroundViewAction.UpdateMerchantCountryCode
import com.stripe.android.paymentsheet.example.samples.ui.shared.MultiToggleButton
import com.stripe.android.paymentsheet.example.samples.ui.shared.PaymentSheetExampleTheme
import com.stripe.android.paymentsheet.example.utils.rememberDrawablePainter
import com.stripe.android.paymentsheet.rememberPaymentSheet
import java.util.Locale

Expand Down Expand Up @@ -188,11 +187,9 @@ class CustomerSheetPlaygroundActivity : AppCompatActivity() {
onClick = onUpdateDefaultPaymentMethod,
) {
Row(verticalAlignment = Alignment.CenterVertically) {
state.selection?.paymentOption?.icon()?.let {
state.selection?.paymentOption?.let {
Image(
painter = rememberDrawablePainter(
drawable = it
),
painter = it.iconPainter,
contentDescription = "Payment Method Icon",
modifier = Modifier.height(32.dp)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,21 @@
package com.stripe.android.paymentsheet.example.utils

import android.graphics.drawable.Animatable
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.view.View
import androidx.compose.runtime.Composable
import androidx.compose.runtime.RememberObserver
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.asAndroidColorFilter
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.graphics.nativeCanvas
import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.compose.ui.graphics.painter.ColorPainter
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.graphics.withSave
import androidx.compose.ui.unit.LayoutDirection
Expand All @@ -52,8 +44,6 @@ private val MAIN_HANDLER by lazy(LazyThreadSafetyMode.NONE) {
/**
* A [Painter] which draws an Android [Drawable] and supports [Animatable] drawables. Instances
* should be remembered to be able to start and stop [Animatable] animations.
*
* Instances are usually retrieved from [rememberDrawablePainter].
*/
class DrawablePainter(
val drawable: Drawable
Expand Down Expand Up @@ -140,27 +130,6 @@ class DrawablePainter(
}
}

/**
* Remembers [Drawable] wrapped up as a [Painter]. This function attempts to un-wrap the
* drawable contents and use Compose primitives where possible.
*
* If the provided [drawable] is `null`, an empty no-op painter is returned.
*
* This function tries to dispatch lifecycle events to [drawable] as much as possible from
* within Compose.
*/
@Composable
fun rememberDrawablePainter(drawable: Drawable?): Painter = remember(drawable) {
when (drawable) {
null -> EmptyPainter
is BitmapDrawable -> BitmapPainter(drawable.bitmap.asImageBitmap())
is ColorDrawable -> ColorPainter(Color(drawable.color))
// Since the DrawablePainter will be remembered and it implements RememberObserver, it
// will receive the necessary events
else -> DrawablePainter(drawable.mutate())
}
}

private val Drawable.intrinsicSize: Size
get() = when {
// Only return a finite size if the drawable has an intrinsic size
Expand Down

0 comments on commit bc0b7b2

Please sign in to comment.