Skip to content

Commit

Permalink
chores: minor fixes and changes
Browse files Browse the repository at this point in the history
  • Loading branch information
SirusCodes committed Apr 14, 2024
1 parent 6fa98d1 commit 3b81c38
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 24 deletions.
4 changes: 2 additions & 2 deletions lib/database/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class Config extends AsyncNotifier<ConfigModel> {
static const String _isAutoBackupEnabledKey = "isAutoBackupEnabled";
static const String _hasCompletedOnboardingKey = "hasCompletedOnboarding";

set lastBackup(DateTime? dateTime) {
_sharedPrefs.setString(_lastBackupKey, dateTime!.toIso8601String());
set lastBackup(DateTime dateTime) {
_sharedPrefs.setString(_lastBackupKey, dateTime.toIso8601String());
state = state.whenData((value) => value.copyWith(lastBackup: dateTime));
}

Expand Down
6 changes: 3 additions & 3 deletions lib/providers/token_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ class TokenManager {
final expireDate = DateTime.now().add(
Duration(seconds: json['expires_in']),
);
final scope = json['scope'];
final scope = json['scope'].toString();

if (scope != DriveApi.driveAppdataScope) {
throw Exception('Invalid scope');
if (!scope.contains(DriveApi.driveAppdataScope)) {
throw Exception('Invalid scope: $scope');
}

await updateAccessToken(accessToken: accessToken, expireDate: expireDate);
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/image_screen/image_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class _ImageScreenState extends State<ImageScreen> {
return;
}

if (!mounted) return;
if (!context.mounted) return;

_showShareTypeDialog(context, images);
},
Expand Down
5 changes: 1 addition & 4 deletions lib/screens/poems_screen/poems_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ class _ChangelogDialog extends StatelessWidget {
style: Theme.of(context).textTheme.headlineMedium,
),
const Divider(),
Markdown(
data: value,
shrinkWrap: true,
),
Expanded(child: Markdown(data: value)),
],
),
error: (Object error, StackTrace stackTrace) => Column(
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/initial_data_setup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import '../database/database.dart';
import '../init_get_it.dart';

class InitialDataSetup {
static Future<void> addDefaultData(String name, WidgetRef ref) async {
static Future<void> addDefaultData(String name, WidgetRef ref) async {
ref.read(configProvider.notifier).name = name;
await addDetailsInDB(name);
}
Expand Down
36 changes: 23 additions & 13 deletions lib/utils/workmanager_helper.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
// ignore_for_file: depend_on_referenced_packages

import 'dart:developer';

import 'package:flutter/foundation.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:heartry/providers/backup_restore_manager_provider.dart';
import 'package:intl/intl.dart';
// ignore: depend_on_referenced_packages
import 'package:riverpod/riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:workmanager/workmanager.dart';

import '../init_get_it.dart';
import '../providers/backup_restore_manager_provider.dart';

const AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails(
'your_channel_id',
'Random channel',
importance: Importance.max,
priority: Priority.high,
ticker: 'ticker',
'heartry_backup',
'Backup Notifications',
importance: Importance.defaultImportance,
priority: Priority.defaultPriority,
icon: "@mipmap/ic_launcher",
);

Expand All @@ -30,12 +29,19 @@ void callbackDispatcher() {
return true;
}

sharedprefs.setString("workmanger-running", "yes");
final container = ProviderContainer();
final backupManager = container.read(backupRestoreManagerProvider);
try {
await backupManager.backup();
sharedprefs.setString("backup", "pass");
final date = await backupManager.backup();
sharedprefs.setString("lastBackup", date.toIso8601String());

final dateFomatter = DateFormat.yMMMEd().add_jm();
FlutterLocalNotificationsPlugin().show(
0,
"Your data is safe! 🎉",
"Backup successful at ${dateFomatter.format(DateTime.now())}",
const NotificationDetails(android: androidNotificationDetails),
);
} catch (e, st) {
FlutterLocalNotificationsPlugin().show(
0,
Expand All @@ -45,7 +51,6 @@ void callbackDispatcher() {
);

log("Backup failed: $e", error: e, stackTrace: st);
sharedprefs.setString("backup", "$e ${'-' * 25} $st");
return false;
}
return true;
Expand All @@ -57,10 +62,15 @@ Future<void> initWorkmanager() async {
}

void registerBackupWorkmanager() {
final now = DateTime.now();
final day = now.hour > 2 ? now.day + 1 : now.day;
final startBackupAt = DateTime(now.year, now.month, day, 2, 0, 0);

Workmanager().registerPeriodicTask(
"backup-task",
"backup",
frequency: const Duration(minutes: 2),
frequency: const Duration(minutes: 1),
// initialDelay: startBackupAt.difference(now),
backoffPolicy: BackoffPolicy.linear,
backoffPolicyDelay: const Duration(seconds: 10),
constraints: Constraints(
Expand Down

0 comments on commit 3b81c38

Please sign in to comment.