Skip to content

Migrate from ObservableObject to @Observable (Swift Observation) - #653

Merged
IvanStepanok merged 65 commits into
openedx:developfrom
raccoongang:feat/Observable-migration
Mar 17, 2026
Merged

Migrate from ObservableObject to @Observable (Swift Observation)#653
IvanStepanok merged 65 commits into
openedx:developfrom
raccoongang:feat/Observable-migration

Conversation

@Demian-Yushyn

Copy link
Copy Markdown
Contributor

This PR migrates ViewModels from the ObservableObject protocol and @Publishedproperties to the new Swift Observation macro @Observable.

As part of this change, the project now targets iOS 17+ as the minimum deployment version, allowing us to rely on modern SwiftUI and observation APIs without backward-compatibility workarounds.

Motivation

ObservableObject + @Published requires a significant amount of manual work and is error-prone:
properties must be explicitly marked as @Published, it’s easy to forget to publish changes, objectWillChange is implicit and not type-safe.

With iOS 17 as the minimum supported version, we can safely adopt Swift’s @Observable macro, which provides a compiler-generated, first-class observation mechanism that is:

  • safer
  • more concise
  • easier to reason about
  • officially supported and forward-looking

What changed

Before


class SomeViewModel: ObservableObject {
    @Published var value: Int = 0
}

After


@Observable
class SomeViewModel {
    var value: Int = 0
}

Benefits

  • Less boilerplate
  • No need for ObservableObject, @Published, or manual change notifications.
  • Fewer bugs
  • All mutable properties are observable by default — no risk of forgetting to publish state changes.
  • Cleaner ViewModels
  • Models focus on business logic instead of SwiftUI plumbing.
  • Better performance characteristics
  • Observation code is generated at compile time, enabling better compiler optimizations.
  • Future-proof architecture
  • By targeting iOS 17+, the codebase fully embraces modern SwiftUI and Swift concurrency APIs.

Demian-Yushyn and others added 30 commits September 16, 2025 11:05
Migrated Course Unit View, CourseContainerViewModel, SignInViewModel, BaseCourseViewModel
Swift, replacing `@ObservedObject` and `@StateObject`.

This modernizes the data flow and simplifies the code by leveraging the compiler to automatically manage observation.
Also it increases the minimum iOS deployment target to 17.0
Updates the iOS deployment target to version 17.0 in the project settings.

Refactors the course outline and progress view to handle loading states correctly.
@mphilbrick211 mphilbrick211 moved this from Waiting on Author to Ready for Review in Contributions Feb 25, 2026
@mphilbrick211
mphilbrick211 requested a review from a team February 25, 2026 20:26
@IvanStepanok IvanStepanok self-assigned this Mar 16, 2026
@IvanStepanok
IvanStepanok self-requested a review March 16, 2026 18:33

@IvanStepanok IvanStepanok left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You’ve put in a tremendous amount of work! Thank you so much!

@IvanStepanok
IvanStepanok merged commit a8e722e into openedx:develop Mar 17, 2026
8 checks passed
@github-project-automation github-project-automation Bot moved this from Ready for Review to Done in Contributions Mar 17, 2026
@codecov

codecov Bot commented Mar 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (develop@0792ece). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff            @@
##             develop   #653   +/-   ##
========================================
  Coverage           ?      0           
========================================
  Files              ?      0           
  Lines              ?      0           
  Branches           ?      0           
========================================
  Hits               ?      0           
  Misses             ?      0           
  Partials           ?      0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

IvanStepanok added a commit that referenced this pull request Jul 31, 2026
Brings in 35 commits, notably the ObservableObject -> @observable
migration (#653) and the connectivity fix for issue 581 (#635).

Conflict resolutions:

- Core/Core/Configuration/Connectivity.swift
  Both branches had independently implemented an offline debounce.
  Took develop's (notReachableDelay/notReachableTask): it verifies with
  a live request before declaring offline, and it assigns internetState,
  which the sandbox side never did -- develop's OfflineSnackBarView now
  reads .onChange(of: connectivity.internetState), so keeping the
  sandbox side would have left the snackbar permanently dead.
  Grafted the sandbox side's de-dupe guard onto internetState.didSet so
  the Combine subject stops re-emitting unchanged states.
  Dropped the now-orphaned scheduleOffline/cancelOfflineDebounce.

- Course/.../Unit/Subviews/WebView.swift
  Took develop's: the init already resolves viewModel/connectivity into
  @State/let, so re-resolving from the DI container in body was stale
  pre-Observable code that also discarded the injected connectivity.

- Theme/.../TextColor/TextSecondary.colorset/Contents.json
  Kept sandbox's: generated by "Sandbox (3)/apply_ios_theme.py" from
  theme.json (textSecondary #97A5BB / #79889F). Deliberate branding
  override of the upstream token update in #658.

- OpenEdX.xcodeproj/project.pbxproj
  Kept sandbox's DEVELOPMENT_TEAM and CURRENT_PROJECT_VERSION.
IvanStepanok added a commit that referenced this pull request Jul 31, 2026
Brings in 35 commits, notably the ObservableObject -> @observable
migration (#653) and the connectivity fix for issue 581 (#635).

Conflict resolutions:

- Core/Core/Configuration/Connectivity.swift
  Both branches had independently implemented an offline debounce.
  Took develop's (notReachableDelay/notReachableTask): it verifies with
  a live request before declaring offline, and it assigns internetState,
  which the sandbox side never did -- develop's OfflineSnackBarView now
  reads .onChange(of: connectivity.internetState), so keeping the
  sandbox side would have left the snackbar permanently dead.
  Grafted the sandbox side's de-dupe guard onto internetState.didSet so
  the Combine subject stops re-emitting unchanged states.
  Dropped the now-orphaned scheduleOffline/cancelOfflineDebounce.

- Course/.../Unit/Subviews/WebView.swift
  Took develop's: the init already resolves viewModel/connectivity into
  @State/let, so re-resolving from the DI container in body was stale
  pre-Observable code that also discarded the injected connectivity.

- Theme/.../TextColor/TextSecondary.colorset/Contents.json
  Kept sandbox's: generated by "Sandbox (3)/apply_ios_theme.py" from
  theme.json (textSecondary #97A5BB / #79889F). Deliberate branding
  override of the upstream token update in #658.

- OpenEdX.xcodeproj/project.pbxproj
  Kept sandbox's DEVELOPMENT_TEAM and CURRENT_PROJECT_VERSION in the two
  conflicting hunks, but kept develop's IPHONEOS_DEPLOYMENT_TARGET bump
  16.4 -> 17.0 across all 6 configurations: Core is now built for
  iOS 17.0, so leaving the app target at 16.4 fails to compile with
  "module 'Core' has a minimum deployment target of iOS 17.0".
IvanStepanok added a commit that referenced this pull request Jul 31, 2026
Brings in 35 commits, notably the ObservableObject -> @observable
migration (#653) and the connectivity fix for issue 581 (#635).

Conflict resolutions:

- Core/Core/Configuration/Connectivity.swift
  Took develop's version verbatim. Both branches had independently
  implemented an offline debounce; develop's (notReachableDelay /
  notReachableTask) verifies with a live request before declaring
  offline and assigns internetState, which the sandbox side never did --
  develop's OfflineSnackBarView reads
  .onChange(of: connectivity.internetState), so the sandbox side would
  have left the snackbar permanently dead. This branch carries only the
  sandbox design and AppStore build configuration, so it keeps no
  connectivity logic of its own.

- Course/.../Unit/Subviews/WebView.swift
  Took develop's: the init already resolves viewModel/connectivity into
  @State/let, so re-resolving from the DI container in body was stale
  pre-Observable code that also discarded the injected connectivity.

- Theme/.../TextColor/TextSecondary.colorset/Contents.json
  Kept sandbox's: generated by "Sandbox (3)/apply_ios_theme.py" from
  theme.json (textSecondary #97A5BB / #79889F). Deliberate branding
  override of the upstream token update in #658.

- OpenEdX.xcodeproj/project.pbxproj
  Kept sandbox's DEVELOPMENT_TEAM and CURRENT_PROJECT_VERSION in the two
  conflicting hunks, but kept develop's IPHONEOS_DEPLOYMENT_TARGET bump
  16.4 -> 17.0 across all 6 configurations: Core is now built for
  iOS 17.0, so leaving the app target at 16.4 fails to compile with
  "module 'Core' has a minimum deployment target of iOS 17.0".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

open-source-contribution PR author is not from Axim or 2U

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

4 participants