Skip to content

Commit

Permalink
Add sign in error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkrieger committed Dec 14, 2021
1 parent 67853e4 commit 795dece
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
43 changes: 34 additions & 9 deletions lib/repositories/drive_backup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,22 @@ GoogleSignIn googleSignIn = GoogleSignIn(
: "658946540988-5ga0e513k05pbteb77kqfg6ak7rdd5ns.apps.googleusercontent.com",
scopes: [DriveApi.driveAppdataScope]);

Future<DriveApi> getDrive() async {
if (googleSignIn.currentUser == null) {
await googleSignIn.signInSilently(reAuthenticate: true);
Future<DriveApi?> getDrive() async {
try {
if (googleSignIn.currentUser == null) {
await googleSignIn.signInSilently(reAuthenticate: true);
}

final httpClient = (await googleSignIn.authenticatedClient());
if (httpClient == null) {
return null;
}
return DriveApi(httpClient);
} catch (e, tr) {
print(tr);
Sentry.captureException(e, stackTrace: tr);
return null;
}
final httpClient = (await googleSignIn.authenticatedClient())!;
return DriveApi(httpClient);
}

bool backingUp = false;
Expand Down Expand Up @@ -53,6 +63,9 @@ Future<void> ifShouldBackup() async {

Future<String> getUsage() async {
final driveApi = await getDrive();
if (driveApi == null) {
return "could not authenticate";
}
final about = await driveApi.about.get($fields: "storageQuota");
final storageQuota = about.storageQuota;
if (storageQuota == null ||
Expand All @@ -67,6 +80,9 @@ Future<String> getUsage() async {

Future<List<Tuple2<DateTime, String>>> getBackups() async {
final driveApi = await getDrive();
if (driveApi == null) {
return [];
}
final existing = await driveApi.files.list(
spaces: "appDataFolder",
pageSize: 1000,
Expand All @@ -82,6 +98,9 @@ Future<List<Tuple2<DateTime, String>>> getBackups() async {

Future<void> makeBackup() async {
final driveApi = await getDrive();
if (driveApi == null) {
return;
}
final docsDir = await getApplicationDocumentsDirectory();
final tmpDir = await getTemporaryDirectory();
final tmpZip = IO.File(
Expand All @@ -104,14 +123,17 @@ Future<void> makeBackup() async {
sp.setInt(lastBackupKey, DateTime.now().millisecondsSinceEpoch);
Sentry.addBreadcrumb(
Breadcrumb(message: "Done backing up", timestamp: DateTime.now()));
} catch (e) {
} catch (e, tr) {
print(e);
Sentry.captureException(e);
Sentry.captureException(e, stackTrace: tr);
}
}

Future<void> restoreById(String id, Function onDone) async {
final driveApi = await getDrive();
if (driveApi == null) {
return;
}
final docsDir = await getApplicationDocumentsDirectory();
await docsDir.list().forEach((element) async {
await element.delete(recursive: true);
Expand All @@ -133,14 +155,17 @@ Future<void> restoreById(String id, Function onDone) async {
Breadcrumb(message: "Restored backup", timestamp: DateTime.now()));
onDone();
});
} catch (e) {
} catch (e, tr) {
print(e);
Sentry.captureException(e);
Sentry.captureException(e, stackTrace: tr);
}
}

Future<void> deleteById(String id) async {
final driveApi = await getDrive();
if (driveApi == null) {
return;
}
Sentry.addBreadcrumb(
Breadcrumb(message: "Deleting backup", timestamp: DateTime.now()));
await driveApi.files.delete(id);
Expand Down
3 changes: 3 additions & 0 deletions lib/state/notes_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,17 @@ class NotesModel extends ChangeNotifier {
if (currentlyPlayingOrRecording != null) {
_playerModel.stopPlaying();
currentlyPlayingOrRecording = null;
notifyListeners();
return;
}
if (currentlyExpanded != null && currentlyExpanded!.id != note.id) {
currentlyExpanded = null;
}
currentlyPlayingOrRecording = note;
notifyListeners();
await _playerModel.playNote(note, () {
currentlyPlayingOrRecording = null;
notifyListeners();
});
}

Expand Down
13 changes: 11 additions & 2 deletions lib/views/drive_settings_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:timeago_flutter/timeago_flutter.dart';
import 'package:tuple/tuple.dart';
Expand Down Expand Up @@ -74,13 +75,21 @@ class _DriveSettingsViewState extends State<DriveSettingsView> {
}

Future<void> handleDriveToggle(bool enable) async {
sharedPreferences?.setBool(driveEnabledKey, enable);
if (enable) {
await googleSignIn.signIn();
try {
await googleSignIn.signIn();
} catch (e, tr) {
ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(content: Text("Couldn't sign in")));
print(tr);
Sentry.captureException(e, stackTrace: tr);
return;
}
} else {
await googleSignIn.signOut();
account = null;
}
sharedPreferences?.setBool(driveEnabledKey, enable);
await checkStatus();
setState(() {});
}
Expand Down

0 comments on commit 795dece

Please sign in to comment.