-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
31f3bd8
commit 6a5a65f
Showing
9 changed files
with
122 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
android/src/main/java/com/reactnativejsiviewhelpers/AndroidAppState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} |