Skip to content

Commit 080b67e

Browse files
chrisbobbegnprice
authored andcommitted
android [nfc]: Move generic app-status code out of notifications/
1 parent be4da1e commit 080b67e

File tree

3 files changed

+52
-49
lines changed

3 files changed

+52
-49
lines changed

android/app/src/main/java/com/zulipmobile/ReactExtensions.kt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,56 @@ import com.facebook.react.ReactApplication
55
import com.facebook.react.ReactInstanceManager
66
import com.facebook.react.ReactNativeHost
77
import com.facebook.react.bridge.ReactContext
8+
import com.facebook.react.common.LifecycleState
89
import com.facebook.react.modules.core.DeviceEventManagerModule
910

11+
/**
12+
* A distillation of ReactContext.getLifecycleState() and related information.
13+
*
14+
* See ReactContext.getAppStatus().
15+
*/
16+
enum class ReactAppStatus {
17+
/**
18+
* The main activity has either never yet been in the foreground,
19+
* or never will again. There might not be an active JS instance.
20+
*/
21+
NOT_RUNNING,
22+
23+
/**
24+
* The main activity has been in the foreground, is out of foreground
25+
* now, but might come back. There must be an active JS instance.
26+
*/
27+
BACKGROUND,
28+
29+
/**
30+
* The main activity is in the foreground.
31+
* There must be an active JS instance.
32+
*/
33+
FOREGROUND
34+
}
35+
36+
val ReactContext.appStatus: ReactAppStatus
37+
get() {
38+
if (!hasActiveCatalystInstance())
39+
return ReactAppStatus.NOT_RUNNING
40+
41+
// The RN lifecycleState:
42+
// * starts as BEFORE_CREATE
43+
// * responds to onResume, onPause, and onDestroy on the host Activity
44+
// * Android upstream docs on those:
45+
// https://developer.android.com/guide/components/activities/activity-lifecycle
46+
// * RN wires those through ReactActivity -> ReactActivityDelegate ->
47+
// ReactInstanceManager (as onHost{Resume,Pause,Destroy}) -> ReactContext
48+
// * notably goes straight BEFORE_CREATE -> RESUMED when first starting
49+
// (at least as of RN v0.59)
50+
return when (lifecycleState!!) {
51+
LifecycleState.BEFORE_CREATE -> ReactAppStatus.NOT_RUNNING
52+
LifecycleState.BEFORE_RESUME -> ReactAppStatus.BACKGROUND
53+
LifecycleState.RESUMED -> ReactAppStatus.FOREGROUND
54+
}
55+
}
56+
57+
1058
// A convenience shortcut.
1159
fun ReactContext.emitEvent(eventName: String, data: Any?) {
1260
getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)

android/app/src/main/java/com/zulipmobile/notifications/NotifyReact.kt

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import android.os.Bundle
66
import android.util.Log
77
import com.facebook.react.bridge.Arguments
88
import com.facebook.react.bridge.ReactContext
9-
import com.facebook.react.common.LifecycleState
9+
import com.zulipmobile.ReactAppStatus
10+
import com.zulipmobile.appStatus
1011
import com.zulipmobile.emitEvent
1112

1213
/**
@@ -16,52 +17,6 @@ import com.zulipmobile.emitEvent
1617
* TODO: Replace this with a fresh implementation based on RN upstream docs.
1718
*/
1819

19-
/**
20-
* A distillation of ReactContext.getLifecycleState() and related information.
21-
*
22-
* See ReactContext.getAppStatus().
23-
*/
24-
enum class ReactAppStatus {
25-
/**
26-
* The main activity has either never yet been in the foreground,
27-
* or never will again. There might not be an active JS instance.
28-
*/
29-
NOT_RUNNING,
30-
31-
/**
32-
* The main activity has been in the foreground, is out of foreground
33-
* now, but might come back. There must be an active JS instance.
34-
*/
35-
BACKGROUND,
36-
37-
/**
38-
* The main activity is in the foreground.
39-
* There must be an active JS instance.
40-
*/
41-
FOREGROUND
42-
}
43-
44-
val ReactContext.appStatus: ReactAppStatus
45-
get() {
46-
if (!hasActiveCatalystInstance())
47-
return ReactAppStatus.NOT_RUNNING
48-
49-
// The RN lifecycleState:
50-
// * starts as BEFORE_CREATE
51-
// * responds to onResume, onPause, and onDestroy on the host Activity
52-
// * Android upstream docs on those:
53-
// https://developer.android.com/guide/components/activities/activity-lifecycle
54-
// * RN wires those through ReactActivity -> ReactActivityDelegate ->
55-
// ReactInstanceManager (as onHost{Resume,Pause,Destroy}) -> ReactContext
56-
// * notably goes straight BEFORE_CREATE -> RESUMED when first starting
57-
// (at least as of RN v0.59)
58-
return when (lifecycleState!!) {
59-
LifecycleState.BEFORE_CREATE -> ReactAppStatus.NOT_RUNNING
60-
LifecycleState.BEFORE_RESUME -> ReactAppStatus.BACKGROUND
61-
LifecycleState.RESUMED -> ReactAppStatus.FOREGROUND
62-
}
63-
}
64-
6520
internal fun notifyReact(reactContext: ReactContext?, data: Bundle) {
6621
// TODO deduplicate this with handleSend in SharingHelper.kt; see
6722
// https://github.com/zulip/zulip-mobile/pull/5146#discussion_r764437055

android/app/src/main/java/com/zulipmobile/sharing/SharingHelper.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import android.util.Log
1010
import com.facebook.react.bridge.Arguments
1111
import com.facebook.react.bridge.ReactContext
1212
import com.facebook.react.bridge.WritableMap
13+
import com.zulipmobile.ReactAppStatus
1314
import com.zulipmobile.ZLog
14-
import com.zulipmobile.notifications.ReactAppStatus
15-
import com.zulipmobile.notifications.appStatus
15+
import com.zulipmobile.appStatus
1616
import com.zulipmobile.emitEvent
1717

1818
@JvmField

0 commit comments

Comments
 (0)