Skip to content

Commit

Permalink
Merge pull request #69 from QuantumPhysique/addBackupReminder
Browse files Browse the repository at this point in the history
Remember user to frequently backup data, #66
  • Loading branch information
braniii authored Oct 1, 2024
2 parents 6b29b06 + 8f41c8f commit f96de4f
Show file tree
Hide file tree
Showing 9 changed files with 343 additions and 96 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [Unreleased]
### Other changes:
- Add backup reminder, see settings for more options


## [0.7.2] - 2024-09-22
Expand Down
54 changes: 54 additions & 0 deletions app/lib/core/backupInterval.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';


/// Enum with all available backup intervals
enum BackupInterval {
/// never
never,
/// weekly
weekly,
/// bi-weekly
biweekly,
/// monthly
monthly,
/// quarterly
quarterly,
}

/// extend interpolation strength
extension BackupIntervalExtension on BackupInterval {
/// get the length [days]
int get inDays => <BackupInterval, int>{
BackupInterval.never: -1,
BackupInterval.weekly: 7,
BackupInterval.biweekly: 14,
BackupInterval.monthly: 30,
BackupInterval.quarterly: 90,
}[this]!;

/// get international name
String nameLong (BuildContext context) => <BackupInterval, String>{
BackupInterval.never: AppLocalizations.of(context)!.never,
BackupInterval.weekly: AppLocalizations.of(context)!.weekly,
BackupInterval.biweekly: AppLocalizations.of(context)!.biweekly,
BackupInterval.monthly: AppLocalizations.of(context)!.monthly,
BackupInterval.quarterly: AppLocalizations.of(context)!.quarterly,
}[this]!;

/// get string expression
String get name => toString().split('.').last;
}

/// convert string to interpolation strength
extension BackupIntervalParsing on String {
/// convert string to interpolation strength
BackupInterval? toBackupInterval() {
for (final BackupInterval interval in BackupInterval.values) {
if (this == interval.name) {
return interval;
}
}
return null;
}
}
35 changes: 35 additions & 0 deletions app/lib/core/preferences.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:shared_preferences/shared_preferences.dart';
import 'package:trale/core/backupInterval.dart';

import 'package:trale/core/interpolation.dart';
import 'package:trale/core/language.dart';
Expand Down Expand Up @@ -70,6 +71,14 @@ class Preferences {
/// default zoomLevel
final ZoomLevel defaultZoomLevel = ZoomLevel.all;

/// default backup interval
final BackupInterval defaultBackupInterval = BackupInterval.monthly;

/// latest backup date
final DateTime defaultLatestBackupDate = DateTime.fromMillisecondsSinceEpoch(
0,
);

/// getter and setter for all preferences
/// set user name
set userName(String name) => prefs.setString('userName', name);
Expand Down Expand Up @@ -149,6 +158,26 @@ class Preferences {
'interpolStrength', strength.name,
);

/// get backup frequency
BackupInterval get backupInterval =>
prefs.getString('backupInterval')!.toBackupInterval()!;

/// set backup frequency
set backupInterval(BackupInterval interval) =>
prefs.setString(
'backupInterval', interval.name,
);

/// get latest backup date
DateTime get latestBackupDate =>
DateTime.parse(prefs.getString('latestBackupDate')!);

/// set latest backup date
set latestBackupDate(DateTime date) =>
prefs.setString(
'latestBackupDate', date.toString(),
);

/// get zoom level
ZoomLevel get zoomLevel =>
prefs.getInt('zoomLevel')!.toZoomLevel()!;
Expand Down Expand Up @@ -194,6 +223,12 @@ class Preferences {
if (override || !prefs.containsKey('zoomLevel')) {
zoomLevel = defaultZoomLevel;
}
if (override || !prefs.containsKey('backupInterval')) {
backupInterval = defaultBackupInterval;
}
if (override || !prefs.containsKey('latestBackupDate')) {
latestBackupDate = defaultLatestBackupDate;
}
}

/// reset all settings
Expand Down
4 changes: 4 additions & 0 deletions app/lib/core/theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,13 @@ class TraleTheme {
/// Get border radius
double get borderRadius => 16;

/// get transition durations
final TransitionDuration transitionDuration =
TransitionDuration(100, 200, 500);

/// get duration of snackbar
final Duration snackbarDuration = Duration(seconds: 5);

/// get background gradient
LinearGradient get bgGradient => LinearGradient(
begin: Alignment.topCenter,
Expand Down
32 changes: 31 additions & 1 deletion app/lib/core/traleNotifier.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:intl/date_time_patterns.dart';
import 'package:intl/intl.dart';
import 'package:trale/core/backupInterval.dart';
import 'package:trale/core/interpolation.dart';
import 'package:trale/core/language.dart';
import 'package:trale/core/measurementDatabase.dart';
Expand Down Expand Up @@ -59,7 +60,36 @@ class TraleNotifier with ChangeNotifier {
prefs.zoomLevel = newLevel;
notifyListeners();
}
}
}

/// get backup frequency
BackupInterval get backupInterval => prefs.backupInterval;
/// setter backup frequency
set backupInterval(BackupInterval newInterval) {
if (backupInterval != newInterval) {
prefs.backupInterval = newInterval;
notifyListeners();
}
}

/// get latest backup date
DateTime? get latestBackupDate {
if (prefs.defaultLatestBackupDate.sameDay(prefs.latestBackupDate)) {
return null;
}
return prefs.latestBackupDate;
}

/// set latest backup date
set latestBackupDate(DateTime? newDate) {
if (latestBackupDate != newDate) {
if (newDate == null) {
prefs.latestBackupDate = prefs.defaultLatestBackupDate;
} else {
prefs.latestBackupDate = newDate;
}
}
}

/// getter
Language get language => prefs.language;
Expand Down
36 changes: 36 additions & 0 deletions app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,42 @@
"@achievements": {
"description": "achievements"
},
"never": "never",
"@never": {
"description": "Frequency of backup interval"
},
"weekly": "weekly",
"@weekly": {
"description": "Frequency of backup interval"
},
"biweekly": "biweekly",
"@biweekly": {
"description": "Frequency of backup interval"
},
"monthly": "monthly",
"@monthly": {
"description": "Frequency of backup interval"
},
"quarterly": "quarterly",
"@quarterly": {
"description": "Frequency of backup interval"
},
"backupReminder": "Please back up regularly",
"@backupReminder": {
"description": "Please remember to back up your data regularly."
},
"backupReminderButton": "back up now",
"@backupReminderButton": {
"description": "Back up now"
},
"backupSuccess": "Backup successfully exported",
"@backupSuccess": {
"description": "Back successfully exported"
},
"backupInterval": "Backups",
"@backupInterval": {
"description": "Frequency of backup interval"
},
"stats": "Statistics",
"@stats": {
"description": "Header for statistics section"
Expand Down
27 changes: 27 additions & 0 deletions app/lib/pages/overview.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:provider/provider.dart';
import 'package:trale/core/backupInterval.dart';
import 'package:trale/core/icons.dart';
import 'package:trale/core/measurement.dart';
import 'package:trale/core/measurementDatabase.dart';
import 'package:trale/core/theme.dart';
import 'package:trale/core/traleNotifier.dart';
import 'package:trale/widget/animate_in_effect.dart';
import 'package:trale/widget/backupDialog.dart';
import 'package:trale/widget/emptyChart.dart';
import 'package:trale/widget/fade_in_effect.dart';
import 'package:trale/widget/linechart.dart';
Expand All @@ -30,6 +34,29 @@ class _OverviewScreen extends State<OverviewScreen> {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (loadedFirst) {
loadedFirst = false;
TraleNotifier traleNotifier = Provider.of<TraleNotifier>(
context, listen: false,
);
if (
traleNotifier.latestBackupDate != null &&
traleNotifier.backupInterval != BackupInterval.never &&
traleNotifier.latestBackupDate!.difference(
DateTime.now()
).inDays > traleNotifier.backupInterval.inDays
) {
final ScaffoldMessengerState sm = ScaffoldMessenger.of(context);
sm.showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.backupReminder),
behavior: SnackBarBehavior.fixed,
duration: TraleTheme.of(context)!.snackbarDuration,
action: SnackBarAction(
label: AppLocalizations.of(context)!.backupReminderButton,
onPressed: () => backupDialog(context),
),
),
);
}
setState(() {});
}
});
Expand Down
Loading

0 comments on commit f96de4f

Please sign in to comment.