Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Fix async UI lifecycles in lock screen and legacy flows

AmanRajSinghMouryae4efee32a05c
diff --git a/mobile/packages/legacy/lib/pages/other_contact_page.dart b/mobile/packages/legacy/lib/pages/other_contact_page.dart
index eb5d803744..9a296be2ff 100644
--- a/mobile/packages/legacy/lib/pages/other_contact_page.dart
+++ b/mobile/packages/legacy/lib/pages/other_contact_page.dart
@@ -147,7 +147,7 @@ class _OtherContactPageState extends State<OtherContactPage> {
try {
await EmergencyContactService.instance
.startRecovery(widget.contact);
- if (mounted) {
+ if (context.mounted) {
_fetchData().ignore();
await showAlertBottomSheet(
context,
@@ -160,10 +160,12 @@ class _OtherContactPageState extends State<OtherContactPage> {
);
}
} catch (e) {
- showGenericErrorDialog(
- context: context,
- error: e,
- ).ignore();
+ if (context.mounted) {
+ showGenericErrorDialog(
+ context: context,
+ error: e,
+ ).ignore();
+ }
}
}
},
@@ -283,7 +285,9 @@ class _OtherContactPageState extends State<OtherContactPage> {
_fetchData().ignore();
}
} catch (e) {
- showGenericErrorDialog(context: context, error: e).ignore();
+ if (mounted) {
+ showGenericErrorDialog(context: context, error: e).ignore();
+ }
}
}
}
@@ -317,7 +321,9 @@ class _OtherContactPageState extends State<OtherContactPage> {
Navigator.of(context).pop();
}
} catch (e) {
- showGenericErrorDialog(context: context, error: e).ignore();
+ if (mounted) {
+ showGenericErrorDialog(context: context, error: e).ignore();
+ }
}
}
}
diff --git a/mobile/packages/legacy/lib/services/emergency_service.dart b/mobile/packages/legacy/lib/services/emergency_service.dart
index 3a053dc6aa..c2aff22f9b 100644
--- a/mobile/packages/legacy/lib/services/emergency_service.dart
+++ b/mobile/packages/legacy/lib/services/emergency_service.dart
@@ -42,37 +42,45 @@ class EmergencyContactService {
}
Future<bool> addContact(
- BuildContext context,
+ BuildContext? context,
String email,
int recoveryNoticeInDays,
) async {
if (!isValidEmail(email)) {
- await showAlertBottomSheet(
- context,
- title: context.strings.letsTryThatAgain,
- message: context.strings.enterValidEmail,
- assetPath: "assets/warning-blue.png",
- );
+ if (context != null && context.mounted) {
+ await showAlertBottomSheet(
+ context,
+ title: context.strings.letsTryThatAgain,
+ message: context.strings.enterValidEmail,
+ assetPath: "assets/warning-blue.png",
+ );
+ }
return false;
}
if (email.trim() == _config.getEmail()) {
- await showAlertBottomSheet(
- context,
- title: context.strings.oops,
- message: context.strings.youCannotShareWithYourself,
- assetPath: "assets/warning-blue.png",
- );
+ if (context != null && context.mounted) {
+ await showAlertBottomSheet(
+ context,
+ title: context.strings.oops,
+ message: context.strings.youCannotShareWithYourself,
+ assetPath: "assets/warning-blue.png",
+ );
+ }
return false;
}
- final dialog = createProgressDialog(context, context.strings.pleaseWait);
- await dialog.show();
+ final dialog = context != null && context.mounted
+ ? createProgressDialog(context, context.strings.pleaseWait)
+ : null;
+ await dialog?.show();
try {
final String? publicKey = await _userService.getPublicKey(email);
if (publicKey == null) {
- await dialog.hide();
- await showInviteSheet(context, email: email);
+ await dialog?.hide();
+ if (context != null && context.mounted) {
+ await showInviteSheet(context, email: email);
+ }
return false;
}
@@ -89,10 +97,10 @@ class EmergencyContactService {
"recoveryNoticeInDays": recoveryNoticeInDays,
},
);
- await dialog.hide();
+ await dialog?.hide();
return true;
} catch (e) {
- await dialog.hide();
+ await dialog?.hide();
rethrow;
}
}
diff --git a/mobile/packages/lock_screen/lib/auth_util.dart b/mobile/packages/lock_screen/lib/auth_util.dart
index eab47c6854..bebf2d582e 100644
--- a/mobile/packages/lock_screen/lib/auth_util.dart
+++ b/mobile/packages/lock_screen/lib/auth_util.dart
@@ -276,9 +276,13 @@ Future<bool> requestAuthentication(
bool isOpeningApp = false,
bool isAuthenticatingForInAppChange = false,
}) async {
+ final l10n = context.strings;
final String? savedPin = await LockScreenSettings.instance.getPin();
final String? savedPassword = await LockScreenSettings.instance.getPassword();
if (savedPassword != null || savedPin != null) {
+ if (!context.mounted) {
+ return false;
+ }
return await LocalAuthenticationService.instance
.requestEnteAuthForLockScreen(
context,
@@ -292,7 +296,6 @@ Future<bool> requestAuthentication(
try {
await localAuth.stopAuthentication();
await _logLocalAuthState(localAuth);
- final l10n = context.strings;
final result = await localAuth.authenticate(
localizedReason: Platform.isMacOS
? (macOSReason ?? defaultReason)
diff --git a/mobile/packages/lock_screen/lib/local_authentication_service.dart b/mobile/packages/lock_screen/lib/local_authentication_service.dart
index 7ef2d5981f..6e91f3eb4c 100644
--- a/mobile/packages/lock_screen/lib/local_authentication_service.dart
+++ b/mobile/packages/lock_screen/lib/local_authentication_service.dart
@@ -31,6 +31,7 @@ class LocalAuthenticationService {
bool refocusWindows = true,
bool useDebugAuthCache = true,
}) async {
+ final unlockReason = context.strings.unlock;
if (kDebugMode && useDebugAuthCache) {
// if last auth time is less than 60 seconds, don't ask for auth again
if (lastAuthTime != 0 &&
@@ -40,6 +41,9 @@ class LocalAuthenticationService {
}
if (await isLocalAuthSupportedOnDevice() ||
LockScreenSettings.instance.getIsAppLockSet()) {
+ if (!context.mounted) {
+ return false;
+ }
final appLock = AppLock.of(context);
appLock?.setEnabled(false);
bool result = false;
@@ -50,7 +54,7 @@ class LocalAuthenticationService {
context,
infoMessage,
title: title,
- macOSReason: context.strings.unlock,
+ macOSReason: unlockReason,
isAuthenticatingForInAppChange: true,
);
} on WindowsLocalAuthenticationException catch (e, s) {
@@ -83,7 +87,9 @@ class LocalAuthenticationService {
return false;
}
if (!result) {
- showToast(context, infoMessage);
+ if (context.mounted) {
+ showToast(context, infoMessage);
+ }
return false;
} else {
if (useDebugAuthCache) {
@@ -120,6 +126,9 @@ class LocalAuthenticationService {
}
}
if (savedPin != null) {
+ if (!context.mounted) {
+ return false;
+ }
final result = await Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
@@ -146,17 +155,22 @@ class LocalAuthenticationService {
String errorDialogContent, [
String errorDialogTitle = "",
]) async {
+ final unlockReason = context.strings.unlock;
if (await isLocalAuthSupportedOnDevice()) {
+ if (!context.mounted) {
+ return false;
+ }
+ final appLock = AppLock.of(context)!;
bool didEnableLockScreen = false;
- AppLock.of(context)!.disable();
+ appLock.disable();
try {
final result = await requestAuthentication(
context,
infoMessage,
- macOSReason: context.strings.unlock,
+ macOSReason: unlockReason,
);
if (result) {
- AppLock.of(context)!.setEnabled(shouldEnableLockScreen);
+ appLock.setEnabled(shouldEnableLockScreen);
await LockScreenSettings.instance.setSystemLockScreen(
shouldEnableLockScreen,
);
@@ -175,14 +189,16 @@ class LocalAuthenticationService {
}
} finally {
if (!didEnableLockScreen) {
- AppLock.of(context)!.setEnabled(
+ appLock.setEnabled(
await LockScreenSettings.instance.shouldShowLockScreen(),
);
}
}
} else {
- // ignore: unawaited_futures
- showErrorDialog(context, errorDialogTitle, errorDialogContent);
+ if (context.mounted) {
+ // ignore: unawaited_futures
+ showErrorDialog(context, errorDialogTitle, errorDialogContent);
+ }
}
return false;
}
diff --git a/mobile/packages/lock_screen/lib/ui/lock_screen_confirm_password.dart b/mobile/packages/lock_screen/lib/ui/lock_screen_confirm_password.dart
index b339db8c38..158ea5b5df 100644
--- a/mobile/packages/lock_screen/lib/ui/lock_screen_confirm_password.dart
+++ b/mobile/packages/lock_screen/lib/ui/lock_screen_confirm_password.dart
@@ -37,8 +37,10 @@ class _LockScreenConfirmPasswordState extends State<LockScreenConfirmPassword> {
if (widget.password == _confirmPasswordController.text) {
await _lockscreenSetting.setPassword(_confirmPasswordController.text);
- Navigator.of(context).pop(true);
- Navigator.of(context).pop(true);
+ if (mounted) {
+ Navigator.of(context).pop(true);
+ Navigator.of(context).pop(true);
+ }
return;
}
await HapticFeedback.vibrate();
diff --git a/mobile/packages/lock_screen/lib/ui/lock_screen_confirm_pin.dart b/mobile/packages/lock_screen/lib/ui/lock_screen_confirm_pin.dart
index d0c3565864..387fcff172 100644
--- a/mobile/packages/lock_screen/lib/ui/lock_screen_confirm_pin.dart
+++ b/mobile/packages/lock_screen/lib/ui/lock_screen_confirm_pin.dart
@@ -41,8 +41,10 @@ class _LockScreenConfirmPinState extends State<LockScreenConfirmPin> {
if (widget.pin == _confirmPinController.text) {
await _lockscreenSetting.setPin(_confirmPinController.text);
- Navigator.of(context).pop(true);
- Navigator.of(context).pop(true);
+ if (mounted) {
+ Navigator.of(context).pop(true);
+ Navigator.of(context).pop(true);
+ }
return;
}
setState(() {
@@ -50,10 +52,12 @@ class _LockScreenConfirmPinState extends State<LockScreenConfirmPin> {
});
await HapticFeedback.vibrate();
await Future.delayed(const Duration(milliseconds: 75));
- _confirmPinController.clear();
- setState(() {
- isConfirmPinValid = false;
- });
+ if (mounted) {
+ _confirmPinController.clear();
+ setState(() {
+ isConfirmPinValid = false;
+ });
+ }
}
@override
diff --git a/mobile/packages/lock_screen/lib/ui/lock_screen_options.dart b/mobile/packages/lock_screen/lib/ui/lock_screen_options.dart
index 6463593a91..7b5c7ab1cc 100644
--- a/mobile/packages/lock_screen/lib/ui/lock_screen_options.dart
+++ b/mobile/packages/lock_screen/lib/ui/lock_screen_options.dart
@@ -55,6 +55,9 @@ class _LockScreenOptionsState extends State<LockScreenOptions> {
.getShouldHideAppContent();
final bool systemLockEnabled = _lockScreenSettings
.shouldShowSystemLockScreen();
+ if (!mounted) {
+ return;
+ }
setState(() {
isPasswordEnabled = passwordEnabled;
isPinEnabled = pinEnabled;
@@ -72,23 +75,27 @@ class _LockScreenOptionsState extends State<LockScreenOptions> {
final linuxStatus = await LocalAuthenticationService.instance
.getLinuxLocalAuthSetupStatus();
if (Platform.isLinux && linuxStatus?.setupRequired == true) {
- await showLinuxSystemAuthSetupDialog(context);
+ if (mounted) {
+ await showLinuxSystemAuthSetupDialog(context);
+ }
await _initializeSettings();
return;
}
- await showDialogWidget(
- context: context,
- title: context.strings.noSystemLockFound,
- body: context.strings.deviceLockEnablePreSteps,
- isDismissible: true,
- buttons: [
- ButtonWidget(
- buttonType: ButtonType.secondary,
- labelText: context.strings.ok,
- isInAlert: true,
- ),
- ],
- );
+ if (mounted) {
+ await showDialogWidget(
+ context: context,
+ title: context.strings.noSystemLockFound,
+ body: context.strings.deviceLockEnablePreSteps,
+ isDismissible: true,
+ buttons: [
+ ButtonWidget(
+ buttonType: ButtonType.secondary,
+ labelText: context.strings.ok,
+ isInAlert: true,
+ ),
+ ],
+ );
+ }
}
await _initializeSettings();
}
diff --git a/mobile/packages/lock_screen/lib/ui/lock_screen_password.dart b/mobile/packages/lock_screen/lib/ui/lock_screen_password.dart
index 5bbb303eac..fb034eede5 100644
--- a/mobile/packages/lock_screen/lib/ui/lock_screen_password.dart
+++ b/mobile/packages/lock_screen/lib/ui/lock_screen_password.dart
@@ -178,20 +178,22 @@ class _LockScreenPasswordState extends State<LockScreenPassword> {
if (matched) {
await _lockscreenSetting.setInvalidAttemptCount(0);
- widget.isAuthenticatingOnAppLaunch ||
- widget.isAuthenticatingForInAppChange
- ? Navigator.of(context).pop(true)
- : Navigator.of(context).pushReplacement(
- MaterialPageRoute(
- builder: (context) => const LockScreenOptions(),
- ),
- );
+ if (mounted) {
+ widget.isAuthenticatingOnAppLaunch ||
+ widget.isAuthenticatingForInAppChange
+ ? Navigator.of(context).pop(true)
+ : Navigator.of(context).pushReplacement(
+ MaterialPageRoute(
+ builder: (context) => const LockScreenOptions(),
+ ),
+ );
+ }
return true;
} else {
if (widget.isAuthenticatingOnAppLaunch) {
invalidAttemptsCount++;
await _lockscreenSetting.setInvalidAttemptCount(invalidAttemptsCount);
- if (invalidAttemptsCount > 4) {
+ if (invalidAttemptsCount > 4 && mounted) {
Navigator.of(context).pop(false);
}
}
@@ -212,7 +214,9 @@ class _LockScreenPasswordState extends State<LockScreenPassword> {
LockScreenConfirmPassword(password: _passwordController.text),
),
);
- _passwordController.clear();
+ if (mounted) {
+ _passwordController.clear();
+ }
}
}
}
diff --git a/mobile/packages/lock_screen/lib/ui/lock_screen_pin.dart b/mobile/packages/lock_screen/lib/ui/lock_screen_pin.dart
index d08c4d7435..1ca6c02d0f 100644
--- a/mobile/packages/lock_screen/lib/ui/lock_screen_pin.dart
+++ b/mobile/packages/lock_screen/lib/ui/lock_screen_pin.dart
@@ -76,30 +76,36 @@ class _LockScreenPinState extends State<LockScreenPin> {
if (matched) {
invalidAttemptsCount = 0;
await _lockscreenSetting.setInvalidAttemptCount(0);
- widget.isAuthenticatingOnAppLaunch ||
- widget.isAuthenticatingForInAppChange
- ? Navigator.of(context).pop(true)
- : Navigator.of(context).pushReplacement(
- MaterialPageRoute(
- builder: (context) => const LockScreenOptions(),
- ),
- );
+ if (mounted) {
+ widget.isAuthenticatingOnAppLaunch ||
+ widget.isAuthenticatingForInAppChange
+ ? Navigator.of(context).pop(true)
+ : Navigator.of(context).pushReplacement(
+ MaterialPageRoute(
+ builder: (context) => const LockScreenOptions(),
+ ),
+ );
+ }
return true;
} else {
- setState(() {
- isPinValid = true;
- });
+ if (mounted) {
+ setState(() {
+ isPinValid = true;
+ });
+ }
await HapticFeedback.vibrate();
await Future.delayed(const Duration(milliseconds: 75));
- _pinController.clear();
- setState(() {
- isPinValid = false;
- });
+ if (mounted) {
+ _pinController.clear();
+ setState(() {
+ isPinValid = false;
+ });
+ }
if (widget.isAuthenticatingOnAppLaunch) {
invalidAttemptsCount++;
await _lockscreenSetting.setInvalidAttemptCount(invalidAttemptsCount);
- if (invalidAttemptsCount > 4) {
+ if (invalidAttemptsCount > 4 && mounted) {
Navigator.of(context).pop(false);
}
}
@@ -118,7 +124,9 @@ class _LockScreenPinState extends State<LockScreenPin> {
LockScreenConfirmPin(pin: inputtedPin),
),
);
- _pinController.clear();
+ if (mounted) {
+ _pinController.clear();
+ }
}
}