Skip to content

Commit

Permalink
iOS. + Key-Value-Store methods
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-tsx committed Apr 14, 2023
1 parent 006eec3 commit ee38534
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 2 deletions.
1 change: 1 addition & 0 deletions ios/RNCloudFs.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@

@interface RNCloudFs : NSObject <RCTBridgeModule>
@property (nonatomic, strong) NSMetadataQuery *query;
@property (nonatomic, strong) NSUbiquitousKeyValueStore *iCloudStore;
@end
84 changes: 83 additions & 1 deletion ios/RNCloudFs.m
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,13 @@ - (void)getIcloudDocumentRecurse

NSMetadataItem *item = [query resultAtIndex:0];
resolver(@{
@"downloadingStatus": [item valueForAttribute:NSMetadataUbiquitousItemDownloadingStatusKey]
@"fileStatus": @{
@"downloading": [item valueForAttribute:NSMetadataUbiquitousItemDownloadingStatusKey],
@"isDownloading": [item valueForAttribute:NSMetadataUbiquitousItemIsDownloadingKey],
@"isUploading": [item valueForAttribute:NSMetadataUbiquitousItemIsUploadingKey],
@"percentDownloaded": [item valueForAttribute:NSMetadataUbiquitousItemPercentDownloadedKey],
@"percentUploaded": [item valueForAttribute:NSMetadataUbiquitousItemPercentUploadedKey]
}
});
}];

Expand Down Expand Up @@ -570,4 +576,80 @@ - (BOOL)startFileDownloadIfNotAvailable:(NSMetadataItem*)item {
});
}


//// # NSUbiquitousKeyValueStore


RCT_EXPORT_METHOD(getKeyValueStoreObject
:(NSString *)key
:(RCTPromiseResolveBlock)resolver
:(RCTPromiseRejectBlock)rejecter
) {
NSUbiquitousKeyValueStore *iCloudStore = [NSUbiquitousKeyValueStore defaultStore];

NSString *value = [iCloudStore objectForKey:key];
if (value) {
resolver(value);
} else {
resolver(nil);
}
}

RCT_EXPORT_METHOD(getKeyValueStoreObjectDetails
:(NSString *)key
:(RCTPromiseResolveBlock)resolver
:(RCTPromiseRejectBlock)rejecter
) {
NSUbiquitousKeyValueStore *iCloudStore = [NSUbiquitousKeyValueStore defaultStore];

NSString *value = [iCloudStore objectForKey:key];
if (value) {
resolver(@{
@"valueLength": @(value.length)
});
} else {
resolver(nil);
}
}

RCT_EXPORT_METHOD(putKeyValueStoreObject
:(NSDictionary *)options
:(RCTPromiseResolveBlock)resolver
:(RCTPromiseRejectBlock)rejecter
) {
NSString *key = [options objectForKey:@"key"];
NSString *value = [options objectForKey:@"value"];

NSUbiquitousKeyValueStore *iCloudStore = [NSUbiquitousKeyValueStore defaultStore];
[iCloudStore setObject:value forKey:key];

resolver(nil);
}

// See: https://developer.apple.com/documentation/foundation/nsubiquitouskeyvaluestore/1415989-synchronize
RCT_EXPORT_METHOD(syncKeyValueStoreData
:(RCTPromiseResolveBlock)resolver
:(RCTPromiseRejectBlock)rejecter
) {
NSUbiquitousKeyValueStore *iCloudStore = [NSUbiquitousKeyValueStore defaultStore];
bool done = [iCloudStore synchronize];

resolver(@(done));
}

RCT_EXPORT_METHOD(removeKeyValueStoreObject
:(NSString *)key
:(RCTPromiseResolveBlock)resolver
:(RCTPromiseRejectBlock)rejecter
) {
NSUbiquitousKeyValueStore *iCloudStore = [NSUbiquitousKeyValueStore defaultStore];

[iCloudStore removeObjectForKey:key];

bool done = [iCloudStore synchronize];

resolver(@(done));
}


@end
28 changes: 27 additions & 1 deletion types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ export interface ICloudFileDetails extends CloudFileDetailsBase {
}

export interface ICloudDocumentDetails {
downloadingStatus: number;
fileStatus: {
downloading: string,
isDownloading: boolean,
isUploading: boolean,
percentDownloaded: number,
percentUploaded: number,
}
}

export interface TargetPathAndScope {
Expand Down Expand Up @@ -107,6 +113,26 @@ declare const defaultExport: Readonly<{
getIcloudDocument: (options: TargetPathAndScope) => Promise<string | undefined>;

getGoogleDriveDocument: (fileId: string) => Promise<string>;

////// # Key-Value-Store

getKeyValueStoreObject: (key: string) => Promise<string | undefined>;

getKeyValueStoreObjectDetails: (key: string) => Promise<{
valueLength: number;
} | undefined>;

/**
* (i) Single value size limit is 4KB; Total limit is 64KB
*/
putKeyValueStoreObject: (data: { key: string; value: string }) => Promise<void>;

/**
* See: https://developer.apple.com/documentation/foundation/nsubiquitouskeyvaluestore/1415989-synchronize
*/
syncKeyValueStoreData: () => Promise<boolean>;

removeKeyValueStoreObject: (key: string) => Promise<boolean>;
}>;

type Platform = 'Android' | 'iOS';

0 comments on commit ee38534

Please sign in to comment.