Skip to content

armgit / ente

Public Imported from GitHub
0

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

Preserve async operations after UI disposal

Neeraj Gupta427847222630
diff --git a/mobile/apps/photos/lib/emergency/emergency_service.dart b/mobile/apps/photos/lib/emergency/emergency_service.dart
index f5dadb7857..d877f3903a 100644
--- a/mobile/apps/photos/lib/emergency/emergency_service.dart
+++ b/mobile/apps/photos/lib/emergency/emergency_service.dart
@@ -42,11 +42,12 @@ class EmergencyContactService {
EmergencyContactService._privateConstructor();
Future<bool> addContact(
- BuildContext context,
+ BuildContext? context,
String email, {
int recoveryNoticeInDays = 30,
}) async {
if (!isValidEmail(email)) {
+ if (context == null || !context.mounted) return false;
await showAlertBottomSheet(
context,
title: AppLocalizations.of(context).letsTryThatAgain,
@@ -55,6 +56,7 @@ class EmergencyContactService {
);
return false;
} else if (email.trim() == Configuration.instance.getEmail()) {
+ if (context == null || !context.mounted) return false;
await showAlertBottomSheet(
context,
title: AppLocalizations.of(context).oops,
@@ -65,7 +67,7 @@ class EmergencyContactService {
}
final String? publicKey = await _userService.getPublicKey(email);
if (publicKey == null) {
- if (!context.mounted) return false;
+ if (context == null || !context.mounted) return false;
await showInviteDialog(context, email);
return false;
}
diff --git a/mobile/apps/photos/lib/emergency/select_contact_page.dart b/mobile/apps/photos/lib/emergency/select_contact_page.dart
index e9097274c2..6e62277360 100644
--- a/mobile/apps/photos/lib/emergency/select_contact_page.dart
+++ b/mobile/apps/photos/lib/emergency/select_contact_page.dart
@@ -194,6 +194,7 @@ class _AddContactSheetState extends State<AddContactSheet> {
}
Future<void> _onAddContactTap() async {
+ final sheetContext = context;
final emailsToAdd = _emailsToAdd;
if (emailsToAdd.isEmpty) {
return;
@@ -210,12 +211,20 @@ class _AddContactSheetState extends State<AddContactSheet> {
var hasSuccess = false;
for (final email in emailsToAdd) {
try {
- if (!mounted) return;
- final success = await EmergencyContactService.instance.addContact(
- context,
- email,
- recoveryNoticeInDays: _selectedRecoveryDays,
- );
+ late final bool success;
+ if (sheetContext.mounted) {
+ success = await EmergencyContactService.instance.addContact(
+ sheetContext,
+ email,
+ recoveryNoticeInDays: _selectedRecoveryDays,
+ );
+ } else {
+ success = await EmergencyContactService.instance.addContact(
+ null,
+ email,
+ recoveryNoticeInDays: _selectedRecoveryDays,
+ );
+ }
if (success) {
hasSuccess = true;
} else {
diff --git a/mobile/apps/photos/lib/services/favorites_service.dart b/mobile/apps/photos/lib/services/favorites_service.dart
index e2b32be2b3..9a8fbf8b3e 100644
--- a/mobile/apps/photos/lib/services/favorites_service.dart
+++ b/mobile/apps/photos/lib/services/favorites_service.dart
@@ -181,9 +181,8 @@ class FavoritesService {
await _collectionsService.addOrCopyToCollection(collectionID, files);
} else {
final Collection? favCollection = await getFavoritesCollection();
- if (!context.mounted) return;
await _collectionActions.moveFilesFromCurrentCollection(
- context,
+ null,
favCollection!,
files,
);
@@ -217,9 +216,8 @@ class FavoritesService {
"Failed to resolve favorites entry for file ${file.uploadedFileID}",
);
}
- if (!context.mounted) return;
await _collectionActions.moveFilesFromCurrentCollection(
- context,
+ null,
favCollection,
[favFile],
);
diff --git a/mobile/apps/photos/lib/services/memories_cache_service.dart b/mobile/apps/photos/lib/services/memories_cache_service.dart
index 256d0d465c..20d0685563 100644
--- a/mobile/apps/photos/lib/services/memories_cache_service.dart
+++ b/mobile/apps/photos/lib/services/memories_cache_service.dart
@@ -1296,7 +1296,7 @@ class MemoriesCacheService {
);
return;
}
- if (context == null || !context.mounted) return;
+ if (context != null && !context.mounted) return;
await _routeToPage(
DetailPage(DetailPageConfiguration([file], 0, "memorywidget-fallback")),
context: context,
@@ -1304,7 +1304,7 @@ class MemoriesCacheService {
);
return;
}
- if (context == null || !context.mounted) return;
+ if (context != null && !context.mounted) return;
await _routeToPage(
AllMemoriesPage(
allMemories: _cachedMemories!,
@@ -1334,7 +1334,7 @@ class MemoriesCacheService {
_logger.warning("Could not find onThisDay memory");
return;
}
- if (context == null || !context.mounted) return;
+ if (context != null && !context.mounted) return;
await _routeToPage(
AllMemoriesPage(
allMemories: allMemories,
@@ -1386,7 +1386,7 @@ class MemoriesCacheService {
_logger.severe("Person with ID $personID not found");
return;
}
- if (context == null || !context.mounted) return;
+ if (context != null && !context.mounted) return;
await _routeToPage(
PeoplePage(person: person, searchResult: null),
context: context,
@@ -1394,7 +1394,7 @@ class MemoriesCacheService {
);
return;
}
- if (context == null || !context.mounted) return;
+ if (context != null && !context.mounted) return;
await _routeToPage(
FullScreenMemoryDataUpdater(
initialIndex: 0,
diff --git a/mobile/apps/photos/lib/ui/actions/collection/collection_sharing_actions.dart b/mobile/apps/photos/lib/ui/actions/collection/collection_sharing_actions.dart
index 81a38c56ec..7c4beaddcc 100644
--- a/mobile/apps/photos/lib/ui/actions/collection/collection_sharing_actions.dart
+++ b/mobile/apps/photos/lib/ui/actions/collection/collection_sharing_actions.dart
@@ -77,7 +77,7 @@ class CollectionActions {
onTap: () async {
// for quickLink collection, we need to trash the collection
if (collection.isQuickLinkCollection() && !collection.hasSharees) {
- await trashCollectionKeepingPhotos(collection, context);
+ await trashCollectionKeepingPhotos(collection);
} else {
await CollectionsService.instance.disableShareUrl(collection);
}
@@ -148,9 +148,7 @@ class CollectionActions {
if (e is SharingNotPermittedForFreeAccountsError) {
if (newCollection.isQuickLinkCollection() &&
!newCollection.hasSharees) {
- if (context.mounted) {
- await trashCollectionKeepingPhotos(newCollection, context);
- }
+ await trashCollectionKeepingPhotos(newCollection);
}
rethrow;
}
@@ -368,7 +366,7 @@ class CollectionActions {
keepPhotos: () async {
for (final collection in collections) {
try {
- await trashCollectionKeepingPhotos(collection, context);
+ await trashCollectionKeepingPhotos(collection);
} catch (e, s) {
logger.severe("Failed to keep photos & delete collection", e, s);
rethrow;
@@ -413,7 +411,7 @@ class CollectionActions {
message: AppLocalizations.of(bContext).deleteAlbumDialog,
keepPhotos: () async {
try {
- await trashCollectionKeepingPhotos(collection, bContext);
+ await trashCollectionKeepingPhotos(collection);
} catch (e, s) {
logger.severe("Failed to keep photos & delete collection", e, s);
rethrow;
@@ -521,16 +519,12 @@ class CollectionActions {
return error is Exception ? error : Exception(error.toString());
}
- Future<void> trashCollectionKeepingPhotos(
- Collection collection,
- BuildContext bContext,
- ) async {
+ Future<void> trashCollectionKeepingPhotos(Collection collection) async {
final List<EnteFile> files = await FilesDB.instance.getAllFilesCollection(
collection.id,
);
- if (!bContext.mounted) return;
await moveFilesFromCurrentCollection(
- bContext,
+ null,
collection,
files,
isHidden: collection.isHidden() && !collection.isDefaultHidden(),
@@ -594,7 +588,7 @@ class CollectionActions {
we will just remove (not move) the files from the given collection.
*/
Future<void> moveFilesFromCurrentCollection(
- BuildContext context,
+ BuildContext? context,
Collection collection,
Iterable<EnteFile> files, {
bool isHidden = false,
@@ -638,11 +632,12 @@ class CollectionActions {
}
if (!isCollectionOwner && split.ownedByOtherUsers.isNotEmpty) {
- if (!context.mounted) return;
- showShortToast(
- context,
- AppLocalizations.of(context).canOnlyRemoveFilesOwnedByYou,
- );
+ if (context != null && context.mounted) {
+ showShortToast(
+ context,
+ AppLocalizations.of(context).canOnlyRemoveFilesOwnedByYou,
+ );
+ }
return;
}
diff --git a/mobile/apps/photos/lib/ui/collections/album/smart_album_people.dart b/mobile/apps/photos/lib/ui/collections/album/smart_album_people.dart
index 4d5adde219..40cdbae05b 100644
--- a/mobile/apps/photos/lib/ui/collections/album/smart_album_people.dart
+++ b/mobile/apps/photos/lib/ui/collections/album/smart_album_people.dart
@@ -140,11 +140,10 @@ class _SmartAlbumPeopleState extends State<SmartAlbumPeople> {
.getCollectionByID(widget.collectionId);
if (files?.isNotEmpty ?? false) {
- if (!context.mounted) return;
await CollectionActions(
CollectionsService.instance,
).moveFilesFromCurrentCollection(
- context,
+ null,
collection!,
enteFiles[widget.collectionId] ?? [],
isHidden: collection.isHidden(),
diff --git a/mobile/apps/photos/lib/ui/payment/payment_web_page.dart b/mobile/apps/photos/lib/ui/payment/payment_web_page.dart
index 69bf5176ad..136160e163 100644
--- a/mobile/apps/photos/lib/ui/payment/payment_web_page.dart
+++ b/mobile/apps/photos/lib/ui/payment/payment_web_page.dart
@@ -195,7 +195,7 @@ class _PaymentWebPageState extends State<PaymentWebPage> {
}
Future<void> _handlePaymentFailure(String reason) async {
- await showDialog(
+ final shouldContactSupport = await showDialog<bool>(
useRootNavigator: false,
context: context,
barrierDismissible: false,
@@ -205,19 +205,16 @@ class _PaymentWebPageState extends State<PaymentWebPage> {
actions: <Widget>[
TextButton(
child: Text(AppLocalizations.of(context).contactSupport),
- onPressed: () async {
- Navigator.of(context).pop('dialog');
- await sendEmail(
- context,
- to: supportEmail,
- subject: "Billing issue",
- );
- },
+ onPressed: () => Navigator.of(context).pop(true),
),
],
),
);
if (!mounted) return;
+ if (shouldContactSupport == true) {
+ await sendEmail(context, to: supportEmail, subject: "Billing issue");
+ }
+ if (!mounted) return;
Navigator.of(context).pop(true);
}
diff --git a/mobile/apps/photos/lib/ui/tabs/shared/all_links_page.dart b/mobile/apps/photos/lib/ui/tabs/shared/all_links_page.dart
index 20c20cc3a8..1f5b66ffba 100644
--- a/mobile/apps/photos/lib/ui/tabs/shared/all_links_page.dart
+++ b/mobile/apps/photos/lib/ui/tabs/shared/all_links_page.dart
@@ -124,7 +124,7 @@ class _AllLinksPageState extends State<AllLinksPage> {
for (final link in List<Collection>.of(_selectedQuickLinks)) {
await CollectionActions(
CollectionsService.instance,
- ).trashCollectionKeepingPhotos(link, context);
+ ).trashCollectionKeepingPhotos(link);
if (!mounted) return;
setState(() {
widget.quickLinks.remove(link);
diff --git a/mobile/apps/photos/lib/utils/delete_file_util.dart b/mobile/apps/photos/lib/utils/delete_file_util.dart
index 614f11d3dc..f8834fa2c0 100644
--- a/mobile/apps/photos/lib/utils/delete_file_util.dart
+++ b/mobile/apps/photos/lib/utils/delete_file_util.dart
@@ -376,12 +376,15 @@ Future<bool> deleteLocalFiles(
final tooManyAssets = localAssetIDs.length > largeCountThreshold;
final bool shouldDeleteInBatches =
await isAndroidSDKVersionLowerThan(android11SDKINT) || tooManyAssets;
- if (shouldDeleteInBatches) {
+ if (!context.mounted) {
+ _logger.info(
+ "Skipping platform asset deletion after the initiating page was disposed",
+ );
+ } else if (shouldDeleteInBatches) {
if (tooManyAssets) {
_logger.info(
"Too many assets (${localAssetIDs.length}) to delete in one shot, deleting in batches",
);
- if (!context.mounted) return false;
await _recursivelyReduceBatchSizeAndRetryDeletion(
batchSize: largeCountThreshold,
context: context,
@@ -390,14 +393,12 @@ Future<bool> deleteLocalFiles(
);
} else {
_logger.info("Deleting in batches");
- if (!context.mounted) return false;
deletedIDs.addAll(
await deleteLocalFilesInBatches(context, localAssetIDs),
);
}
} else {
_logger.info("Deleting in one shot");
- if (!context.mounted) return false;
deletedIDs.addAll(
await _deleteLocalFilesInOneShot(context, localAssetIDs),
);
@@ -469,15 +470,17 @@ Future<bool> deleteLocalFilesAfterRemovingAlreadyDeletedIDs(
final bool shouldDeleteInBatches = await isAndroidSDKVersionLowerThan(
android11SDKINT,
);
- if (shouldDeleteInBatches) {
+ if (!context.mounted) {
+ _logger.info(
+ "Skipping platform asset deletion after the initiating page was disposed",
+ );
+ } else if (shouldDeleteInBatches) {
_logger.info("Deleting in batches");
- if (!context.mounted) return false;
deletedIDs.addAll(
await deleteLocalFilesInBatches(context, localAssetIDs),
);
} else {
_logger.info("Deleting in one shot");
- if (!context.mounted) return false;
deletedIDs.addAll(
await _deleteLocalFilesInOneShot(context, localAssetIDs),
);
@@ -557,15 +560,17 @@ Future<bool> retryFreeUpSpaceAfterRemovingAssetsNonExistingInDisk(
final bool shouldDeleteInBatches = await isAndroidSDKVersionLowerThan(
android11SDKINT,
);
- if (shouldDeleteInBatches) {
+ if (!context.mounted) {
+ _logger.info(
+ "Skipping platform asset deletion after the initiating page was disposed",
+ );
+ } else if (shouldDeleteInBatches) {
_logger.info("Deleting in batches");
- if (!context.mounted) return false;
deletedIDs.addAll(
await deleteLocalFilesInBatches(context, localAssetIDs),
);
} else {
_logger.info("Deleting in one shot");
- if (!context.mounted) return false;
deletedIDs.addAll(
await _deleteLocalFilesInOneShot(context, localAssetIDs),
);