Skip to content

Commit 0ba5f64

Browse files
authored
Merge pull request #80 from functionland/registerLifecycleListener
Register lifecycle listener
2 parents 4de9873 + d668099 commit 0ba5f64

File tree

8 files changed

+422
-434
lines changed

8 files changed

+422
-434
lines changed

android/src/main/java/land/fx/fula/FulaModule.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.facebook.react.bridge.Arguments;
1515
import com.facebook.react.bridge.WritableArray;
1616
import com.facebook.react.bridge.ReadableArray;
17+
import com.facebook.react.bridge.LifecycleEventListener;
1718

1819

1920
import org.apache.commons.io.FileUtils;
@@ -183,6 +184,33 @@ private byte[] convertStringToByte(@NonNull String data) {
183184
return convertIntToByte(keyInt);
184185
}
185186

187+
@ReactMethod
188+
public void registerLifecycleListener(Promise promise) {
189+
getReactApplicationContext().addLifecycleEventListener(new LifecycleEventListener() {
190+
@Override
191+
public void onHostResume() {
192+
// App is in the foreground
193+
}
194+
195+
@Override
196+
public void onHostPause() {
197+
// App is in the background
198+
}
199+
200+
@Override
201+
public void onHostDestroy() {
202+
// Attempt to shut down Fula cleanly
203+
try {
204+
Log.e("ReactNative", "shutting down Fula onHostDestroy");
205+
shutdownInternal();
206+
} catch (Exception e) {
207+
Log.e("ReactNative", "Error shutting down Fula", e);
208+
}
209+
}
210+
});
211+
promise.resolve(true);
212+
}
213+
186214
@ReactMethod
187215
public void checkConnection(int timeout, Promise promise) {
188216
Log.d("ReactNative", "checkConnection started");

example/src/App.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useEffect } from 'react';
22
import { StyleSheet, Text, View, Button } from 'react-native';
33

44
import {
@@ -21,6 +21,19 @@ const App = () => {
2121
{ peerId: string; rootCid: string; private_ref: string } | {}
2222
>({});
2323

24+
useEffect(() => {
25+
if (!__DEV__) {
26+
console.log = () => null;
27+
//console.error = () => null
28+
}
29+
fula
30+
.registerLifecycleListener()
31+
.then(() => console.log('Lifecycle listener registered'))
32+
.catch((error) =>
33+
console.error('Failed to register lifecycle listener', error)
34+
);
35+
}, []);
36+
2437
let RNFS = require('react-native-fs');
2538
const readFile = () => {
2639
RNFS.readDir(RNFS.DocumentDirectoryPath)

ios/Fula.mm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
@interface RCT_EXTERN_MODULE(FulaModule, NSObject)
44

5+
RCT_EXTERN_METHOD(registerLifecycleListener)
6+
57
RCT_EXTERN_METHOD(checkConnection: (nonnull NSNumber *) timeout
68
withResolver:(RCTPromiseResolveBlock)resolve
79
withRejecter:(RCTPromiseRejectBlock)reject)

ios/Fula.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,48 @@ class FulaModule: NSObject {
135135
return convertIntToByte(keyInt)
136136
}
137137

138+
@objc func registerLifecycleListener() {
139+
NotificationCenter.default.addObserver(
140+
self,
141+
selector: #selector(applicationWillResignActive),
142+
name: UIApplication.willResignActiveNotification,
143+
object: nil)
144+
145+
NotificationCenter.default.addObserver(
146+
self,
147+
selector: #selector(applicationDidEnterBackground),
148+
name: UIApplication.didEnterBackgroundNotification,
149+
object: nil)
150+
151+
NotificationCenter.default.addObserver(
152+
self,
153+
selector: #selector(applicationWillTerminate),
154+
name: UIApplication.willTerminateNotification,
155+
object: nil)
156+
}
157+
158+
deinit {
159+
NotificationCenter.default.removeObserver(self)
160+
}
161+
162+
@objc func applicationWillResignActive() {
163+
// Handle app will resign active (similar to onHostPause)
164+
}
165+
166+
@objc func applicationDidEnterBackground() {
167+
// Handle app entered background
168+
}
169+
170+
@objc func applicationWillTerminate() {
171+
// Attempt to shut down Fula cleanly (similar to onHostDestroy)
172+
do {
173+
try shutdownInternal()
174+
} catch {
175+
print("Error shutting down Fula: \(error)")
176+
}
177+
}
178+
179+
138180
@objc(checkConnection:withResolver:withRejecter:)
139181
func checkConnection(timeout: NSNumber, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
140182
OSLog.viewCycle.info("ReactNative checkConnection started with timeout=\(timeout)")

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@functionland/react-native-fula",
3-
"version": "1.36.2",
3+
"version": "1.36.4",
44
"description": "This package is a bridge to use the Fula libp2p protocols in the react-native which is using wnfs",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",

src/interfaces/fulaNativeModule.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { NativeModules, Platform } from 'react-native';
22

33
interface FulaNativeModule {
4+
registerLifecycleListener: () => Promise<void>;
45
initFula: (
56
identity: string, //Private key of did identity
67
storePath: string, //You can leave empty

src/protocols/fula.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ import {
77
} from './chain-api';
88
import { ApiPromise } from '@polkadot/api';
99

10+
/**
11+
* Register the app's lifecycle listeners to handle foreground, background, and termination states.
12+
*/
13+
export const registerLifecycleListener = (): Promise<void> => {
14+
console.log('called registerLifecycleListener');
15+
return Fula.registerLifecycleListener();
16+
};
17+
1018
/**
1119
* Get gets the value corresponding to the given key from the local datastore.
1220
// The key must be a valid ipld.Link.

0 commit comments

Comments
 (0)