Skip to content

Commit

Permalink
improve settings page
Browse files Browse the repository at this point in the history
  • Loading branch information
waozixyz committed Jun 19, 2024
1 parent 0b37ea0 commit 7ebc668
Show file tree
Hide file tree
Showing 18 changed files with 548 additions and 351 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Changelog
## [1.3.2] - 2024-06-20
- Add french translation thanks to @arkryonia
- improve settings page

## [1.3.1] - 2024-04-26
- Improve bar chart

Expand Down
8 changes: 7 additions & 1 deletion lib/i18n/de_DE.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@

"sessions": "Sitzungen",
"graph": "Grafik",
"breath_hold": "Atem anhalten"
"breath_hold": "Atem anhalten",

"about_support_title": "Über und Support",
"language_settings_title": "Spracheinstellungen",
"app_settings_title": "App-Einstellungen",
"data_management_button": "Datenverwaltung",
"language_settings_button": "Spracheinstellungen",
"about_button": "Über"
}
9 changes: 8 additions & 1 deletion lib/i18n/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,14 @@

"sessions": "Sessions",
"graph": "Graph",
"breath_hold": "Breath hold"
"breath_hold": "Breath hold",

"about_support_title": "About and Support",
"language_settings_title": "Language Settings",
"app_settings_title": "App Settings",
"data_management_button": "Data Management",
"language_settings_button": "Language Settings",
"about_button": "About"

}

10 changes: 9 additions & 1 deletion lib/i18n/es_ES.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@

"sessions": "Sesiones",
"graph": "Gráfica",
"breath_hold": "Retención de la respiración"
"breath_hold": "Retención de la respiración",

"about_support_title": "Acerca de y Soporte",
"language_settings_title": "Configuración de Idioma",
"app_settings_title": "Configuración de la Aplicación",
"data_management_button": "Gestión de Datos",
"language_settings_button": "Configuración de Idioma",
"about_button": "Acerca de"


}
11 changes: 10 additions & 1 deletion lib/i18n/fr_FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,14 @@

"sessions": "Sessions",
"graph": "Graphique",
"breath_hold": "Retenue de souffle"
"breath_hold": "Retenue de souffle",


"about_support_title": "À propos et Support",
"language_settings_title": "Paramètres de Langue",
"app_settings_title": "Paramètres de l'Application",
"data_management_button": "Gestion des Données",
"language_settings_button": "Paramètres de Langue",
"about_button": "À propos"

}
10 changes: 9 additions & 1 deletion lib/i18n/id_ID.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,15 @@

"sessions": "Sesi",
"graph": "Grafik",
"breath_hold": "Tahan napas"
"breath_hold": "Tahan napas",

"about_support_title": "Tentang dan Dukungan",
"language_settings_title": "Pengaturan Bahasa",
"app_settings_title": "Pengaturan Aplikasi",
"data_management_button": "Manajemen Data",
"language_settings_button": "Pengaturan Bahasa",
"about_button": "Tentang"


}

10 changes: 9 additions & 1 deletion lib/i18n/it_IT.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,15 @@

"sessions": "Sessioni",
"graph": "Grafico",
"breath_hold": "Trattenimento del respiro"
"breath_hold": "Trattenimento del respiro",

"about_support_title": "Informazioni e Supporto",
"language_settings_title": "Impostazioni della Lingua",
"app_settings_title": "Impostazioni dell'App",
"data_management_button": "Gestione dei Dati",
"language_settings_button": "Impostazioni della Lingua",
"about_button": "Informazioni"


}

Empty file added lib/models/data.dart
Empty file.
88 changes: 61 additions & 27 deletions lib/providers/user_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class UserProvider with ChangeNotifier {
user = User(id: Uuid().v4(), preferences: Preferences());
} else {
final userJson = prefs.getString(userId);
user = userJson != null ? User.fromJson(jsonDecode(userJson)) : User(id: userId, preferences: Preferences());
user = userJson != null
? User.fromJson(jsonDecode(userJson))
: User(id: userId, preferences: Preferences());
}
prefs.setString(user.id, jsonEncode(user.toJson()));
prefs.setString('userId', user.id);
Expand All @@ -55,15 +57,17 @@ class UserProvider with ChangeNotifier {

Future<Session?> loadSessionData() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? sessionJson = prefs.getString('${user.id}/sessions/${user.currentSessionId}');
String? sessionJson =
prefs.getString('${user.id}/sessions/${user.currentSessionId}');
if (sessionJson != null) {
Session session = Session.fromJson(jsonDecode(sessionJson));
return session;
}
return null;
}

Future<Map<String, dynamic>> _loadData(List<String> keys, String prefix) async {
Future<Map<String, dynamic>> _loadData(
List<String> keys, String prefix) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
Map<String, dynamic> loadedData = {};
for (var key in keys) {
Expand Down Expand Up @@ -97,28 +101,32 @@ class UserProvider with ChangeNotifier {
Future<void> saveUserPreferences(Preferences preferences) async {
await _saveData(preferences.toJson(), user.id);
}

Future<String> startNewSession([DateTime? dateTime]) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
DateTime sessionDateTime = dateTime?.roundToNearestMinute() ?? DateTime.now().roundToNearestMinute();
int timestamp = sessionDateTime.millisecondsSinceEpoch; // Convert to timestamp
DateTime sessionDateTime = dateTime?.roundToNearestMinute() ??
DateTime.now().roundToNearestMinute();
int timestamp =
sessionDateTime.millisecondsSinceEpoch; // Convert to timestamp
String sessionId = timestamp.toString();
Session session = Session(id: sessionId, rounds: {});
prefs.setString('${user.id}/sessions/$sessionId', jsonEncode(session.toJson()));
prefs.setString(
'${user.id}/sessions/$sessionId', jsonEncode(session.toJson()));
user.currentSessionId = sessionId;
notifyListeners();
return sessionId;
}

Future<void> saveSessionData(Session session) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('${user.id}/sessions/${session.id}', jsonEncode(session.toJson()));
prefs.setString(
'${user.id}/sessions/${session.id}', jsonEncode(session.toJson()));
}

Future<void> convertOldSessions() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
final allKeys = prefs.getKeys();
for (String key in allKeys) {

if (key.startsWith('${user.id}/sessions/')) {
var sessionJson = prefs.getString(key);
if (sessionJson != null) {
Expand All @@ -128,10 +136,12 @@ class UserProvider with ChangeNotifier {
DateTime roundedDateTime = oldDateTime.roundToNearestMinute();
String newSessionId = roundedDateTime.toIso8601String();

if (!RegExp(r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$').hasMatch(sessionData['id'])) {
if (!RegExp(r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$')
.hasMatch(sessionData['id'])) {
sessionData['id'] = newSessionId;
prefs.remove(key);
prefs.setString('${user.id}/sessions/$newSessionId', jsonEncode(sessionData));
prefs.setString(
'${user.id}/sessions/$newSessionId', jsonEncode(sessionData));
}
}
}
Expand Down Expand Up @@ -163,6 +173,7 @@ class UserProvider with ChangeNotifier {

return allData;
}

Future<void> importData(Map<String, dynamic> importedData) async {
SharedPreferences prefs = await SharedPreferences.getInstance();

Expand All @@ -183,15 +194,17 @@ class UserProvider with ChangeNotifier {
for (var sessionJson in sessionsList) {
if (sessionJson is Map<String, dynamic>) {
if (sessionJson.containsKey('dateTime')) {
DateTime oldDateTime = DateTime.tryParse(sessionJson['dateTime']) ?? DateTime.now();
DateTime oldDateTime =
DateTime.tryParse(sessionJson['dateTime']) ?? DateTime.now();
DateTime roundedDateTime = oldDateTime.roundToNearestMinute();
int timestamp = roundedDateTime.millisecondsSinceEpoch;
String sessionId = timestamp.toString();
sessionJson['id'] = sessionId;
}

Session importedSession = Session.fromJson(sessionJson);
await prefs.setString('${user.id}/sessions/${importedSession.id}', jsonEncode(importedSession.toJson()));
await prefs.setString('${user.id}/sessions/${importedSession.id}',
jsonEncode(importedSession.toJson()));
} else {
print('Invalid format for session data');
}
Expand All @@ -206,7 +219,8 @@ class UserProvider with ChangeNotifier {

Future<String> getLanguagePreference() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getString('languagePreference') ?? 'en'; // Default to 'en' if not set
return prefs.getString('languagePreference') ??
'en'; // Default to 'en' if not set
}

Future<void> setLanguagePreference(String languageCode) async {
Expand All @@ -218,20 +232,29 @@ class UserProvider with ChangeNotifier {
notifyListeners();
}

Future<void> moveRoundToSession(String oldSessionId, int roundNumber, int newTimestamp) async {
Future<void> moveRoundToSession(
String oldSessionId, int roundNumber, int newTimestamp) async {
SharedPreferences prefs = await SharedPreferences.getInstance();

Session? oldSession = (prefs.getString('${user.id}/sessions/$oldSessionId') != null)
? Session.fromJson(jsonDecode(prefs.getString('${user.id}/sessions/$oldSessionId')!))
Session? oldSession = (prefs
.getString('${user.id}/sessions/$oldSessionId') !=
null)
? Session.fromJson(
jsonDecode(prefs.getString('${user.id}/sessions/$oldSessionId')!))
: null;

String newSessionId = newTimestamp.toString();
Session newSession = (prefs.getString('${user.id}/sessions/$newSessionId') != null)
? Session.fromJson(jsonDecode(prefs.getString('${user.id}/sessions/$newSessionId')!))
Session newSession = (prefs
.getString('${user.id}/sessions/$newSessionId') !=
null)
? Session.fromJson(
jsonDecode(prefs.getString('${user.id}/sessions/$newSessionId')!))
: Session(id: newSessionId, rounds: {});

if (oldSession != null && oldSession.rounds.containsKey(roundNumber)) {
int newRoundNumber = newSession.rounds.isEmpty ? 1 : newSession.rounds.keys.reduce(max) + 1;
int newRoundNumber = newSession.rounds.isEmpty
? 1
: newSession.rounds.keys.reduce(max) + 1;
newSession.rounds[newRoundNumber] = oldSession.rounds[roundNumber]!;
oldSession.rounds.remove(roundNumber);

Expand All @@ -242,7 +265,8 @@ class UserProvider with ChangeNotifier {
notifyListeners();
}

Future<void> updateRoundDuration(String sessionId, int roundNumber, Duration newDuration) async {
Future<void> updateRoundDuration(
String sessionId, int roundNumber, Duration newDuration) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? sessionJson = prefs.getString('${user.id}/sessions/$sessionId');
if (sessionJson == null) return;
Expand All @@ -258,33 +282,36 @@ class UserProvider with ChangeNotifier {

Future<Map<int, Duration>> loadRoundDurations() async {
final prefs = await SharedPreferences.getInstance();
final sessionJson = prefs.getString('${user.id}/sessions/${user.currentSessionId}');
final sessionJson =
prefs.getString('${user.id}/sessions/${user.currentSessionId}');
if (sessionJson == null) return {};
final sessionData = Session.fromJson(jsonDecode(sessionJson));
return sessionData.rounds;
}

Future<void> deleteSessionData() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? sessionJson = prefs.getString('${user.id}/sessions/${user.currentSessionId}');
String? sessionJson =
prefs.getString('${user.id}/sessions/${user.currentSessionId}');
if (sessionJson == null) return;
Session session = Session.fromJson(jsonDecode(sessionJson));
Set<String> allKeys = prefs.getKeys();
for (String key in allKeys) {
if (key.startsWith('${user.id}/sessions/${user.currentSessionId}/rounds/')) {
if (key
.startsWith('${user.id}/sessions/${user.currentSessionId}/rounds/')) {
prefs.remove(key);
}
}
session.rounds = {};
saveSessionData(session);
notifyListeners();

}

Future<void> deleteRound(int roundNumber, [String? sessionId]) async {
final prefs = await SharedPreferences.getInstance();
String? finalSessionId = sessionId ?? user.currentSessionId;
String? sessionJson = prefs.getString('${user.id}/sessions/$finalSessionId');
String? sessionJson =
prefs.getString('${user.id}/sessions/$finalSessionId');
if (sessionJson == null) return;
Session session = Session.fromJson(jsonDecode(sessionJson));
session.rounds.remove(roundNumber);
Expand All @@ -299,6 +326,7 @@ class UserProvider with ChangeNotifier {
session.rounds = updatedRounds;
saveSessionData(session);
}

Future<List<Session>> getAllSessions() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
final allKeys = prefs.getKeys();
Expand All @@ -323,4 +351,10 @@ class UserProvider with ChangeNotifier {
return sessions;
}

}
Future<void> clearAllData() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.clear();
user = User(id: '', preferences: Preferences(), sessions: []);
notifyListeners();
}
}
Loading

0 comments on commit 7ebc668

Please sign in to comment.