Skip to content

armgit / ente

Public Imported from GitHub
0

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

Fix async BuildContext use in shared mobile packages

AmanRajSinghMourya92e28691145a
diff --git a/mobile/packages/ente_components/lib/components/text_input_component.dart b/mobile/packages/ente_components/lib/components/text_input_component.dart
index 8f6c40408a..a3a059712b 100644
--- a/mobile/packages/ente_components/lib/components/text_input_component.dart
+++ b/mobile/packages/ente_components/lib/components/text_input_component.dart
@@ -607,6 +607,7 @@ class _TextInputComponentState extends State<TextInputComponent> {
if (widget.onSubmit == null || widget.isDisabled || _isSubmitting) {
return;
}
+ final popNavAfterSubmission = widget.popNavAfterSubmission;
setState(() {
_isSubmitting = true;
@@ -619,18 +620,19 @@ class _TextInputComponentState extends State<TextInputComponent> {
try {
await widget.onSubmit!.call(_controller.text);
} catch (error) {
- if (error.toString().contains('Incorrect password')) {
- _surfaceWrongPasswordState();
- }
if (mounted) {
+ if (error.toString().contains('Incorrect password')) {
+ _surfaceWrongPasswordState();
+ }
setState(() => _isSubmitting = false);
+ if (popNavAfterSubmission) {
+ _popNavigatorStack(
+ context,
+ e: error is Exception ? error : Exception(error.toString()),
+ );
+ }
}
- if (widget.popNavAfterSubmission) {
- _popNavigatorStack(
- context,
- e: error is Exception ? error : Exception(error.toString()),
- );
- } else {
+ if (!popNavAfterSubmission) {
rethrow;
}
return;
diff --git a/mobile/packages/log_viewer/lib/log_viewer.dart b/mobile/packages/log_viewer/lib/log_viewer.dart
index e813dba1de..651939e384 100644
--- a/mobile/packages/log_viewer/lib/log_viewer.dart
+++ b/mobile/packages/log_viewer/lib/log_viewer.dart
@@ -66,6 +66,9 @@ class LogViewer {
if (!_initialized) {
await initialize();
}
+ if (!context.mounted) {
+ return;
+ }
await Navigator.push(
context,
diff --git a/mobile/packages/qr/lib/qr_code_dialog.dart b/mobile/packages/qr/lib/qr_code_dialog.dart
index 23abbe6e6f..60d57ff4b2 100644
--- a/mobile/packages/qr/lib/qr_code_dialog.dart
+++ b/mobile/packages/qr/lib/qr_code_dialog.dart
@@ -121,6 +121,9 @@ class _QrCodeDialogState extends State<QrCodeDialog> {
final directory = await getTemporaryDirectory();
final file = File('${directory.path}/${widget.shareFileName}');
await file.writeAsBytes(pngBytes);
+ if (!mounted) {
+ return;
+ }
final box = context.findRenderObject() as RenderBox?;
final shareOrigin = box != null
diff --git a/mobile/packages/sharing/lib/verify_identity_dialog.dart b/mobile/packages/sharing/lib/verify_identity_dialog.dart
index a28a0bb56d..920425c03e 100644
--- a/mobile/packages/sharing/lib/verify_identity_dialog.dart
+++ b/mobile/packages/sharing/lib/verify_identity_dialog.dart
@@ -144,15 +144,14 @@ class _VerifyIdentitySheetState extends State<VerifyIdentitySheet> {
if (verificationID.isEmpty) {
return;
}
+ final shareMessage = widget.self
+ ? context.strings.shareMyVerificationID(verificationID)
+ : context.strings.shareTextConfirmOthersVerificationID(
+ verificationID,
+ );
await Clipboard.setData(ClipboardData(text: verificationID));
// ignore: unawaited_futures
- shareText(
- widget.self
- ? context.strings.shareMyVerificationID(verificationID)
- : context.strings.shareTextConfirmOthersVerificationID(
- verificationID,
- ),
- );
+ shareText(shareMessage);
},
child: Container(
decoration: BoxDecoration(
diff --git a/mobile/packages/ui/lib/components/buttons/button_widget.dart b/mobile/packages/ui/lib/components/buttons/button_widget.dart
index 301d2c8648..55f251906f 100644
--- a/mobile/packages/ui/lib/components/buttons/button_widget.dart
+++ b/mobile/packages/ui/lib/components/buttons/button_widget.dart
@@ -419,9 +419,11 @@ class _ButtonChildWidgetState extends State<ButtonChildWidget> {
if (widget.onTap != null) {
_debouncer.run(
() => Future(() {
- setState(() {
- executionState = ExecutionState.inProgress;
- });
+ if (mounted) {
+ setState(() {
+ executionState = ExecutionState.inProgress;
+ });
+ }
}),
);
await widget.onTap!.call().then(
@@ -434,6 +436,10 @@ class _ButtonChildWidgetState extends State<ButtonChildWidget> {
_debouncer.cancelDebounce();
},
);
+ if (!mounted) {
+ _debouncer.cancelDebounce();
+ return;
+ }
widget.shouldShowSuccessConfirmation && _debouncer.isActive()
? executionState = ExecutionState.successful
: null;
@@ -449,6 +455,9 @@ class _ButtonChildWidgetState extends State<ButtonChildWidget> {
// condition so that the calback in the debouncer finishes execution before.
await Future.delayed(const Duration(milliseconds: 5));
}
+ if (!mounted) {
+ return;
+ }
if (executionState == ExecutionState.inProgress ||
executionState == ExecutionState.error) {
if (executionState == ExecutionState.inProgress) {
@@ -462,6 +471,9 @@ class _ButtonChildWidgetState extends State<ButtonChildWidget> {
: 0,
),
() {
+ if (!mounted) {
+ return;
+ }
widget.isInAlert
? _popWithButtonAction(
context,
@@ -482,14 +494,16 @@ class _ButtonChildWidgetState extends State<ButtonChildWidget> {
setState(() {
executionState = ExecutionState.idle;
widget.isInAlert
- ? Future.delayed(
- const Duration(seconds: 0),
- () => _popWithButtonAction(
+ ? Future.delayed(const Duration(seconds: 0), () {
+ if (!mounted) {
+ return;
+ }
+ _popWithButtonAction(
context,
buttonAction: ButtonAction.error,
exception: _exception,
- ),
- )
+ );
+ })
: null;
});
}
@@ -497,8 +511,12 @@ class _ButtonChildWidgetState extends State<ButtonChildWidget> {
if (widget.isInAlert) {
Future.delayed(
Duration(seconds: widget.shouldShowSuccessConfirmation ? 1 : 0),
- () =>
- _popWithButtonAction(context, buttonAction: widget.buttonAction),
+ () {
+ if (!mounted) {
+ return;
+ }
+ _popWithButtonAction(context, buttonAction: widget.buttonAction);
+ },
);
}
}
diff --git a/mobile/packages/ui/lib/components/text_input_widget.dart b/mobile/packages/ui/lib/components/text_input_widget.dart
index 5076bef725..94dd7e1f07 100644
--- a/mobile/packages/ui/lib/components/text_input_widget.dart
+++ b/mobile/packages/ui/lib/components/text_input_widget.dart
@@ -229,9 +229,11 @@ class _TextInputWidgetState extends State<TextInputWidget> {
void _onSubmit() async {
_debouncer.run(
() => Future(() {
- setState(() {
- executionState = ExecutionState.inProgress;
- });
+ if (mounted) {
+ setState(() {
+ executionState = ExecutionState.inProgress;
+ });
+ }
}),
);
if (widget.shouldUnfocusOnCancelOrSubmit) {
@@ -240,6 +242,13 @@ class _TextInputWidgetState extends State<TextInputWidget> {
try {
await widget.onSubmit!.call(_textController.text);
} catch (e) {
+ if (!mounted) {
+ _debouncer.cancelDebounce();
+ if (!widget.popNavAfterSubmission) {
+ rethrow;
+ }
+ return;
+ }
executionState = ExecutionState.error;
_debouncer.cancelDebounce();
_exception = e as Exception;
@@ -251,6 +260,10 @@ class _TextInputWidgetState extends State<TextInputWidget> {
rethrow;
}
}
+ if (!mounted) {
+ _debouncer.cancelDebounce();
+ return;
+ }
widget.alwaysShowSuccessState && _debouncer.isActive()
? executionState = ExecutionState.successful
: null;
@@ -265,6 +278,9 @@ class _TextInputWidgetState extends State<TextInputWidget> {
// idle state. This Future is for delaying the execution of the if
// condition so that the calback in the debouncer finishes execution before.
await Future.delayed(const Duration(milliseconds: 5));
+ if (!mounted) {
+ return;
+ }
if (executionState == ExecutionState.inProgress ||
executionState == ExecutionState.error) {
if (executionState == ExecutionState.inProgress) {
@@ -284,6 +300,9 @@ class _TextInputWidgetState extends State<TextInputWidget> {
: 0,
),
() {
+ if (!mounted) {
+ return;
+ }
widget.popNavAfterSubmission
? _popNavigatorStack(context)
: null;
@@ -302,10 +321,12 @@ class _TextInputWidgetState extends State<TextInputWidget> {
setState(() {
executionState = ExecutionState.idle;
widget.popNavAfterSubmission
- ? Future.delayed(
- const Duration(seconds: 0),
- () => _popNavigatorStack(context, e: _exception),
- )
+ ? Future.delayed(const Duration(seconds: 0), () {
+ if (!mounted) {
+ return;
+ }
+ _popNavigatorStack(context, e: _exception);
+ })
: null;
});
}
@@ -313,7 +334,12 @@ class _TextInputWidgetState extends State<TextInputWidget> {
if (widget.popNavAfterSubmission) {
Future.delayed(
Duration(seconds: widget.alwaysShowSuccessState ? 1 : 0),
- () => _popNavigatorStack(context),
+ () {
+ if (!mounted) {
+ return;
+ }
+ _popNavigatorStack(context);
+ },
);
}
}
diff --git a/mobile/packages/ui/lib/pages/developer_settings_page.dart b/mobile/packages/ui/lib/pages/developer_settings_page.dart
index 16273ee207..76bb11b466 100644
--- a/mobile/packages/ui/lib/pages/developer_settings_page.dart
+++ b/mobile/packages/ui/lib/pages/developer_settings_page.dart
@@ -64,18 +64,24 @@ class _DeveloperSettingsPageState extends State<DeveloperSettingsPage> {
if ((uri.scheme == "http" || uri.scheme == "https")) {
await _ping(url);
await widget.setEndpoint(url);
- showToast(context, context.strings.endpointUpdatedMessage);
- Navigator.of(context).pop();
} else {
throw const FormatException();
}
} catch (e) {
+ if (!context.mounted) {
+ return;
+ }
// ignore: unawaited_futures
showErrorDialog(
context,
context.strings.invalidEndpoint,
context.strings.invalidEndpointMessage,
);
+ return;
+ }
+ if (context.mounted) {
+ showToast(context, context.strings.endpointUpdatedMessage);
+ Navigator.of(context).pop();
}
},
text: context.strings.save,
diff --git a/mobile/packages/ui/lib/utils/toast_util.dart b/mobile/packages/ui/lib/utils/toast_util.dart
index fd5a90f667..fadd4c460c 100644
--- a/mobile/packages/ui/lib/utils/toast_util.dart
+++ b/mobile/packages/ui/lib/utils/toast_util.dart
@@ -9,6 +9,7 @@ void showToast(
toastLength = Toast.LENGTH_LONG,
iOSDismissOnTap = true,
}) async {
+ final colorScheme = getEnteColorScheme(context);
try {
await Fluttertoast.cancel();
await Fluttertoast.showToast(
@@ -16,23 +17,23 @@ void showToast(
toastLength: toastLength,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
- backgroundColor: getEnteColorScheme(context).toastBackgroundColor,
- textColor: getEnteColorScheme(context).toastTextColor,
+ backgroundColor: colorScheme.toastBackgroundColor,
+ textColor: colorScheme.toastTextColor,
fontSize: 16.0,
);
} on MissingPluginException catch (_) {
+ if (!context.mounted) {
+ return;
+ }
final toast = Container(
padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 12.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(25.0),
- color: getEnteColorScheme(context).toastBackgroundColor,
+ color: colorScheme.toastBackgroundColor,
),
child: Text(
message,
- style: TextStyle(
- color: getEnteColorScheme(context).toastTextColor,
- fontSize: 16.0,
- ),
+ style: TextStyle(color: colorScheme.toastTextColor, fontSize: 16.0),
),
);
diff --git a/mobile/packages/utils/lib/email_util.dart b/mobile/packages/utils/lib/email_util.dart
index 367c2e55fa..ae325a24d1 100644
--- a/mobile/packages/utils/lib/email_util.dart
+++ b/mobile/packages/utils/lib/email_util.dart
@@ -87,7 +87,7 @@ Future<void> sendLogs(
buttonAction: ButtonAction.third,
onTap: () async {
final zipFilePath = await getZippedLogsFile();
- await exportLogs(context, zipFilePath);
+ await exportLogs(null, zipFilePath);
},
),
ButtonWidget(
@@ -118,6 +118,9 @@ Future<void> _sendLogs(
await FlutterEmailSender.send(email);
} catch (e, s) {
_logger.severe('email sender failed', e, s);
+ if (!context.mounted) {
+ return;
+ }
Navigator.of(context).pop();
await shareLogs(context, toEmail, zipFilePath);
}
@@ -158,7 +161,7 @@ Future<void> shareLogs(
],
);
if (result?.action != null && result!.action == ButtonAction.second) {
- await exportLogs(context, zipFilePath);
+ await exportLogs(null, zipFilePath);
}
}
@@ -184,7 +187,7 @@ Future<String> getZippedLogsFile({String logsSubPath = "logs"}) async {
}
Future<void> exportLogs(
- BuildContext context,
+ BuildContext? context,
String zipFilePath, [
bool isSharing = false,
]) async {
@@ -251,11 +254,16 @@ Future<void> sendEmail(
throw Exception('Could not launch ${params.toString()}');
}
} else {
+ if (!context.mounted) {
+ return;
+ }
_showNoMailAppsDialog(context, to);
}
} catch (e) {
_logger.severe("Failed to send email to $to", e);
- _showNoMailAppsDialog(context, to);
+ if (context.mounted) {
+ _showNoMailAppsDialog(context, to);
+ }
}
}
@@ -292,8 +300,12 @@ void _showNoMailAppsDialog(BuildContext context, String toEmail) {
text: 'Copy Email Address',
onTap: () async {
await Clipboard.setData(ClipboardData(text: toEmail));
- Navigator.of(bottomSheetContext).pop();
- showShortToast(context, 'Email address copied');
+ if (bottomSheetContext.mounted) {
+ Navigator.of(bottomSheetContext).pop();
+ }
+ if (context.mounted) {
+ showShortToast(context, 'Email address copied');
+ }
},
),
],