Skip to content

Commit

Permalink
save tagdata in icloud storage (#195)
Browse files Browse the repository at this point in the history
* file provider add tag

* fix crash in file,app

* save tagData to NSUserDefaults

* code refactor

* save tagdata in icloud storage

code refactor

code refactor
  • Loading branch information
lilthree authored and poetwang committed Dec 8, 2017
1 parent 035ea5f commit 77e1364
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 12 deletions.
4 changes: 4 additions & 0 deletions Pod/Classes/SeafConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ BOOL SeafServerTrustIsValid(SecTrustRef _Nonnull serverTrust);
- (void)refreshRepoPassowrds;
- (void)clearRepoPasswords;

// fileProvider tagData
- (void)saveFileProviderTagData:(NSData * _Nullable)tagData withItemIdentifier:(NSString * _Nullable)itemId;
- (NSData * _Nullable)loadFileProviderTagDataWithItemIdentifier:(NSString * _Nullable)itemId;

+ (AFHTTPRequestSerializer <AFURLRequestSerialization> * _Nonnull)requestSerializer;

@end
65 changes: 65 additions & 0 deletions Pod/Classes/SeafConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#define KEY_STARREDFILES @"STARREDFILES"
#define KEY_CONTACTS @"CONTACTS"
#define TAGDATA @"TagData"

static SecTrustRef AFUTTrustWithCertificate(SecCertificateRef certificate) {
NSArray *certs = [NSArray arrayWithObject:(__bridge id)(certificate)];
Expand Down Expand Up @@ -110,6 +111,7 @@ @interface SeafConnection ()
@property (readonly) id<SeafCacheProvider> cacheProvider;

@property (readonly) NSString *platformVersion;
@property (readonly) NSString *tagDataKey;

@property (readwrite, nonatomic, getter=isFirstTimeSync) BOOL firstTimeSync;

Expand All @@ -127,6 +129,7 @@ @implementation SeafConnection
@synthesize localUploadDir = _localUploadDir;
@synthesize platformVersion = _platformVersion;
@synthesize accountIdentifier = _accountIdentifier;
@synthesize tagDataKey = _tagDataKey;

- (id)initWithUrl:(NSString *)url cacheProvider:(id<SeafCacheProvider>)cacheProvider
{
Expand All @@ -147,6 +150,8 @@ - (id)initWithUrl:(NSString *)url cacheProvider:(id<SeafCacheProvider>)cacheProv
_inAutoSync = false;
_inContactsSync = false;
_cacheProvider = cacheProvider;

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateKeyValuePairs:) name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification object:[NSUbiquitousKeyValueStore defaultStore]];
}
return self;
}
Expand Down Expand Up @@ -488,6 +493,13 @@ - (NSString *)uniqueUploadDir
return [SeafStorage uniqueDirUnder:self.localUploadDir];
}

- (NSString *)tagDataKey {
if (!_tagDataKey) {
_tagDataKey = [NSString stringWithFormat:@"%@/%@",TAGDATA,self.accountIdentifier];
}
return _tagDataKey;
}

- (void)saveRepo:(NSString *)repoId password:(NSString *)password
{
Debug("save repo %@ password %@", repoId, password);
Expand Down Expand Up @@ -666,6 +678,7 @@ - (void)clearAccount
[SeafStorage.sharedObject removeObjectForKey:_address];
[SeafStorage.sharedObject removeObjectForKey:self.accountIdentifier];
[SeafStorage.sharedObject removeObjectForKey:[NSString stringWithFormat:@"%@/settings", self.accountIdentifier]];
[SeafStorage.sharedObject removeObjectForKey:_tagDataKey];

NSString *path = [self certPathForHost:[self host]];
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
Expand Down Expand Up @@ -1931,4 +1944,56 @@ - (void)clearAccountCache
[[NSNotificationCenter defaultCenter] postNotificationName:@"clearCache" object:nil];
[self clearUploadCache];
}

// fileProvider tagData
- (void)saveFileProviderTagData:(NSData*)tagData withItemIdentifier:(NSString*)itemId {
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:[SeafStorage.sharedObject objectForKey:self.tagDataKey]];
if (tagData) {
[dict setObject:tagData forKey:itemId];
} else {
[dict removeObjectForKey:itemId];
}

[SeafStorage.sharedObject setObject:dict forKey:self.tagDataKey];
// Save to iCloud
[self performSelectorInBackground:@selector(saveTagDataToICloudWithObject:) withObject:dict];
}

- (void)saveTagDataToICloudWithObject:(id)object{
NSUbiquitousKeyValueStore *store = [NSUbiquitousKeyValueStore defaultStore];
[store setObject:object forKey:self.tagDataKey];
[store synchronize];
}

- (NSData*)loadFileProviderTagDataWithItemIdentifier:(NSString*)itemId {
NSDictionary *dict = [SeafStorage.sharedObject objectForKey:self.tagDataKey];
if (dict) {
return [dict objectForKey:itemId];
} else {
return nil;
}
}

- (void)updateKeyValuePairs:(NSNotification*)notification {
if ([notification.userInfo objectForKey:NSUbiquitousKeyValueStoreChangeReasonKey]) {
NSInteger changeReason = [[notification.userInfo objectForKey:NSUbiquitousKeyValueStoreChangeReasonKey] integerValue];
if (changeReason == NSUbiquitousKeyValueStoreServerChange || changeReason == NSUbiquitousKeyValueStoreInitialSyncChange) {
NSArray *changeKeys = [notification.userInfo objectForKey:NSUbiquitousKeyValueStoreChangedKeysKey];
@synchronized (changeKeys) {
NSUbiquitousKeyValueStore *store = [NSUbiquitousKeyValueStore defaultStore];
for (NSString *key in changeKeys) {
if ([key isEqualToString:self.tagDataKey]) {
NSDictionary *dict = [store objectForKey:key];
[SeafStorage.sharedObject setObject:dict forKey:self.tagDataKey];
}
}
}
}
}
}

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification object:[NSUbiquitousKeyValueStore defaultStore]];
}

@end
1 change: 0 additions & 1 deletion Pod/Classes/SeafStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,4 @@
- (void)chooseCertFrom:(NSDictionary *)dict handler:(void (^)(CFDataRef persistentRef, SecIdentityRef identity)) completeHandler from:(UIViewController *)c;
- (NSURLCredential *)getCredentialForKey:(NSData *)key;


@end
15 changes: 9 additions & 6 deletions SeafFileProvider/FileProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,15 @@ - (void)deleteItemWithIdentifier:(NSFileProviderItemIdentifier)itemIdentifier
}];
}

- (void)setTagData:(NSData *)tagData forItemIdentifier:(NSFileProviderItemIdentifier)itemIdentifier completionHandler:(void (^)(NSFileProviderItem _Nullable, NSError * _Nullable))completionHandler
{
Debug("itemIdentifier: %@, tagData:%@", itemIdentifier, tagData);
SeafItem *item = [[SeafItem alloc] initWithItemIdentity:itemIdentifier];
[item setTagData:tagData];
SeafProviderItem *tagedItem = [[SeafProviderItem alloc] initWithSeafItem:item];
completionHandler(tagedItem, nil);
}

/*
- (void)trashItemWithIdentifier:(NSFileProviderItemIdentifier)itemIdentifier
completionHandler:(void (^)(NSFileProviderItem _Nullable trashedItem, NSError * _Nullable error))completionHandler
Expand All @@ -352,12 +361,6 @@ - (void)setLastUsedDate:(nullable NSDate *)lastUsedDate
}
- (void)setTagData:(nullable NSData *)tagData
forItemIdentifier:(NSFileProviderItemIdentifier)itemIdentifier
completionHandler:(void(^)(NSFileProviderItem _Nullable taggedItem, NSError * _Nullable error))completionHandler
{
}
- (void)setFavoriteRank:(nullable NSNumber *)favoriteRank
forItemIdentifier:(NSFileProviderItemIdentifier)itemIdentifier
completionHandler:(void (^)(NSFileProviderItem _Nullable favoriteItem, NSError * _Nullable error))completionHandler
Expand Down
6 changes: 6 additions & 0 deletions SeafFileProvider/SeafEnumerator.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ - (void)enumerateItemsForObserver:(id<NSFileProviderEnumerationObserver>)observe
[observer finishEnumeratingUpToPage:nil];
return;
}

if (_item.isFile) {
[observer didEnumerateItems:@[[[SeafProviderItem alloc] initWithSeafItem:_item]]];
[observer finishEnumeratingUpToPage:nil];
return;
}

SeafDir *dir = (SeafDir *)[_item toSeafObj];
if (dir.hasCache) {
Expand Down
2 changes: 1 addition & 1 deletion SeafFileProvider/SeafItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@property (readonly) NSString *repoId;
@property (readonly) NSString *path; //folder path
@property (readonly) NSString *filename;

@property (readwrite) NSData *tagData;

@property (readonly) NSString *name;
@property (readonly) SeafConnection *conn;
Expand Down
13 changes: 13 additions & 0 deletions SeafFileProvider/SeafItem.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ @implementation SeafItem
@synthesize repoId = _repoId;
@synthesize path = _path;
@synthesize filename = _filename;
@synthesize tagData = _tagData;

@synthesize conn = _conn;
@synthesize name = _name;
Expand Down Expand Up @@ -186,6 +187,18 @@ - (SeafBase *)toSeafObj
}
}

- (void)setTagData:(NSData *)tagData {
_tagData = tagData;
[self.conn saveFileProviderTagData:_tagData withItemIdentifier:_itemIdentifier];
}

- (NSData *)tagData {
if (!_tagData) {
_tagData = [self.conn loadFileProviderTagDataWithItemIdentifier:_itemIdentifier];
}
return _tagData;
}

+ (SeafItem *)fromAccount:(SeafConnection *)conn
{
return [[SeafItem alloc] initWithServer:conn.address username:conn.username repo:nil path:nil filename:nil];
Expand Down
4 changes: 4 additions & 0 deletions SeafFileProvider/SeafProviderItem.m
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,8 @@ - (BOOL)isUploading
return false;
}

- (NSData *)tagData {
return _item.tagData;
}

@end
8 changes: 4 additions & 4 deletions seafile/SeafSyncInfoCell.xib
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="1111" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="AG8-he-8Ay">
<rect key="frame" x="64" y="58.5" width="22.5" height="14.5"/>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="AG8-he-8Ay">
<rect key="frame" x="64" y="58.5" width="0.0" height="14.5"/>
<constraints>
<constraint firstAttribute="height" constant="14.5" id="3iy-FE-a4k"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="12"/>
<color key="textColor" white="0.33333333333333331" alpha="1" colorSpace="calibratedWhite"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="1111" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Fkw-Kf-9WT">
<rect key="frame" x="94.5" y="58" width="22.5" height="14.5"/>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Fkw-Kf-9WT">
<rect key="frame" x="72" y="58" width="0.0" height="14.5"/>
<constraints>
<constraint firstAttribute="height" constant="14.5" id="Vqa-Hx-gaH"/>
</constraints>
Expand Down
2 changes: 2 additions & 0 deletions seafile/seafile.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<array>
<string>iCloud.$(CFBundleIdentifier)</string>
</array>
<key>com.apple.developer.ubiquity-kvstore-identifier</key>
<string>$(TeamIdentifierPrefix)$(CFBundleIdentifier)</string>
<key>com.apple.security.application-groups</key>
<array>
<string>group.com.seafile.seafilePro</string>
Expand Down

0 comments on commit 77e1364

Please sign in to comment.