import {
all as i,
call as e,
fork as o,
getContext as a,
put as t,
select as l,
take as n,
takeEvery as r,
takeLatest as d,
takeLeading as y,
} from "redux-saga/effects"
import { getVersionApi as m } from "../../api/core"
import { safeCall as s, spawnRestarted as p } from "../lib"
import { appErrorSaga as c } from "./saga/app-error.saga"
import {
appChatAddMessageSaga as f,
appChatUpdateMessageSaga as u,
appDmResponseSaga as I,
appRoomJoinSaga as N,
appRoomSwitchSaga as E,
appRootFunctionalitySaga as g,
currentRoomSaga as R,
setAppNotificationsTypeSaga as T,
appStateCleanup as C,
waitingDmRoomSaga as S,
} from "./app.saga"
import { APP_STATUS as O } from "./app.constants"
import {
EMPTY_ROOM_ID as _,
ROOM_DM_REPLY as A,
closeRoomSubmit as D,
createRoomSubmit as b,
dmReplySubmit as v,
getDmRequestById as H,
getRoomPairingIdActive as h,
joinRoomSubmit as M,
leaveRoomSubmit as w,
leaveRoomSuccessEvent as P,
roomCreateHandler as Y,
roomLeaveHandler as G,
roomListSaga as L,
roomSaga as U,
setRoomPairingActive as V,
setWaitingDmMemberId as x,
switchRoomSubmit as K,
getRoomLeavingId as k,
shareMessageInSaga as q,
forwardMessageEvt as B,
chatForwardMessageHandler as F,
} from "../room"
import {
getAppCurrentRoomId as J,
initApplication as Q,
setAppCurrentRoomId as W,
setAppLocale as j,
setAppNotificationsType as z,
setAppInitialStatus as X,
} from "./app.reducer"
import { APP_CONTEXT_KEY as Z, PLATFORM_CONTEXT_KEY as $ } from "../constants"
import { sendChatMessageSubmit as ii, updateChatMessageSubmit as ei } from "../chat"
import { forceUpdateHandlerSaga as oi, systemUpdateHandlerSaga as ai } from "./saga/app-update.saga"
import {
getUIAppModalData as ti,
setUIAppState as li,
uiNavigateToRoute as ni,
uiPushNotificationSaga as ri,
uiSaga as di,
uiDevicePushNotificationSetupSaga as yi,
getUIAppNavigatorCurrentRoute as mi,
} from "../ui-mobile"
import { appCallManagerSaga as si, appRoomsPresencesSaga as pi } from "./saga/app-call.saga"
import { mobileRoomNotificationsSaga as ci } from "./saga/mobile-room-notifications.saga"
import {
identitySaga as fi,
loadIdentitySync as ui,
setSyncDeviceSeedId as Ii,
getIsIdentityConfirmed as Ni,
} from "../identity"
import { networkSaga as Ei } from "../network"
import { taskErrorSaga as gi } from "../error"
import { statsSaga as Ri } from "../stats"
import {
getPreferencesLocale as Ti,
preferencesInitData as Ci,
preferencesSaga as Si,
} from "../preferences"
import { uploadsInitData as Oi, uploadsPersistSaga as _i } from "../uploads"
import { UI_APP_STATE as Ai } from "../ui-mobile/ui.constants"
import { uiCallSaga as Di } from "../ui-mobile/ui-call.saga"
import { appDestructSaga as bi } from "./saga/app-destruct.saga"
import { clearAllFiles as vi, clearOldFiles as Hi } from "../media/file"
import { appClearAllFilesHandler as hi, appClearOldFilesHandler as Mi } from "./saga/app-files.saga"
import { debugMobileSaga as wi } from "../debug"
function* Pi() {
try {
const { applicationKey: o } = yield a($)
yield t(Q({ applicationKey: o, lastToastNotificationSeen: Date.now() }))
const [n] = yield i([e(m), e(Ci), e(ui), e(Oi)]),
{ localeManager: r, AppState: d, setupQuickActions: y } = yield a(Z),
p = (yield l(Ti)) || r.clientLocale || "en"
;(yield r.setLocale(p), yield t(j(p)))
const c = yield l(Ni)
;(yield s(y, { identityConfirmed: c }),
yield t(X({ status: O.RUNNING, appVersion: n })),
yield t(li(d?.currentState || Ai.ACTIVE)))
} catch (i) {
yield gi("appInitialStartupHandler error", i)
}
}
function* Yi(e) {
try {
let o = [],
a = Ai.INIT
for (;;) {
const { payload: t } = yield n(li)
a !== t &&
((a = t),
o.forEach((i) => {
i.isRunning() && i.cancel()
}),
t === Ai.ACTIVE && (o = yield i(e[t].map(p))))
}
} catch (i) {
yield gi("appStateFunctionalitySaga", i)
}
}
function* Gi({ payload: i }) {
const e = { ...i, onJoinNotifications: !0 }
yield o(R, { payload: e })
}
function* Li(i) {
try {
yield I(i)
const { replyType: e, requestId: o } = i.payload
if (e !== A.ACCEPT) return
const { senderId: n, displayName: r } = yield l(H(o))
yield t(x({ waitingDmMemberId: n }))
const { navigationConstants: d } = yield a(Z)
;(yield t(W({ roomId: o })),
yield t(ni({ routeName: d.SCREEN_ROOM, params: { dmConfig: { displayName: r } } })))
} catch (i) {
yield gi("dmReplySubmitHandler", i)
}
}
export function* switchRoomSubmitHandler(i) {
try {
;(yield t(x({ waitingDmMemberId: _ })), yield E(i))
const { navigationConstants: e } = yield a(Z)
i.payload?.navigateRoomInfoOnly
? yield t(ni({ routeName: e.SCREEN_ROOM_OPTIONS, params: { navigateRoomInfoOnly: !0 } }))
: yield t(ni({ routeName: e.SCREEN_ROOM, params: i.payload || {} }))
} catch (i) {
yield gi("switchRoomSubmitHandler", i)
}
}
function* Ui({ payload: i }) {
try {
const { navigationConstants: e, navigationHelpers: o } = yield a(Z)
i === (yield l(J)) &&
(yield t(W({ roomId: _ })), o.reset(e.APP_ROOT, { screen: e.SCREEN_HOME }))
} catch (i) {
yield gi("leaveRoomSuccessHandler", i)
}
}
function* Vi() {
try {
const { navigationConstants: i, navigationHelpers: e } = yield a(Z)
;(yield t(W({ roomId: _ })), e.reset(i.APP_ROOT, { screen: i.SCREEN_HOME }))
} catch (i) {
yield gi("closeRoomSubmitHandler", i)
}
}
function* xi() {
;(yield l(h)) && (yield t(W({ roomId: _ })))
}
function* Ki({ payload: i }) {
const { navigationConstants: e, navigationHelpers: o } = yield a(Z),
t = yield l(ti),
n = yield l(mi)
if (i && n !== e.SCREEN_IDENTITY_SYNC) {
const i = n === e.SCREEN_HOME,
a = n === e.SCREEN_MODAL && t?.modalType === o.ModalTypes.IDENTITY
;(i || a ? o.navigate : o.navReplace)(e.SCREEN_IDENTITY_SYNC)
}
}
function* ki() {
;(yield y(v, Li),
yield y(w, function* () {
const i = yield l(k)
yield e(G, { roomId: i })
}),
yield d(W, Gi),
yield y(K, switchRoomSubmitHandler),
yield r(b, Y),
yield r(M, N),
yield r(D, Vi),
yield r(V, xi),
yield r(P, Ui))
}
function* qi() {
;(yield o(Di),
yield o(ri),
yield o(ki),
yield o(S),
yield d(z, T),
yield y(ii, f),
yield r(B, F),
yield r(ei, u),
yield r(Ii, Ki),
yield d(vi, hi),
yield d(Hi, Mi))
}
export function* mobileAppSaga() {
const i = Date.now()
;(yield p(c),
yield o(bi),
yield o(di),
yield o(yi),
yield o(g, {
[O.LOADING]: [],
[O.RUNNING]: [qi, si, ci, U, L, q, pi],
[O.IDENTITY_SETUP]: [C],
[O.DESTRUCTING]: [],
[O.TEARDOWN]: [],
[O.CRITICAL_ERROR]: [],
}),
yield o(Yi, { [Ai.INIT]: [], [Ai.BACKGROUND]: [], [Ai.ACTIVE]: [Ei], [Ai.INACTIVE]: [] }),
yield p(ai),
yield s(Pi))
const e = Date.now()
;(yield p(Ri, { startTime: i, startDoneTime: e }),
yield p(fi),
yield p(Si),
yield p(_i),
yield p(oi),
yield p(wi))
}