Skip to content

Commit

Permalink
Merge pull request #3714 from osmandapp/fix-file-sharing-screens
Browse files Browse the repository at this point in the history
Fix file sharing screens
  • Loading branch information
alex-osm authored May 20, 2024
2 parents 6aefe0e + 8c2cbed commit 1f897d8
Show file tree
Hide file tree
Showing 15 changed files with 143 additions and 97 deletions.
6 changes: 2 additions & 4 deletions Sources/Controllers/Help/OAHelpViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,8 @@ - (void)sendLogFile
if (files.count > 0)
{
NSString *latestLogFile = [logsPath stringByAppendingPathComponent:[files firstObject]];
NSURL *logFileURL = [NSURL fileURLWithPath:latestLogFile];

UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[logFileURL] applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];
CGRect bottomScreenCenterPoint = CGRectMake(CGRectGetMidX(self.view.bounds), self.view.bounds.size.height, 0, 0);
[self showActivity:@[[NSURL fileURLWithPath:latestLogFile]] applicationActivities:nil excludedActivityTypes:nil sourceView:self.view sourceRect:bottomScreenCenterPoint barButtonItem:nil permittedArrowDirections:UIPopoverArrowDirectionDown completionWithItemsHandler:nil];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#import "OAObservable.h"
#import "OAAutoObserverProxy.h"

@interface OAFavoriteListViewController : OACompoundViewController<UITableViewDataSource, UITableViewDelegate, UIDocumentInteractionControllerDelegate, UIScrollViewDelegate>
@interface OAFavoriteListViewController : OACompoundViewController<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *favoriteTableView;
@property (weak, nonatomic) IBOutlet UIView *editToolbarView;
Expand Down
38 changes: 17 additions & 21 deletions Sources/Controllers/MyPlaces/OAFavoriteListViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@

#define FavoriteTableGroup _(FavoriteTableGroup)

static const NSInteger _exportButtonIndex = 1;

@interface FavoriteTableGroup : NSObject
@property BOOL isOpen;
@property OAFavoriteGroup *favoriteGroup;
Expand Down Expand Up @@ -864,12 +866,13 @@ - (IBAction)editButtonClicked:(id)sender
- (IBAction) shareButtonClicked:(id)sender
{
// Share selected favorites
[self shareItems:_selectedItems];
UIButton *clickedButton = (UIButton *)sender;
[self shareItems:_selectedItems sounceItem:clickedButton];
[self finishEditing];
[self generateData];
}

- (void)shareItems:(NSArray<NSIndexPath *> *)selectedItems
- (void)shareItems:(NSArray<NSIndexPath *> *)selectedItems sounceItem:(UIView *)sounceItem
{
if ([selectedItems count] == 0)
{
Expand Down Expand Up @@ -911,17 +914,10 @@ - (void)shareItems:(NSArray<NSIndexPath *> *)selectedItems
[OAFavoritesHelper saveFile:groups.allValues file:fullFilename];

NSURL *favoritesUrl = [NSURL fileURLWithPath:fullFilename];
UIActivityViewController *activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:@[favoritesUrl] applicationActivities:nil];
activityViewController.popoverPresentationController.sourceView = self.view;
activityViewController.popoverPresentationController.sourceRect = self.view.frame;
activityViewController.completionWithItemsHandler = ^void(UIActivityType activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
[NSFileManager.defaultManager removeItemAtURL:favoritesUrl error:nil];
};

[self presentViewController:activityViewController
animated:YES
completion:nil];
[self showActivity:@[favoritesUrl] sourceView:sounceItem barButtonItem:nil completionWithItemsHandler:^{
[NSFileManager.defaultManager removeItemAtURL:favoritesUrl error:nil];
}];
}

- (IBAction)goRootScreen:(id)sender
Expand Down Expand Up @@ -951,15 +947,13 @@ - (void)onExportClicked
UIActivityViewController *activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:@[favoritesUrl]
applicationActivities:nil];
activityViewController.popoverPresentationController.sourceView = self.view;
activityViewController.popoverPresentationController.sourceRect = _exportButton.frame;
activityViewController.completionWithItemsHandler = ^void(UIActivityType activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
[NSFileManager.defaultManager removeItemAtURL:favoritesUrl error:nil];
};

[self presentViewController:activityViewController
animated:YES
completion:nil];
//export button is on last section, last row
NSIndexPath *exportButtonIndex = [NSIndexPath indexPathForRow:_exportButtonIndex inSection:_data.count - 1];
UITableViewCell *cell = [self.favoriteTableView cellForRowAtIndexPath:exportButtonIndex];
[self showActivity:@[favoritesUrl] sourceView:cell barButtonItem:nil completionWithItemsHandler:^{
[NSFileManager.defaultManager removeItemAtURL:favoritesUrl error:nil];
}];
}

#pragma mark - UITableViewDataSource
Expand Down Expand Up @@ -1082,7 +1076,9 @@ - (UIContextMenuConfiguration *)tableView:(UITableView *)tableView contextMenuCo
{
[indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:indexPath.section]];
}
[self shareItems:indexPaths];

UITableViewCell *cell = [self.favoriteTableView cellForRowAtIndexPath:indexPath];
[self shareItems:indexPaths sounceItem:cell];
}];
shareAction.accessibilityLabel = OALocalizedString(@"shared_string_share");
[menuElements addObject:shareAction];
Expand Down
11 changes: 7 additions & 4 deletions Sources/Controllers/MyPlaces/TracksViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -698,9 +698,9 @@ class TracksViewController: OACompoundViewController, UITableViewDelegate, UITab
}
}

private func onTrackShareClicked(_ track: OAGPX?, isCurrentTrack: Bool) {
private func onTrackShareClicked(_ track: OAGPX?, isCurrentTrack: Bool, touchPointArea: CGRect) {
if let gpx = isCurrentTrack ? savingHelper.getCurrentGPX() : track {
gpxHelper.openExport(forTrack: gpx, gpxDoc: nil, isCurrentTrack: isCurrentTrack, in: self, hostViewControllerDelegate: self)
gpxHelper.openExport(forTrack: gpx, gpxDoc: nil, isCurrentTrack: isCurrentTrack, in: self, hostViewControllerDelegate: self, touchPointArea: touchPointArea)
}
}

Expand Down Expand Up @@ -1178,7 +1178,8 @@ class TracksViewController: OACompoundViewController, UITableViewDelegate, UITab
let secondButtonsSection = UIMenu(title: "", options: .displayInline, children: [renameAction])

let exportAction = UIAction(title: localizedString("shared_string_export"), image: UIImage.icCustomExportOutlined) { [weak self] _ in
self?.onFolderExportButtonClicked(selectedFolderName)
guard let self else { return }
self.onFolderExportButtonClicked(selectedFolderName)
}
let moveAction = UIAction(title: localizedString("shared_string_move"), image: UIImage.icCustomFolderMoveOutlined) { [weak self] _ in
self?.onFolderMoveButtonClicked(selectedFolderName)
Expand Down Expand Up @@ -1225,7 +1226,9 @@ class TracksViewController: OACompoundViewController, UITableViewDelegate, UITab
let secondButtonsSection = UIMenu(title: "", options: .displayInline, children: [analyzeAction])

let shareAction = UIAction(title: localizedString("shared_string_share"), image: UIImage.icCustomExportOutlined) { [weak self] _ in
self?.onTrackShareClicked(track, isCurrentTrack: isCurrentTrack)
guard let self else { return }
let cellScreenArea = self.view.convert(self.tableView.rectForRow(at: indexPath), from: self.tableView)
self.onTrackShareClicked(track, isCurrentTrack: isCurrentTrack, touchPointArea: cellScreenArea)
}
let uploadToOsmAction = UIAction(title: localizedString("upload_to_osm_short"), image: UIImage.icCustomUploadToOpenstreetmapOutlined) { [weak self] _ in
self?.onTrackUploadToOsmClicked(track, isCurrentTrack: isCurrentTrack)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@
#define kMyPlacesSectionIndex 1
#define kResourcesSectionIndex 2

typedef NS_ENUM(NSInteger, EOAExportItemsViewControllerStateType) {
EOAExportItemsViewControllerStateTypeInited,
EOAExportItemsViewControllerStateTypeExportStarted,
EOAExportItemsViewControllerStateTypeExportDone
};

@implementation OAExportItemsViewController
{
OASettingsHelper *_settingsHelper;
OAApplicationMode *_appMode;

BOOL _exportStarted;
EOAExportItemsViewControllerStateType _state;
BOOL _shouldOpenSettingsOnInit;
BOOL _shouldOpenMyPlacesOnInit;
BOOL _shouldOpenResourcesOnInit;
Expand Down Expand Up @@ -71,7 +77,7 @@ - (instancetype)initWithType:(OAExportSettingsType *)type selectedItems:(NSArray
return self;
}

- (instancetype)initWithTypes:(NSDictionary<OAExportSettingsType *, NSArray<id> *> *)typesItems;
- (instancetype)initWithTypes:(NSDictionary<OAExportSettingsType *, NSArray<id> *> *)typesItems
{
self = [super init];
if (self)
Expand All @@ -94,6 +100,7 @@ - (instancetype)initWithTypes:(NSDictionary<OAExportSettingsType *, NSArray<id>
- (void)commonInit
{
_settingsHelper = [OASettingsHelper sharedInstance];
_state = EOAExportItemsViewControllerStateTypeInited;
self.itemsMap = [_settingsHelper getSettingsByCategory:YES];
self.itemTypes = self.itemsMap.allKeys;
}
Expand All @@ -108,17 +115,17 @@ - (void)postInit

- (NSString *)getTitle
{
return _exportStarted ? OALocalizedString(@"shared_string_preparing") : _appMode ? OALocalizedString(@"export_profile") : OALocalizedString(@"shared_string_export");
return _state == EOAExportItemsViewControllerStateTypeExportStarted ? OALocalizedString(@"shared_string_preparing") : _appMode ? OALocalizedString(@"export_profile") : OALocalizedString(@"shared_string_export");
}

- (NSArray<UIBarButtonItem *> *)getRightNavbarButtons
{
return _exportStarted ? nil : [super getRightNavbarButtons];
return _state == EOAExportItemsViewControllerStateTypeInited ? [super getRightNavbarButtons] : nil;
}

- (NSAttributedString *)getTableHeaderDescriptionAttr
{
NSString *exportSelectDescr = _exportStarted ? @"" : [OALocalizedString(@"export_profile_select_descr") stringByAppendingString:@"\n"];
NSString *exportSelectDescr = _state == EOAExportItemsViewControllerStateTypeInited ? [OALocalizedString(@"export_profile_select_descr") stringByAppendingString:@"\n"] : @"";
long itemsSize = [self calculateItemsSize:self.getSelectedItems];
NSString *approximateFileSize = [NSString stringWithFormat:@"%@: %@",
OALocalizedString(@"approximate_file_size"),
Expand All @@ -133,38 +140,43 @@ - (NSAttributedString *)getTableHeaderDescriptionAttr

- (NSString *)getBottomButtonTitle
{
return _exportStarted ? @"" : [super getBottomButtonTitle];
return _state == EOAExportItemsViewControllerStateTypeInited ? [super getBottomButtonTitle] : @"";
}

#pragma mark - Table data

- (void)generateData
{
if (_exportStarted)
if (_state == EOAExportItemsViewControllerStateTypeInited)
{
[super generateData];

if (_shouldOpenSettingsOnInit)
{
self.data[kSettingsSectionIndex].isOpen = YES;
_shouldOpenSettingsOnInit = NO;
}
if (_shouldOpenMyPlacesOnInit)
{
self.data[kMyPlacesSectionIndex].isOpen = YES;
_shouldOpenMyPlacesOnInit = NO;
}
if (_shouldOpenResourcesOnInit)
{
self.data[kResourcesSectionIndex].isOpen = YES;
_shouldOpenResourcesOnInit = NO;
}
}
else if (_state == EOAExportItemsViewControllerStateTypeExportStarted)
{
OATableCollapsableGroup *group = [[OATableCollapsableGroup alloc] init];
group.type = [OAProgressTitleCell getCellIdentifier];
group.groupName = OALocalizedString(@"preparing_file");
self.data = @[group];
return;
}

[super generateData];

if (_shouldOpenSettingsOnInit)
{
self.data[kSettingsSectionIndex].isOpen = YES;
_shouldOpenSettingsOnInit = NO;
}
if (_shouldOpenMyPlacesOnInit)
{
self.data[kMyPlacesSectionIndex].isOpen = YES;
_shouldOpenMyPlacesOnInit = NO;
}
if (_shouldOpenResourcesOnInit)
else if (_state == EOAExportItemsViewControllerStateTypeExportDone)
{
self.data[kResourcesSectionIndex].isOpen = YES;
_shouldOpenResourcesOnInit = NO;
self.data = @[];
}
}

Expand Down Expand Up @@ -200,7 +212,7 @@ - (void)updateSelectedProfile

- (void)shareProfile
{
_exportStarted = YES;
_state = EOAExportItemsViewControllerStateTypeExportStarted;
[self updateUI];

OASettingsHelper *settingsHelper = OASettingsHelper.sharedInstance;
Expand Down Expand Up @@ -267,22 +279,20 @@ - (void)onSettingsCollectFinished:(BOOL)succeed empty:(BOOL)empty items:(NSArray

- (void)onSettingsExportFinished:(NSString *)file succeed:(BOOL)succeed
{
[self.navigationController popViewControllerAnimated:YES];

if (succeed)
{
dispatch_async(dispatch_get_main_queue(), ^{
OARootViewController *rootVC = [OARootViewController instance];
UIViewController *topViewController = rootVC.presentedViewController ?: rootVC;
UIActivityViewController *activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:@[[NSURL fileURLWithPath:file]]
applicationActivities:nil];
activityViewController.popoverPresentationController.sourceView = topViewController.view;
activityViewController.popoverPresentationController.sourceRect = CGRectMake(CGRectGetMidX(topViewController.view.bounds), CGRectGetMidY(topViewController.view.bounds), 0., 0.);
activityViewController.popoverPresentationController.permittedArrowDirections = 0;
[topViewController presentViewController:activityViewController animated:YES completion:nil];
});
}
dispatch_async(dispatch_get_main_queue(), ^{
_state = EOAExportItemsViewControllerStateTypeExportDone;
[self updateUI];

if (succeed)
{
NSURL *fileUrl = [NSURL fileURLWithPath:file];
CGRect bottomScreenCenterPoint = CGRectMake(CGRectGetMidX(self.view.bounds), self.view.bounds.size.height, 0, 0);
[self showActivity:@[fileUrl] applicationActivities:nil excludedActivityTypes:nil sourceView:self.view sourceRect:bottomScreenCenterPoint barButtonItem:nil permittedArrowDirections:UIPopoverArrowDirectionDown completionWithItemsHandler:^{
[self.navigationController popViewControllerAnimated:YES];
[NSFileManager.defaultManager removeItemAtURL:fileUrl error:nil];
}];
}
});
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ - (void)onButtonPressed:(OAGPXBaseTableData *)tableData sourceView:(UIView *)sou
else if ([tableData.key isEqualToString:@"analyze"] && self.trackMenuDelegate)
[self.trackMenuDelegate openAnalysis:@[@(GPXDataSetTypeAltitude), @(GPXDataSetTypeSlope)]];
else if ([tableData.key isEqualToString:@"share"] && self.trackMenuDelegate)
[self.trackMenuDelegate openExport];
[self.trackMenuDelegate openExport:sourceView];
else if ([tableData.key isEqualToString:@"edit"] && self.trackMenuDelegate)
[self.trackMenuDelegate editSegment];
else if ([tableData.key isEqualToString:@"edit_create_duplicate"] && self.trackMenuDelegate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ - (void)onAppearancePressed:(id)sender
- (void)onExportPressed:(id)sender
{
if (self.trackMenuDelegate)
[self.trackMenuDelegate openExport];
[self.trackMenuDelegate openExport:((UIButton *)sender)];
}

- (void)onNavigationPressed:(id)sender
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
- (CLLocationCoordinate2D)getPinLocation;
- (OARouteKey *)getRouteKey;
- (void)openAppearance;
- (void)openExport;
- (void)openExport:(UIView *)sourceView;
- (void)openNavigation;
- (void)openDescription;
- (void)openDescriptionEditor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ @implementation OATrackMenuHudViewController
OAAutoObserverProxy *_headingUpdateObserver;
NSTimeInterval _lastUpdate;

UIDocumentInteractionController *_exportController;
OATrackMenuHeaderView *_headerView;
OAGPXTableData *_tableData;

Expand Down Expand Up @@ -355,7 +354,6 @@ - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIVi
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
_exportController = nil;
_isViewVisible = YES;
[self restoreNavControllerHistoryIfNeeded];
}
Expand Down Expand Up @@ -1467,9 +1465,21 @@ - (void)openAppearance
}];
}

- (void)openExport
- (void)openExport:(UIView *)sourceView
{
[_gpxUIHelper openExportForTrack:self.gpx gpxDoc:self.doc isCurrentTrack:self.isCurrentTrack inViewController:self hostViewControllerDelegate:nil];
CGRect touchPointArea = CGRectZero;
if ([sourceView isKindOfClass:UIButton.class])
{
UIButton *topButtonShare = (UIButton *)sourceView;
touchPointArea = [self.view convertRect:topButtonShare.bounds fromView:topButtonShare];
}
else if ([sourceView isKindOfClass:UITableViewCell.class])
{
UITableViewCell *actionsTabCell = (UITableViewCell *)sourceView;
NSIndexPath *indexPath = [self.tableView indexPathForCell:actionsTabCell];
touchPointArea = [self.view convertRect:[self.tableView rectForRowAtIndexPath:indexPath] fromView:self.tableView];
}
[_gpxUIHelper openExportForTrack:self.gpx gpxDoc:self.doc isCurrentTrack:self.isCurrentTrack inViewController:self hostViewControllerDelegate:nil touchPointArea:touchPointArea];
}

- (void)openNavigation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,11 +556,7 @@ - (void)onImagesNavbarButtonPressed

- (void)onTopButtonPressed
{
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[[self getUrl]]
applicationActivities:nil];
activityViewController.popoverPresentationController.sourceView = self.view;
activityViewController.popoverPresentationController.sourceRect = self.topButton.frame;
[self.navigationController presentViewController:activityViewController animated:YES completion:nil];
[self showActivity:@[[self getUrl]] sourceView:self.topButton barButtonItem:nil completionWithItemsHandler:nil];
}

- (void)onBottomButtonPressed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,7 @@ final class TravelArticleDialogViewController: OABaseWebViewController, TravelAr
let articleUrl = "https://osmand.net/travel?title=" + title + "&lang=" + lang!

let items = [URL(string: articleUrl)!]
let ac = UIActivityViewController(activityItems: items, applicationActivities: nil)
present(ac, animated: true)
showActivity(items, sourceView: view, barButtonItem: navigationItem.rightBarButtonItem)
}

// MARK: Data
Expand Down
Loading

0 comments on commit 1f897d8

Please sign in to comment.