Finished PLZ Inputs and saving
This commit is contained in:
		
							parent
							
								
									e0581c3fa7
								
							
						
					
					
						commit
						2c7dc727d3
					
				|  | @ -1,5 +1,6 @@ | ||||||
| <?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||||||
| <project version="4"> | <project version="4"> | ||||||
|  |   <component name="GradleMigrationSettings" migrationVersion="1" /> | ||||||
|   <component name="GradleSettings"> |   <component name="GradleSettings"> | ||||||
|     <option name="linkedExternalProjectsSettings"> |     <option name="linkedExternalProjectsSettings"> | ||||||
|       <GradleProjectSettings> |       <GradleProjectSettings> | ||||||
|  |  | ||||||
|  | @ -2,6 +2,7 @@ plugins { | ||||||
|     alias(libs.plugins.android.application) |     alias(libs.plugins.android.application) | ||||||
|     alias(libs.plugins.kotlin.android) |     alias(libs.plugins.kotlin.android) | ||||||
|     alias(libs.plugins.kotlin.compose) |     alias(libs.plugins.kotlin.compose) | ||||||
|  |     id("kotlin-parcelize") | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| android { | android { | ||||||
|  | @ -60,4 +61,9 @@ dependencies { | ||||||
|     androidTestImplementation(libs.androidx.ui.test.junit4) |     androidTestImplementation(libs.androidx.ui.test.junit4) | ||||||
|     debugImplementation(libs.androidx.ui.tooling) |     debugImplementation(libs.androidx.ui.tooling) | ||||||
|     debugImplementation(libs.androidx.ui.test.manifest) |     debugImplementation(libs.androidx.ui.test.manifest) | ||||||
|  | 
 | ||||||
|  |     implementation("androidx.datastore:datastore-preferences:1.0.0") | ||||||
|  |     implementation("androidx.datastore:datastore:1.0.0") | ||||||
|  |     implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1") // For JSON serialization | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  | @ -1,5 +1,6 @@ | ||||||
| package com.module.breeze | package com.module.breeze | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
| import android.content.Context | import android.content.Context | ||||||
| import android.content.Intent | import android.content.Intent | ||||||
| import android.os.Bundle | import android.os.Bundle | ||||||
|  | @ -16,12 +17,17 @@ import androidx.compose.foundation.layout.width | ||||||
| import androidx.compose.foundation.text.KeyboardOptions | import androidx.compose.foundation.text.KeyboardOptions | ||||||
| import androidx.compose.material.icons.outlined.Home | import androidx.compose.material.icons.outlined.Home | ||||||
| import androidx.compose.material.icons.outlined.Map | import androidx.compose.material.icons.outlined.Map | ||||||
|  | import androidx.compose.material3.CircularProgressIndicator | ||||||
| import androidx.compose.material3.Icon | import androidx.compose.material3.Icon | ||||||
| import androidx.compose.material3.ModalBottomSheet |  | ||||||
| import androidx.compose.material3.Scaffold | import androidx.compose.material3.Scaffold | ||||||
| import androidx.compose.material3.Text | import androidx.compose.material3.Text | ||||||
| import androidx.compose.material3.TextField | import androidx.compose.material3.TextField | ||||||
| import androidx.compose.runtime.Composable | import androidx.compose.runtime.Composable | ||||||
|  | import androidx.compose.runtime.LaunchedEffect | ||||||
|  | import androidx.compose.runtime.getValue | ||||||
|  | import androidx.compose.runtime.mutableStateOf | ||||||
|  | import androidx.compose.runtime.remember | ||||||
|  | import androidx.compose.runtime.setValue | ||||||
| import androidx.compose.ui.Alignment | import androidx.compose.ui.Alignment | ||||||
| import androidx.compose.ui.Modifier | import androidx.compose.ui.Modifier | ||||||
| import androidx.compose.ui.platform.LocalContext | import androidx.compose.ui.platform.LocalContext | ||||||
|  | @ -29,8 +35,58 @@ import androidx.compose.ui.text.input.KeyboardType | ||||||
| import androidx.compose.ui.tooling.preview.Preview | import androidx.compose.ui.tooling.preview.Preview | ||||||
| import androidx.compose.ui.unit.dp | import androidx.compose.ui.unit.dp | ||||||
| import androidx.compose.ui.unit.sp | import androidx.compose.ui.unit.sp | ||||||
|  | import androidx.datastore.core.DataStore | ||||||
|  | import androidx.datastore.preferences.core.Preferences | ||||||
|  | import androidx.datastore.preferences.core.edit | ||||||
|  | import androidx.datastore.preferences.core.stringPreferencesKey | ||||||
|  | import androidx.datastore.preferences.preferencesDataStore | ||||||
| import com.module.breeze.ui.theme.BreezeTheme | import com.module.breeze.ui.theme.BreezeTheme | ||||||
| import java.nio.file.WatchEvent | import kotlinx.coroutines.flow.first | ||||||
|  | import kotlinx.serialization.encodeToString | ||||||
|  | import kotlinx.serialization.json.Json | ||||||
|  | 
 | ||||||
|  | var plzArrayStatus: List<String> = listOf( | ||||||
|  |     "Loading...", | ||||||
|  |     "Loading...", | ||||||
|  |     "Loading...", | ||||||
|  |     "Loading...", | ||||||
|  |     "Loading...", | ||||||
|  |     "Loading...", | ||||||
|  |     "Loading...", | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | // ### Start ChatGPT + Fixing | ||||||
|  | val SHARED_PLZ_KEY = stringPreferencesKey("shared-plz") | ||||||
|  | val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings") | ||||||
|  | 
 | ||||||
|  | suspend fun saveArray(context: Context, array: List<String>) { | ||||||
|  |     context.dataStore.edit { preferences -> | ||||||
|  |         preferences[SHARED_PLZ_KEY] = Json.encodeToString(array) | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | suspend fun getArray(context: Context): List<String> { | ||||||
|  |     val preferences = context.dataStore.data.first() | ||||||
|  |     return preferences[SHARED_PLZ_KEY]?.let { Json.decodeFromString(it) } ?: emptyList() | ||||||
|  | } | ||||||
|  | // ### End ChatGPT | ||||||
|  | 
 | ||||||
|  | suspend fun getPLZ(index: Int, ctx: Context): String { | ||||||
|  |     var arr = getArray(ctx) | ||||||
|  |     if (arr.isEmpty() || arr.size != 7) { | ||||||
|  |         arr = listOf("8005", "8005", "8005", "8400", "8400", "8500", "8500") | ||||||
|  |     } | ||||||
|  |     plzArrayStatus = arr | ||||||
|  |     return arr[index] | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | suspend fun setPLZ(index: Int, value: String, ctx: Context) { | ||||||
|  |     var arr = plzArrayStatus.toMutableList() | ||||||
|  |     arr[index] = value | ||||||
|  |     saveArray(ctx, arr) | ||||||
|  | } | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| class SettingsActivity : ComponentActivity() { | class SettingsActivity : ComponentActivity() { | ||||||
|     override fun onCreate(savedInstanceState: Bundle?) { |     override fun onCreate(savedInstanceState: Bundle?) { | ||||||
|  | @ -52,14 +108,17 @@ class SettingsActivity : ComponentActivity() { | ||||||
|             var intent = Intent(ctx, MainActivity::class.java) |             var intent = Intent(ctx, MainActivity::class.java) | ||||||
|             ctx.startActivity(intent) |             ctx.startActivity(intent) | ||||||
|         } |         } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @Composable | @Composable | ||||||
| fun Settings(modifier: Modifier = Modifier) { | fun Settings(modifier: Modifier = Modifier) { | ||||||
|     val ctx = LocalContext.current |     val ctx = LocalContext.current | ||||||
|  | 
 | ||||||
|     Navigation(iconStyle.Home, "Home Icon", onClick = { |     Navigation(iconStyle.Home, "Home Icon", onClick = { | ||||||
|        SettingsActivity.openHome(ctx) |         SettingsActivity.openHome(ctx) | ||||||
|     }) |     }) | ||||||
|     Column( |     Column( | ||||||
|         modifier = Modifier.fillMaxSize(), |         modifier = Modifier.fillMaxSize(), | ||||||
|  | @ -83,13 +142,13 @@ fun Settings(modifier: Modifier = Modifier) { | ||||||
|                 fontSize = 22.sp |                 fontSize = 22.sp | ||||||
|             ) |             ) | ||||||
|         } |         } | ||||||
|         ConfiguredLocationDay("Monday") |         ConfiguredLocationDay("Monday", 0, ctx) | ||||||
|         ConfiguredLocationDay("Tuesday") |         ConfiguredLocationDay("Tuesday", 1, ctx) | ||||||
|         ConfiguredLocationDay("Wednesday") |         ConfiguredLocationDay("Wednesday", 2, ctx) | ||||||
|         ConfiguredLocationDay("Thursday") |         ConfiguredLocationDay("Thursday", 3, ctx) | ||||||
|         ConfiguredLocationDay("Friday") |         ConfiguredLocationDay("Friday", 4, ctx) | ||||||
|         ConfiguredLocationDay("Saturday") |         ConfiguredLocationDay("Saturday", 5, ctx) | ||||||
|         ConfiguredLocationDay("Sunday") |         ConfiguredLocationDay("Sunday", 6, ctx) | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -102,22 +161,48 @@ fun GreetingPreview2() { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @Composable | @Composable | ||||||
| fun ConfiguredLocationDay(dayName: String = "day") { | fun ConfiguredLocationDay(dayName: String = "day", index: Int, ctx: Context) { | ||||||
|     Row( |     var plz by remember { mutableStateOf("") } | ||||||
|         verticalAlignment = Alignment.CenterVertically, |     var isLoadingGet by remember { mutableStateOf(false) } | ||||||
|         modifier = Modifier.padding(0.dp, 4.dp) |     var isLoadingSet by remember { mutableStateOf(false) } | ||||||
|     ) { | 
 | ||||||
|         Text( |     LaunchedEffect(index) { | ||||||
|             text = dayName, |         isLoadingGet = true | ||||||
|             fontWeight = breezeFontWeight, |         plz = getPLZ(index, ctx) | ||||||
|             modifier = Modifier.width(100.dp) |         isLoadingGet = false | ||||||
|         ) |     } | ||||||
|         TextField( | 
 | ||||||
|             value = "0000", |     LaunchedEffect(plz) { | ||||||
|             onValueChange = {}, |         isLoadingSet = true | ||||||
|             label = { Text("PLZ") }, |         setPLZ(index, plz, ctx) | ||||||
|             keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), |         isLoadingSet = false | ||||||
|             modifier = Modifier.width(110.dp) |     } | ||||||
|         ) | 
 | ||||||
|  |     if (isLoadingSet || isLoadingGet) { | ||||||
|  |         CircularProgressIndicator() | ||||||
|  |     } else { | ||||||
|  |         Row( | ||||||
|  |             verticalAlignment = Alignment.CenterVertically, | ||||||
|  |             modifier = Modifier.padding(0.dp, 4.dp) | ||||||
|  |         ) { | ||||||
|  |             Text( | ||||||
|  |                 text = dayName, | ||||||
|  |                 fontWeight = breezeFontWeight, | ||||||
|  |                 modifier = Modifier.width(100.dp) | ||||||
|  |             ) | ||||||
|  |             TextField( | ||||||
|  |                 value = plz, | ||||||
|  |                 onValueChange = { newPlz -> | ||||||
|  |                     var cutNewPlz = newPlz | ||||||
|  |                     if (newPlz.length > 4) { | ||||||
|  |                         cutNewPlz = newPlz.substring(0, 4) | ||||||
|  |                     } | ||||||
|  |                     plz = cutNewPlz | ||||||
|  |                 }, | ||||||
|  |                 label = { Text("PLZ") }, | ||||||
|  |                 keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), | ||||||
|  |                 modifier = Modifier.width(110.dp) | ||||||
|  |             ) | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
		Loading…
	
		Reference in New Issue