[ios] add category actions to downloaded bookmarks screen

This commit is contained in:
Alexey Belousov 2018-06-18 20:14:38 +03:00 committed by Vlad Mihaylenko
parent 24ab5eefb0
commit 811ac5bf81
8 changed files with 153 additions and 35 deletions

View file

@ -1,5 +1,12 @@
protocol CatalogCategoryCellDelegate {
func cell(_ cell: CatalogCategoryCell, didCheck visible: Bool)
func cell(_ cell: CatalogCategoryCell, didPress moreButton: UIButton)
}
class CatalogCategoryCell: UITableViewCell {
var delegate: CatalogCategoryCellDelegate?
@IBOutlet weak var visibleCheckmark: Checkmark! {
didSet {
visibleCheckmark.offTintColor = .blackHintText()
@ -21,13 +28,17 @@ class CatalogCategoryCell: UITableViewCell {
@IBOutlet weak var moreButton: UIButton!
@IBAction func onVisibleChanged(_ sender: Checkmark) {
}
@IBAction func onMoreButton(_ sender: UIButton) {
delegate?.cell(self, didCheck: sender.isChecked)
}
func update(with category: MWMCatalogCategory) {
@IBAction func onMoreButton(_ sender: UIButton) {
delegate?.cell(self, didPress: sender)
}
func update(with category: MWMCatalogCategory, delegate: CatalogCategoryCellDelegate?) {
titleLabel.text = category.title
subtitleLabel.text = "\(category.bookmarksCount) places • by \(category.author ?? "")"
visibleCheckmark.isChecked = category.isVisible
self.delegate = delegate
}
}

View file

@ -7,11 +7,40 @@ class DownloadedBookmarksDataSource {
}
}
var allCategoriesVisible: Bool {
get {
var result = true
categories.forEach { if !$0.isVisible { result = false } }
return result
}
set {
categories.forEach {
$0.isVisible = newValue
MWMBookmarksManager.setCategory($0.categoryId, isVisible: newValue)
}
}
}
init() {
categories = MWMBookmarksManager.categoriesFromCatalog()
reload()
}
func category(at index: Int) -> MWMCatalogCategory {
return categories[index]
}
func reload() {
categories = MWMBookmarksManager.categoriesFromCatalog()
}
func setCategory(visible: Bool, at index: Int) {
let category = categories[index]
category.isVisible = visible
MWMBookmarksManager.setCategory(category.categoryId, isVisible: visible)
}
func deleteCategory(at index: Int) {
MWMBookmarksManager.deleteCategory(category(at: index).categoryId)
reload()
}
}

View file

@ -1,7 +1,7 @@
class DownloadedBookmarksViewController: UITableViewController {
@IBOutlet weak var topView: UIView!
@IBOutlet weak var bottomView: UIView!
@IBOutlet var topView: UIView!
@IBOutlet var bottomView: UIView!
let dataSource = DownloadedBookmarksDataSource()
@ -16,8 +16,15 @@ class DownloadedBookmarksViewController: UITableViewController {
tableView.registerNibForHeaderFooterView(BMCCategoriesHeader.self)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
dataSource.reload()
tableView.reloadData()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
return dataSource.categoriesCount > 0 ? 1 : 0
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
@ -26,7 +33,7 @@ class DownloadedBookmarksViewController: UITableViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(cell: CatalogCategoryCell.self, indexPath: indexPath)
cell.update(with: dataSource.category(at: indexPath.row))
cell.update(with: dataSource.category(at: indexPath.row), delegate: self)
return cell
}
@ -36,7 +43,7 @@ class DownloadedBookmarksViewController: UITableViewController {
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = tableView.dequeueReusableHeaderFooterView(BMCCategoriesHeader.self)
headerView.isShowAll = true
headerView.isShowAll = !dataSource.allCategoriesVisible
headerView.delegate = self
return headerView
}
@ -48,20 +55,77 @@ class DownloadedBookmarksViewController: UITableViewController {
animated: true)
}
}
}
extension DownloadedBookmarksViewController: BMCCategoryCellDelegate {
func visibilityAction(category: BMCCategory) {
private func setCategoryVisible(_ visible: Bool, at index: Int) {
dataSource.setCategory(visible: visible, at: index)
let categoriesHeader = tableView.headerView(forSection: 0) as! BMCCategoriesHeader
categoriesHeader.isShowAll = !dataSource.allCategoriesVisible
}
func moreAction(category: BMCCategory, anchor: UIView) {
private func shareCategory(at index: Int) {
let category = dataSource.category(at: index)
if let url = MWMBookmarksManager.sharingUrl(forCategoryId: category.categoryId) {
let message = L("share_bookmarks_email_body")
let shareController = MWMActivityViewController.share(for: url, message: message)
shareController?.present(inParentViewController: self, anchorView: nil)
}
}
private func deleteCategory(at index: Int) {
self.dataSource.deleteCategory(at: index)
if self.dataSource.categoriesCount > 0 {
self.tableView.deleteRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
} else {
self.tableView.tableHeaderView = self.topView
self.tableView.reloadData()
}
}
}
extension DownloadedBookmarksViewController: CatalogCategoryCellDelegate {
func cell(_ cell: CatalogCategoryCell, didCheck visible: Bool) {
if let indexPath = tableView.indexPath(for: cell) {
setCategoryVisible(visible, at: indexPath.row)
}
}
func cell(_ cell: CatalogCategoryCell, didPress moreButton: UIButton) {
if let indexPath = tableView.indexPath(for: cell) {
let category = dataSource.category(at: indexPath.row)
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
if let ppc = actionSheet.popoverPresentationController {
ppc.sourceView = moreButton
ppc.sourceRect = moreButton.bounds
}
let showHide = L(category.isVisible ? "hide" : "show").capitalized
actionSheet.addAction(UIAlertAction(title: showHide, style: .default, handler: { _ in
self.setCategoryVisible(!category.isVisible, at: indexPath.row)
self.tableView.reloadRows(at: [indexPath], with: .none)
}))
let share = L("share").capitalized
actionSheet.addAction(UIAlertAction(title: share, style: .default, handler: { _ in
self.shareCategory(at: indexPath.row)
}))
let delete = L("delete").capitalized
let deleteAction = UIAlertAction(title: delete, style: .destructive, handler: { _ in
self.deleteCategory(at: indexPath.row)
})
actionSheet.addAction(deleteAction)
let cancel = L("cancel").capitalized
actionSheet.addAction(UIAlertAction(title: cancel, style: .cancel, handler: nil))
present(actionSheet, animated: true, completion: nil)
}
}
}
extension DownloadedBookmarksViewController: BMCCategoriesHeaderDelegate {
func visibilityAction(isShowAll: Bool) {
func visibilityAction(_ categoriesHeader: BMCCategoriesHeader) {
let showAll = categoriesHeader.isShowAll
dataSource.allCategoriesVisible = showAll
tableView.reloadData()
}
}

View file

@ -316,9 +316,8 @@ extension BMCViewController: BMCPermissionsHeaderDelegate {
}
extension BMCViewController: BMCCategoriesHeaderDelegate {
func visibilityAction(isShowAll: Bool) {
viewModel.updateAllCategoriesVisibility(isShowAll: isShowAll)
let categoriesHeader = tableView.headerView(forSection: viewModel.sectionIndex(section: .categories)) as! BMCCategoriesHeader
func visibilityAction(_ categoriesHeader: BMCCategoriesHeader) {
viewModel.updateAllCategoriesVisibility(isShowAll: categoriesHeader.isShowAll)
categoriesHeader.isShowAll = viewModel.areAllCategoriesInvisible()
}
}

View file

@ -1,5 +1,5 @@
protocol BMCCategoriesHeaderDelegate {
func visibilityAction(isShowAll: Bool)
func visibilityAction(_ categoriesHeader: BMCCategoriesHeader)
}
final class BMCCategoriesHeader: UITableViewHeaderFooterView {
@ -30,6 +30,6 @@ final class BMCCategoriesHeader: UITableViewHeaderFooterView {
var delegate: BMCCategoriesHeaderDelegate?
@IBAction private func buttonAction() {
delegate?.visibilityAction(isShowAll: isShowAll)
delegate?.visibilityAction(self)
}
}

View file

@ -11,7 +11,7 @@
+ (instancetype)shareControllerForURL:(NSURL *)url
message:(NSString *)message
completionHandler:
(UIActivityViewControllerCompletionWithItemsHandler)completionHandler;
(_Nullable UIActivityViewControllerCompletionWithItemsHandler)completionHandler;
- (void)presentInParentViewController:(UIViewController *)parentVC anchorView:(UIView *)anchorView;

View file

@ -47,6 +47,7 @@
+ (void)cancelRestoring;
+ (NSURL * _Nullable)catalogFrontendUrl;
+ (NSURL * _Nullable)sharingUrlForCategoryId:(MWMMarkGroupID)groupId;
+ (void)downloadItemWithId:(NSString * _Nonnull)itemId
name:(NSString * _Nonnull)name
completion:(void (^_Nullable)(NSError * _Nullable error))completion;

View file

@ -263,7 +263,10 @@ NSString * const CloudErrorToString(Cloud::SynchronizationResult result)
auto const & list = GetFramework().GetBookmarkManager().GetBmGroupsIdList();
NSMutableArray<NSNumber *> * collection = @[].mutableCopy;
for (auto const & groupId : list)
[collection addObject:@(groupId)];
{
if (!GetFramework().GetBookmarkManager().IsCategoryFromCatalog(groupId))
[collection addObject:@(groupId)];
}
return collection.copy;
}
@ -491,12 +494,21 @@ NSString * const CloudErrorToString(Cloud::SynchronizationResult result)
return GetFramework().GetBookmarkManager().AreNotificationsEnabled();
}
+ (NSURL * _Nullable )catalogFrontendUrl {
+ (NSURL * _Nullable )catalogFrontendUrl
{
NSString *urlString = @(GetFramework().GetBookmarkManager().GetCatalogFrontendUrl().c_str());
return urlString ? [NSURL URLWithString:urlString] : nil;
}
+ (void)downloadItemWithId:(NSString *)itemId name:(NSString *)name completion:(void (^)(NSError * error))completion {
+ (NSURL * _Nullable)sharingUrlForCategoryId:(MWMMarkGroupID)groupId
{
NSString *urlString = @(GetFramework().GetBookmarkManager().GetCategoryCatalogDeeplink(groupId).c_str());
return urlString ? [NSURL URLWithString:urlString] : nil;
}
+ (void)downloadItemWithId:(NSString *)itemId name:(NSString *)name completion:(void (^)(NSError * error))completion
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[MWMBookmarksManager manager].catalogObservers = [NSMutableDictionary dictionary];
@ -544,24 +556,26 @@ NSString * const CloudErrorToString(Cloud::SynchronizationResult result)
GetFramework().GetBookmarkManager().DownloadFromCatalogAndImport(itemId.UTF8String, name.UTF8String);
}
+ (BOOL)isCategoryFromCatalog:(MWMMarkGroupID)groupId {
+ (BOOL)isCategoryFromCatalog:(MWMMarkGroupID)groupId
{
return GetFramework().GetBookmarkManager().IsCategoryFromCatalog(groupId);
}
+ (NSArray<MWMCatalogCategory *> *)categoriesFromCatalog {
+ (NSArray<MWMCatalogCategory *> *)categoriesFromCatalog
{
NSMutableArray * result = [NSMutableArray array];
MWMGroupIDCollection categoryIds = [self groupsIdList];
[categoryIds enumerateObjectsUsingBlock:^(NSNumber * categoryId, NSUInteger idx, BOOL * stop) {
MWMMarkGroupID catId = categoryId.unsignedIntValue;
if ([self isCategoryFromCatalog:catId])
auto const & list = GetFramework().GetBookmarkManager().GetBmGroupsIdList();
for (auto const & groupId : list)
{
if ([self isCategoryFromCatalog:groupId])
{
kml::CategoryData categoryData = GetFramework().GetBookmarkManager().GetCategoryData(categoryId.unsignedIntValue);
UInt64 bookmarksCount = [self getCategoryMarksCount:catId] + [self getCategoryTracksCount:catId];
kml::CategoryData categoryData = GetFramework().GetBookmarkManager().GetCategoryData(groupId);
UInt64 bookmarksCount = [self getCategoryMarksCount:groupId] + [self getCategoryTracksCount:groupId];
MWMCatalogCategory *category = [[MWMCatalogCategory alloc] initWithCategoryData:categoryData
bookmarksCount:bookmarksCount];
[result addObject:category];
}
}];
}
return [result copy];
}