|
| 1 | +package com.example.runningavater.settings |
| 2 | + |
| 3 | +import android.app.Application |
| 4 | +import androidx.compose.foundation.layout.Column |
| 5 | +import androidx.compose.foundation.text.KeyboardOptions |
| 6 | +import androidx.compose.material3.AlertDialog |
| 7 | +import androidx.compose.material3.Text |
| 8 | +import androidx.compose.material3.TextButton |
| 9 | +import androidx.compose.material3.TextField |
| 10 | +import androidx.compose.material3.TextFieldDefaults |
| 11 | +import androidx.compose.runtime.Composable |
| 12 | +import androidx.compose.runtime.LaunchedEffect |
| 13 | +import androidx.compose.runtime.collectAsState |
| 14 | +import androidx.compose.runtime.getValue |
| 15 | +import androidx.compose.runtime.mutableStateOf |
| 16 | +import androidx.compose.runtime.saveable.rememberSaveable |
| 17 | +import androidx.compose.runtime.setValue |
| 18 | +import androidx.compose.ui.graphics.Color |
| 19 | +import androidx.compose.ui.graphics.vector.ImageVector |
| 20 | +import androidx.compose.ui.text.input.KeyboardType |
| 21 | +import androidx.datastore.preferences.core.edit |
| 22 | +import androidx.lifecycle.AndroidViewModel |
| 23 | +import androidx.lifecycle.viewModelScope |
| 24 | +import androidx.lifecycle.viewmodel.compose.viewModel |
| 25 | +import com.example.runningavater.ui.theme.GranulatedSugar |
| 26 | +import dataStore |
| 27 | +import kotlinx.coroutines.flow.Flow |
| 28 | +import kotlinx.coroutines.flow.map |
| 29 | +import kotlinx.coroutines.launch |
| 30 | +import targetSteps |
| 31 | + |
| 32 | +@Composable |
| 33 | +fun GoalSettingDialog( |
| 34 | + onDismissRequest: () -> Unit, |
| 35 | + onConfirmation: () -> Unit, |
| 36 | + dialogTitle: String, |
| 37 | + dialogText: String, |
| 38 | + icon: ImageVector, |
| 39 | + viewModel: RoadDataStoreProfile = viewModel(), |
| 40 | +) { |
| 41 | + val bearName by viewModel.roadTargetSteps.collectAsState(initial = "未設定") |
| 42 | + var bearTextFieldValue by rememberSaveable { mutableStateOf("") } |
| 43 | + LaunchedEffect(bearName) { |
| 44 | + bearTextFieldValue = bearName |
| 45 | + } |
| 46 | + AlertDialog( |
| 47 | + containerColor = Color.White, |
| 48 | + title = { |
| 49 | + Text("目標歩数設定") |
| 50 | + }, |
| 51 | + text = { |
| 52 | + Column { |
| 53 | + Text("1日の目標歩数を入力してね") |
| 54 | + TextField( |
| 55 | + colors = |
| 56 | + TextFieldDefaults.colors( |
| 57 | + focusedContainerColor = GranulatedSugar, |
| 58 | + unfocusedContainerColor = GranulatedSugar, |
| 59 | + ), |
| 60 | + value = bearTextFieldValue, |
| 61 | + onValueChange = { newValue -> |
| 62 | + bearTextFieldValue = newValue |
| 63 | + }, |
| 64 | + keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number) |
| 65 | + ) |
| 66 | + } |
| 67 | + }, |
| 68 | + |
| 69 | + onDismissRequest = { |
| 70 | + onDismissRequest() |
| 71 | + }, |
| 72 | + confirmButton = { |
| 73 | + TextButton( |
| 74 | + onClick = { |
| 75 | + viewModel.saveTargetSteps(bearTextFieldValue) |
| 76 | + onConfirmation() |
| 77 | + } |
| 78 | + ) { |
| 79 | + Text("保存して閉じる") |
| 80 | + } |
| 81 | + }, |
| 82 | + dismissButton = { |
| 83 | + TextButton( |
| 84 | + onClick = { |
| 85 | + onDismissRequest() |
| 86 | + } |
| 87 | + ) { |
| 88 | + Text("閉じる") |
| 89 | + } |
| 90 | + } |
| 91 | + ) |
| 92 | +} |
| 93 | + |
| 94 | +class RoadDataStoreProfile(application: Application) : AndroidViewModel(application) { |
| 95 | + val roadTargetSteps: Flow<String> = |
| 96 | + getApplication<Application>().dataStore.data.map { preferences -> |
| 97 | + preferences[targetSteps] ?: "" |
| 98 | + } |
| 99 | + |
| 100 | + fun saveTargetSteps(name: String) { |
| 101 | + viewModelScope.launch { |
| 102 | + getApplication<Application>().dataStore.edit { preferences -> |
| 103 | + preferences[targetSteps] = name |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments