made settingsservice to collection of util functions to prevent diffrent instances and therefore different states

main
Julian Gegner 2023-02-19 14:24:39 +01:00
parent 4e2dd41adc
commit e4cfa701d6
4 changed files with 44 additions and 65 deletions

View File

@ -16,7 +16,6 @@ class MyHomePage extends StatefulWidget {
class MyHomePageState extends State<MyHomePage> {
int _selectedIndex = 4;
final SettingsService settingsService = SettingsService();
final List<String> _titles = [
'Stimmung',
@ -33,17 +32,15 @@ class MyHomePageState extends State<MyHomePage> {
SettingsPage(),
];
void _onItemTapped(int index) {
setState(() async {
(await settingsService.getGroup() != null)
Future<void> _onItemTapped(int index) async {
bool isConfigured = (await getGroup()) != null;
print('Gruppe: ${await getGroup()}, isConfigured: $isConfigured');
setState(() {
isConfigured
? _selectedIndex = index
: showDialog(
context: context,
builder: (BuildContext context) {
Future.delayed(
const Duration(seconds: 3),
() => Navigator.of(context)
.pop(true)); //Dismiss popup after 3 seconds
return const MissingConfigPopup();
});
});

View File

@ -3,7 +3,6 @@ import 'package:smoke_cess_app/service/settings_service.dart';
class SettingsPage extends StatelessWidget {
SettingsPage({super.key});
SettingsService service = SettingsService();
@override
Widget build(BuildContext context) {
@ -17,7 +16,7 @@ class SettingsPage extends StatelessWidget {
style: ElevatedButton.styleFrom(
textStyle: const TextStyle(fontSize: 20)),
onPressed: () {
service.loadSettings();
loadSettings();
},
child: const Text('Read JSON'),
)

View File

@ -11,7 +11,6 @@ class StopWatchTimerPage extends StatefulWidget {
}
class StopWatchTimerPageState extends State<StopWatchTimerPage> {
SettingsService settings = SettingsService();
Duration duration = const Duration(minutes: 1);
Timer? timer;
@ -19,22 +18,12 @@ class StopWatchTimerPageState extends State<StopWatchTimerPage> {
@override
void initState() {
setDurationWithSetting();
super.initState();
}
void setDurationWithSetting() {
settings.getIntSetting('workout_duration_minutes').then((workoutMinutes) =>
{setState(() => duration = Duration(minutes: workoutMinutes ?? 10))});
}
void reset() {
if (countDown) {
setDurationWithSetting();
} else {
setState(() => duration = const Duration());
}
}
void startTimer() {
timer = Timer.periodic(const Duration(seconds: 1), (_) => addTime());

View File

@ -2,13 +2,8 @@ import 'package:shared_preferences/shared_preferences.dart';
import 'package:smoke_cess_app/models/settings.dart';
import 'package:smoke_cess_app/service/json_service.dart';
class SettingsService {
final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
SettingsService() {
setIntSetting('workout_duration_minutes', 5);
}
//access group setting which was saved in local storage
Future<String?> getGroup() {
return getStringSetting('group');
@ -46,4 +41,3 @@ class SettingsService {
setStringSetting('group', settings.group);
setStringSetting('key', settings.key);
}
}