1+ package com.mapcode.map
2+
3+ import androidx.compose.foundation.clickable
4+ import androidx.compose.foundation.layout.*
5+ import androidx.compose.material.ScaffoldState
6+ import androidx.compose.runtime.Composable
7+ import androidx.compose.runtime.collectAsState
8+ import androidx.compose.runtime.getValue
9+ import androidx.compose.runtime.rememberCoroutineScope
10+ import androidx.compose.ui.Modifier
11+ import androidx.compose.ui.res.stringResource
12+ import androidx.compose.ui.unit.dp
13+ import com.mapcode.R
14+ import kotlinx.coroutines.launch
15+
16+ @Composable
17+ fun VerticalInfoArea (
18+ modifier : Modifier = Modifier ,
19+ state : UiState ,
20+ onAddressChange : (String ) -> Unit ,
21+ onLatitudeChange : (String ) -> Unit ,
22+ onLongitudeChange : (String ) -> Unit ,
23+ onTerritoryClick : () -> Unit ,
24+ onMapcodeClick : () -> Unit
25+ ) {
26+ Column (modifier) {
27+ AddressArea (Modifier , state.addressUi.address, onAddressChange, state.addressUi.helper, state.addressUi.error)
28+ Spacer (Modifier .height(8 .dp))
29+ TerritoryBox (
30+ modifier = Modifier
31+ .fillMaxWidth()
32+ .clickable { onTerritoryClick() },
33+ index = state.mapcodeUi.number,
34+ count = state.mapcodeUi.count,
35+ territoryName = state.mapcodeUi.territoryFullName
36+ )
37+ Spacer (Modifier .height(8 .dp))
38+ MapcodeBox (
39+ Modifier
40+ .fillMaxWidth()
41+ .clickable { onMapcodeClick() },
42+ state.mapcodeUi.code,
43+ state.mapcodeUi.territoryShortName
44+ )
45+ Spacer (Modifier .height(8 .dp))
46+ LatitudeTextBox (Modifier .fillMaxWidth(), state.latitude, onLatitudeChange)
47+ Spacer (Modifier .height(8 .dp))
48+ LongitudeTextBox (Modifier .fillMaxWidth(), state.longitude, onLongitudeChange)
49+ }
50+ }
51+
52+ @Composable
53+ fun HorizontalInfoArea (
54+ modifier : Modifier = Modifier ,
55+ state : UiState ,
56+ onAddressChange : (String ) -> Unit ,
57+ onLatitudeChange : (String ) -> Unit ,
58+ onLongitudeChange : (String ) -> Unit ,
59+ onTerritoryClick : () -> Unit ,
60+ onMapcodeClick : () -> Unit
61+ ) {
62+ Column (modifier) {
63+ AddressArea (
64+ Modifier .fillMaxWidth(),
65+ state.addressUi.address,
66+ onAddressChange,
67+ state.addressUi.helper,
68+ state.addressUi.error
69+ )
70+ Row (Modifier .padding(top = 8 .dp)) {
71+ TerritoryBox (
72+ modifier = Modifier
73+ .weight(0.5f )
74+ .padding(end = 8 .dp)
75+ .clickable { onTerritoryClick() },
76+ index = state.mapcodeUi.number,
77+ count = state.mapcodeUi.count,
78+ territoryName = state.mapcodeUi.territoryFullName
79+ )
80+ MapcodeBox (
81+ Modifier
82+ .weight(0.5f )
83+ .padding(start = 8 .dp)
84+ .clickable { onMapcodeClick() },
85+ state.mapcodeUi.code,
86+ state.mapcodeUi.territoryShortName
87+ )
88+ }
89+
90+ Row (Modifier .padding(top = 8 .dp)) {
91+ LatitudeTextBox (
92+ Modifier
93+ .weight(0.5f )
94+ .padding(end = 8 .dp)
95+ .fillMaxWidth(), state.latitude, onLatitudeChange
96+ )
97+ LongitudeTextBox (
98+ Modifier
99+ .weight(0.5f )
100+ .padding(start = 8 .dp), state.longitude, onLongitudeChange
101+ )
102+ }
103+ }
104+ }
105+
106+ @Composable
107+ fun InfoArea (
108+ modifier : Modifier = Modifier ,
109+ state : UiState ,
110+ onAddressChange : (String ) -> Unit = {},
111+ onLatitudeChange : (String ) -> Unit = {},
112+ onLongitudeChange : (String ) -> Unit = {},
113+ onTerritoryClick : () -> Unit = {},
114+ onMapcodeClick : () -> Unit = {},
115+ isVerticalLayout : Boolean
116+ ) {
117+ if (isVerticalLayout) {
118+ VerticalInfoArea (
119+ modifier,
120+ state,
121+ onAddressChange,
122+ onLatitudeChange,
123+ onLongitudeChange,
124+ onTerritoryClick,
125+ onMapcodeClick
126+ )
127+ } else {
128+ HorizontalInfoArea (
129+ modifier,
130+ state,
131+ onAddressChange,
132+ onLatitudeChange,
133+ onLongitudeChange,
134+ onTerritoryClick,
135+ onMapcodeClick
136+ )
137+ }
138+ }
139+
140+ @Composable
141+ fun InfoArea (
142+ modifier : Modifier ,
143+ viewModel : MapViewModel ,
144+ scaffoldState : ScaffoldState ,
145+ isVerticalLayout : Boolean
146+ ) {
147+ val uiState by viewModel.uiState.collectAsState()
148+ val scope = rememberCoroutineScope()
149+ val copiedMessageStr = stringResource(R .string.copied_to_clipboard_snackbar_text)
150+
151+ InfoArea (
152+ modifier,
153+ uiState,
154+ onMapcodeClick = {
155+ val copied = viewModel.copyMapcode()
156+ if (copied) {
157+ scope.launch {
158+ // dismiss current snack bar so they aren't queued up
159+ scaffoldState.snackbarHostState.currentSnackbarData?.dismiss()
160+ scaffoldState.snackbarHostState.showSnackbar(copiedMessageStr)
161+ }
162+ }
163+ },
164+ onAddressChange = { viewModel.queryAddress(it) },
165+ onTerritoryClick = { viewModel.onTerritoryClick() },
166+ onLatitudeChange = { viewModel.queryLatitude(it) },
167+ onLongitudeChange = { viewModel.queryLongitude(it) },
168+ isVerticalLayout = isVerticalLayout
169+ )
170+ }
0 commit comments