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"?> | ||||
| <project version="4"> | ||||
|   <component name="GradleMigrationSettings" migrationVersion="1" /> | ||||
|   <component name="GradleSettings"> | ||||
|     <option name="linkedExternalProjectsSettings"> | ||||
|       <GradleProjectSettings> | ||||
|  |  | |||
|  | @ -2,6 +2,7 @@ plugins { | |||
|     alias(libs.plugins.android.application) | ||||
|     alias(libs.plugins.kotlin.android) | ||||
|     alias(libs.plugins.kotlin.compose) | ||||
|     id("kotlin-parcelize") | ||||
| } | ||||
| 
 | ||||
| android { | ||||
|  | @ -60,4 +61,9 @@ dependencies { | |||
|     androidTestImplementation(libs.androidx.ui.test.junit4) | ||||
|     debugImplementation(libs.androidx.ui.tooling) | ||||
|     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 | ||||
| 
 | ||||
| 
 | ||||
| import android.content.Context | ||||
| import android.content.Intent | ||||
| import android.os.Bundle | ||||
|  | @ -16,12 +17,17 @@ import androidx.compose.foundation.layout.width | |||
| import androidx.compose.foundation.text.KeyboardOptions | ||||
| import androidx.compose.material.icons.outlined.Home | ||||
| import androidx.compose.material.icons.outlined.Map | ||||
| import androidx.compose.material3.CircularProgressIndicator | ||||
| import androidx.compose.material3.Icon | ||||
| import androidx.compose.material3.ModalBottomSheet | ||||
| import androidx.compose.material3.Scaffold | ||||
| import androidx.compose.material3.Text | ||||
| import androidx.compose.material3.TextField | ||||
| 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.Modifier | ||||
| 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.unit.dp | ||||
| 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 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() { | ||||
|     override fun onCreate(savedInstanceState: Bundle?) { | ||||
|  | @ -52,12 +108,15 @@ class SettingsActivity : ComponentActivity() { | |||
|             var intent = Intent(ctx, MainActivity::class.java) | ||||
|             ctx.startActivity(intent) | ||||
|         } | ||||
| 
 | ||||
| 
 | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| @Composable | ||||
| fun Settings(modifier: Modifier = Modifier) { | ||||
|     val ctx = LocalContext.current | ||||
| 
 | ||||
|     Navigation(iconStyle.Home, "Home Icon", onClick = { | ||||
|         SettingsActivity.openHome(ctx) | ||||
|     }) | ||||
|  | @ -83,13 +142,13 @@ fun Settings(modifier: Modifier = Modifier) { | |||
|                 fontSize = 22.sp | ||||
|             ) | ||||
|         } | ||||
|         ConfiguredLocationDay("Monday") | ||||
|         ConfiguredLocationDay("Tuesday") | ||||
|         ConfiguredLocationDay("Wednesday") | ||||
|         ConfiguredLocationDay("Thursday") | ||||
|         ConfiguredLocationDay("Friday") | ||||
|         ConfiguredLocationDay("Saturday") | ||||
|         ConfiguredLocationDay("Sunday") | ||||
|         ConfiguredLocationDay("Monday", 0, ctx) | ||||
|         ConfiguredLocationDay("Tuesday", 1, ctx) | ||||
|         ConfiguredLocationDay("Wednesday", 2, ctx) | ||||
|         ConfiguredLocationDay("Thursday", 3, ctx) | ||||
|         ConfiguredLocationDay("Friday", 4, ctx) | ||||
|         ConfiguredLocationDay("Saturday", 5, ctx) | ||||
|         ConfiguredLocationDay("Sunday", 6, ctx) | ||||
|     } | ||||
| } | ||||
| 
 | ||||
|  | @ -102,7 +161,26 @@ fun GreetingPreview2() { | |||
| } | ||||
| 
 | ||||
| @Composable | ||||
| fun ConfiguredLocationDay(dayName: String = "day") { | ||||
| fun ConfiguredLocationDay(dayName: String = "day", index: Int, ctx: Context) { | ||||
|     var plz by remember { mutableStateOf("") } | ||||
|     var isLoadingGet by remember { mutableStateOf(false) } | ||||
|     var isLoadingSet by remember { mutableStateOf(false) } | ||||
| 
 | ||||
|     LaunchedEffect(index) { | ||||
|         isLoadingGet = true | ||||
|         plz = getPLZ(index, ctx) | ||||
|         isLoadingGet = false | ||||
|     } | ||||
| 
 | ||||
|     LaunchedEffect(plz) { | ||||
|         isLoadingSet = true | ||||
|         setPLZ(index, plz, ctx) | ||||
|         isLoadingSet = false | ||||
|     } | ||||
| 
 | ||||
|     if (isLoadingSet || isLoadingGet) { | ||||
|         CircularProgressIndicator() | ||||
|     } else { | ||||
|         Row( | ||||
|             verticalAlignment = Alignment.CenterVertically, | ||||
|             modifier = Modifier.padding(0.dp, 4.dp) | ||||
|  | @ -113,11 +191,18 @@ fun ConfiguredLocationDay(dayName: String = "day") { | |||
|                 modifier = Modifier.width(100.dp) | ||||
|             ) | ||||
|             TextField( | ||||
|             value = "0000", | ||||
|             onValueChange = {}, | ||||
|                 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