Skip to content

Commit

Permalink
[ios] add support for the recently deleted feature to the bookmark …
Browse files Browse the repository at this point in the history
…manager

- add and implement @protocol RecentlyDeletedCategoriesManager
- move the bmmanager observing method to the @protocol BookmarksObservable to make manager mokable
- add RecentlyDeletedCategory class as a lightweight version of the `CategoryData` to pass to it the swift
Signed-off-by: Kiryl Kaveryn <kirylkaveryn@gmail.com>
  • Loading branch information
kirylkaveryn authored and biodranik committed Aug 15, 2024
1 parent a639a94 commit f3d1cc6
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 5 deletions.
20 changes: 20 additions & 0 deletions iphone/CoreApi/CoreApi.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
9974CA2A23DF1968003FE824 /* ElevationProfileData.mm in Sources */ = {isa = PBXBuildFile; fileRef = 9974CA2823DF1968003FE824 /* ElevationProfileData.mm */; };
9974CA2D23DF197B003FE824 /* ElevationProfileData+Core.h in Headers */ = {isa = PBXBuildFile; fileRef = 9974CA2B23DF197B003FE824 /* ElevationProfileData+Core.h */; };
AC6A585728057EF6003EABAF /* StringUtils.h in Headers */ = {isa = PBXBuildFile; fileRef = AC6A585628057CC1003EABAF /* StringUtils.h */; settings = {ATTRIBUTES = (Public, ); }; };
EDC4E3512C5D222D009286A2 /* RecentlyDeletedCategory.mm in Sources */ = {isa = PBXBuildFile; fileRef = EDC4E34E2C5D222D009286A2 /* RecentlyDeletedCategory.mm */; };
EDC4E3522C5D222D009286A2 /* RecentlyDeletedCategory.h in Headers */ = {isa = PBXBuildFile; fileRef = EDC4E34F2C5D222D009286A2 /* RecentlyDeletedCategory.h */; settings = {ATTRIBUTES = (Public, ); }; };
EDC4E3532C5D222D009286A2 /* RecentlyDeletedCategory+Core.h in Headers */ = {isa = PBXBuildFile; fileRef = EDC4E3502C5D222D009286A2 /* RecentlyDeletedCategory+Core.h */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -175,6 +178,9 @@
9974CA2823DF1968003FE824 /* ElevationProfileData.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = ElevationProfileData.mm; sourceTree = "<group>"; };
9974CA2B23DF197B003FE824 /* ElevationProfileData+Core.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ElevationProfileData+Core.h"; sourceTree = "<group>"; };
AC6A585628057CC1003EABAF /* StringUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StringUtils.h; sourceTree = "<group>"; };
EDC4E34E2C5D222D009286A2 /* RecentlyDeletedCategory.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = RecentlyDeletedCategory.mm; sourceTree = "<group>"; };
EDC4E34F2C5D222D009286A2 /* RecentlyDeletedCategory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RecentlyDeletedCategory.h; sourceTree = "<group>"; };
EDC4E3502C5D222D009286A2 /* RecentlyDeletedCategory+Core.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RecentlyDeletedCategory+Core.h"; sourceTree = "<group>"; };
FAA6D8CF29205D2D00E8D50D /* libcppjansson.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; path = libcppjansson.a; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -338,6 +344,7 @@
47CA68E72506F61300671019 /* MWMTrack.mm */,
47F0D2132516847F00BC685E /* MWMBookmarksSection.h */,
47F0D2142516847F00BC685E /* MWMBookmarksSection.m */,
EDC4E3542C5D2251009286A2 /* RecentlyDeletedCategory */,
);
path = Bookmarks;
sourceTree = "<group>";
Expand Down Expand Up @@ -410,6 +417,16 @@
path = ElevationProfile;
sourceTree = "<group>";
};
EDC4E3542C5D2251009286A2 /* RecentlyDeletedCategory */ = {
isa = PBXGroup;
children = (
EDC4E34F2C5D222D009286A2 /* RecentlyDeletedCategory.h */,
EDC4E34E2C5D222D009286A2 /* RecentlyDeletedCategory.mm */,
EDC4E3502C5D222D009286A2 /* RecentlyDeletedCategory+Core.h */,
);
path = RecentlyDeletedCategory;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXHeadersBuildPhase section */
Expand Down Expand Up @@ -452,13 +469,15 @@
479F7063234FBC5900011E2E /* MWMCarPlayBookmarkObject.h in Headers */,
47E8163723B188D3008FD836 /* MWMStorage.h in Headers */,
47EEAFF62350CF48005CF316 /* AppInfo.h in Headers */,
EDC4E3532C5D222D009286A2 /* RecentlyDeletedCategory+Core.h in Headers */,
47F701F3238C877C00D18E95 /* PlacePageButtonsData+Core.h in Headers */,
47CA68E12506C01F00671019 /* MWMBookmark+Core.h in Headers */,
471527392491EDAA00E91BBA /* MWMBookmarkColor.h in Headers */,
47F4F1F923A3336C0022FD56 /* MWMMapNodeAttributes.h in Headers */,
479F704A234F785B00011E2E /* MWMTypes.h in Headers */,
47C637D72354AEBE00E12DE0 /* MWMMapOverlayManager.h in Headers */,
47942D9C237D927800DEFAE3 /* PlacePageBookmarkData.h in Headers */,
EDC4E3522C5D222D009286A2 /* RecentlyDeletedCategory.h in Headers */,
47942D72237CC40B00DEFAE3 /* OpeningHours.h in Headers */,
47942D6B237CC3D600DEFAE3 /* PlacePageData.h in Headers */,
47D609DC234FE625008ECC47 /* MWMBookmarksObserver.h in Headers */,
Expand Down Expand Up @@ -567,6 +586,7 @@
9940622123EAC57900493D1A /* ElevationHeightPoint.m in Sources */,
47EEAFF42350CEDB005CF316 /* AppInfo.mm in Sources */,
47E8163623B1889C008FD836 /* MWMStorage.mm in Sources */,
EDC4E3512C5D222D009286A2 /* RecentlyDeletedCategory.mm in Sources */,
47CA68DE2502022400671019 /* MWMBookmark.mm in Sources */,
9957FAE9237AE5B000855F48 /* Logger.mm in Sources */,
);
Expand Down
13 changes: 9 additions & 4 deletions iphone/CoreApi/CoreApi/Bookmarks/MWMBookmarksManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
@class MWMBookmarksSection;
@class MWMCarPlayBookmarkObject;
@class MWMTrack;
@class RecentlyDeletedCategory;
@class UIColor;

NS_ASSUME_NONNULL_BEGIN
Expand All @@ -26,14 +27,18 @@ typedef void (^SearchBookmarksCompletionBlock)(NSArray<MWMBookmark *> *bookmarks
typedef void (^SortBookmarksCompletionBlock)(NSArray<MWMBookmarksSection *> * _Nullable sortedSections);
typedef void (^SharingResultCompletionHandler)(MWMBookmarksShareStatus status, NSURL * _Nullable urlToALocalFile);

@protocol RecentlyDeletedCategoriesManager <NSObject>
- (uint64_t)recentlyDeletedCategoriesCount;
- (NSArray<RecentlyDeletedCategory *> *)getRecentlyDeletedCategories;
- (void)deleteRecentlyDeletedCategoryAtURLs:(NSArray<NSURL *> *)urls;
- (void)recoverRecentlyDeletedCategoriesAtURLs:(NSArray<NSURL *> *)urls;
@end

NS_SWIFT_NAME(BookmarksManager)
@interface MWMBookmarksManager : NSObject
@interface MWMBookmarksManager : NSObject<BookmarksObservable, RecentlyDeletedCategoriesManager>

+ (MWMBookmarksManager *)sharedManager;

- (void)addObserver:(id<MWMBookmarksObserver>)observer;
- (void)removeObserver:(id<MWMBookmarksObserver>)observer;

- (BOOL)areBookmarksLoaded;
- (void)loadBookmarks;
- (void)loadBookmarkFile:(NSURL *)url;
Expand Down
34 changes: 33 additions & 1 deletion iphone/CoreApi/CoreApi/Bookmarks/MWMBookmarksManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#import "MWMBookmarkGroup.h"
#import "MWMCarPlayBookmarkObject.h"
#import "MWMTrack+Core.h"
#import "RecentlyDeletedCategory+Core.h"

#include "Framework.h"

Expand Down Expand Up @@ -355,7 +356,7 @@ - (void)setCatalogCategoriesVisible:(BOOL)isVisible {

- (void)deleteCategory:(MWMMarkGroupID)groupId
{
self.bm.GetEditSession().DeleteBmCategory(groupId);
self.bm.GetEditSession().DeleteBmCategory(groupId, false /* move to the Trash */);
[self loopObservers:^(id<MWMBookmarksObserver> observer) {
if ([observer respondsToSelector:@selector(onBookmarksCategoryDeleted:)])
[observer onBookmarksCategoryDeleted:groupId];
Expand Down Expand Up @@ -787,6 +788,37 @@ - (void)setCategory:(MWMMarkGroupID)groupId authorType:(MWMBookmarkGroupAuthorTy
}
}

// MARK: - RecentlyDeletedCategoriesManager
- (uint64_t)recentlyDeletedCategoriesCount {
return self.bm.GetRecentlyDeletedCategoriesCount();
}

- (NSArray<RecentlyDeletedCategory *> *)getRecentlyDeletedCategories {
auto const categoriesCollection = self.bm.GetRecentlyDeletedCategories();
NSMutableArray<RecentlyDeletedCategory *> * recentlyDeletedCategories = [[NSMutableArray alloc] initWithCapacity:categoriesCollection->size()];

for (auto const & [filePath, categoryPtr] : * categoriesCollection) {
ASSERT(categoryPtr, ("Recently deleted category shouldn't be nil."));
RecentlyDeletedCategory * category = [[RecentlyDeletedCategory alloc] initWithCategoryData:categoryPtr->m_categoryData filePath:filePath];
[recentlyDeletedCategories addObject:category];
}
return recentlyDeletedCategories;
}

- (void)deleteRecentlyDeletedCategoryAtURLs:(NSArray<NSURL *> *)urls {
std::vector<std::string> filePaths;
for (NSURL * url in urls)
filePaths.push_back(url.filePathURL.path.UTF8String);
self.bm.DeleteRecentlyDeletedCategoriesAtPaths(filePaths);
}

- (void)recoverRecentlyDeletedCategoriesAtURLs:(NSArray<NSURL *> *)urls {
std::vector<std::string> filePaths;
for (NSURL * url in urls)
filePaths.push_back(url.filePathURL.path.UTF8String);
self.bm.RecoverRecentlyDeletedCategoriesAtPaths(filePaths);
}

#pragma mark - Helpers

- (void)loopObservers:(void (^)(id<MWMBookmarksObserver> observer))block
Expand Down
6 changes: 6 additions & 0 deletions iphone/CoreApi/CoreApi/Bookmarks/MWMBookmarksObserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ NS_SWIFT_NAME(BookmarksObserver)
- (void)onBookmarksFileLoadSuccess;
- (void)onBookmarksFileLoadError;
- (void)onBookmarksCategoryDeleted:(MWMMarkGroupID)groupId;
- (void)onRecentlyDeletedBookmarksCategoriesChanged;
- (void)onBookmarkDeleted:(MWMMarkID)bookmarkId;
@end

@protocol BookmarksObservable<NSObject>
- (void)addObserver:(id<MWMBookmarksObserver>)observer;
- (void)removeObserver:(id<MWMBookmarksObserver>)observer;
@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#import "RecentlyDeletedCategory.h"

#include "kml/types.hpp"

@interface RecentlyDeletedCategory (Core)

- (instancetype)initWithCategoryData:(kml::CategoryData)data filePath:(std::string const &)filePath;

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface RecentlyDeletedCategory : NSObject

@property(nonatomic, readonly) NSString * title;
@property(nonatomic, readonly) NSURL * fileURL;
@property(nonatomic, readonly) NSDate * deletionDate;

- (instancetype)initTitle:(NSString *)title fileURL:(NSURL *)fileURL deletionDate:(NSDate *)deletionDate;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#import "RecentlyDeletedCategory+Core.h"

#include <map/bookmark_helpers.hpp>
#include <platform/platform_ios.h>

@implementation RecentlyDeletedCategory

- (instancetype)initTitle:(NSString *)title fileURL:(NSURL *)fileURL deletionDate:(NSDate *)deletionDate {
self = [super init];
if (self) {
_title = title;
_fileURL = fileURL;
_deletionDate = deletionDate;
}
return self;
}

@end

@implementation RecentlyDeletedCategory (Core)

- (instancetype)initWithCategoryData:(kml::CategoryData)data filePath:(std::string const &)filePath {
self = [super init];
if (self) {
auto const name = GetPreferredBookmarkStr(data.m_name);
_title = [NSString stringWithCString:name.c_str() encoding:NSUTF8StringEncoding];
auto const pathString = [NSString stringWithCString:filePath.c_str() encoding:NSUTF8StringEncoding];
_fileURL = [NSURL fileURLWithPath:pathString];
NSTimeInterval creationTime = Platform::GetFileCreationTime(filePath);
_deletionDate = [NSDate dateWithTimeIntervalSince1970:creationTime];
}
return self;
}

@end
1 change: 1 addition & 0 deletions iphone/CoreApi/CoreApi/CoreApi-swift.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ FOUNDATION_EXPORT const unsigned char CoreApiVersionString[];
#import <CoreApi/MWMStorage.h>
#import <CoreApi/MWMTrack.h>
#import <CoreApi/MWMTypes.h>
#import <CoreApi/RecentlyDeletedCategory.h>

#pragma mark - Place Page

Expand Down

0 comments on commit f3d1cc6

Please sign in to comment.