Added SettingsProvider around MyApp

main
Kai Mannweiler 2023-02-27 02:27:42 +01:00
parent a574e7f718
commit a2ed17fd5c
4 changed files with 58 additions and 2 deletions

View File

@ -1,8 +1,10 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:smoke_cess_app/pages/main_page.dart';
import 'package:smoke_cess_app/services/notification_service.dart';
import 'package:timezone/data/latest.dart' as tz;
import 'globals.dart' as globals;
import 'providers/settings_provider.dart';
void main() {
// to ensure all the widgets are initialized.
@ -21,6 +23,11 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return const MaterialApp(title: _title, home: MyHomePage());
return MaterialApp(
title: _title,
home: ChangeNotifierProvider(
create: (context) => SettingsProvider(),
child: const MyHomePage(),
));
}
}

View File

@ -1,9 +1,11 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:smoke_cess_app/pages/mood_page.dart';
import 'package:smoke_cess_app/pages/relapse_page.dart';
import 'package:smoke_cess_app/pages/scanner_page.dart';
import 'package:smoke_cess_app/pages/sleep_page.dart';
import 'package:smoke_cess_app/pages/interval_page.dart';
import 'package:smoke_cess_app/providers/settings_provider.dart';
import 'package:smoke_cess_app/services/settings_service.dart';
import 'package:smoke_cess_app/widgets/missing_config_popup.dart';

View File

@ -1,6 +1,8 @@
import 'package:flutter/material.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:provider/provider.dart';
import 'package:smoke_cess_app/models/mood.dart';
import 'package:smoke_cess_app/models/relapse.dart';
import 'package:smoke_cess_app/models/settings.dart';
import 'package:smoke_cess_app/services/database_service.dart';
import 'package:smoke_cess_app/services/json_service.dart';
@ -8,6 +10,7 @@ import 'package:smoke_cess_app/services/settings_service.dart';
import 'package:smoke_cess_app/services/notification_service.dart';
import '../models/sleep.dart';
import '../providers/settings_provider.dart';
import '../widgets/missing_config_popup.dart';
import '../globals.dart' as globals;
@ -23,6 +26,7 @@ class ScannerPageState extends State<ScannerPage> {
@override
Widget build(BuildContext context) {
var settings = context.watch<SettingsProvider>();
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
@ -72,7 +76,10 @@ class ScannerPageState extends State<ScannerPage> {
textStyle: const TextStyle(fontSize: 20)),
onPressed: () {
loadSettingsFromLocalJSON();
NotificationService().setAllNotifications();
Future.delayed(Duration(milliseconds: 100), () {
settings.initSettings();
NotificationService().setAllNotifications();
});
},
child: const Text('Read JSON'),
),
@ -84,12 +91,17 @@ class ScannerPageState extends State<ScannerPage> {
List<Mood> moods = await globals.databaseService.getMoodRecords();
List<Sleep> sleeps =
await globals.databaseService.getSleepRecords();
List<Relapse> relapses =
await globals.databaseService.getRelapseRecords();
for (Mood mood in moods) {
print(mood.toCSV());
}
for (Sleep sleep in sleeps) {
print(sleep.toCSV());
}
for (Relapse relapse in relapses) {
print(relapse.toCSV());
}
},
child: const Text('Export'),
)

View File

@ -0,0 +1,35 @@
import 'package:flutter/material.dart';
import 'package:smoke_cess_app/services/settings_service.dart';
import '../models/settings.dart';
class SettingsProvider extends ChangeNotifier {
Settings? _settings;
Settings? get settings => _settings;
SettingsProvider() {
initSettings();
}
void initSettings() async {
int? group = await getGroup();
List<String>? relapseCategories = await getRelapseCategories();
int? moodHours = await getMoodQueryHours();
int? moodMinutes = await getMoodQueryMinutes();
List<String>? moodDays = await getMoodQueryDaysCategories();
int? sleepHours = await getSleepQueryHours();
int? sleepMinutes = await getSleepQueryMinutes();
List<String>? sleepDays = await getSleepQueryDaysCategories();
int? chessHours = await getChessHours();
int? chessMinutes = await getChessMinutes();
_settings = Settings(
group ?? 0,
relapseCategories,
QueryConfig(moodHours ?? 0, moodMinutes ?? 0, moodDays),
QueryConfig(sleepHours ?? 0, sleepMinutes ?? 0, sleepDays),
TimeConfig(chessHours ?? 0, chessMinutes ?? 0));
notifyListeners();
}
}