diff --git a/lib/main.dart b/lib/main.dart index 2c2245d..a1cf5ac 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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(), + )); } } diff --git a/lib/pages/main_page.dart b/lib/pages/main_page.dart index 2af4f13..739f0d1 100644 --- a/lib/pages/main_page.dart +++ b/lib/pages/main_page.dart @@ -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'; diff --git a/lib/pages/scanner_page.dart b/lib/pages/scanner_page.dart index 7159694..82485f8 100644 --- a/lib/pages/scanner_page.dart +++ b/lib/pages/scanner_page.dart @@ -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 { @override Widget build(BuildContext context) { + var settings = context.watch(); return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, @@ -72,7 +76,10 @@ class ScannerPageState extends State { 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 { List moods = await globals.databaseService.getMoodRecords(); List sleeps = await globals.databaseService.getSleepRecords(); + List 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'), ) diff --git a/lib/providers/settings_provider.dart b/lib/providers/settings_provider.dart new file mode 100644 index 0000000..71efb11 --- /dev/null +++ b/lib/providers/settings_provider.dart @@ -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? relapseCategories = await getRelapseCategories(); + int? moodHours = await getMoodQueryHours(); + int? moodMinutes = await getMoodQueryMinutes(); + List? moodDays = await getMoodQueryDaysCategories(); + int? sleepHours = await getSleepQueryHours(); + int? sleepMinutes = await getSleepQueryMinutes(); + List? 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(); + } +}