From 0a5657738e98544135abebf8d531f5906cecf349 Mon Sep 17 00:00:00 2001 From: "vladimir.kuznetsov" Date: Sat, 9 Sep 2023 15:00:34 +0500 Subject: [PATCH] added a wait for the file dialog to close when sharing ios files --- client/platforms/ios/MobileUtils.h | 8 +-- client/platforms/ios/MobileUtils.mm | 58 ++++++++++++++-------- client/ui/controllers/systemController.cpp | 7 ++- 3 files changed, 47 insertions(+), 26 deletions(-) diff --git a/client/platforms/ios/MobileUtils.h b/client/platforms/ios/MobileUtils.h index fe7549c5..04a12c51 100644 --- a/client/platforms/ios/MobileUtils.h +++ b/client/platforms/ios/MobileUtils.h @@ -7,12 +7,12 @@ class MobileUtils : public QObject { Q_OBJECT - -//public: -// MobileUtils() = delete; + +public: + explicit MobileUtils(QObject *parent = nullptr); public slots: - static void shareText(const QStringList &filesToSend); + bool shareText(const QStringList &filesToSend); QString openFile(); signals: diff --git a/client/platforms/ios/MobileUtils.mm b/client/platforms/ios/MobileUtils.mm index 7f5fc1e6..fbf26ffd 100644 --- a/client/platforms/ios/MobileUtils.mm +++ b/client/platforms/ios/MobileUtils.mm @@ -3,7 +3,6 @@ #include #include -#include #include static UIViewController* getViewController() { @@ -16,7 +15,11 @@ static UIViewController* getViewController() { return nil; } -void MobileUtils::shareText(const QStringList& filesToSend) { +MobileUtils::MobileUtils(QObject *parent) : QObject(parent) { + +} + +bool MobileUtils::shareText(const QStringList& filesToSend) { NSMutableArray *sharingItems = [NSMutableArray new]; for (int i = 0; i < filesToSend.size(); i++) { @@ -28,35 +31,48 @@ void MobileUtils::shareText(const QStringList& filesToSend) { if (!qtController) return; UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil]; + + __block bool isAccepted = false; + + [activityController setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) { + isAccepted = completed; + emit finished(); + }]; [qtController presentViewController:activityController animated:YES completion:nil]; UIPopoverPresentationController *popController = activityController.popoverPresentationController; if (popController) { popController.sourceView = qtController.view; } + + QEventLoop wait; + QObject::connect(this, &MobileUtils::finished, &wait, &QEventLoop::quit); + wait.exec(); + + return isAccepted; } -typedef void (^FileSelectionCallback)(NSString *fileContent); +typedef void (^DocumentPickerClosedCallback)(NSString *path); -@interface MyFilePickerDelegate : NSObject +@interface DocumentPickerDelegate : NSObject -@property (nonatomic, copy) FileSelectionCallback fileSelectionCallback; +@property (nonatomic, copy) DocumentPickerClosedCallback documentPickerClosedCallback; @end -@implementation MyFilePickerDelegate +@implementation DocumentPickerDelegate - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray *)urls { for (NSURL *url in urls) { - if (self.fileSelectionCallback) { - self.fileSelectionCallback([url path]); + if (self.documentPickerClosedCallback) { + self.documentPickerClosedCallback([url path]); } } } - (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller { - if (self.fileSelectionCallback) { - self.fileSelectionCallback(nil); + if (self.documentPickerClosedCallback) { + self.documentPickerClosedCallback(nil); } } @@ -65,28 +81,28 @@ typedef void (^FileSelectionCallback)(NSString *fileContent); QString MobileUtils::openFile() { UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.item"] inMode:UIDocumentPickerModeOpen]; - MyFilePickerDelegate *filePickerDelegate = [[MyFilePickerDelegate alloc] init]; - documentPicker.delegate = filePickerDelegate; + DocumentPickerDelegate *documentPickerDelegate = [[DocumentPickerDelegate alloc] init]; + documentPicker.delegate = documentPickerDelegate; UIViewController *qtController = getViewController(); if (!qtController) return; [qtController presentViewController:documentPicker animated:YES completion:nil]; - __block QString path1; + __block QString filePath; - filePickerDelegate.fileSelectionCallback = ^(NSString *filePath) { - if (filePath) { - path1 = QString::fromUtf8(filePath.UTF8String); + documentPickerDelegate.documentPickerClosedCallback = ^(NSString *path) { + if (path) { + filePath = QString::fromUtf8(path.UTF8String); } else { - path1 = QString(""); + filePath = QString(); } emit finished(); }; - QEventLoop wait1; - QObject::connect(this, &MobileUtils::finished, &wait1, &QEventLoop::quit); - wait1.exec(); + QEventLoop wait; + QObject::connect(this, &MobileUtils::finished, &wait, &QEventLoop::quit); + wait.exec(); - return path1; + return filePath; } diff --git a/client/ui/controllers/systemController.cpp b/client/ui/controllers/systemController.cpp index 8e1fb6c8..7de071cc 100644 --- a/client/ui/controllers/systemController.cpp +++ b/client/ui/controllers/systemController.cpp @@ -46,7 +46,9 @@ void SystemController::saveFile(QString fileName, const QString &data) #ifdef Q_OS_IOS QStringList filesToSend; filesToSend.append(fileUrl.toString()); - MobileUtils::shareText(filesToSend); + MobileUtils mobileUtils; + // todo check if save successful + mobileUtils.shareText(filesToSend); return; #else QFileInfo fi(fileName); @@ -62,6 +64,9 @@ QString SystemController::getFileName(const QString &acceptLabel, const QString MobileUtils mobileUtils; fileName = mobileUtils.openFile(); + if (fileName.isEmpty()) { + return fileName; + } CFURLRef url = CFURLCreateWithFileSystemPath( kCFAllocatorDefault,