Skip to content

Commit

Permalink
feat: new architecture support and region quering
Browse files Browse the repository at this point in the history
  • Loading branch information
Samitha authored and Samitha committed Dec 10, 2023
1 parent 8b6c2b6 commit 9506b1f
Show file tree
Hide file tree
Showing 25 changed files with 15,108 additions and 87 deletions.
64 changes: 46 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,59 @@
# react-native-timezone
# React Native Timezone and Region

A Simple react native module to get Timezone and Region of the Android/iOS device.
[![Maintainability](https://api.codeclimate.com/v1/badges/3713253a365fe6a55615/maintainability)](https://codeclimate.com/github/samitha9125/react-native-timezone/maintainability)

## Installation
A Simple react native module to get Timezone and the Region of the Android/iOS devices.

```sh
npm install react-native-timezone
```
# Motivation

## Usage
For a project of mine, I had to acquire the current selected timezone of the user. But unfortunately I could not find any react native package or react native in-build function which facilitates this. Thus I created a small library.
Also, in v3.0.0 and above, you can access the Region details. More details can be found below.

```js
import { multiply } from 'react-native-timezone';
# Compatibility

// ...
Timezone version 3.0.0 only supports React Native version 0.62.3 and above due to React Native Regular Expression Denial of Service (ReDoS) vulnerability.

const result = await multiply(3, 7);
```
| React native version | Tested | Result |
| -------------------- | ------ | ------ |
| 0.62.3 + |||
| 0.70.0 + |||
| 0.73.0 + |||

# Installation

`npm i react-native-timezone`

## iOS

## Contributing
Do `cd ios/ && pod install`.

See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
# Usage

```javascript
import TimeZone from 'react-native-timezone';

export default function App() {
React.useEffect(() => {
const timezone = Timezone.getTimeZone();
const isAutoTimeZoneEnabled = Timezone.isAutoTimeZoneEnabled();
const telephonyRegion = Timezone.getRegionByTelephony();
const localeRegion = Timezone.getRegionByLocale();
// Update state or use data as needed
}, []);

// Render your component
}
```

## License
Check out the [example]("https://github.com/samitha9125/react-native-timezone/tree/master/example") folder.

MIT
# APIs

---
# API

Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
| API | Description |
| --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| getTimeZone | Android: Returns timezone ID using `java.util.TimeZone.getID()`<br>iOS: Reflects the current system time zone using `localTimeZone` of `NSTimeZone` |
| isAutoTimeZoneEnabled | Returns a boolean indicating if auto timezone is enabled on the device **(Android Only)** |
| getRegionByTelephony | Retrieves the region information based on the telephony (SIM card) of the device. Returns `undefined` if the device has no SIM card. |
| getRegionByLocale | Retrieves the region information based on the device's locale settings |
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def supportsNamespace() {

android {
if (supportsNamespace()) {
namespace "com.timezone"
namespace "com.samitha.rn.timezone"

sourceSets {
main {
Expand Down Expand Up @@ -107,6 +107,6 @@ if (isNewArchitectureEnabled()) {
react {
jsRootDir = file("../src/")
libraryName = "Timezone"
codegenJavaPackageName = "com.timezone"
codegenJavaPackageName = "com.samitha.rn.timezone"
}
}
2 changes: 1 addition & 1 deletion android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.timezone">
package="com.samitha.rn.timezone">
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.samitha.rn.timezone;

import androidx.annotation.NonNull;
import android.os.Build;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMethod;

import java.util.TimeZone;
import java.util.Calendar;
import android.telephony.TelephonyManager;

import android.provider.Settings;
import android.content.ContentResolver;
import android.util.Log;

public class TimezoneModuleImpl {
public static final String NAME = "Timezone";

// Whether the device is set to automatically detect the time zone.
@ReactMethod
public static boolean isAutoTimeZoneEnabled(ReactApplicationContext reactContext) {
ContentResolver resolver = reactContext.getContentResolver();
return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1
? Settings.Global.getInt(resolver, Settings.Global.AUTO_TIME_ZONE, 0)
: Settings.System.getInt(resolver, Settings.System.AUTO_TIME_ZONE, 0)) != 0;
}

@ReactMethod
public static String getTimeZone() {
try {
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
TimeZone zone = calendar.getTimeZone();
return zone.getID();
}catch (Exception e){
return null;
}
}

@ReactMethod
public static String getRegionByTelephony(ReactApplicationContext reactContext) {
try {
TelephonyManager tm = (TelephonyManager)reactContext.getSystemService(reactContext.TELEPHONY_SERVICE);
return tm.getNetworkCountryIso();
}catch (Exception e){
return null;
}

}

@ReactMethod
public static String getRegionByLocale(ReactApplicationContext reactContext) {
try {
return reactContext.getResources().getConfiguration().locale.getCountry();
}catch (Exception e){
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.timezone;
package com.samitha.rn.timezone;

import androidx.annotation.Nullable;

Expand Down
29 changes: 0 additions & 29 deletions android/src/main/java/com/timezone/TimezoneModule.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.timezone;
package com.samitha.rn.timezone;

import com.facebook.react.bridge.ReactApplicationContext;

Expand Down
39 changes: 39 additions & 0 deletions android/src/oldarch/TimezoneModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.samitha.rn.timezone;

import androidx.annotation.NonNull;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.module.annotations.ReactModule;

public class TimezoneModule extends ReactContextBaseJavaModule {
public static final String NAME = TimezoneModuleImpl.NAME;

TimezoneModule(ReactApplicationContext context) {
super(context);
}

@Override
@NonNull
public String getName() {
return TimezoneModuleImpl.NAME;
}

@ReactMethod(isBlockingSynchronousMethod = true)
public boolean isAutoTimeZoneEnabled() {
return TimezoneModuleImpl.isAutoTimeZoneEnabled(getReactApplicationContext());
}
@ReactMethod(isBlockingSynchronousMethod = true)
public String getTimeZone() {
return TimezoneModuleImpl.getTimeZone();
}
@ReactMethod(isBlockingSynchronousMethod = true)
public String getRegionByTelephony() {
return TimezoneModuleImpl.getRegionByTelephony(getReactApplicationContext());
}
@ReactMethod(isBlockingSynchronousMethod = true)
public String getRegionByLocale() {
return TimezoneModuleImpl.getRegionByLocale(getReactApplicationContext());
}
}
13 changes: 0 additions & 13 deletions android/src/oldarch/TimezoneSpec.java

This file was deleted.

3 changes: 3 additions & 0 deletions example/android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ newArchEnabled=false
# Use this property to enable or disable the Hermes JS engine.
# If set to false, you will be using JSC instead.
hermesEnabled=true

# Manually added the Java 17 path.
org.gradle.java.home=/opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk/Contents/Home
2 changes: 2 additions & 0 deletions example/ios/.xcode.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export NODE_BINARY=/opt/homebrew/opt/node@18/bin/node

5 changes: 5 additions & 0 deletions example/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,10 @@ target 'TimezoneExample' do
config[:reactNativePath],
:mac_catalyst_enabled => false
)
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings["ONLY_ACTIVE_ARCH"] = "NO"
end
end
end
end
Loading

0 comments on commit 9506b1f

Please sign in to comment.