Fix the camera resume model for a call screen

Review the AI's codeEasy10 minGoogle style

Your task

An AI wrote a Flutter video call screen that sets up the camera and the call once, in initState. When the app went to the background during a phone call and came back, the preview stayed black. The fix observes app lifecycle changes and sets the camera up again on resume. Your team replays lifecycle logs and counts how often the user comes back to a black preview.

An assistant wrote count_black_preview_resumes(lifecycle_events, observes_lifecycle), the replay. It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • lifecycle_events: the log, in order. inserted is the screen's first build, which sets up the camera. paused means the app went to the background and the system took the camera back. resumed means the app came back. disposed means the screen closed.
  • observes_lifecycle: the screen handles lifecycle changes and sets the camera up again on resume.
  • Return how many resumes found a black preview.

Rules the tests check:

  • inserted: the screen is open and the camera is live. disposed: the screen is closed and the camera is not live.
  • While the screen is closed, ignore every other event. Ignore any event name not listed above.
  • paused: the camera is no longer live.
  • resumed with a live camera: nothing happens.
  • resumed without a live camera: if observes_lifecycle is true, the camera is set up again and is live. Otherwise count one black preview, and the camera stays dark.

Examples

count_black_preview_resumes(lifecycle_events: list[str], observes_lifecycle: bool) -> int
Example 1
Inputcount_black_preview_resumes(["inserted", "paused", "resumed"], False)
Output1
Example 2
Inputcount_black_preview_resumes(["inserted", "paused", "resumed"], True)
Output0
Example 3
Inputcount_black_preview_resumes(["inserted", "paused", "resumed", "paused", "resumed"], False)
Output2