feat:storage demo success to run
This commit is contained in:
parent
af53fd9c75
commit
c4d64ff74a
@ -9,4 +9,5 @@ export default [
|
||||
'src/ImageDemo',
|
||||
'src/ModalDemo',
|
||||
'src/NetworkDemo',
|
||||
'src/StorageDemo',
|
||||
]
|
60
demo/src/StorageDemo.ts
Normal file
60
demo/src/StorageDemo.ts
Normal 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)
|
||||
}
|
||||
|
||||
}
|
@ -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 <NSString *, YYDiskCache *> *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 {
|
||||
|
@ -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<string>
|
||||
return context.storage.getItem({ key, zone }) as Promise<string>
|
||||
},
|
||||
remove: (key: string, zone?: string) => {
|
||||
return context.storage.remove({ key, zone })
|
||||
|
Reference in New Issue
Block a user