|
17 | 17 | package com.example.platform.ui.appwidgets.remote_compose |
18 | 18 |
|
19 | 19 | import android.annotation.SuppressLint |
20 | | -import android.appwidget.AppWidgetManager |
21 | 20 | import android.content.Context |
22 | | -import android.os.Build |
23 | | -import androidx.annotation.RequiresApi |
| 21 | +import androidx.compose.remote.creation.compose.action.valueChange |
24 | 22 | import androidx.compose.remote.creation.compose.layout.RemoteAlignment |
25 | 23 | import androidx.compose.remote.creation.compose.layout.RemoteBox |
| 24 | +import androidx.compose.remote.creation.compose.layout.RemoteColumn |
26 | 25 | import androidx.compose.remote.creation.compose.layout.RemoteComposable |
27 | 26 | import androidx.compose.remote.creation.compose.layout.RemoteText |
28 | 27 | import androidx.compose.remote.creation.compose.modifier.RemoteModifier |
29 | 28 | import androidx.compose.remote.creation.compose.modifier.background |
30 | 29 | import androidx.compose.remote.creation.compose.modifier.fillMaxSize |
| 30 | +import androidx.compose.remote.creation.compose.modifier.onTouchDown |
31 | 31 | import androidx.compose.remote.creation.compose.state.rc |
| 32 | +import androidx.compose.remote.creation.compose.state.rememberMutableRemoteInt |
| 33 | +import androidx.compose.remote.creation.compose.state.ri |
32 | 34 | import androidx.compose.remote.creation.compose.state.rs |
| 35 | +import androidx.compose.remote.creation.compose.state.selectIfGe |
33 | 36 | import androidx.compose.remote.creation.compose.widgets.RemoteComposeWidget |
34 | 37 | import androidx.compose.runtime.Composable |
35 | 38 | import androidx.compose.ui.graphics.Color |
36 | 39 |
|
37 | 40 | @SuppressLint("RestrictedApiAndroidX", "RestrictedApi") |
38 | 41 | class AndroidPetWidget : RemoteComposeWidget() { |
39 | 42 |
|
40 | | - @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM) |
41 | | - override fun onUpdate( |
42 | | - context: Context, |
43 | | - appWidgetManager: AppWidgetManager, |
44 | | - appWidgetIds: IntArray |
45 | | - ) { |
46 | | - super.onUpdate(context, appWidgetManager, appWidgetIds) |
| 43 | + override fun onDeleted(context: Context, appWidgetIds: IntArray) { |
| 44 | + super.onDeleted(context, appWidgetIds) |
47 | 45 | appWidgetIds.forEach { id -> |
48 | | - widget.updateRemoteView(context, appWidgetManager, this, id, -1) |
| 46 | + BuddyPreferences.removeWidgetState(context, id) |
49 | 47 | } |
50 | 48 | } |
51 | 49 |
|
52 | 50 | @SuppressLint("RestrictedApiAndroidX", "RestrictedApi") |
53 | 51 | @RemoteComposable |
54 | 52 | @Composable |
55 | 53 | override fun Content(context: Context, widgetId: Int) { |
| 54 | + val appContext = context.applicationContext ?: context |
| 55 | + val coins = BuddyPreferences.getCoinsForWidget(appContext, widgetId) |
| 56 | + |
| 57 | + val petTaps = rememberMutableRemoteInt(0) |
| 58 | + val nextPetTaps = petTaps + 1.ri |
| 59 | + // Evaluates to 1 (visible) in the host's expression graph when petTaps >= 3, |
| 60 | + // or 0 (hidden) otherwise, allowing the coin button to appear after 3 taps |
| 61 | + // without waking the app process. |
| 62 | + val isClaimVisible = selectIfGe(petTaps, REQUIRED_TAPS_FOR_COIN.ri, 1.ri, 0.ri) |
| 63 | + |
56 | 64 | RemoteBox( |
57 | 65 | modifier = RemoteModifier.fillMaxSize().background(Color.White.rc), |
58 | 66 | contentAlignment = RemoteAlignment.Center |
59 | 67 | ) { |
60 | | - RemoteText( |
61 | | - text = "Android Pet Coming Soon".rs, |
62 | | - color = Color.Black.rc |
| 68 | + RemoteColumn( |
| 69 | + modifier = RemoteModifier.onTouchDown(valueChange(petTaps, nextPetTaps)), |
| 70 | + horizontalAlignment = RemoteAlignment.CenterHorizontally |
| 71 | + ) { |
| 72 | + RemoteText( |
| 73 | + text = "Android Pet".rs, |
| 74 | + color = Color.Black.rc |
| 75 | + ) |
| 76 | + RemoteText( |
| 77 | + text = "Coins: $coins".rs, |
| 78 | + color = Color.DarkGray.rc |
| 79 | + ) |
| 80 | + } |
| 81 | + |
| 82 | + WidgetActionBar( |
| 83 | + context = appContext, |
| 84 | + widgetId = widgetId, |
| 85 | + isClaimVisible = isClaimVisible, |
63 | 86 | ) |
64 | 87 | } |
65 | 88 | } |
| 89 | + |
| 90 | + companion object { |
| 91 | + const val REQUIRED_TAPS_FOR_COIN = 3 |
| 92 | + } |
66 | 93 | } |
0 commit comments