Skip to content

Commit

Permalink
fix save data
Browse files Browse the repository at this point in the history
  • Loading branch information
waozixyz committed Nov 12, 2023
1 parent a3f1b71 commit 19372f6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 32 deletions.
31 changes: 5 additions & 26 deletions lib/providers/user_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,17 @@ class UserProvider with ChangeNotifier {
_initializeUser();
}

Future<void> _initializeUser() async {
Future<void> _initializeUser() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
final userId = prefs.getString('userId');
if (userId == null) {
final newUser = User(
id: Uuid().v4(),
preferences: Preferences(),
);
user = newUser;
await _saveUser();
user = User(id: Uuid().v4(), preferences: Preferences());
} else {
final userJson = prefs.getString(userId);
if (userJson != null) {
user = User.fromJson(jsonDecode(userJson));
} else {
final newUser = User(
id: userId,
preferences: Preferences(),
);
user = newUser;
await _saveUser();
}
user = userJson != null ? User.fromJson(jsonDecode(userJson)) : User(id: userId, preferences: Preferences());
}
}

Future<void> _saveUser() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString(user.id, jsonEncode(user.toJson()));
prefs.setString('userId', user.id);
}

Future<void> markTutorialAsComplete() async {
Expand Down Expand Up @@ -148,14 +131,10 @@ Future<void> _initializeUser() async {
return sessionData.rounds;
}

Future<void> clearCurrentSession() async {
Future<void> deleteSessionData() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.remove('${user.id}/currentSession');
}

Future<void> deleteSessionData() async {
SharedPreferences prefs = await SharedPreferences.getInstance();

Set<String> allKeys = prefs.getKeys();

Iterable<String> sessionKeys = allKeys.where((key) => key.startsWith('${user.id}/sessions/${user.currentSessionId}/'));
Expand Down
1 change: 0 additions & 1 deletion lib/screens/results_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class _ResultsScreenState extends State<ResultsScreen> {
Future<void> _handleClose() async {
final userProvider = Provider.of<UserProvider>(context, listen: false);
if (!saveProgress) {
await userProvider.clearCurrentSession();
await userProvider.deleteSessionData();
}
}
Expand Down
9 changes: 4 additions & 5 deletions lib/screens/splash_screen.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:go_router/go_router.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SplashScreen extends StatefulWidget {
@override
State<SplashScreen> createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {



Future<bool> _checkTutorialCompletion() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getBool('tutorialComplete') ?? false;
final userId = prefs.getString('userId');
return prefs.getBool('$userId/tutorialComplete') ?? false;
}

@override
void initState() {
super.initState();
Expand Down

0 comments on commit 19372f6

Please sign in to comment.