Просмотр исходного кода

iOS 13: Fix long-press copy menu in network pages

Tanner Bennett лет назад: 6
Родитель
Сommit
7b1b6f9e24

+ 1 - 0
Classes/Core/FLEXTableViewController.m

@@ -9,6 +9,7 @@
 #import "FLEXTableViewController.h"
 #import "FLEXScopeCarousel.h"
 #import "FLEXTableView.h"
+#import "FLEXUtility.h"
 #import <objc/runtime.h>
 
 @interface Block : NSObject

+ 1 - 1
Classes/GlobalStateExplorers/FileBrowser/FLEXFileBrowserTableViewController.m

@@ -310,7 +310,7 @@
 - (UIContextMenuConfiguration *)tableView:(UITableView *)tableView contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point __IOS_AVAILABLE(13.0)
 {
     __weak typeof(self) weakSelf = self;
-    return [UIContextMenuConfiguration configurationWithIdentifier:NSUUID.UUID.UUIDString
+    return [UIContextMenuConfiguration configurationWithIdentifier:nil
                                                    previewProvider:nil
                                                     actionProvider:^UIMenu * _Nullable(NSArray<UIMenuElement *> * _Nonnull suggestedActions) {
         UITableViewCell * const cell = [tableView cellForRowAtIndexPath:indexPath];

+ 34 - 11
Classes/Network/FLEXNetworkHistoryTableViewController.m

@@ -7,6 +7,7 @@
 //
 
 #import "FLEXColor.h"
+#import "FLEXUtility.h"
 #import "FLEXNetworkHistoryTableViewController.h"
 #import "FLEXNetworkTransaction.h"
 #import "FLEXNetworkTransactionTableViewCell.h"
@@ -252,11 +253,6 @@
 
 #pragma mark - Table view data source
 
-- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
-{
-    return 1;
-}
-
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
     return self.searchController.isActive ? self.filteredNetworkTransactions.count : self.networkTransactions.count;
@@ -278,7 +274,7 @@
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
     FLEXNetworkTransactionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kFLEXNetworkTransactionCellIdentifier forIndexPath:indexPath];
-    cell.transaction = [self transactionAtIndexPath:indexPath inTableView:tableView];
+    cell.transaction = [self transactionAtIndexPath:indexPath];
 
     // Since we insert from the top, assign background colors bottom up to keep them consistent for each transaction.
     NSInteger totalRows = [tableView numberOfRowsInSection:indexPath.section];
@@ -294,7 +290,7 @@
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
     FLEXNetworkTransactionDetailTableViewController *detailViewController = [FLEXNetworkTransactionDetailTableViewController new];
-    detailViewController.transaction = [self transactionAtIndexPath:indexPath inTableView:tableView];
+    detailViewController.transaction = [self transactionAtIndexPath:indexPath];
     [self.navigationController pushViewController:detailViewController animated:YES];
 }
 
@@ -313,13 +309,40 @@
 - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
 {
     if (action == @selector(copy:)) {
-        FLEXNetworkTransaction *transaction = [self transactionAtIndexPath:indexPath inTableView:tableView];
-        NSString *requestURLString = transaction.request.URL.absoluteString ?: @"";
-        [UIPasteboard.generalPasteboard setString:requestURLString];
+        NSURLRequest *request = [self transactionAtIndexPath:indexPath].request;
+        UIPasteboard.generalPasteboard.string = request.URL.absoluteString ?: @"";
     }
 }
 
-- (FLEXNetworkTransaction *)transactionAtIndexPath:(NSIndexPath *)indexPath inTableView:(UITableView *)tableView
+#if FLEX_AT_LEAST_IOS13_SDK
+
+- (UIContextMenuConfiguration *)tableView:(UITableView *)tableView contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point __IOS_AVAILABLE(13.0)
+{
+    return [UIContextMenuConfiguration
+        configurationWithIdentifier:nil
+        previewProvider:nil
+        actionProvider:^UIMenu *(NSArray<UIMenuElement *> *suggestedActions) {
+            UIAction *copy = [UIAction
+                actionWithTitle:@"Copy"
+                image:nil
+                identifier:nil
+                handler:^(__kindof UIAction *action) {
+                    NSURLRequest *request = [self transactionAtIndexPath:indexPath].request;
+                    UIPasteboard.generalPasteboard.string = request.URL.absoluteString ?: @"";
+                }
+            ];
+            return [UIMenu
+                menuWithTitle:@"" image:nil identifier:nil
+                options:UIMenuOptionsDisplayInline
+                children:@[copy]
+            ];
+        }
+    ];
+}
+
+#endif
+
+- (FLEXNetworkTransaction *)transactionAtIndexPath:(NSIndexPath *)indexPath
 {
     return self.searchController.isActive ? self.filteredNetworkTransactions[indexPath.row] : self.networkTransactions[indexPath.row];
 }

+ 29 - 1
Classes/Network/FLEXNetworkTransactionDetailTableViewController.m

@@ -206,10 +206,38 @@ typedef UIViewController *(^FLEXNetworkDetailRowSelectionFuture)(void);
 {
     if (action == @selector(copy:)) {
         FLEXNetworkDetailRow *row = [self rowModelAtIndexPath:indexPath];
-        [UIPasteboard.generalPasteboard setString:row.detailText];
+        UIPasteboard.generalPasteboard.string = row.detailText;
     }
 }
 
+#if FLEX_AT_LEAST_IOS13_SDK
+
+- (UIContextMenuConfiguration *)tableView:(UITableView *)tableView contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point __IOS_AVAILABLE(13.0)
+{
+    return [UIContextMenuConfiguration
+        configurationWithIdentifier:nil
+        previewProvider:nil
+        actionProvider:^UIMenu *(NSArray<UIMenuElement *> *suggestedActions) {
+            UIAction *copy = [UIAction
+                actionWithTitle:@"Copy"
+                image:nil
+                identifier:nil
+                handler:^(__kindof UIAction *action) {
+                    FLEXNetworkDetailRow *row = [self rowModelAtIndexPath:indexPath];
+                    UIPasteboard.generalPasteboard.string = row.detailText;
+                }
+            ];
+            return [UIMenu
+                menuWithTitle:@"" image:nil identifier:nil
+                options:UIMenuOptionsDisplayInline
+                children:@[copy]
+            ];
+        }
+    ];
+}
+
+#endif
+
 #pragma mark - View Configuration
 
 + (NSAttributedString *)attributedTextForRow:(FLEXNetworkDetailRow *)row

+ 1 - 1
Classes/Utility/FLEXUtility.h

@@ -15,7 +15,7 @@
 
 #define FLEXFloor(x) (floor(UIScreen.mainScreen.scale * (x)) / UIScreen.mainScreen.scale)
 
-#if defined(__IPHONE_13_0)
+#ifdef __IPHONE_13_0
 #define FLEX_AT_LEAST_IOS13_SDK (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0)
 #else
 #define FLEX_AT_LEAST_IOS13_SDK NO