Skip to content

Commit 733ebbb

Browse files
committed
Update lib to use constants
1 parent 6fa59ad commit 733ebbb

File tree

3 files changed

+43
-43
lines changed

3 files changed

+43
-43
lines changed

android/src/main/java/xyz/miron/reactnativehourformat/RNHourFormatModule.java

+18-13
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,42 @@
44
import android.content.Context;
55
import android.content.SharedPreferences;
66

7+
import android.text.format.DateFormat;
8+
79
import com.facebook.react.bridge.ReactApplicationContext;
810
import com.facebook.react.bridge.ReactContextBaseJavaModule;
9-
import com.facebook.react.bridge.ReactMethod;
10-
import com.facebook.react.bridge.Promise;
11-
import android.text.format.DateFormat;
11+
import java.util.Map;
12+
import java.util.HashMap;
1213

1314
public class RNHourFormatModule extends ReactContextBaseJavaModule {
1415

15-
private final ReactApplicationContext reactContext;
16+
private static final String LOCALE = "LOCALE";
17+
private static final String HOUR_FORMAT = "HOUR_FORMAT";
18+
1619

1720
public RNHourFormatModule(ReactApplicationContext reactContext) {
1821
super(reactContext);
19-
this.reactContext = reactContext;
2022
}
2123

2224
@Override
2325
public String getName() {
2426
return "RNHourFormat";
2527
}
2628

27-
@ReactMethod
28-
public void getLocale(Promise promise) {
29-
promise.resolve(getPreferences().getString("locale_override", null));
29+
@Override
30+
public Map<String, Object> getConstants() {
31+
final Map<String, Object> constants = new HashMap<>();
32+
constants.put(LOCALE, this.getLocale());
33+
constants.put(HOUR_FORMAT, this.getHourFormat());
34+
return constants;
3035
}
3136

32-
@ReactMethod
33-
public void getHourFormat(Promise promise) {
34-
promise.resolve(DateFormat.is24HourFormat(getReactApplicationContext()) ? "24" : "12");
37+
private String getLocale() {
38+
SharedPreferences preferences = getReactApplicationContext().getSharedPreferences("react-native", Context.MODE_PRIVATE);
39+
return preferences.getString("locale_override", null);
3540
}
3641

37-
private SharedPreferences getPreferences() {
38-
return getReactApplicationContext().getSharedPreferences("react-native", Context.MODE_PRIVATE);
42+
private String getHourFormat() {
43+
return DateFormat.is24HourFormat(getReactApplicationContext()) ? "24" : "12";
3944
}
4045
}

index.js

+6-18
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,16 @@
11
import { NativeModules } from 'react-native';
22
const { RNHourFormat } = NativeModules;
33

4-
let locale = 'en-US';
5-
RNHourFormat.getLocale().then(result => (locale = result));
6-
7-
let is24hourformat = false;
8-
RNHourFormat.getHourFormat().then(result => (is24hourformat = result === '24'));
9-
104
export class HourFormat {
115
static getLocale() {
12-
return locale;
6+
return RNHourFormat.LOCALE;
137
}
148

15-
static is24HourFormat() {
16-
return is24hourformat;
9+
static getHourFormat() {
10+
return RNHourFormat.HOUR_FORMAT;
1711
}
18-
}
1912

20-
export class HourFormatAsync {
21-
static getLocaleAsync() {
22-
return RNHourFormat.getLocale();
23-
}
24-
25-
static is24HourFormatAsync() {
26-
return RNHourFormat.getHourFormat().then(result => result === "24");
27-
}
13+
static is24HourFormat() {
14+
return RNHourFormat.HOUR_FORMAT === '24';
15+
}
2816
}

ios/RNHourFormat.m

+19-12
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,41 @@
99

1010
@implementation RNHourFormat
1111

12-
RCT_EXPORT_MODULE();
1312

13+
- (dispatch_queue_t)methodQueue
14+
{
15+
return dispatch_get_main_queue();
16+
}
17+
18+
RCT_EXPORT_MODULE();
1419

15-
RCT_REMAP_METHOD(getLocale,
16-
resolver:(RCTPromiseResolveBlock)resolve
17-
rejecter:(RCTPromiseRejectBlock)reject)
20+
- (NSString *)getLocale
1821
{
1922
NSArray *locales = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
20-
if (locales == nil) { reject(@"no_locales", @"No locales", nil); }
21-
if ([locales count] == 0) { reject(@"no_locales", @"No locales", nil); }
23+
if (locales == nil) { return @""; }
24+
if ([locales count] == 0) { return @""; }
2225

2326
NSString* currentLocale = locales[0];
24-
resolve(currentLocale);
27+
return currentLocale;
2528
}
2629

27-
RCT_REMAP_METHOD(getHourFormat,
28-
hourFormatResolve:(RCTPromiseResolveBlock)resolve
29-
hourFormatReject:(RCTPromiseRejectBlock)reject)
30+
- (NSString *)getHourFormat
3031
{
3132
// https://stackoverflow.com/a/11660380
3233
NSString *formatStringForHours = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale currentLocale]];
3334

3435
NSRange containsA = [formatStringForHours rangeOfString:@"a"];
3536
if (containsA.location == NSNotFound) {
36-
resolve(@"24");
37+
return @"24";
3738
} else{
38-
resolve(@"12");
39+
return @"12";
3940
};
4041
}
4142

43+
- (NSDictionary *)constantsToExport
44+
{
45+
return @{ @"LOCALE" : ([self getLocale]),
46+
@"HOUR_FORMAT" : ([self getHourFormat]) };
47+
}
48+
4249
@end

0 commit comments

Comments
 (0)