From ee38534accf2a2c091cf43ef5061fec255e95687 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 14 Apr 2023 23:16:10 +0300 Subject: [PATCH] iOS. + Key-Value-Store methods --- ios/RNCloudFs.h | 1 + ios/RNCloudFs.m | 84 ++++++++++++++++++++++++++++++++++++++++++++++++- types.d.ts | 28 ++++++++++++++++- 3 files changed, 111 insertions(+), 2 deletions(-) diff --git a/ios/RNCloudFs.h b/ios/RNCloudFs.h index f6bf5c4..e0e38f2 100644 --- a/ios/RNCloudFs.h +++ b/ios/RNCloudFs.h @@ -7,4 +7,5 @@ @interface RNCloudFs : NSObject @property (nonatomic, strong) NSMetadataQuery *query; +@property (nonatomic, strong) NSUbiquitousKeyValueStore *iCloudStore; @end diff --git a/ios/RNCloudFs.m b/ios/RNCloudFs.m index 614074d..21660a8 100644 --- a/ios/RNCloudFs.m +++ b/ios/RNCloudFs.m @@ -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] + } }); }]; @@ -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 diff --git a/types.d.ts b/types.d.ts index 0f4722e..2cc7182 100644 --- a/types.d.ts +++ b/types.d.ts @@ -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 { @@ -107,6 +113,26 @@ declare const defaultExport: Readonly<{ getIcloudDocument: (options: TargetPathAndScope) => Promise; getGoogleDriveDocument: (fileId: string) => Promise; + + ////// # Key-Value-Store + + getKeyValueStoreObject: (key: string) => Promise; + + 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; + + /** + * See: https://developer.apple.com/documentation/foundation/nsubiquitouskeyvaluestore/1415989-synchronize + */ + syncKeyValueStoreData: () => Promise; + + removeKeyValueStoreObject: (key: string) => Promise; }>; type Platform = 'Android' | 'iOS';