made settingsservice to collection of util functions to prevent diffrent instances and therefore different states
parent
4e2dd41adc
commit
e4cfa701d6
|
@ -16,7 +16,6 @@ class MyHomePage extends StatefulWidget {
|
||||||
|
|
||||||
class MyHomePageState extends State<MyHomePage> {
|
class MyHomePageState extends State<MyHomePage> {
|
||||||
int _selectedIndex = 4;
|
int _selectedIndex = 4;
|
||||||
final SettingsService settingsService = SettingsService();
|
|
||||||
|
|
||||||
final List<String> _titles = [
|
final List<String> _titles = [
|
||||||
'Stimmung',
|
'Stimmung',
|
||||||
|
@ -33,17 +32,15 @@ class MyHomePageState extends State<MyHomePage> {
|
||||||
SettingsPage(),
|
SettingsPage(),
|
||||||
];
|
];
|
||||||
|
|
||||||
void _onItemTapped(int index) {
|
Future<void> _onItemTapped(int index) async {
|
||||||
setState(() async {
|
bool isConfigured = (await getGroup()) != null;
|
||||||
(await settingsService.getGroup() != null)
|
print('Gruppe: ${await getGroup()}, isConfigured: $isConfigured');
|
||||||
|
setState(() {
|
||||||
|
isConfigured
|
||||||
? _selectedIndex = index
|
? _selectedIndex = index
|
||||||
: showDialog(
|
: showDialog(
|
||||||
context: context,
|
context: context,
|
||||||
builder: (BuildContext context) {
|
builder: (BuildContext context) {
|
||||||
Future.delayed(
|
|
||||||
const Duration(seconds: 3),
|
|
||||||
() => Navigator.of(context)
|
|
||||||
.pop(true)); //Dismiss popup after 3 seconds
|
|
||||||
return const MissingConfigPopup();
|
return const MissingConfigPopup();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -3,7 +3,6 @@ import 'package:smoke_cess_app/service/settings_service.dart';
|
||||||
|
|
||||||
class SettingsPage extends StatelessWidget {
|
class SettingsPage extends StatelessWidget {
|
||||||
SettingsPage({super.key});
|
SettingsPage({super.key});
|
||||||
SettingsService service = SettingsService();
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
@ -17,7 +16,7 @@ class SettingsPage extends StatelessWidget {
|
||||||
style: ElevatedButton.styleFrom(
|
style: ElevatedButton.styleFrom(
|
||||||
textStyle: const TextStyle(fontSize: 20)),
|
textStyle: const TextStyle(fontSize: 20)),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
service.loadSettings();
|
loadSettings();
|
||||||
},
|
},
|
||||||
child: const Text('Read JSON'),
|
child: const Text('Read JSON'),
|
||||||
)
|
)
|
||||||
|
|
|
@ -11,7 +11,6 @@ class StopWatchTimerPage extends StatefulWidget {
|
||||||
}
|
}
|
||||||
|
|
||||||
class StopWatchTimerPageState extends State<StopWatchTimerPage> {
|
class StopWatchTimerPageState extends State<StopWatchTimerPage> {
|
||||||
SettingsService settings = SettingsService();
|
|
||||||
Duration duration = const Duration(minutes: 1);
|
Duration duration = const Duration(minutes: 1);
|
||||||
Timer? timer;
|
Timer? timer;
|
||||||
|
|
||||||
|
@ -19,22 +18,12 @@ class StopWatchTimerPageState extends State<StopWatchTimerPage> {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
setDurationWithSetting();
|
|
||||||
super.initState();
|
super.initState();
|
||||||
}
|
}
|
||||||
|
|
||||||
void setDurationWithSetting() {
|
|
||||||
settings.getIntSetting('workout_duration_minutes').then((workoutMinutes) =>
|
|
||||||
{setState(() => duration = Duration(minutes: workoutMinutes ?? 10))});
|
|
||||||
}
|
|
||||||
|
|
||||||
void reset() {
|
void reset() {
|
||||||
if (countDown) {
|
|
||||||
setDurationWithSetting();
|
|
||||||
} else {
|
|
||||||
setState(() => duration = const Duration());
|
setState(() => duration = const Duration());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void startTimer() {
|
void startTimer() {
|
||||||
timer = Timer.periodic(const Duration(seconds: 1), (_) => addTime());
|
timer = Timer.periodic(const Duration(seconds: 1), (_) => addTime());
|
||||||
|
|
|
@ -2,48 +2,42 @@ import 'package:shared_preferences/shared_preferences.dart';
|
||||||
import 'package:smoke_cess_app/models/settings.dart';
|
import 'package:smoke_cess_app/models/settings.dart';
|
||||||
import 'package:smoke_cess_app/service/json_service.dart';
|
import 'package:smoke_cess_app/service/json_service.dart';
|
||||||
|
|
||||||
class SettingsService {
|
final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
|
||||||
final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
|
|
||||||
|
|
||||||
SettingsService() {
|
//access group setting which was saved in local storage
|
||||||
setIntSetting('workout_duration_minutes', 5);
|
Future<String?> getGroup() {
|
||||||
}
|
|
||||||
|
|
||||||
//access group setting which was saved in local storage
|
|
||||||
Future<String?> getGroup() {
|
|
||||||
return getStringSetting('group');
|
return getStringSetting('group');
|
||||||
}
|
}
|
||||||
|
|
||||||
void setStringSetting(String settingKey, String settingValue) =>
|
void setStringSetting(String settingKey, String settingValue) =>
|
||||||
_prefs.then((pref) => pref.setString(settingKey, settingValue));
|
_prefs.then((pref) => pref.setString(settingKey, settingValue));
|
||||||
|
|
||||||
Future<String?> getStringSetting(String settingKey) =>
|
Future<String?> getStringSetting(String settingKey) =>
|
||||||
_prefs.then((pref) => pref.getString(settingKey));
|
_prefs.then((pref) => pref.getString(settingKey));
|
||||||
|
|
||||||
void setIntSetting(String settingKey, int settingValue) =>
|
void setIntSetting(String settingKey, int settingValue) =>
|
||||||
_prefs.then((pref) => pref.setInt(settingKey, settingValue));
|
_prefs.then((pref) => pref.setInt(settingKey, settingValue));
|
||||||
|
|
||||||
Future<int?> getIntSetting(String settingKey) =>
|
Future<int?> getIntSetting(String settingKey) =>
|
||||||
_prefs.then((pref) => pref.getInt(settingKey));
|
_prefs.then((pref) => pref.getInt(settingKey));
|
||||||
|
|
||||||
void setStringList(String settingKey, List<String> list) =>
|
void setStringList(String settingKey, List<String> list) =>
|
||||||
_prefs.then((pref) => pref.setStringList(settingKey, list));
|
_prefs.then((pref) => pref.setStringList(settingKey, list));
|
||||||
|
|
||||||
//Add other setters and getters if needed
|
//Add other setters and getters if needed
|
||||||
//other possible SharedPreferences Types: Int, Bool, Double, StringList
|
//other possible SharedPreferences Types: Int, Bool, Double, StringList
|
||||||
//see https://pub.dev/packages/shared_preferences
|
//see https://pub.dev/packages/shared_preferences
|
||||||
|
|
||||||
Future<void> loadSettings() async {
|
Future<void> loadSettings() async {
|
||||||
Map<String, dynamic> configJSON = await loadLocalConfigJSON();
|
Map<String, dynamic> configJSON = await loadLocalConfigJSON();
|
||||||
Settings settings = Settings.fromJson(configJSON);
|
Settings settings = Settings.fromJson(configJSON);
|
||||||
print(settings.group);
|
print(settings.group);
|
||||||
saveSettings(settings);
|
saveSettings(settings);
|
||||||
String gruppe = (await getGroup())!;
|
String gruppe = (await getGroup())!;
|
||||||
print('Gruppe: $gruppe');
|
print('Gruppe: $gruppe');
|
||||||
}
|
}
|
||||||
|
|
||||||
void saveSettings(Settings settings) {
|
void saveSettings(Settings settings) {
|
||||||
setStringSetting('group', settings.group);
|
setStringSetting('group', settings.group);
|
||||||
setStringSetting('key', settings.key);
|
setStringSetting('key', settings.key);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue