feat:storage demo success to run

This commit is contained in:
pengfei.zhou 2019-11-22 13:19:54 +08:00
parent af53fd9c75
commit c4d64ff74a
4 changed files with 76 additions and 5 deletions

View File

@ -9,4 +9,5 @@ export default [
'src/ImageDemo', 'src/ImageDemo',
'src/ModalDemo', 'src/ModalDemo',
'src/NetworkDemo', 'src/NetworkDemo',
'src/StorageDemo',
] ]

60
demo/src/StorageDemo.ts Normal file
View File

@ -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)
}
}

View File

@ -20,17 +20,26 @@
#import "DoricStoragePlugin.h" #import "DoricStoragePlugin.h"
#import "YYDiskCache.h" #import "YYDiskCache.h"
static NSString *doric_prefix = @"pref_doric"; static NSString *doric_prefix = @"pref";
@interface DoricStoragePlugin () @interface DoricStoragePlugin ()
@property(atomic, strong) NSMutableDictionary <NSString *, YYDiskCache *> *cachedMap; @property(atomic, strong) NSMutableDictionary <NSString *, YYDiskCache *> *cachedMap;
@property(nonatomic, strong) YYDiskCache *defaultCache; @property(nonatomic, strong) YYDiskCache *defaultCache;
@property(nonatomic, copy) NSString *basePath;
@end @end
@implementation DoricStoragePlugin @implementation DoricStoragePlugin
- (instancetype)initWithContext:(DoricContext *)doricContext {
if (self = [super initWithContext:doricContext]) {
_basePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
stringByAppendingPathComponent:@"doric"];
}
return self;
}
- (YYDiskCache *)defaultCache { - (YYDiskCache *)defaultCache {
if (!_defaultCache) { if (!_defaultCache) {
_defaultCache = [[YYDiskCache alloc] initWithPath:doric_prefix]; _defaultCache = [[YYDiskCache alloc] initWithPath:[self.basePath stringByAppendingPathComponent:doric_prefix]];
} }
return _defaultCache; return _defaultCache;
} }
@ -40,7 +49,8 @@ - (YYDiskCache *)getDiskCache:(NSString *)zone {
if (zone) { if (zone) {
diskCache = self.cachedMap[zone]; diskCache = self.cachedMap[zone];
if (!diskCache) { 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; self.cachedMap[zone] = diskCache;
} }
} else { } else {

View File

@ -154,10 +154,10 @@ export function network(context: BridgeContext) {
export function storage(context: BridgeContext) { export function storage(context: BridgeContext) {
return { return {
setItem: (key: string, value: string, zone?: string) => { 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) => { getItem: (key: string, zone?: string) => {
return context.storage.retreive({ key, zone }) as Promise<string> return context.storage.getItem({ key, zone }) as Promise<string>
}, },
remove: (key: string, zone?: string) => { remove: (key: string, zone?: string) => {
return context.storage.remove({ key, zone }) return context.storage.remove({ key, zone })