diff --git a/demo/index.ts b/demo/index.ts index 51bfdae5..36729e73 100644 --- a/demo/index.ts +++ b/demo/index.ts @@ -9,4 +9,5 @@ export default [ 'src/ImageDemo', 'src/ModalDemo', 'src/NetworkDemo', + 'src/StorageDemo', ] \ No newline at end of file diff --git a/demo/src/StorageDemo.ts b/demo/src/StorageDemo.ts new file mode 100644 index 00000000..36d7586a --- /dev/null +++ b/demo/src/StorageDemo.ts @@ -0,0 +1,60 @@ +import { storage, Panel, scroller, vlayout, text, layoutConfig, LayoutSpec, Color, gravity, IVLayout, Group, IText, modal, Text, log, loge } from "doric"; +import { colors, label } from "./utils"; +const storedKey = 'StoredKey' +const zone = 'StorageDemo' +@Entry +class StorageDemo extends Panel { + stored!: Text + + update() { + storage(context).getItem(storedKey).then(e => { + this.stored.text = '12345' + loge('set Text') + modal(context).toast('current in then:' + this.stored.isDirty()) + }) + } + + build(root: Group) { + scroller(vlayout([ + text({ + text: "Storage Demo", + layoutConfig: layoutConfig().w(LayoutSpec.AT_MOST), + textSize: 30, + textColor: Color.WHITE, + bgColor: colors[1], + textAlignment: gravity().center(), + height: 50, + }), + label('Stored'), + text({ + layoutConfig: layoutConfig().w(LayoutSpec.AT_MOST), + textSize: 20, + textColor: Color.WHITE, + bgColor: colors[3], + textAlignment: gravity().center(), + height: 50, + }).also(it => this.stored = it), + label('store a value').apply({ + width: 200, + height: 50, + bgColor: colors[0], + textSize: 30, + textColor: Color.WHITE, + layoutConfig: layoutConfig().exactly(), + onClick: () => { + storage(context).setItem(storedKey, 'This is my stored value').then(e => { + log('Stored') + this.update() + }) + }, + } as IText), + ]).apply({ + layoutConfig: layoutConfig().atmost().h(LayoutSpec.WRAP_CONTENT), + gravity: gravity().center(), + space: 10, + } as IVLayout)).apply({ + layoutConfig: layoutConfig().atmost(), + }).in(root) + } + +} \ No newline at end of file diff --git a/iOS/Pod/Classes/Plugin/DoricStoragePlugin.m b/iOS/Pod/Classes/Plugin/DoricStoragePlugin.m index 2564a072..c0f00c7d 100644 --- a/iOS/Pod/Classes/Plugin/DoricStoragePlugin.m +++ b/iOS/Pod/Classes/Plugin/DoricStoragePlugin.m @@ -20,17 +20,26 @@ #import "DoricStoragePlugin.h" #import "YYDiskCache.h" -static NSString *doric_prefix = @"pref_doric"; +static NSString *doric_prefix = @"pref"; @interface DoricStoragePlugin () @property(atomic, strong) NSMutableDictionary *cachedMap; @property(nonatomic, strong) YYDiskCache *defaultCache; +@property(nonatomic, copy) NSString *basePath; @end @implementation DoricStoragePlugin +- (instancetype)initWithContext:(DoricContext *)doricContext { + if (self = [super initWithContext:doricContext]) { + _basePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] + stringByAppendingPathComponent:@"doric"]; + } + return self; +} + - (YYDiskCache *)defaultCache { if (!_defaultCache) { - _defaultCache = [[YYDiskCache alloc] initWithPath:doric_prefix]; + _defaultCache = [[YYDiskCache alloc] initWithPath:[self.basePath stringByAppendingPathComponent:doric_prefix]]; } return _defaultCache; } @@ -40,7 +49,8 @@ - (YYDiskCache *)getDiskCache:(NSString *)zone { if (zone) { diskCache = self.cachedMap[zone]; if (!diskCache) { - diskCache = [[YYDiskCache alloc] initWithPath:[NSString stringWithFormat:@"%@_%@", doric_prefix, zone]]; + diskCache = [[YYDiskCache alloc] initWithPath:[self.basePath + stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%@", doric_prefix, zone]]]; self.cachedMap[zone] = diskCache; } } else { diff --git a/js-framework/src/util/nativeModules.ts b/js-framework/src/util/nativeModules.ts index 7f1c8448..fb4ce699 100644 --- a/js-framework/src/util/nativeModules.ts +++ b/js-framework/src/util/nativeModules.ts @@ -154,10 +154,10 @@ export function network(context: BridgeContext) { export function storage(context: BridgeContext) { return { setItem: (key: string, value: string, zone?: string) => { - return context.storage.store({ key, value, zone }) + return context.storage.setItem({ key, value, zone }) }, getItem: (key: string, zone?: string) => { - return context.storage.retreive({ key, zone }) as Promise + return context.storage.getItem({ key, zone }) as Promise }, remove: (key: string, zone?: string) => { return context.storage.remove({ key, zone })