Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [IOPLT-959] Implements the Offline check loop feature #6734

Merged
merged 16 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

exports[`featuresPersistor should match snapshot 1`] = `
{
"connectivityStatus": {
"isConnected": true,
},
"fci": {
"documentPreview": {
"kind": "PotNone",
Expand Down
7 changes: 6 additions & 1 deletion ts/features/common/store/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ import {
spidLoginReducer,
SpidLoginState
} from "../../../spidLogin/store/reducers";
import connectivityStateReducer, {
ConnectivityState
} from "../../../connectivity/store/reducers";
import { GlobalState } from "../../../../store/reducers/types";
import { isIOMarkdownDisabledForMessagesAndServices } from "../../../../store/reducers/backendStatus/remoteConfig";
import { isIOMarkdownEnabledLocallySelector } from "../../../../store/reducers/persistedPreferences";
Expand Down Expand Up @@ -93,6 +96,7 @@ export type FeaturesState = {
mixpanel: MixpanelState;
ingress: IngressScreenState;
landingBanners: LandingScreenBannerState;
connectivityStatus: ConnectivityState;
};

export type PersistedFeaturesState = FeaturesState & PersistPartial;
Expand All @@ -119,7 +123,8 @@ const rootReducer = combineReducers<FeaturesState, Action>({
profileSettings: profileSettingsReducerPersistor,
mixpanel: mixpanelReducer,
ingress: ingressScreenReducer,
landingBanners: landingScreenBannersReducer
landingBanners: landingScreenBannersReducer,
connectivityStatus: connectivityStateReducer
});

const CURRENT_REDUX_FEATURES_STORE_VERSION = 1;
Expand Down
66 changes: 66 additions & 0 deletions ts/features/connectivity/saga/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import * as E from "fp-ts/lib/Either";
import { call, fork, put, select } from "typed-redux-saga/macro";
import { Millisecond } from "@pagopa/ts-commons/lib/units";
import { configureNetInfo, fetchNetInfoState } from "../utils";
import { startTimer } from "../../../utils/timer";
import { setConnectionStatus } from "../store/actions";
import { ReduxSagaEffect, SagaCallReturnType } from "../../../types/utils";
import { isConnectedSelector } from "../store/selectors";

const CONNECTIVITY_STATUS_LOAD_INTERVAL = (60 * 1000) as Millisecond;
const CONNECTIVITY_STATUS_FAILURE_INTERVAL = (10 * 1000) as Millisecond;

/**
* this saga requests and checks the connection status
*/
export function* connectionStatusSaga(): Generator<
ReduxSagaEffect,
boolean,
SagaCallReturnType<typeof fetchNetInfoState>
> {
try {
const response = yield* call(fetchNetInfoState());
if (E.isRight(response) && response.right.isConnected) {
yield* put(setConnectionStatus(response.right.isConnected));
return true;
}
return false;
} catch (e) {
// do nothing. it should be a library error
return false;
}
}

/**
* this saga requests and checks in loop connection status
* if the connection is off app could show a warning message or avoid
* the whole usage.
*/
export function* connectionStatusWatcherLoop() {
// check connectivity status periodically
while (true) {
const response: SagaCallReturnType<typeof connectionStatusSaga> =
yield* call(connectionStatusSaga);

// if we have no connection increase rate
if (response === false) {
yield* call(startTimer, CONNECTIVITY_STATUS_FAILURE_INTERVAL);
continue;
}

const isAppConnected = yield* select(isConnectedSelector);

// if connection is off increase rate
if (!isAppConnected) {
yield* call(startTimer, CONNECTIVITY_STATUS_FAILURE_INTERVAL);
} else {
yield* call(startTimer, CONNECTIVITY_STATUS_LOAD_INTERVAL);
}
}
}

export default function* root(): IterableIterator<ReduxSagaEffect> {
// configure net info library to check status and fetch a specific url
configureNetInfo();
yield* fork(connectionStatusWatcherLoop);
}
13 changes: 13 additions & 0 deletions ts/features/connectivity/store/actions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ActionType, createStandardAction } from "typesafe-actions";

export const requestConnectionStatus = createStandardAction(
"REQUEST_CONNECTION_STATUS"
)();

export const setConnectionStatus = createStandardAction(
"SET_CONNECTION_STATUS"
)<boolean>();

export type ConnectivityActions =
| ActionType<typeof requestConnectionStatus>
| ActionType<typeof setConnectionStatus>;
31 changes: 31 additions & 0 deletions ts/features/connectivity/store/reducers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { getType } from "typesafe-actions";
import { Action } from "../../../../store/actions/types";
import { setConnectionStatus } from "../actions";

// Define the type for the connection state
export type ConnectivityState = {
isConnected: boolean;
};

// Define the initial state
const initialState: ConnectivityState = {
isConnected: true
};

// Define the reducer
const connectivityStateReducer = (
state: ConnectivityState = initialState,
action: Action
): ConnectivityState => {
switch (action.type) {
case getType(setConnectionStatus):
return {
...state,
isConnected: action.payload
};
default:
return state;
}
};

export default connectivityStateReducer;
7 changes: 7 additions & 0 deletions ts/features/connectivity/store/selectors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { GlobalState } from "../../../../store/reducers/types";

export const connectivityStatusSelector = (state: GlobalState) =>
state.features.connectivityStatus;

export const isConnectedSelector = (state: GlobalState) =>
state.features.connectivityStatus.isConnected;
23 changes: 23 additions & 0 deletions ts/features/connectivity/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { configure, fetch } from "@react-native-community/netinfo";
import { pipe } from "fp-ts/lib/function";
import * as TE from "fp-ts/lib/TaskEither";
import { apiUrlPrefix } from "../../../config";

const IO_REACHABIITY_URL = `${apiUrlPrefix}/api/v1/status`;

export const configureNetInfo = () =>
configure({
reachabilityUrl: IO_REACHABIITY_URL,
useNativeReachability: false,
reachabilityRequestTimeout: 15000,
reachabilityLongTimeout: 60000,
reachabilityShortTimeout: 5000
});

export const fetchNetInfoState = () =>
pipe(
TE.tryCatch(
() => fetch(),
error => new Error(`Error fetching net info state: ${error}`)
)
);
2 changes: 2 additions & 0 deletions ts/sagas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { watchTokenRefreshSaga } from "../features/fastLogin/saga/tokenRefreshSa
import { watchPendingActionsSaga } from "../features/fastLogin/saga/pendingActionsSaga";
import { watchZendeskSupportSaga } from "../features/zendesk/saga";
import { zendeskEnabled } from "../config";
import connectivityStatusSaga from "../features/connectivity/saga";
import backendStatusSaga from "./backendStatus";
import { watchContentSaga } from "./contentLoaders";
import { loadSystemPreferencesSaga } from "./preferences";
Expand All @@ -22,6 +23,7 @@ export default function* root() {
call(watchApplicationActivitySaga),
call(startupSaga),
call(backendStatusSaga),
call(connectivityStatusSaga),
call(versionInfoSaga),
call(loadSystemPreferencesSaga),
call(removePersistedStatesSaga),
Expand Down
2 changes: 2 additions & 0 deletions ts/store/actions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { IngressScreenActions } from "../../features/ingress/store/actions";
import { MixpanelFeatureActions } from "../../features/mixpanel/store/actions";
import { LandingScreenBannerActions } from "../../features/landingScreenMultiBanner/store/actions";
import { SpidConfigActions } from "../../features/spidLogin/store/actions";
import { ConnectivityActions } from "../../features/connectivity/store/actions";
import { LoginPreferencesActions } from "../../features/login/preferences/store/actions";
import { AnalyticsActions } from "./analytics";
import { ApplicationActions } from "./application";
Expand Down Expand Up @@ -109,6 +110,7 @@ export type Action =
| MixpanelFeatureActions
| LandingScreenBannerActions
| SpidConfigActions
| ConnectivityActions
| LoginPreferencesActions;

export type Dispatch = DispatchAPI<Action>;
Expand Down
Loading