Skip to content

Commit

Permalink
Fixed Flutter 3.7 material changes
Browse files Browse the repository at this point in the history
Fixed initialization
  • Loading branch information
xpavle00 committed Jan 28, 2023
1 parent 30e2e3a commit 8fb4413
Show file tree
Hide file tree
Showing 12 changed files with 618 additions and 483 deletions.
2 changes: 1 addition & 1 deletion lib/habits/habit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class HabitState extends State<Habit> {
textAlign: TextAlign.center,
style: const TextStyle(color: Colors.white),
),
backgroundColor: Theme.of(context).toggleableActiveColor,
backgroundColor: Theme.of(context).colorScheme.primary,
behavior: SnackBarBehavior.floating,
),
);
Expand Down
17 changes: 9 additions & 8 deletions lib/habits/habits_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ class HabitsManager extends ChangeNotifier {
GlobalKey<ScaffoldMessengerState>();

late List<Habit> allHabits = [];
bool dataLoaded = false;
bool _isInitialized = false;

Habit? deletedHabit;
Queue<Habit> toDelete = Queue();

HabitsManager() {
initModel();
}

void initialize() async {
await initModel();
await Future.delayed(const Duration(seconds: 5));
notifyListeners();
}

Expand All @@ -37,7 +35,7 @@ class HabitsManager extends ChangeNotifier {
initModel() async {
await _haboModel.initDatabase();
allHabits = await _haboModel.getAllHabits();
dataLoaded = true;
_isInitialized = true;
notifyListeners();
}

Expand Down Expand Up @@ -118,6 +116,10 @@ class HabitsManager extends ChangeNotifier {
return allHabits;
}

bool get isInitialized {
return _isInitialized;
}

reorderList(oldIndex, newIndex) {
if (newIndex > oldIndex) {
newIndex -= 1;
Expand Down Expand Up @@ -199,8 +201,7 @@ class HabitsManager extends ChangeNotifier {
notifyListeners();
}

String getNameOfHabit(int id)
{
String getNameOfHabit(int id) {
Habit? hab = findHabitById(id);
return (hab != null) ? hab.habitData.title : "";
}
Expand Down
1 change: 1 addition & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class _HaboState extends State<Habo> {
_appRouter = AppRouter(
appStateManager: _appStateManager,
settingsManager: _settingsManager,
habitsManager: _habitManager,
);
super.initState();
}
Expand Down
6 changes: 3 additions & 3 deletions lib/model/settings_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class SettingsData {
'soundEffects': soundEffects,
'showMonthName': showMonthName,
'seenOnboarding': seenOnboarding,
'checkColor': checkColor.hashCode,
'failColor': failColor.hashCode,
'skipColor': skipColor.hashCode,
'checkColor': checkColor.value,
'failColor': failColor.value,
'skipColor': skipColor.value,
};
}
21 changes: 16 additions & 5 deletions lib/navigation/app_router.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import 'package:flutter/material.dart';
import 'package:habo/habits/edit_habit_screen.dart';
import 'package:habo/habits/habits_manager.dart';
import 'package:habo/habits/habits_screen.dart';
import 'package:habo/navigation/app_state_manager.dart';
import 'package:habo/navigation/routes.dart';
import 'package:habo/onboarding/onboarding_screen.dart';
import 'package:habo/settings/settings_manager.dart';
import 'package:habo/settings/settings_screen.dart';
import 'package:habo/splash_screen.dart';
import 'package:habo/habits/habits_screen.dart';
import 'package:habo/statistics/statistics_screen.dart';

class AppRouter extends RouterDelegate
Expand All @@ -16,18 +17,24 @@ class AppRouter extends RouterDelegate

final AppStateManager appStateManager;
final SettingsManager settingsManager;
final HabitsManager habitsManager;

AppRouter({required this.appStateManager, required this.settingsManager})
AppRouter(
{required this.appStateManager,
required this.settingsManager,
required this.habitsManager})
: navigatorKey = GlobalKey<NavigatorState>() {
appStateManager.addListener(notifyListeners);
settingsManager.addListener(notifyListeners);
habitsManager.addListener(notifyListeners);
settingsManager.getSeenOnboarding;
}

@override
void dispose() {
appStateManager.removeListener(notifyListeners);
settingsManager.removeListener(notifyListeners);
habitsManager.removeListener(notifyListeners);
super.dispose();
}

Expand All @@ -37,19 +44,23 @@ class AppRouter extends RouterDelegate
key: navigatorKey,
onPopPage: _handlePopPage,
pages: [
if (!appStateManager.isInitialized) SplashScreen.page(),
if (appStateManager.isInitialized) HabitsScreen.page(),
if (allInitialized()) HabitsScreen.page(),
if (appStateManager.getStatistics) StatisticsScreen.page(),
if (appStateManager.getSettings) SettingsScreen.page(),
if (appStateManager.getOnboarding || !settingsManager.getSeenOnboarding)
OnboardingScreen.page(),
if (appStateManager.getCreateHabit) EditHabitScreen.page(null),
if (appStateManager.getCreateHabit) EditHabitScreen.page(null),
if (appStateManager.getEditHabit != null)
EditHabitScreen.page(appStateManager.getEditHabit!),
if (!allInitialized()) SplashScreen.page(),
],
);
}

bool allInitialized() {
return settingsManager.isInitialized && habitsManager.isInitialized;
}

bool _handlePopPage(Route<dynamic> route, result) {
if (!route.didPop(result)) {
return false;
Expand Down
14 changes: 0 additions & 14 deletions lib/navigation/app_state_manager.dart
Original file line number Diff line number Diff line change
@@ -1,33 +1,19 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:habo/model/habit_data.dart';

class AppStateManager extends ChangeNotifier {
bool _initialized = false;
bool _statistics = false;
bool _settings = false;
bool _onboarding = false;
bool _createHabit = false;
HabitData? _editHabit;

bool get isInitialized => _initialized;
bool get getStatistics => _statistics;
bool get getSettings => _settings;
bool get getOnboarding => _onboarding;
bool get getCreateHabit => _createHabit;
HabitData? get getEditHabit => _editHabit;

void initializeApp() {
Timer(
const Duration(milliseconds: 2000),
() {
_initialized = true;
notifyListeners();
},
);
}

void goStatistics(bool state) {
_statistics = state;
notifyListeners();
Expand Down
2 changes: 1 addition & 1 deletion lib/settings/color_icon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class _ColorIconState extends State<ColorIcon> {
onColorChanged: (Color color) {
setState(
() {
tempColor = color;
tempColor = color.withOpacity(1.0);
},
);
},
Expand Down
11 changes: 8 additions & 3 deletions lib/settings/settings_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import 'package:shared_preferences/shared_preferences.dart';
class SettingsManager extends ChangeNotifier {
final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
SettingsData _settingsData = SettingsData();
bool _isInitialized = false;

final _checkPlayer = AudioPlayer();
final _clickPlayer = AudioPlayer();

void initialize() async {
loadData();

await loadData();
_isInitialized = true;
notifyListeners();
_checkPlayer.setAsset('assets/sounds/check.wav');
_clickPlayer.setAsset('assets/sounds/click.wav');
Expand Down Expand Up @@ -58,7 +59,7 @@ class SettingsManager extends ChangeNotifier {
prefs.setString('habo_settings', jsonEncode(_settingsData));
}

void loadData() async {
Future<void> loadData() async {
final SharedPreferences prefs = await _prefs;
String? json = prefs.getString('habo_settings');
if (json != null) {
Expand Down Expand Up @@ -134,6 +135,10 @@ class SettingsManager extends ChangeNotifier {
return _settingsData.skipColor;
}

bool get isInitialized {
return _isInitialized;
}

set setTheme(String value) {
_settingsData.theme = Themes.values[_settingsData.themeList.indexOf(value)];
saveData();
Expand Down
Loading

0 comments on commit 8fb4413

Please sign in to comment.