/
GlanceSnippets.kt
997 lines (858 loc) · 29.2 KB
/
GlanceSnippets.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.compose.snippets.glance
import android.annotation.SuppressLint
import android.app.Activity
import android.app.Service
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.IBinder
import android.widget.RemoteViews
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material3.ColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.glance.Button
import androidx.glance.ColorFilter
import androidx.glance.GlanceId
import androidx.glance.GlanceModifier
import androidx.glance.GlanceTheme
import androidx.glance.Image
import androidx.glance.ImageProvider
import androidx.glance.LocalContext
import androidx.glance.LocalSize
import androidx.glance.action.ActionParameters
import androidx.glance.action.actionParametersOf
import androidx.glance.action.actionStartActivity
import androidx.glance.action.clickable
import androidx.glance.appwidget.AndroidRemoteViews
import androidx.glance.appwidget.CheckBox
import androidx.glance.appwidget.CheckboxDefaults
import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.GlanceAppWidgetManager
import androidx.glance.appwidget.GlanceAppWidgetReceiver
import androidx.glance.appwidget.RadioButton
import androidx.glance.appwidget.RadioButtonDefaults
import androidx.glance.appwidget.SizeMode
import androidx.glance.appwidget.Switch
import androidx.glance.appwidget.SwitchDefaults
import androidx.glance.appwidget.action.ActionCallback
import androidx.glance.appwidget.action.actionRunCallback
import androidx.glance.appwidget.action.actionSendBroadcast
import androidx.glance.appwidget.action.actionStartService
import androidx.glance.appwidget.provideContent
import androidx.glance.appwidget.updateAll
import androidx.glance.appwidget.updateIf
import androidx.glance.background
import androidx.glance.color.ColorProvider
import androidx.glance.layout.Alignment
import androidx.glance.layout.Column
import androidx.glance.layout.Row
import androidx.glance.layout.RowScope
import androidx.glance.layout.fillMaxSize
import androidx.glance.layout.fillMaxWidth
import androidx.glance.layout.padding
import androidx.glance.material3.ColorProviders
import androidx.glance.text.Text
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.example.compose.snippets.MyActivity
import com.example.compose.snippets.R
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
lateinit var LightColors: ColorScheme
lateinit var DarkColors: ColorScheme
private object GlanceCreateAppWidgetSnippet01 {
// [START android_compose_glance_receiver01]
class MyAppWidgetReceiver : GlanceAppWidgetReceiver() {
override val glanceAppWidget: GlanceAppWidget = TODO("Create GlanceAppWidget")
}
// [END android_compose_glance_receiver01]
}
private object GlanceCreateAppWidgetSnippet02 {
// [START android_compose_glance_receiver02]
class MyAppWidgetReceiver : GlanceAppWidgetReceiver() {
// Let MyAppWidgetReceiver know which GlanceAppWidget to use
override val glanceAppWidget: GlanceAppWidget = MyAppWidget()
}
// [END android_compose_glance_receiver02]
// [START android_compose_glance_widget]
class MyAppWidget : GlanceAppWidget() {
override suspend fun provideGlance(context: Context, id: GlanceId) {
// In this method, load data needed to render the AppWidget.
// Use `withContext` to switch to another thread for long running
// operations.
provideContent {
// create your AppWidget here
Text("Hello World")
}
}
}
// [END android_compose_glance_widget]
}
private object CreateUI {
// [START android_compose_glance_createui]
/* Import Glance Composables
In the event there is a name clash with the Compose classes of the same name,
you may rename the imports per https://kotlinlang.org/docs/packages.html#imports
using the `as` keyword.
import androidx.glance.Button
import androidx.glance.layout.Column
import androidx.glance.layout.Row
import androidx.glance.text.Text
*/
class MyAppWidget : GlanceAppWidget() {
override suspend fun provideGlance(context: Context, id: GlanceId) {
// Load data needed to render the AppWidget.
// Use `withContext` to switch to another thread for long running
// operations.
provideContent {
// create your AppWidget here
GlanceTheme {
MyContent()
}
}
}
@Composable
private fun MyContent() {
Column(
modifier = GlanceModifier.fillMaxSize()
.background(GlanceTheme.colors.background),
verticalAlignment = Alignment.Top,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(text = "Where to?", modifier = GlanceModifier.padding(12.dp))
Row(horizontalAlignment = Alignment.CenterHorizontally) {
Button(
text = "Home",
onClick = actionStartActivity<MyActivity>()
)
Button(
text = "Work",
onClick = actionStartActivity<MyActivity>()
)
}
}
}
}
// [END android_compose_glance_createui]
}
private object ActionLaunchActivity {
// [START android_compose_glance_launchactivity]
@Composable
fun MyContent() {
// ..
Button(
text = "Go Home",
onClick = actionStartActivity<MyActivity>()
)
}
// [END android_compose_glance_launchactivity]
}
private object ActionLaunchService {
// [START android_compose_glance_launchservice]
@Composable
fun MyButton() {
// ..
Button(
text = "Sync",
onClick = actionStartService<SyncService>(
isForegroundService = true // define how the service is launched
)
)
}
// [END android_compose_glance_launchservice]
}
private object ActionLaunchSendBroadcastEvent {
// [START android_compose_glance_sendbroadcastevent]
@Composable
fun MyButton() {
// ..
Button(
text = "Send",
onClick = actionSendBroadcast<MyReceiver>()
)
}
// [END android_compose_glance_sendbroadcastevent]
}
private object ActionLambda {
@Composable
fun actionLambda() {
// [START android_compose_glance_lambda01]
Text(
text = "Submit",
modifier = GlanceModifier.clickable {
submitData()
}
)
// [END android_compose_glance_lambda01]
}
@Composable
fun actionLambda2() {
// [START android_compose_glance_lambda02]
Button(
text = "Submit",
onClick = {
submitData()
}
)
// [END android_compose_glance_lambda02]
}
}
private object ActionCallbackSnippet02 {
// [START android_compose_glance_actioncallback02]
class RefreshAction : ActionCallback {
override suspend fun onAction(
context: Context,
glanceId: GlanceId,
parameters: ActionParameters
) {
// do some work but offset long-term tasks (e.g a Worker)
MyAppWidget().update(context, glanceId)
}
}
// [END android_compose_glance_actioncallback02]
/*dummy class*/
class MyAppWidget : GlanceAppWidget() {
override suspend fun provideGlance(context: Context, id: GlanceId) {
TODO("Not yet implemented")
}
}
}
private object ActionCallbackSnippet01 {
// [START android_compose_glance_actioncallback01]
@Composable
private fun MyContent() {
// ..
Image(
provider = ImageProvider(R.drawable.ic_hourglass_animated),
modifier = GlanceModifier.clickable(
onClick = actionRunCallback<RefreshAction>()
),
contentDescription = "Refresh"
)
}
class RefreshAction : ActionCallback {
override suspend fun onAction(
context: Context,
glanceId: GlanceId,
parameters: ActionParameters
) {
// TODO implement
}
}
// [END android_compose_glance_actioncallback01]
}
private object ActionParameters {
// [START android_compose_glance_actionparameters01]
private val destinationKey = ActionParameters.Key<String>(
NavigationActivity.KEY_DESTINATION
)
class MyAppWidget : GlanceAppWidget() {
// ..
@Composable
private fun MyContent() {
// ..
Button(
text = "Home",
onClick = actionStartActivity<NavigationActivity>(
actionParametersOf(destinationKey to "home")
)
)
Button(
text = "Work",
onClick = actionStartActivity<NavigationActivity>(
actionParametersOf(destinationKey to "work")
)
)
}
override suspend fun provideGlance(context: Context, id: GlanceId) {
provideContent { MyContent() }
}
}
// [END android_compose_glance_actionparameters01]
abstract class ActionParametersActivity : Activity() {
val KEY_DESTINATION = "destination"
// [START android_compose_glance_actionparameters02]
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val destination = intent.extras?.getString(KEY_DESTINATION) ?: return
// ...
}
}
// [END android_compose_glance_actionparameters02]
// [START android_compose_glance_actionparameters03]
class RefreshAction : ActionCallback {
private val destinationKey = ActionParameters.Key<String>(
NavigationActivity.KEY_DESTINATION
)
override suspend fun onAction(
context: Context,
glanceId: GlanceId,
parameters: ActionParameters
) {
val destination: String = parameters[destinationKey] ?: return
// ...
}
}
// [END android_compose_glance_actionparameters03]
}
@SuppressLint("RememberReturnType")
object ManageAndUpdate {
abstract
// [START android_compose_glance_manageupdate01]
class DestinationAppWidget : GlanceAppWidget() {
// ...
@Composable
fun MyContent() {
val repository = remember { DestinationsRepository.getInstance() }
// Retrieve the cache data everytime the content is refreshed
val destinations by repository.destinations.collectAsState(State.Loading)
when (destinations) {
is State.Loading -> {
// show loading content
}
is State.Error -> {
// show widget error content
}
is State.Completed -> {
// show the list of destinations
}
}
}
}
// [END android_compose_glance_manageupdate01]
suspend fun update02(context: Context, glanceId: GlanceId) {
// [START android_compose_glance_manageupdate02]
MyAppWidget().update(context, glanceId)
// [END android_compose_glance_manageupdate02]
// [START android_compose_glance_manageupdate03]
val manager = GlanceAppWidgetManager(context)
val widget = GlanceSizeModeWidget()
val glanceIds = manager.getGlanceIds(widget.javaClass)
glanceIds.forEach { glanceId ->
widget.update(context, glanceId)
}
// [END android_compose_glance_manageupdate03]
// [START android_compose_glance_manageupdate04]
// Updates all placed instances of MyAppWidget
MyAppWidget().updateAll(context)
// Iterate over all placed instances of MyAppWidget and update if the state of
// the instance matches the given predicate
MyAppWidget().updateIf<State>(context) { state ->
state == State.Completed
}
// [END android_compose_glance_manageupdate04]
}
// [START android_compose_glance_manageupdate05]
class DataSyncWorker(
val context: Context,
val params: WorkerParameters,
) : CoroutineWorker(context, params) {
override suspend fun doWork(): Result {
// Fetch data or do some work and then update all instance of your widget
MyAppWidget().updateAll(context)
return Result.success()
}
}
// [END android_compose_glance_manageupdate05]
}
object BuildUIWithGlance {
@Composable
fun example1() {
// [START android_compose_glance_buildUI01]
Row(modifier = GlanceModifier.fillMaxWidth().padding(16.dp)) {
val modifier = GlanceModifier.defaultWeight()
Text("first", modifier)
Text("second", modifier)
Text("third", modifier)
}
// [END android_compose_glance_buildUI01]
}
@Composable
fun example2() {
// [START android_compose_glance_buildUI02]
// Remember to import Glance Composables
// import androidx.glance.appwidget.layout.LazyColumn
LazyColumn {
items(10) { index: Int ->
Text(
text = "Item $index",
modifier = GlanceModifier.fillMaxWidth()
)
}
}
// [END android_compose_glance_buildUI02]
}
@Composable
fun example3() {
// [START android_compose_glance_buildUI03]
LazyColumn {
item {
Text("First Item")
}
item {
Text("Second Item")
}
}
// [END android_compose_glance_buildUI03]
}
@Composable
fun example4() {
val peopleNameList = arrayListOf<String>()
val peopleList = arrayListOf<Person>()
// [START android_compose_glance_buildUI04]
LazyColumn {
items(peopleNameList) { name ->
Text(name)
}
}
// [END android_compose_glance_buildUI04]
// [START android_compose_glance_buildUI05]
LazyColumn {
item {
Text("Names:")
}
items(peopleNameList) { name ->
Text(name)
}
// or in case you need the index:
itemsIndexed(peopleNameList) { index, person ->
Text("$person at index $index")
}
}
// [END android_compose_glance_buildUI05]
LazyColumn {
// [START android_compose_glance_buildUI06]
items(items = peopleList, key = { person -> person.id }) { person ->
Text(person.name)
}
// [END android_compose_glance_buildUI06]
}
}
}
object SizeModeSnippets {
// [START android_compose_glance_buildUI07]
class MyAppWidget : GlanceAppWidget() {
override val sizeMode = SizeMode.Single
override suspend fun provideGlance(context: Context, id: GlanceId) {
// ...
provideContent {
MyContent()
}
}
@Composable
private fun MyContent() {
// Size will be the minimum size or resizable
// size defined in the App Widget metadata
val size = LocalSize.current
// ...
}
}
// [END android_compose_glance_buildUI07]
}
object SizeModeSnippets2 {
// [START android_compose_glance_buildUI08]
class MyAppWidget : GlanceAppWidget() {
companion object {
private val SMALL_SQUARE = DpSize(100.dp, 100.dp)
private val HORIZONTAL_RECTANGLE = DpSize(250.dp, 100.dp)
private val BIG_SQUARE = DpSize(250.dp, 250.dp)
}
override val sizeMode = SizeMode.Responsive(
setOf(
SMALL_SQUARE,
HORIZONTAL_RECTANGLE,
BIG_SQUARE
)
)
override suspend fun provideGlance(context: Context, id: GlanceId) {
// ...
provideContent {
MyContent()
}
}
@Composable
private fun MyContent() {
// Size will be one of the sizes defined above.
val size = LocalSize.current
Column {
if (size.height >= BIG_SQUARE.height) {
Text(text = "Where to?", modifier = GlanceModifier.padding(12.dp))
}
Row(horizontalAlignment = Alignment.CenterHorizontally) {
Button()
Button()
if (size.width >= HORIZONTAL_RECTANGLE.width) {
Button("School")
}
}
if (size.height >= BIG_SQUARE.height) {
Text(text = "provided by X")
}
}
}
}
// [END android_compose_glance_buildUI08]
}
object SizeModeSnippets3 {
// [START android_compose_glance_buildUI09]
class MyAppWidget : GlanceAppWidget() {
override val sizeMode = SizeMode.Exact
override suspend fun provideGlance(context: Context, id: GlanceId) {
// ...
provideContent {
MyContent()
}
}
@Composable
private fun MyContent() {
// Size will be the size of the AppWidget
val size = LocalSize.current
Column {
Text(text = "Where to?", modifier = GlanceModifier.padding(12.dp))
Row(horizontalAlignment = Alignment.CenterHorizontally) {
Button()
Button()
if (size.width > 250.dp) {
Button("School")
}
}
}
}
}
// [END android_compose_glance_buildUI09]
}
object AccessResources {
@Composable
fun example1() {
// [START android_compose_glance_buildUI10]
LocalContext.current.getString(R.string.glance_title)
// [END android_compose_glance_buildUI10]
// [START android_compose_glance_buildUI11]
Column(
modifier = GlanceModifier.background(R.color.default_widget_background)
) { /**...*/ }
Image(
provider = ImageProvider(R.drawable.ic_logo),
contentDescription = "My image",
)
// [END android_compose_glance_buildUI11]
}
}
object CompoundButton {
@Composable
fun example1() {
// [START android_compose_glance_buildUI12]
var isApplesChecked by remember { mutableStateOf(false) }
var isEnabledSwitched by remember { mutableStateOf(false) }
var isRadioChecked by remember { mutableStateOf(0) }
CheckBox(
checked = isApplesChecked,
onCheckedChange = { isApplesChecked = !isApplesChecked },
text = "Apples"
)
Switch(
checked = isEnabledSwitched,
onCheckedChange = { isEnabledSwitched = !isEnabledSwitched },
text = "Enabled"
)
RadioButton(
checked = isRadioChecked == 1,
onClick = { isRadioChecked = 1 },
text = "Checked"
)
// [END android_compose_glance_buildUI12]
}
// [START android_compose_glance_buildUI13]
class MyAppWidget : GlanceAppWidget() {
override suspend fun provideGlance(context: Context, id: GlanceId) {
val myRepository = MyRepository.getInstance()
provideContent {
val scope = rememberCoroutineScope()
val saveApple: (Boolean) -> Unit =
{ scope.launch { myRepository.saveApple(it) } }
MyContent(saveApple)
}
}
@Composable
private fun MyContent(saveApple: (Boolean) -> Unit) {
var isAppleChecked by remember { mutableStateOf(false) }
Button(
text = "Save",
onClick = { saveApple(isAppleChecked) }
)
}
}
// [END android_compose_glance_buildUI13]
@Composable
fun example3() {
val colorAccentDay = Color.Blue
val colorAccentNight = Color.Blue
var isChecked by remember { mutableStateOf(false) }
// [START android_compose_glance_buildUI14]
CheckBox(
// ...
colors = CheckboxDefaults.colors(
checkedColor = ColorProvider(day = colorAccentDay, night = colorAccentNight),
uncheckedColor = ColorProvider(day = Color.DarkGray, night = Color.LightGray)
),
checked = isChecked,
onCheckedChange = { isChecked = !isChecked }
)
Switch(
// ...
colors = SwitchDefaults.colors(
checkedThumbColor = ColorProvider(day = Color.Red, night = Color.Cyan),
uncheckedThumbColor = ColorProvider(day = Color.Green, night = Color.Magenta),
checkedTrackColor = ColorProvider(day = Color.Blue, night = Color.Yellow),
uncheckedTrackColor = ColorProvider(day = Color.Magenta, night = Color.Green)
),
checked = isChecked,
onCheckedChange = { isChecked = !isChecked },
text = "Enabled"
)
RadioButton(
// ...
colors = RadioButtonDefaults.colors(
checkedColor = ColorProvider(day = Color.Cyan, night = Color.Yellow),
uncheckedColor = ColorProvider(day = Color.Red, night = Color.Blue)
),
)
// [END android_compose_glance_buildUI14]
}
private fun RadioButton(colors: Any) {
}
}
object GlanceTheming {
class ExampleAppWidget : GlanceAppWidget() {
// [START android_compose_glance_glancetheming01]
override suspend fun provideGlance(context: Context, id: GlanceId) {
provideContent {
GlanceTheme {
MyContent()
}
}
}
@Composable
private fun MyContent() {
Image(
colorFilter = ColorFilter.tint(GlanceTheme.colors.secondary),
// [START_EXCLUDE]
contentDescription = "Example Image",
provider = ImageProvider(R.drawable.ic_logo)
// [END_EXCLUDE]
)
}
// [END android_compose_glance_glancetheming01]
}
class ExampleAppWidget2 : GlanceAppWidget() {
// [START android_compose_glance_glancetheming06]
// Remember, use the Glance imports
// import androidx.glance.material3.ColorProviders
// Example Imports from your own app
// import com.example.myapp.ui.theme.DarkColors
// import com.example.myapp.ui.theme.LightColors
object MyAppWidgetGlanceColorScheme {
val colors = ColorProviders(
light = LightColors,
dark = DarkColors
)
}
// [END android_compose_glance_glancetheming06]
// [START android_compose_glance_glancetheming02]
override suspend fun provideGlance(context: Context, id: GlanceId) {
// ...
provideContent {
GlanceTheme(colors = MyAppWidgetGlanceColorScheme.colors) {
MyContent()
}
}
}
@Composable
private fun MyContent() {
Image(
colorFilter = ColorFilter.tint(GlanceTheme.colors.secondary),
// [START_EXCLUDE]
provider = ImageProvider(R.drawable.ic_logo),
contentDescription = "Example"
// [END_EXCLUDE]
)
}
// [END android_compose_glance_glancetheming02]
}
class ExampleAppWidget3 : GlanceAppWidget() {
object MyAppWidgetGlanceColorScheme {
val colors = ColorProviders(
light = LightColors,
dark = DarkColors
)
}
// [START android_compose_glance_glancetheming03]
override suspend fun provideGlance(context: Context, id: GlanceId) {
provideContent {
GlanceTheme(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
GlanceTheme.colors
else
MyAppWidgetGlanceColorScheme.colors
) {
MyContent()
}
}
}
@Composable
private fun MyContent() {
// ...
Image(
colorFilter = ColorFilter.tint(GlanceTheme.colors.secondary),
// [START_EXCLUDE]
provider = ImageProvider(R.drawable.ic_logo),
contentDescription = "Example"
// [END_EXCLUDE]
)
}
// [END android_compose_glance_glancetheming03]
}
@Composable
fun shapeExample() {
// Note : android_compose_glance_glancetheming04 is found in button_outline.xml
// [START android_compose_glance_glancetheming05]
GlanceModifier.background(
imageProvider = ImageProvider(R.drawable.button_outline)
)
// [END android_compose_glance_glancetheming05]
}
}
object GlanceInteroperability {
@Composable
fun example01() {
// [START android_compose_glance_glanceinteroperability01]
val packageName = LocalContext.current.packageName
Column(modifier = GlanceModifier.fillMaxSize()) {
Text("Isn't that cool?")
AndroidRemoteViews(RemoteViews(packageName, R.layout.example_layout))
}
// [END android_compose_glance_glanceinteroperability01]
}
@Composable
fun example02() {
val packageName = null
// [START android_compose_glance_glanceinteroperability02]
AndroidRemoteViews(
remoteViews = RemoteViews(packageName, R.layout.my_container_view),
containerViewId = R.id.example_view
) {
Column(modifier = GlanceModifier.fillMaxSize()) {
Text("My title")
Text("Maybe a long content...")
}
}
// [END android_compose_glance_glanceinteroperability02]
}
}
/**
* Dummy interface
*/
interface MyRepository {
suspend fun saveApple(a: Any)
companion object {
fun getInstance(): MyRepository {
TODO("Example")
}
}
}
/**dummy function*/
private fun RowScope.Button(s: String = "") {
TODO("Not yet implemented")
}
/** dummy class */
data class Person(val id: String, val name: String)
/** dummy class */
class GlanceSizeModeWidget : GlanceAppWidget() {
override suspend fun provideGlance(context: Context, id: GlanceId) {
TODO("Not yet implemented")
}
}
/** dummy class */
class MyAppWidget : GlanceAppWidget() {
override suspend fun provideGlance(context: Context, id: GlanceId) {
TODO("Not yet implemented")
}
}
/**
* Dummy Interface
*/
sealed interface State {
object Loading : State
object Error : State
object Completed : State
}
/**
* Dummy class
*/
class DestinationsRepository {
lateinit var destinations: StateFlow<State>
companion object {
fun getInstance(): DestinationsRepository {
TODO("Not yet implemented")
}
}
}
/**
* Dummy activity for snippet
*/
class NavigationActivity : AppCompatActivity() {
companion object {
val KEY_DESTINATION: String = "destination"
}
}
/**
* Dummy lambda
*/
private fun submitData() {
TODO("Not yet implemented")
}
/**
* Dummy broadcast receiver for snippets
*/
class MyReceiver : BroadcastReceiver() {
override fun onReceive(p0: Context?, p1: Intent?) {
TODO("Not yet implemented")
}
}
/**
* Dummy service for snippets
*/
class SyncService : Service() {
override fun onBind(p0: Intent?): IBinder? {
TODO("Not yet implemented")
}
}