Skip to content

Fetch candlesticks on scene activation#1766

Merged
DRadmir merged 5 commits intomainfrom
1760-fetch-candlestick-when-the-app-becomes-active
Mar 5, 2026
Merged

Fetch candlesticks on scene activation#1766
DRadmir merged 5 commits intomainfrom
1760-fetch-candlestick-when-the-app-becomes-active

Conversation

@DRadmir
Copy link
Contributor

@DRadmir DRadmir commented Mar 4, 2026

Track the SwiftUI scenePhase in PerpetualScene and call the view model when it changes. Adds an @Environment(.scenePhase) property and an .onChange handler that forwards phase updates to PerpetualSceneViewModel. Implements onScenePhaseChange in the view model to trigger fetchCandlesticks() when the scene becomes .active (no-op for .inactive/.background). This ensures candlestick data is refreshed when the app/scene returns to the foreground.

Fix: #1760

Track the SwiftUI scenePhase in PerpetualScene and call the view model when it changes. Adds an @Environment(\.scenePhase) property and an .onChange handler that forwards phase updates to PerpetualSceneViewModel. Implements onScenePhaseChange in the view model to trigger fetchCandlesticks() when the scene becomes .active (no-op for .inactive/.background). This ensures candlestick data is refreshed when the app/scene returns to the foreground.
@DRadmir DRadmir self-assigned this Mar 4, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the PerpetualScene by integrating SwiftUI's scene phase observation to ensure that candlestick data is automatically refreshed when the application or scene becomes active. This change addresses a known issue where data might become stale if the app was backgrounded and then brought back to the foreground, improving the user experience by always presenting up-to-date information.

Highlights

  • Scene Phase Tracking: Introduced @Environment(\.scenePhase) in PerpetualScene to monitor the application's lifecycle state.
  • View Model Integration: Configured PerpetualScene to notify its view model (PerpetualSceneViewModel) whenever the scenePhase changes.
  • Candlestick Refresh Logic: Implemented onScenePhaseChange in PerpetualSceneViewModel to automatically fetch candlestick data when the scene becomes active, ensuring data freshness upon returning to the foreground.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • Features/Perpetuals/Sources/Scenes/PerpetualScene.swift
    • Added @Environment(\.scenePhase) property.
    • Attached an .onChange modifier to scenePhase to call model.onScenePhaseChange.
  • Features/Perpetuals/Sources/ViewModels/PerpetualSceneViewModel.swift
    • Implemented onScenePhaseChange method to trigger fetchCandlesticks() when the new phase is .active.
Activity
  • No specific activity (comments, reviews, progress) has been recorded for this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds functionality to refresh candlestick data when the app scene becomes active. This is achieved by observing the scenePhase in PerpetualScene and triggering a data fetch in the PerpetualSceneViewModel. My review identifies a high-severity issue where errors during the data fetch are silently ignored, which could leave the UI in a perpetual loading state. I've suggested a fix to properly handle errors and update the UI state, consistent with other parts of the view model.

Comment on lines +146 to +152
func onScenePhaseChange(_ oldPhase: ScenePhase, _ newPhase: ScenePhase) {
switch newPhase {
case .active: Task { try? await fetchCandlesticks() }
case .inactive, .background: break
@unknown default: break
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Silently ignoring the error from fetchCandlesticks() using try? can lead to a poor user experience. The fetchCandlesticks() function sets the state to .loading at the beginning. If it then throws an error, the state will remain .loading indefinitely, and the user will see a perpetual loading indicator without any error message. It's better to handle the potential error and update the UI state accordingly, similar to how it's done in onPeriodChange.

    func onScenePhaseChange(_ oldPhase: ScenePhase, _ newPhase: ScenePhase) {
        guard newPhase == .active else { return }

        Task {
            do {
                try await fetchCandlesticks()
            } catch {
                state = .error(error)
            }
        }
    }


func onScenePhaseChange(_ oldPhase: ScenePhase, _ newPhase: ScenePhase) {
switch newPhase {
case .active: Task { try? await fetchCandlesticks() }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
case .active: Task { try? await fetchCandlesticks() }
case .active: Task { try? await updateCandlesticks() }

@gemcoder21
Copy link
Contributor

@DRadmir make sure to fetch state for the market (prices / candle stick) and then connect websocket, same if you just open this screen

@DRadmir DRadmir force-pushed the 1760-fetch-candlestick-when-the-app-becomes-active branch from 6af509e to 479a347 Compare March 5, 2026 16:13
Introduce fetchState() to concurrently update market data and candlesticks, and add updateCandlesticks() (renamed from fetchCandlesticks). Call fetchState() on onAppear and when the scene becomes active, and call updateCandlesticks() on period changes. Also remove the initial:true on the view's period onChange so the initial load is handled by fetchState(). These changes ensure market and candle data are fetched together and avoid duplicate initial period-triggered fetches; errors during initial load set state to .error.
@DRadmir DRadmir force-pushed the 1760-fetch-candlestick-when-the-app-becomes-active branch from 479a347 to f20b17e Compare March 5, 2026 16:33
@DRadmir DRadmir merged commit 59a80dd into main Mar 5, 2026
@DRadmir DRadmir deleted the 1760-fetch-candlestick-when-the-app-becomes-active branch March 5, 2026 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fetch candlestick when the app becomes active

3 participants