Skip to content

Commit

Permalink
fix: [IOBP-972] Vision camera functionality has been restricted by th…
Browse files Browse the repository at this point in the history
…e operating system (#6410)

## Short description
This pull request includes changes to the
`BarcodeScanBaseScreenComponent` to address an issue where a black
screen appears when the app is not in an active state.

## List of changes proposed in this pull request
- Implemented a `useEffect` to update the app state and background
status when the app state changes
- Updated the `isDisabled` property to also check if the app is in the
background, ensuring the barcode scanner is disabled when the app is not
active

## How to test
‼️**Use a physical device**‼️ To test the old behavior omit the
`isAppInBackground` flag.
- Add some logs to the `BarcodeScanBaseScreenComponent.tsx` file:
```
React.useEffect(() => {
    const subscription = AppState.addEventListener("change", nextAppState => {
      if (
        appState.current.match(/inactive|background/) &&
        nextAppState === "active"
      ) {
        console.log("App has come to the foreground!");
      }

      // eslint-disable-next-line functional/immutable-data
      appState.current = nextAppState;
      setIsAppInBackground(appState.current !== "active");
      console.log("AppState", appState.current);
    });

    return () => {
      subscription.remove();
    };
  }, []);
```
- Tap on `Inquadra`
- Switch to the device's `Camera` application without closing the
`io-app`. Observe if an error is logged while switching.
- Minimize the `io-app` and return to the device's home screen without
closing the app. Wait for approximately one/two minutes and check if any
errors are logged during this time.
- Reopen the `io-app` from the background and verify that the scanner
screen do not appears black and works as expected.

## Preview
| Without `isAppInBackground` | With `isAppInBackground` |
|--------|--------|
| <img width="902" alt="Screenshot 2024-11-15 at 17 26 27"
src="https://github.com/user-attachments/assets/2210ca95-64ad-4bb6-ae04-38aecf37f54f">
| <img width="902" alt="Screenshot 2024-11-15 at 17 30 17"
src="https://github.com/user-attachments/assets/a819c230-597c-47df-96a8-97368a2ac7aa">|

Co-authored-by: Alessandro <[email protected]>
  • Loading branch information
LeleDallas and Hantex9 authored Nov 19, 2024
1 parent db36527 commit 5002895
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions ts/features/barcode/components/BarcodeScanBaseScreenComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
useRoute
} from "@react-navigation/native";
import React from "react";
import { StyleSheet, View } from "react-native";
import { AppState, StyleSheet, View } from "react-native";
import LinearGradient from "react-native-linear-gradient";
import {
SafeAreaView,
Expand Down Expand Up @@ -134,11 +134,32 @@ const BarcodeScanBaseScreenComponent = ({

const currentScreenName = useIOSelector(currentRouteSelector);

const [isAppInBackground, setIsAppInBackground] = React.useState(
AppState.currentState !== "active"
);

const dispatch = useIODispatch();
const assistanceToolConfig = useIOSelector(assistanceToolConfigSelector);
const canShowHelp = useIOSelector(canShowHelpSelector);
const choosenTool = assistanceToolRemoteConfig(assistanceToolConfig);

/**
* Updates the app state when it changes.
*
* @param {string} nextAppState - The next state of the app.
*
* @returns {void}
*/
React.useEffect(() => {
const subscription = AppState.addEventListener("change", nextAppState => {
setIsAppInBackground(nextAppState !== "active");
});

return () => {
subscription.remove();
};
}, []);

useFocusEffect(
React.useCallback(() => {
trackBarcodeScanScreenView(barcodeAnalyticsFlow);
Expand Down Expand Up @@ -190,7 +211,7 @@ const BarcodeScanBaseScreenComponent = ({
onBarcodeError,
barcodeFormats,
barcodeTypes,
isDisabled: !isFocused || isDisabled,
isDisabled: isAppInBackground || !isFocused || isDisabled,
isLoading
});

Expand Down

0 comments on commit 5002895

Please sign in to comment.