Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added callbacks to save/delete to make the calls "awaitable" #8

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,22 @@ public void get(String key, String bucketName, Callback callback) {
}

@ReactMethod
public void set(String key, String value, String bucketName) {
public void set(String key, String value, String bucketName, Callback callback) {
SharedPreferences preferences = getBucket(bucketName);

SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
editor.commit();
callback.invoke();
}

@ReactMethod
public void remove(String key, String bucketName) {
public void remove(String key, String bucketName, Callback callback) {
SharedPreferences preferences = getBucket(bucketName);

SharedPreferences.Editor editor = preferences.edit();
editor.remove(key);
editor.commit();
callback.invoke();
}
}
10 changes: 8 additions & 2 deletions ios/RNSKBucket.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@
413522D21D18398B007ACD21 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
HEADER_SEARCH_PATHS = "$(SRCROOT)/../../../node_modules/react-native/React/**";
HEADER_SEARCH_PATHS = (
"$(SRCROOT)/../../../node_modules/react-native/React/**",
"$(SRCROOT)/../../../ios/Pods/**",
);
OTHER_LDFLAGS = "-ObjC";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
Expand All @@ -220,7 +223,10 @@
413522D31D18398B007ACD21 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
HEADER_SEARCH_PATHS = "$(SRCROOT)/../../../node_modules/react-native/React/**";
HEADER_SEARCH_PATHS = (
"$(SRCROOT)/../../../node_modules/react-native/React/**",
"$(SRCROOT)/../../../ios/Pods/**",
);
OTHER_LDFLAGS = "-ObjC";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
Expand Down
8 changes: 6 additions & 2 deletions ios/RNSKBucket/RNSKBucket.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ - (NSUserDefaults *)bucketByName:(NSString*)name {

RCT_EXPORT_METHOD(set:(NSString *) key
value:(NSString *) value
bucketName:(NSString*) bucketName)
bucketName:(NSString*) bucketName
callback:(RCTResponseSenderBlock)callback)
{
NSUserDefaults* bucket = [self bucketByName: bucketName];
[bucket setObject:value forKey:key];
[bucket synchronize];
callback(NULL);
}

RCT_EXPORT_METHOD(get:(NSString *) key
Expand All @@ -37,11 +39,13 @@ - (NSUserDefaults *)bucketByName:(NSString*)name {
}

RCT_EXPORT_METHOD(remove:(NSString *) key
bucketName:(NSString*) bucketName)
bucketName:(NSString*) bucketName
callback:(RCTResponseSenderBlock)callback)
{
NSUserDefaults* bucket = [self bucketByName: bucketName];
[bucket removeObjectForKey: key];
[bucket synchronize];
callback(NULL);
}

@end
14 changes: 11 additions & 3 deletions lib/bucket/index.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@ const { RNSKBucket } = NativeModules

const Bucket = {
set: (key, value, groupName) => {
RNSKBucket.set(key, value, groupName)
return new Promise(resolve => {
RNSKBucket.set(key, value, groupName, () => {
resolve()
})
})
},
get: (key, groupName) => {
return new Promise((resolve, reject) => {
return new Promise(resolve => {
RNSKBucket.get(key, groupName, (value) => {
resolve(value)
})
})
},
remove: (key, groupName) => {
RNSKBucket.remove(key, groupName)
return new Promise(resolve => {
RNSKBucket.remove(key, groupName, () => {
resolve()
})
})
}
}

Expand Down
16 changes: 12 additions & 4 deletions lib/bucket/index.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,26 @@ const { RNSKBucket } = NativeModules

const Bucket = {
set: (key, value, groupName) => {
RNSKBucket.set(key, value, groupName)
return new Promise(resolve => {
RNSKBucket.set(key, value, groupName, () => {
resolve()
})
})
},
get: (key, groupName) => {
return new Promise((resolve, reject) => {
return new Promise(resolve => {
RNSKBucket.get(key, groupName, (value) => {
resolve(value)
})
})
},
remove: (key, groupName) => {
RNSKBucket.remove(key, groupName)
return new Promise(resolve => {
RNSKBucket.remove(key, groupName, () => {
resolve()
})
})
}
}
};

export default Bucket
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"homepage": "https://github.com/alexkuttig/react-native-swiss-knife",
"keywords": ["react-native", "react-component"],
"author": "Ali Najafizadeh",
"license": "MIT"
Expand Down
19 changes: 19 additions & 0 deletions react-native-swiss-knife.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'json'

package = JSON.parse(File.read(File.join(__dir__, 'package.json')))

Pod::Spec.new do |s|
s.name = "react-native-swiss-knife"
s.version = package['version']
s.summary = package['description']
s.license = package['license']

s.authors = package['author']
s.homepage = package['homepage']
s.platform = :ios, "9.0"

s.source = { :git => "https://github.com/alexkuttig/react-native-swiss-knife.git" }
s.source_files = "ios/**/*.{h,m}"

s.dependency 'React'
end