Skip to content

Commit

Permalink
add useAppState
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeymild committed Mar 20, 2023
1 parent 31f3bd8 commit 6a5a65f
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ React Native Jsi view helpers

```sh
package.json
"react-native-jsi-view-helpers": "sergeymild/react-native-jsi-view-helpers#0.71.1"
"react-native-jsi-view-helpers": "sergeymild/react-native-jsi-view-helpers#0.71.2"
yarn
npx pod-install
```
Expand Down
10 changes: 9 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ buildscript {
}
}

def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["Sheet_kotlinVersion"]

dependencies {
classpath 'com.android.tools.build:gradle:4.2.2'
classpath "com.android.tools.build:gradle:7.2.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

apply plugin: 'com.android.library'
apply plugin: "kotlin-android"

def getExtOrDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['JsiViewHelper_' + name]
Expand Down Expand Up @@ -138,7 +142,11 @@ repositories {
google()
}

def kotlin_version = getExtOrDefault("kotlinVersion")

dependencies {
implementation "com.facebook.react:react-android"
implementation "androidx.lifecycle:lifecycle-common:2.6.0"
implementation "androidx.lifecycle:lifecycle-process:2.6.0"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
2 changes: 2 additions & 0 deletions android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ JsiViewHelper_minSdkVersion=23
JsiViewHelper_targetSdkVersion=33
JsiViewHelper_compileSdkVersion=33
JsiViewHelper_ndkversion=21.4.7075529
JsiViewHelper_lifecycle_common=2.6.0
JsiViewHelper_lifecycle_process=2.6.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.reactnativejsiviewhelpers;

import android.os.Handler;
import android.os.Looper;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.ProcessLifecycleOwner;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.modules.core.DeviceEventManagerModule;

public class AndroidAppState extends ReactContextBaseJavaModule implements DefaultLifecycleObserver {
@NonNull
@Override
public String getName() {
return "AndroidAppState";
}

private static ReactApplicationContext reactContext;

public AndroidAppState(@Nullable ReactApplicationContext reactContext) {
super(reactContext);
AndroidAppState.reactContext = reactContext;
}

@Override
public void initialize() {
super.initialize();
new Handler(Looper.getMainLooper()).post(() -> ProcessLifecycleOwner.get().getLifecycle().addObserver(AndroidAppState.this));
}

@Override
public void onCatalystInstanceDestroy() {
super.onCatalystInstanceDestroy();
new Handler(Looper.getMainLooper()).post(() -> ProcessLifecycleOwner.get().getLifecycle().removeObserver(AndroidAppState.this));
}

@Override
public void onStart(@NonNull LifecycleOwner owner) {
sendEvent("change", "active");
}

@Override
public void onPause(@NonNull LifecycleOwner owner) {
sendEvent("change", "inactive");
}

@Override
public void onStop(@NonNull LifecycleOwner owner) {
sendEvent("change", "background");
}

public void sendEvent(String eventName, String type) {
if (reactContext == null) return;
WritableArray array = Arguments.createArray();
array.pushString(type);
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, array);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class JsiViewHelpersPackage implements ReactPackage {
public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new JsiViewHelpersModule(reactContext));
modules.add(new AndroidAppState(reactContext));
return modules;
}

Expand Down
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ PODS:
- React-jsinspector (0.66.0)
- React-logger (0.66.0):
- glog
- react-native-jsi-view-helpers (0.71.0):
- react-native-jsi-view-helpers (0.71.1):
- React-Core
- React-perflogger (0.66.0)
- React-RCTActionSheet (0.66.0):
Expand Down Expand Up @@ -400,7 +400,7 @@ SPEC CHECKSUMS:
React-jsiexecutor: 6a05173dc0142abc582bd4edd2d23146b8cc218a
React-jsinspector: be95ad424ba9f7b817aff22732eb9b1b810a000a
React-logger: 9a9cd87d4ea681ae929b32ef580638ff1b50fb24
react-native-jsi-view-helpers: 3ec53d2b40371ecee097e52bd1d8dff36f665901
react-native-jsi-view-helpers: 854cf670e3bd23114a7d5d9a57ff130f8a1a0193
React-perflogger: 1f554c2b684e2f484f9edcdfdaeedab039fbaca8
React-RCTActionSheet: 610d5a5d71ab4808734782c8bca6a12ec3563672
React-RCTAnimation: ec6ed97370ace32724c253f29f0586cafcab8126
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-jsi-view-helpers",
"version": "0.71.1",
"version": "0.71.2",
"description": "React Native Jsi view helpers",
"main": "src/index",
"module": "src/index",
Expand Down
2 changes: 2 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type React from 'react';
import { findNodeHandle, NativeModules, Platform, TextStyle } from 'react-native';

export {subscribeOnAppState} from './useAppState'

const LINKING_ERROR =
`The package 'react-native-jsi-view-helpers' doesn't seem to be linked. Make sure: \n\n` +
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
Expand Down
38 changes: 38 additions & 0 deletions src/useAppState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
AppState,
AppStateStatus,
NativeEventEmitter,
NativeModules,
Platform
} from 'react-native'

const AndroidAppState =
Platform.OS === 'android'
? new NativeEventEmitter(NativeModules.AndroidAppState)
: undefined

export class HeadWayAppState {
static addListener(callback: (state: 'active' | 'background') => void) {
if (Platform.OS === 'ios') {
return AppState.addEventListener('change', (state) => {
console.log('🚰 [UseActiveState.change]', state)
if (state === 'active' || state === 'background') callback(state)
})
}
return AndroidAppState!.addListener('change', (state) => {
console.log('🚰 [UseActiveState.change]', state[0])
callback(state[0])
})
}
}

export function subscribeOnAppState(
state: AppStateStatus[],
callback: (state: AppStateStatus) => void
) {
return HeadWayAppState.addListener((nextAppState: AppStateStatus) => {
if (state.includes(nextAppState)) {
callback(nextAppState)
}
})
}

0 comments on commit 6a5a65f

Please sign in to comment.