forked from artsy/eigen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NativeModel.ts
105 lines (98 loc) · 3.25 KB
/
NativeModel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { LegacyNativeModules } from "app/NativeModules/LegacyNativeModules"
import { NotificationsManager } from "app/NativeModules/NotificationsManager"
import { navigate, navigationEvents } from "app/navigation/navigate"
import { InfoType } from "app/utils/track/providers"
import { SegmentTrackingProvider } from "app/utils/track/SegmentTrackingProvider"
import { Action, action, Thunk, thunk } from "easy-peasy"
import { GlobalStore } from "./GlobalStore"
// These should match the values in emission/Pod/Classes/EigenCommunications/ARNotificationsManager.m
export type NativeEvent =
| {
type: "STATE_CHANGED"
payload: NativeState
}
| {
type: "NOTIFICATION_RECEIVED"
payload: any
}
| {
type: "REQUEST_NAVIGATION"
payload: { route: string; props: {} }
}
| {
type: "REQUEST_MODAL_DISMISS"
}
| {
type: "MODAL_DISMISSED"
}
| {
type: "EVENT_TRACKING"
payload: InfoType
}
| {
type: "IDENTIFY_TRACKING"
payload: InfoType
}
export interface NativeState {
userID: string
userEmail: string
authenticationToken: string
launchCount: number
userAgent: string
}
export interface NativeModel {
sessionState: NativeState
setLocalState: Action<NativeModel, Partial<NativeState>>
setApplicationIconBadgeNumber: Thunk<NativeModel, number>
}
export const getNativeModel = (): NativeModel => ({
sessionState: LegacyNativeModules.ARNotificationsManager?.nativeState ?? {},
setLocalState: action((state, nextNativeState) => {
Object.assign(state.sessionState, nextNativeState)
}),
setApplicationIconBadgeNumber: thunk((_actions, count) => {
LegacyNativeModules.ARTemporaryAPIModule.setApplicationIconBadgeNumber(count)
}),
})
export function listenToNativeEvents(cb: (event: NativeEvent) => void) {
return NotificationsManager.addListener("event", cb)
}
listenToNativeEvents((event: NativeEvent) => {
switch (event.type) {
case "IDENTIFY_TRACKING":
// Segment should automatically stitch identify calls to existing user even if userid is null
SegmentTrackingProvider.identify
? SegmentTrackingProvider.identify(null, event.payload)
: (() => undefined)()
return
case "EVENT_TRACKING":
SegmentTrackingProvider.postEvent(event.payload)
return
case "STATE_CHANGED":
// We need to set the values we get from the native state on iOS to the global store
// to have parity between the auth on native and react-native
if (event.payload.userEmail && event.payload.userID && event.payload.authenticationToken) {
GlobalStore.actions.auth.setState({
userEmail: event.payload.userEmail,
userAccessToken: event.payload.authenticationToken,
userID: event.payload.userID,
})
}
return
case "NOTIFICATION_RECEIVED":
GlobalStore.actions.bottomTabs.fetchCurrentUnreadConversationCount()
return
case "REQUEST_NAVIGATION":
const { route, props } = event.payload
navigate(route, { passProps: props })
return
case "MODAL_DISMISSED":
navigationEvents.emit("modalDismissed")
return
case "REQUEST_MODAL_DISMISS":
navigationEvents.emit("requestModalDismiss")
return
default:
assertNever(event)
}
})