Skip to content

Commit

Permalink
Merge pull request #5 from jordanbisato/master
Browse files Browse the repository at this point in the history
Update Firebase SDK to 8.10.0 and add methods fetchAndActivate and setMinimumFetchIntervalInSeconds
  • Loading branch information
hansemannn authored Dec 25, 2021
2 parents 604c918 + e6ce2b2 commit f3e03b2
Show file tree
Hide file tree
Showing 34 changed files with 177 additions and 135 deletions.
30 changes: 18 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ thank you!
import TiFirebaseConfig from 'firebase.config';

export default class ConfigManager {

static fetch () {
TiFirebaseConfig.fetch({
callback: event => {
Expand All @@ -51,6 +51,12 @@ export default class ConfigManager {

### Cross Platform Methods

#### `fetchAndActivate(callback)` (iOS / Android)
- `callback` (Function)

#### `setMinimumFetchIntervalInSeconds(minimumFetchInterval)` (iOS / Android)
- `minimumFetchInterval` (Number)

#### `activateFetched()` (iOS / Android)

#### `fetch(parameters)` (iOS / Android)
Expand All @@ -62,6 +68,15 @@ export default class ConfigManager {
- `key` (String)
- `namespace` (String, optional)

#### `getString(key)` -> String //Use for JSON too
- `key` (String)

#### `getBool(key)` -> String
- `key` (String)

#### `getNumber(key)` -> String
- `key` (String)

### iOS-only Methods

#### `objectForKeyedSubscript(keyedSubscript) -> Dictionary`
Expand All @@ -86,16 +101,7 @@ export default class ConfigManager {
#### `defaultValueForKey(parameters) -> Dictionary`
- `key` (String)
- `namespace` (String, optional)

#### `getString(key)` -> String
- `key` (String)

#### `getBool(key)` -> String
- `key` (String)

#### `getNumber(key)` -> String
- `key` (String)


#### `getData(key)` -> String
- `key` (String)

Expand Down Expand Up @@ -125,4 +131,4 @@ appc run -p [ios|android] --build-only

## Legal

This module is Copyright (c) 2017-present by Hans Knöchel. All Rights Reserved.
This module is Copyright (c) 2017-present by Hans Knöchel. All Rights Reserved.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ repositories {
}

dependencies {
implementation 'com.google.firebase:firebase-config:20.0.2'
implementation 'com.google.firebase:firebase-config:21.0.1'
}
47 changes: 0 additions & 47 deletions android/java-sources.txt

This file was deleted.

72 changes: 60 additions & 12 deletions android/src/firebase/config/TitaniumFirebaseConfigModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package firebase.config;

import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollFunction;
Expand All @@ -22,23 +23,61 @@
@Kroll.module(name="TitaniumFirebaseConfig", id="firebase.config")
public class TitaniumFirebaseConfigModule extends KrollModule
{
public static final String PROPERTY_SUCCESS = "success";
public static final String PROPERTY_ERROR = "error";
public static final String PROPERTY_RESULT = "result";
// Methods

@Kroll.method
public void fetch(KrollDict params)
{
final KrollFunction callback = (KrollFunction) params.get("callback");
int expirationDuration = params.optInt("expirationDuration", -1);
int expirationDuration = params.optInt("expirationDuration", 43200); //Using the default minimum fetch interval if no duration is informed

if (expirationDuration != -1) {
FirebaseRemoteConfig.getInstance().fetch(expirationDuration).addOnCompleteListener(task -> {
callback.callAsync(getKrollObject(), new KrollDict());
});
} else {
FirebaseRemoteConfig.getInstance().fetch().addOnCompleteListener(task -> {
callback.callAsync(getKrollObject(), new KrollDict());
});
}
FirebaseRemoteConfig.getInstance().fetch(expirationDuration).addOnCompleteListener(task -> {
if (callback != null) {
KrollDict result = new KrollDict();
if (task.isSuccessful()) {
result.put(PROPERTY_SUCCESS, true);
result.put(PROPERTY_RESULT, task.getResult());
} else {
result.put(PROPERTY_SUCCESS, false);
if (task.getException() != null) {
result.put(PROPERTY_ERROR, task.getException().getMessage());
}
}
callback.callAsync(getKrollObject(), result);
}
});
}

@Kroll.method
public void fetchAndActivate(KrollFunction callback)
{
FirebaseRemoteConfig.getInstance().fetchAndActivate().addOnCompleteListener(task -> {
if (callback != null) {
KrollDict result = new KrollDict();
if (task.isSuccessful()) {
result.put(PROPERTY_SUCCESS, true);
result.put(PROPERTY_RESULT, task.getResult());
} else {
result.put(PROPERTY_SUCCESS, false);
if (task.getException() != null) {
result.put(PROPERTY_ERROR, task.getException().getMessage());
}
}
callback.callAsync(getKrollObject(), result);
}
});
}

@Kroll.method
public void setMinimumFetchIntervalInSeconds(int minimumFetchInterval)
{
FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
.setMinimumFetchIntervalInSeconds(minimumFetchInterval)
.build();
FirebaseRemoteConfig.getInstance().setConfigSettingsAsync(configSettings);
}

@Kroll.method
Expand All @@ -57,7 +96,17 @@ public void activateFetched(KrollFunction callback)
{
FirebaseRemoteConfig.getInstance().activate().addOnCompleteListener(task -> {
if (callback != null) {
callback.callAsync(getKrollObject(), new KrollDict());
KrollDict result = new KrollDict();
if (task.isSuccessful()) {
result.put(PROPERTY_SUCCESS, true);
result.put(PROPERTY_RESULT, task.getResult());
} else {
result.put(PROPERTY_SUCCESS, false);
if (task.getException() != null) {
result.put(PROPERTY_ERROR, task.getException().getMessage());
}
}
callback.callAsync(getKrollObject(), result);
}
});
}
Expand Down Expand Up @@ -100,4 +149,3 @@ public String getData(String key) {
return null; // TODO: Implement once available
}
}

22 changes: 15 additions & 7 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@ import TiFirebaseConfig from 'firebase.config';

const window = Ti.UI.createWindow({ backgroundColor: '#fff' });
window.addEventListener('open', () => {
TiFirebaseConfig.fetch({
callback: () => {
TiFirebaseConfig.activateFetched(); // Activate the fetched values
}
});
TiFirebaseConfig.setDefaults({
stringValue: "Test",
numberValue: 12345.00,
booleanValue: true
});

TiFirebaseConfig.fetchAndActivate(function(e) {
if (e.success) {
Ti.API.info('String value: ' + TiFirebaseConfig.getString('stringValue'));
Ti.API.info('Number value: ' + TiFirebaseConfig.getNumber('numberValue'));
Ti.API.info('Boolean value: ' + TiFirebaseConfig.getBool('boolValue'));
}
});
});

window.open();

setTimeout(() => {
console.warn('String value: ' + TiFirebaseConfig.getString('my_config_string_value'));
}, 2000);
console.warn('String value: ' + TiFirebaseConfig.getString('stringValue'));
}, 2000);
27 changes: 27 additions & 0 deletions ios/Classes/FirebaseConfigModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,33 @@ - (NSNumber *)activateFetched:(id)callback
return nil;
}

- (NSNumber *)fetchAndActivate:(id)callback
{
ENSURE_SINGLE_ARG_OR_NIL(callback, KrollCallback);
[[FIRRemoteConfig remoteConfig] fetchAndActivateWithCompletionHandler:^(FIRRemoteConfigFetchAndActivateStatus status, NSError* _Nullable error)
{
if (error != nil) {
[callback call:@[ @{
@"success" : @NO,
@"error" : error.localizedDescription
}] thisObject:self];
return;
}

[callback call:@[ @{
@"success" : @YES,
@"status" : @(status)
}] thisObject:self];
}];
}

- (void)setMinimumFetchIntervalInSeconds:(NSTimeInterval)minimumFetchInterval
{;
FIRRemoteConfigSettings *remoteConfigSettings = [[FIRRemoteConfigSettings alloc] init];
remoteConfigSettings.minimumFetchInterval = minimumFetchInterval;
[FIRRemoteConfig remoteConfig].configSettings = remoteConfigSettings;
}

- (void)fetch:(id)arguments
{
ENSURE_SINGLE_ARG(arguments, NSDictionary);
Expand Down
2 changes: 1 addition & 1 deletion ios/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 6.0.0
version: 7.0.0
apiversion: 2
architectures: arm64 x86_64
description: titanium-firebase-config
Expand Down
Loading

0 comments on commit f3e03b2

Please sign in to comment.