feat: tag every call to disable the idle timer and expose static method for non react-native usage (#31)
add support for tagging calls and native code usage --------- Co-authored-by: Marc Shilling <marcshilling@gmail.com>
This commit is contained in:
parent
9763ef8aa8
commit
bc344737a1
43
README.md
43
README.md
@ -56,14 +56,16 @@ protected List<ReactPackage> getPackages() {
|
||||
|
||||
## Usage
|
||||
|
||||
1. In your React Native javascript code, bring in the native module:
|
||||
### In your React Native javascript code, bring in the native module:
|
||||
|
||||
```javascript
|
||||
import IdleTimerManager from 'react-native-idle-timer';
|
||||
```
|
||||
<br/>
|
||||
|
||||
2. To disable the idle timer on a specific view component:
|
||||
### To disable the idle timer while a certain component is mounted:
|
||||
|
||||
Class component
|
||||
```javascript
|
||||
componentWillMount() {
|
||||
IdleTimerManager.setIdleTimerDisabled(true);
|
||||
@ -73,3 +75,40 @@ componentWillUnmount() {
|
||||
IdleTimerManager.setIdleTimerDisabled(false);
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Function component
|
||||
|
||||
```javascript
|
||||
useEffect(() => {
|
||||
IdleTimerManager.setIdleTimerDisabled(true);
|
||||
|
||||
return () => IdleTimerManager.setIdleTimerDisabled(false);
|
||||
}, [])
|
||||
```
|
||||
<br/>
|
||||
|
||||
### If you have multiple components that are responsible for interacting with the idle timer, you can pass a tag as the second parameter:
|
||||
|
||||
```javascript
|
||||
useEffect(() => {
|
||||
IdleTimerManager.setIdleTimerDisabled(true, "video");
|
||||
|
||||
return () => IdleTimerManager.setIdleTimerDisabled(false, "video");
|
||||
}, [])
|
||||
```
|
||||
<br/>
|
||||
|
||||
### If you need to interact from the native Android or iOS side:
|
||||
|
||||
Android
|
||||
```java
|
||||
IdleTimerManager.activate(activity, "video");
|
||||
IdleTimerManager.deactivate(activity, "video");
|
||||
```
|
||||
|
||||
iOS
|
||||
```objectivec
|
||||
[IdleTimerManager activate:@"video"];
|
||||
[IdleTimerManager deactivate:@"video"];
|
||||
```
|
||||
|
||||
@ -1,20 +1,22 @@
|
||||
package com.marcshilling.idletimer;
|
||||
|
||||
import com.facebook.react.bridge.NativeModule;
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.bridge.ReactContext;
|
||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||
import com.facebook.react.bridge.ReactMethod;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.view.WindowManager;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class IdleTimerManager extends ReactContextBaseJavaModule
|
||||
{
|
||||
static final String MODULE_NAME = "IdleTimerManager";
|
||||
|
||||
static final HashSet<String> tags = new HashSet();
|
||||
|
||||
public IdleTimerManager(ReactApplicationContext reactContext) {
|
||||
super(reactContext);
|
||||
}
|
||||
@ -25,22 +27,30 @@ public class IdleTimerManager extends ReactContextBaseJavaModule
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void setIdleTimerDisabled(final boolean disabled) {
|
||||
|
||||
public void setIdleTimerDisabled(final boolean disabled, final String tag) {
|
||||
final Activity activity = this.getCurrentActivity();
|
||||
if (activity != null) {
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (disabled) {
|
||||
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
} else {
|
||||
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
}
|
||||
}
|
||||
if (disabled) {
|
||||
activate(activity, tag);
|
||||
} else {
|
||||
deactivate(activity, tag);
|
||||
}
|
||||
}
|
||||
|
||||
public static void activate(@NotNull final Activity activity, final String tag) {
|
||||
if (tags.isEmpty()) {
|
||||
activity.runOnUiThread(() -> {
|
||||
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
});
|
||||
}
|
||||
tags.add((tag == null ? "" : tag));
|
||||
}
|
||||
|
||||
|
||||
public static void deactivate(@NotNull final Activity activity, final String tag) {
|
||||
if (tags.size() == 1 && tags.contains((tag))) {
|
||||
activity.runOnUiThread(() -> {
|
||||
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
});
|
||||
}
|
||||
tags.remove((tag == null ? "" : tag));
|
||||
}
|
||||
}
|
||||
|
||||
2
index.d.ts
vendored
2
index.d.ts
vendored
@ -1,4 +1,4 @@
|
||||
declare namespace RNIdleTimer {
|
||||
function setIdleTimerDisabled(disabled: boolean): void;
|
||||
function setIdleTimerDisabled(disabled: boolean, tag: string | undefined): void;
|
||||
}
|
||||
export = RNIdleTimer
|
||||
|
||||
4
index.js
4
index.js
@ -2,4 +2,6 @@
|
||||
|
||||
var { NativeModules } = require('react-native')
|
||||
|
||||
module.exports = NativeModules.IdleTimerManager;
|
||||
module.exports.setIdleTimerDisabled = (disabled, tag = "") => {
|
||||
NativeModules.IdleTimerManager.setIdleTimerDisabled(disabled, tag);
|
||||
}
|
||||
|
||||
@ -7,4 +7,7 @@
|
||||
|
||||
@interface IdleTimerManager : NSObject <RCTBridgeModule>
|
||||
|
||||
+ (void)activate:(NSString*)tag;
|
||||
+ (void)deactivate:(NSString*)tag;
|
||||
|
||||
@end
|
||||
|
||||
@ -1,13 +1,44 @@
|
||||
#import "IdleTimerManager.h"
|
||||
|
||||
const static NSMutableSet *tags;
|
||||
|
||||
@implementation IdleTimerManager
|
||||
|
||||
RCT_EXPORT_MODULE();
|
||||
|
||||
RCT_EXPORT_METHOD(setIdleTimerDisabled:(BOOL)disabled) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[UIApplication sharedApplication].idleTimerDisabled = disabled;
|
||||
+ (void) initialize {
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
tags = [NSMutableSet set];
|
||||
});
|
||||
}
|
||||
|
||||
RCT_EXPORT_MODULE();
|
||||
|
||||
RCT_EXPORT_METHOD(setIdleTimerDisabled:(BOOL)disabled tag:(NSString *)tag) {
|
||||
if (disabled) {
|
||||
[IdleTimerManager activate:tag];
|
||||
} else {
|
||||
[IdleTimerManager deactivate:tag];
|
||||
}
|
||||
}
|
||||
|
||||
+ (void)activate:(NSString*)tag {
|
||||
if ([tags count] == 0) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[UIApplication sharedApplication].idleTimerDisabled = YES;
|
||||
});
|
||||
}
|
||||
|
||||
[tags addObject:tag ?: @""];
|
||||
}
|
||||
|
||||
+ (void)deactivate:(NSString*)tag {
|
||||
if ([tags count] == 1 && [tags containsObject:tag]) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[UIApplication sharedApplication].idleTimerDisabled = NO;
|
||||
});
|
||||
}
|
||||
|
||||
[tags removeObject:tag ?: @""];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user