Skip to content
This repository has been archived by the owner on Apr 16, 2023. It is now read-only.

Commit

Permalink
feat: return promise from methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jd1378 committed Jan 31, 2023
1 parent c76add6 commit 3d7a357
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 67 deletions.
11 changes: 6 additions & 5 deletions android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
#Fri Oct 07 11:05:15 IRST 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
Expand Down Expand Up @@ -59,89 +60,99 @@ public void setAlarm(
boolean wakeup,
boolean keepAwake,
boolean allowedInForeground,
String extra) {
String extra,
final Promise promise) {

if (extra == null) {
extra = "";
}
try {
if (extra == null) {
extra = "";
}

long timeEpochMilli = Long.parseLong(epochMilli);
AlarmManager alarmManager = this.getAlarmManager();
long timeEpochMilli = Long.parseLong(epochMilli);
AlarmManager alarmManager = this.getAlarmManager();

PendingIntent pendingIntent =
PendingIntent pendingIntent =
this.createPendingIntentForAlarm(
taskName, timeEpochMilli, wakeup, keepAwake, allowedInForeground, extra);
taskName, timeEpochMilli, wakeup, keepAwake, allowedInForeground, extra);

if (type == null) {
type = "setAndAllowWhileIdle";
}
if (type == null) {
type = "setAndAllowWhileIdle";
}

switch (type) {
case "setAlarmClock":
Class<?> mainActivity;
Intent showIntent = null;
switch (type) {
case "setAlarmClock":
Class<?> mainActivity;
Intent showIntent = null;

try {
String packageName = this.getReactApplicationContext().getPackageName();
Intent launchIntent =
try {
String packageName = this.getReactApplicationContext().getPackageName();
Intent launchIntent =
this.getReactApplicationContext()
.getPackageManager()
.getLaunchIntentForPackage(packageName);
String className = launchIntent.getComponent().getClassName();
mainActivity = Class.forName(className);
showIntent = new Intent(this.getReactApplicationContext(), mainActivity);
showIntent.putExtra("extra", extra);
showIntent.putExtra("fromAlarmModule", true);
} catch (Exception e) {
mainActivity = null;
showIntent = new Intent();
}

int mutabilityFlag = PendingIntent.FLAG_UPDATE_CURRENT;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
mutabilityFlag = PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT;
}
PendingIntent pendingShowIntent =
.getPackageManager()
.getLaunchIntentForPackage(packageName);
String className = launchIntent.getComponent().getClassName();
mainActivity = Class.forName(className);
showIntent = new Intent(this.getReactApplicationContext(), mainActivity);
showIntent.putExtra("extra", extra);
showIntent.putExtra("fromAlarmModule", true);
} catch (Exception e) {
mainActivity = null;
showIntent = new Intent();
}

int mutabilityFlag = PendingIntent.FLAG_UPDATE_CURRENT;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
mutabilityFlag = PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT;
}
PendingIntent pendingShowIntent =
PendingIntent.getActivity(
this.getReactApplicationContext(),
this.createRequestCode(Long.toString(timeEpochMilli)),
showIntent,
mutabilityFlag);
AlarmManagerCompat.setAlarmClock(
this.getReactApplicationContext(),
this.createRequestCode(Long.toString(timeEpochMilli)),
showIntent,
mutabilityFlag);
AlarmManagerCompat.setAlarmClock(
alarmManager, timeEpochMilli, pendingShowIntent, pendingIntent);
break;
case "setExact":
AlarmManagerCompat.setExact(
break;
case "setExact":
AlarmManagerCompat.setExact(
alarmManager,
wakeup ? AlarmManager.RTC_WAKEUP : AlarmManager.RTC,
timeEpochMilli,
pendingIntent);
case "setExactAndAllowWhileIdle":
AlarmManagerCompat.setExactAndAllowWhileIdle(
case "setExactAndAllowWhileIdle":
AlarmManagerCompat.setExactAndAllowWhileIdle(
alarmManager,
wakeup ? AlarmManager.RTC_WAKEUP : AlarmManager.RTC,
timeEpochMilli,
pendingIntent);
default:
case "setAndAllowWhileIdle":
AlarmManagerCompat.setAndAllowWhileIdle(
default:
case "setAndAllowWhileIdle":
AlarmManagerCompat.setAndAllowWhileIdle(
alarmManager,
wakeup ? AlarmManager.RTC_WAKEUP : AlarmManager.RTC,
timeEpochMilli,
pendingIntent);
break;
break;
}
promise.resolve(null);
} catch (Exception e) {
promise.reject(e);
}

}

@ReactMethod
public void cancelAlarm(String taskName, String epochMilli) {
long timeEpochMilli = Long.parseLong(epochMilli);
int requestCode = this.createRequestCode(epochMilli);
Intent intent = new Intent(this.getReactApplicationContext(), AlarmReceiver.class);
public void cancelAlarm(String taskName, String epochMilli, Promise promise) {
try {
long timeEpochMilli = Long.parseLong(epochMilli);

this.getAlarmManager()
this.getAlarmManager()
.cancel(
this.createPendingIntentForAlarm(taskName, timeEpochMilli, false, false, false, ""));
this.createPendingIntentForAlarm(taskName, timeEpochMilli, false, false, false, ""));
promise.resolve(null);
} catch (Exception e) {
promise.reject(e);
}
}

private PendingIntent createPendingIntentForAlarm(
Expand Down
14 changes: 7 additions & 7 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ interface AlarmModuleInterface {
keepAwake: boolean,
allowedInForeground: boolean,
extra: string,
): void;
cancelAlarm(taskName: string, timestamp: string): void;
): Promise<void>;
cancelAlarm(taskName: string, timestamp: string): Promise<void>;
}

export type AlarmOptions = {
Expand All @@ -54,7 +54,7 @@ export type AlarmOptions = {
extra?: string;
};

export function setAlarm(options: AlarmOptions): void {
export async function setAlarm(options: AlarmOptions): Promise<void> {
if (!options) {
throw new Error('AlarmOptions is required.');
}
Expand All @@ -70,7 +70,7 @@ export function setAlarm(options: AlarmOptions): void {
extra = '',
} = options;

AlarmModule.setAlarm(
await AlarmModule.setAlarm(
taskName,
timestamp.toString(),
type,
Expand All @@ -81,16 +81,16 @@ export function setAlarm(options: AlarmOptions): void {
);
}

export function cancelAlarm(
export async function cancelAlarm(
options: Pick<AlarmOptions, 'taskName' | 'timestamp'>,
): void {
): Promise<void> {
if (!options) {
throw new Error('AlarmOptions is required.');
}
required(options, 'taskName');
required(options, 'timestamp');
const {taskName, timestamp} = options;
AlarmModule.cancelAlarm(taskName, timestamp.toString());
await AlarmModule.cancelAlarm(taskName, timestamp.toString());
}

export type TaskArgs = {
Expand Down

0 comments on commit 3d7a357

Please sign in to comment.