From 1a0896a1c1b347458aa3d076fb07712851e33ac5 Mon Sep 17 00:00:00 2001 From: Kiryl Kaveryn Date: Thu, 29 Feb 2024 16:29:19 +0400 Subject: [PATCH] [ios] implement 'share all bookmarks' button Signed-off-by: Kiryl Kaveryn --- .../CoreApi/Bookmarks/MWMBookmarksManager.h | 1 + .../CoreApi/Bookmarks/MWMBookmarksManager.mm | 31 +++++++++++++------ .../Categories/Actions/BMCActionsCell.swift | 4 +-- .../Maps/Bookmarks/Categories/BMCModels.swift | 21 +++++++++++++ .../BMCView/BMCViewController.swift | 19 ++++++++++-- .../BMCViewModel/BMCDefaultViewModel.swift | 7 ++++- 6 files changed, 69 insertions(+), 14 deletions(-) diff --git a/iphone/CoreApi/CoreApi/Bookmarks/MWMBookmarksManager.h b/iphone/CoreApi/CoreApi/Bookmarks/MWMBookmarksManager.h index f22195cb6b..62120c882c 100644 --- a/iphone/CoreApi/CoreApi/Bookmarks/MWMBookmarksManager.h +++ b/iphone/CoreApi/CoreApi/Bookmarks/MWMBookmarksManager.h @@ -84,6 +84,7 @@ NS_SWIFT_NAME(BookmarksManager) - (MWMTrackIDCollection)trackIdsForCategory:(MWMMarkGroupID)categoryId; - (void)shareCategory:(MWMMarkGroupID)groupId; +- (void)shareAllCategories; - (NSURL *)shareCategoryURL; - (void)finishShareCategory; diff --git a/iphone/CoreApi/CoreApi/Bookmarks/MWMBookmarksManager.mm b/iphone/CoreApi/CoreApi/Bookmarks/MWMBookmarksManager.mm index d23d85f497..e1194dda6c 100644 --- a/iphone/CoreApi/CoreApi/Bookmarks/MWMBookmarksManager.mm +++ b/iphone/CoreApi/CoreApi/Bookmarks/MWMBookmarksManager.mm @@ -572,9 +572,23 @@ static BookmarkManager::SortingType convertSortingTypeToCore(MWMBookmarksSorting { self.bm.PrepareFileForSharing({groupId}, [self](auto sharingResult) { - MWMBookmarksShareStatus status; - switch (sharingResult.m_code) - { + [self handleSharingResult:sharingResult]; + }); +} + +- (void)shareAllCategories +{ + self.bm.PrepareAllFilesForSharing([self](auto sharingResult) + { + [self handleSharingResult:sharingResult]; + }); +} + +- (void)handleSharingResult:(BookmarkManager::SharingResult)sharingResult +{ + MWMBookmarksShareStatus status; + switch (sharingResult.m_code) + { case BookmarkManager::SharingResult::Code::Success: { self.shareCategoryURL = [NSURL fileURLWithPath:@(sharingResult.m_sharingPath.c_str()) isDirectory:NO]; @@ -591,13 +605,12 @@ static BookmarkManager::SortingType convertSortingTypeToCore(MWMBookmarksSorting case BookmarkManager::SharingResult::Code::FileError: status = MWMBookmarksShareStatusFileError; break; - } + } - [self loopObservers:^(id observer) { - if ([observer respondsToSelector:@selector(onBookmarksCategoryFilePrepared:)]) - [observer onBookmarksCategoryFilePrepared:status]; - }]; - }); + [self loopObservers:^(id observer) { + if ([observer respondsToSelector:@selector(onBookmarksCategoryFilePrepared:)]) + [observer onBookmarksCategoryFilePrepared:status]; + }]; } - (NSURL *)shareCategoryURL diff --git a/iphone/Maps/Bookmarks/Categories/Actions/BMCActionsCell.swift b/iphone/Maps/Bookmarks/Categories/Actions/BMCActionsCell.swift index 4f8491bd50..277e72d6d2 100644 --- a/iphone/Maps/Bookmarks/Categories/Actions/BMCActionsCell.swift +++ b/iphone/Maps/Bookmarks/Categories/Actions/BMCActionsCell.swift @@ -5,8 +5,8 @@ final class BMCActionsCell: MWMTableViewCell { private var model: BMCAction! { didSet { - actionImage.image = #imageLiteral(resourceName: "ic24PxAddCopy") - actionTitle.text = L("bookmarks_create_new_group") + actionImage.image = model.image + actionTitle.text = model.title } } diff --git a/iphone/Maps/Bookmarks/Categories/BMCModels.swift b/iphone/Maps/Bookmarks/Categories/BMCModels.swift index 853845d683..a3a7aa4099 100644 --- a/iphone/Maps/Bookmarks/Categories/BMCModels.swift +++ b/iphone/Maps/Bookmarks/Categories/BMCModels.swift @@ -8,6 +8,27 @@ protocol BMCModel {} enum BMCAction: BMCModel { case create + case exportAll +} + +extension BMCAction { + var title: String { + switch self { + case .create: + return L("bookmarks_create_new_group") + case .exportAll: + return L("bookmarks_export") + } + } + + var image: UIImage { + switch self { + case .create: + return UIImage(named: "ic24PxAddCopy")! + case .exportAll: + return UIImage(named: "ic24PxShare")! + } + } } enum BMCNotification: BMCModel { diff --git a/iphone/Maps/Bookmarks/Categories/BMCView/BMCViewController.swift b/iphone/Maps/Bookmarks/Categories/BMCView/BMCViewController.swift index 29160ed386..9681fc20fc 100644 --- a/iphone/Maps/Bookmarks/Categories/BMCView/BMCViewController.swift +++ b/iphone/Maps/Bookmarks/Categories/BMCView/BMCViewController.swift @@ -77,8 +77,7 @@ final class BMCViewController: MWMViewController { viewModel.shareCategoryFile(at: index) { switch $0 { case let .success(url): - let shareController = ActivityViewController.share(for: url, - message: L("share_bookmarks_email_body")) + let shareController = ActivityViewController.share(for: url, message: L("share_bookmarks_email_body")) { [weak self] _, _, _, _ in self?.viewModel?.finishShareCategory() } @@ -89,6 +88,21 @@ final class BMCViewController: MWMViewController { } } + private func shareAllCategories() { + viewModel.shareAllCategories { + switch $0 { + case let .success(url): + let shareController = ActivityViewController.share(for: url, message: L("share_bookmarks_email_body")) + { [weak self] _, _, _, _ in + self?.viewModel?.finishShareCategory() + } + shareController?.present(inParentViewController: self, anchorView: nil) + case let .error(title, text): + MWMAlertViewController.activeAlert().presentInfoAlert(title, text: text) + } + } + } + private func openCategorySettings(category: BookmarkGroup) { let settingsController = CategorySettingsViewController(bookmarkGroup: BookmarksManager.shared().category(withId: category.categoryId)) settingsController.delegate = self @@ -255,6 +269,7 @@ extension BMCViewController: UITableViewDelegate { case .actions: switch viewModel.action(at: indexPath.row) { case .create: createNewCategory() + case .exportAll: shareAllCategories() } default: assertionFailure() diff --git a/iphone/Maps/Bookmarks/Categories/BMCViewModel/BMCDefaultViewModel.swift b/iphone/Maps/Bookmarks/Categories/BMCViewModel/BMCDefaultViewModel.swift index 0b5ebf8a8b..3232f82a00 100644 --- a/iphone/Maps/Bookmarks/Categories/BMCViewModel/BMCDefaultViewModel.swift +++ b/iphone/Maps/Bookmarks/Categories/BMCViewModel/BMCDefaultViewModel.swift @@ -40,7 +40,7 @@ final class BMCDefaultViewModel: NSObject { } private func setActions() { - actions = [.create] + actions = [.create, .exportAll] } private func setNotifications() { @@ -143,6 +143,11 @@ extension BMCDefaultViewModel { manager.shareCategory(category.categoryId) } + func shareAllCategories(handler: @escaping OnPreparedToShareHandler) { + onPreparedToShareCategory = handler + manager.shareAllCategories() + } + func finishShareCategory() { manager.finishShareCategory() onPreparedToShareCategory = nil