Skip to content

Commit b131ced

Browse files
committed
merge main
2 parents ffef8aa + 6913f22 commit b131ced

File tree

5 files changed

+270
-224
lines changed

5 files changed

+270
-224
lines changed

Diff for: .github/workflows/main.yml

-83
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
11
import android.content.Context
22
import androidx.datastore.core.DataStore
33
import androidx.datastore.preferences.core.Preferences
4-
import androidx.datastore.preferences.core.edit
54
import androidx.datastore.preferences.core.intPreferencesKey
5+
import androidx.datastore.preferences.core.stringPreferencesKey
66
import androidx.datastore.preferences.preferencesDataStore
7-
import kotlinx.coroutines.flow.Flow
8-
import kotlinx.coroutines.flow.map
97

10-
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")
8+
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "dataStore")
119

1210
// Key definition
1311
val EXAMPLE_COUNTER = intPreferencesKey("example_counter")
14-
15-
// Read data from DataStore
16-
fun exampleCounterFlow(context: Context): Flow<Int> =
17-
context.dataStore.data
18-
.map { preferences ->
19-
preferences[EXAMPLE_COUNTER] ?: 0
20-
}
21-
22-
// Save data to DataStore
23-
suspend fun incrementCounter(context: Context) {
24-
context.dataStore.edit { preferences ->
25-
val currentCounterValue = preferences[EXAMPLE_COUNTER] ?: 0
26-
preferences[EXAMPLE_COUNTER] = currentCounterValue + 1
27-
}
28-
}
12+
val bearName = stringPreferencesKey("bearName")
13+
val targetPeriod = stringPreferencesKey("target_period")
14+
val targetSteps = stringPreferencesKey("target_steps")
15+
val userName = stringPreferencesKey("UserName")
16+
val userIcon = stringPreferencesKey("UserIcon")
17+
val enthusiaasm = stringPreferencesKey("Enthusiasm")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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

Comments
 (0)