1+ package com.mapbox.maps.compose.testapp.examples.style
2+
3+ import android.os.Bundle
4+ import androidx.activity.ComponentActivity
5+ import androidx.activity.compose.setContent
6+ import androidx.compose.foundation.layout.Column
7+ import androidx.compose.foundation.layout.fillMaxSize
8+ import androidx.compose.foundation.layout.padding
9+ import androidx.compose.foundation.shape.RoundedCornerShape
10+ import androidx.compose.material.FloatingActionButton
11+ import androidx.compose.material.Text
12+ import androidx.compose.runtime.getValue
13+ import androidx.compose.runtime.mutableStateOf
14+ import androidx.compose.runtime.remember
15+ import androidx.compose.runtime.setValue
16+ import androidx.compose.ui.Modifier
17+ import androidx.compose.ui.unit.dp
18+ import com.mapbox.maps.MapboxExperimental
19+ import com.mapbox.maps.Style
20+ import com.mapbox.maps.compose.testapp.ExampleScaffold
21+ import com.mapbox.maps.compose.testapp.examples.utils.CityLocations
22+ import com.mapbox.maps.compose.testapp.ui.theme.MapboxMapComposeTheme
23+ import com.mapbox.maps.extension.compose.MapboxMap
24+ import com.mapbox.maps.extension.compose.animation.viewport.rememberMapViewportState
25+ import com.mapbox.maps.extension.compose.style.DoubleValue
26+ import com.mapbox.maps.extension.compose.style.MapStyle
27+ import com.mapbox.maps.extension.compose.style.precipitations.generated.RainState
28+ import com.mapbox.maps.extension.compose.style.precipitations.generated.SnowState
29+ import com.mapbox.maps.extension.compose.style.precipitations.generated.rememberRainState
30+ import com.mapbox.maps.extension.compose.style.precipitations.generated.rememberSnowState
31+ import com.mapbox.maps.extension.compose.style.rememberStyleState
32+
33+ /* *
34+ * Example to showcase usage of [RainState] and [SnowState].
35+ */
36+ @OptIn(MapboxExperimental ::class )
37+ public class PrecipitationsActivity : ComponentActivity () {
38+
39+ /* *
40+ * Describes the heaviness of precipitation.
41+ */
42+ public sealed class PrecipitationState (
43+ public val intensity : Double ,
44+ public val density : Double ,
45+ public val opacity : Double ,
46+ public val text : String
47+ ) {
48+ public object None : PrecipitationState(intensity = 0.0 , density = 0.0 , opacity = 0.0 , text = " no" )
49+
50+ public object Light : PrecipitationState(intensity = 0.2 , density = 0.2 , opacity = 0.3 , text = " light" )
51+
52+ public object Medium : PrecipitationState(intensity = 0.6 , density = 0.6 , opacity = 0.5 , text = " medium" )
53+
54+ public object Heavy : PrecipitationState(intensity = 1.0 , density = 1.0 , opacity = 0.8 , text = " heavy" )
55+
56+ public fun toggleNext (): PrecipitationState {
57+ return when (this ) {
58+ Heavy -> None
59+ None -> Light
60+ Light -> Medium
61+ Medium -> Heavy
62+ }
63+ }
64+ }
65+
66+ override fun onCreate (savedInstanceState : Bundle ? ) {
67+ super .onCreate(savedInstanceState)
68+
69+ setContent {
70+ var snowPrecipitationState: PrecipitationState by remember {
71+ mutableStateOf(PrecipitationState .Light )
72+ }
73+
74+ var rainPrecipitationState: PrecipitationState by remember {
75+ mutableStateOf(PrecipitationState .Light )
76+ }
77+
78+ val mapViewportState = rememberMapViewportState {
79+ setCameraOptions {
80+ zoom(ZOOM )
81+ pitch(PITCH )
82+ bearing(BEARING )
83+ center(CityLocations .HELSINKI )
84+ }
85+ }
86+
87+ val rainState = rememberRainState().also {
88+ it.opacity = DoubleValue (rainPrecipitationState.opacity)
89+ it.intensity = DoubleValue (rainPrecipitationState.intensity)
90+ it.density = DoubleValue (rainPrecipitationState.density)
91+ }
92+
93+ val snowState = rememberSnowState().also {
94+ it.opacity = DoubleValue (snowPrecipitationState.opacity)
95+ it.intensity = DoubleValue (snowPrecipitationState.intensity)
96+ it.density = DoubleValue (snowPrecipitationState.density)
97+ }
98+
99+ MapboxMapComposeTheme {
100+ ExampleScaffold (
101+ floatingActionButton = {
102+ Column {
103+ FloatingActionButton (
104+ modifier = Modifier .padding(bottom = 10 .dp),
105+ onClick = {
106+ rainPrecipitationState = rainPrecipitationState.toggleNext()
107+ },
108+ shape = RoundedCornerShape (16 .dp),
109+ ) {
110+ Text (
111+ modifier = Modifier .padding(10 .dp),
112+ text = " ${rainPrecipitationState.text} rain"
113+ )
114+ }
115+ FloatingActionButton (
116+ modifier = Modifier .padding(bottom = 10 .dp),
117+ onClick = {
118+ snowPrecipitationState = snowPrecipitationState.toggleNext()
119+ },
120+ shape = RoundedCornerShape (16 .dp),
121+ ) {
122+ Text (
123+ modifier = Modifier .padding(10 .dp),
124+ text = " ${snowPrecipitationState.text} snow"
125+ )
126+ }
127+ }
128+ }
129+ ) {
130+ MapboxMap (
131+ Modifier .fillMaxSize(),
132+ mapViewportState = mapViewportState,
133+ style = {
134+ MapStyle (
135+ style = Style .STANDARD ,
136+ styleState = rememberStyleState().apply {
137+ this .rainState =
138+ if (rainPrecipitationState == PrecipitationState .None ) RainState .DISABLED else rainState
139+ this .snowState =
140+ if (snowPrecipitationState == PrecipitationState .None ) SnowState .DISABLED else snowState
141+ }
142+ )
143+ }
144+ )
145+ }
146+ }
147+ }
148+ }
149+
150+ private companion object {
151+ private const val ZOOM : Double = 16.0
152+ private const val PITCH : Double = 40.0
153+ private const val BEARING : Double = 70.0
154+ }
155+ }
0 commit comments