1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.glance.appwidget
18 
19 import androidx.annotation.RestrictTo
20 import androidx.compose.runtime.Composable
21 import androidx.compose.ui.graphics.Color
22 import androidx.glance.Emittable
23 import androidx.glance.GlanceModifier
24 import androidx.glance.GlanceNode
25 import androidx.glance.unit.ColorProvider
26 
27 /**
28  * Adds a determinate linear progress indicator view to the glance view.
29  *
30  * @param progress of this progress indicator, where 0.0 represents no progress and 1.0 represents
31  *   full progress
32  * @param modifier the modifier to apply to the progress bar
33  * @param color The color of the progress indicator.
34  * @param backgroundColor The color of the background behind the indicator, visible when the
35  *   progress has not reached that area of the overall indicator yet.
36  */
37 @Composable
LinearProgressIndicatornull38 fun LinearProgressIndicator(
39     /*@FloatRange(from = 0.0, to = 1.0)*/
40     progress: Float,
41     modifier: GlanceModifier = GlanceModifier,
42     color: ColorProvider = ProgressIndicatorDefaults.IndicatorColorProvider,
43     backgroundColor: ColorProvider = ProgressIndicatorDefaults.BackgroundColorProvider
44 ) {
45     GlanceNode(
46         factory = ::EmittableLinearProgressIndicator,
47         update = {
48             this.set(modifier) { this.modifier = it }
49             this.set(progress) { this.progress = it }
50             this.set(color) { this.color = it }
51             this.set(backgroundColor) { this.backgroundColor = it }
52         }
53     )
54 }
55 
56 /**
57  * Adds an indeterminate linear progress indicator view to the glance view.
58  *
59  * @param modifier the modifier to apply to the progress bar
60  * @param color The color of the progress indicator.
61  * @param backgroundColor The color of the background behind the indicator, visible when the
62  *   progress has not reached that area of the overall indicator yet.
63  */
64 @Composable
LinearProgressIndicatornull65 fun LinearProgressIndicator(
66     modifier: GlanceModifier = GlanceModifier,
67     color: ColorProvider = ProgressIndicatorDefaults.IndicatorColorProvider,
68     backgroundColor: ColorProvider = ProgressIndicatorDefaults.BackgroundColorProvider
69 ) {
70     GlanceNode(
71         factory = ::EmittableLinearProgressIndicator,
72         update = {
73             this.set(modifier) { this.modifier = it }
74             this.set(true) { this.indeterminate = it }
75             this.set(color) { this.color = it }
76             this.set(backgroundColor) { this.backgroundColor = it }
77         }
78     )
79 }
80 
81 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
82 class EmittableLinearProgressIndicator : Emittable {
83     override var modifier: GlanceModifier = GlanceModifier
84     var progress: Float = 0.0f
85     var indeterminate: Boolean = false
86     var color: ColorProvider = ProgressIndicatorDefaults.IndicatorColorProvider
87     var backgroundColor: ColorProvider = ProgressIndicatorDefaults.BackgroundColorProvider
88 
copynull89     override fun copy(): Emittable =
90         EmittableLinearProgressIndicator().also {
91             it.modifier = modifier
92             it.progress = progress
93             it.indeterminate = indeterminate
94             it.color = color
95             it.backgroundColor = backgroundColor
96         }
97 
toStringnull98     override fun toString(): String =
99         "EmittableLinearProgressIndicator(" +
100             "modifier=$modifier, " +
101             "progress=$progress, " +
102             "indeterminate=$indeterminate, " +
103             "color=$color, " +
104             "backgroundColor=$backgroundColor" +
105             ")"
106 }
107 
108 /** Contains the default values used for [LinearProgressIndicator]. */
109 object ProgressIndicatorDefaults {
110 
111     /**
112      * Default color for [LinearProgressIndicator]. [Material color
113      * specification](https://material.io/design/color/the-color-system.html#color-theme-creation)
114      */
115     private val Color = Color(0xFF6200EE)
116 
117     /** Default ColorProvider for the progress indicator in [LinearProgressIndicator]. */
118     val IndicatorColorProvider = ColorProvider(Color)
119 
120     /** Default ColorProvider for the background in [LinearProgressIndicator]. */
121     val BackgroundColorProvider = ColorProvider(Color.copy(alpha = 0.24f))
122 }
123