How opficdev/swiftui_calculator works
Project type and purpose
• This is a native iOS calculator app built in SwiftUI using the MVVM pattern.
• Front‑end: SwiftUI views. Back‑end: Swift model/business logic.
• Features: basic & scientific modes, history persistence via UserDefaults, orientation detection, operator precedence via a stack.
File structure
• Top level:
• Calculator.xcodeproj (Xcode project files)
• Calculator/ (app source )
– CalculatorApp.swift (app entry point)
– ContentView.swift (root SwiftUI view)
– Info.plist (app config)
– Image/ (static images)
– Model/ (data structures & business logic)
– View/ (UI components)
– ViewModel/ (presentation logic)
– Preview Content/ (Xcode previews)
• Third‑party:
• RxSwift integrated via SwiftPM (seen under xcshareddata/swiftpm).
• Persistence:
• UserDefaults used to store and retrieve history.
Key architectural insights from the README
• MVVM is the chosen pattern.
• RxSwift powers asynchronous change‑detection in both calculation results and device orientation.
• A stack data structure implements operator precedence.
• History stored in in‑app memory (UserDefaults), exposed via RxSwift observables to the View.
a. Main components
• iOS Platform Layer
• App Entry (CalculatorApp)
• SwiftUI View Layer (ContentView → PortraitView, LandscapeView, ModeView, HistoryView, ButtonLabelView)
• ViewModel Layer (CalculatorViewModel, HistoryViewModel, OrientationViewModel)
• Model Layer (BtnGrid, BtnType, Token, History, iPhonePointRes, CalculationEngine/Stack)
• Service Layer
– PersistenceService (wraps UserDefaults)
– OrientationService (wraps device orientation events)
• External Library: RxSwift
b. Interactions & data flow
1. App Launch → CalculatorApp initializes root ContentView
2. ContentView binds to CalculatorViewModel & OrientationViewModel
3. User taps a button → View sends event to CalculatorViewModel
4. CalculatorViewModel updates Model (uses CalculationEngine + stack)
5. Model emits new result/history via RxSwift observables
6. PersistenceService saves history to UserDefaults
7. HistoryViewModel observes changes in PersistenceService → updates HistoryView
8. OrientationService observes device rotation via RxSwift → OrientationViewModel → View updates UI layout
c. Architectural patterns & principles
• MVVM separation of concerns: Views only do layout & binding; ViewModels hold all UI state & logic; Models encapsulate pure business logic.
• Reactive programming with RxSwift: unidirectional, observable streams for both calculation state and orientation events.
• Single‐source‐of‐truth for history in PersistenceService (UserDefaults wrapper).
d. Key technologies & frameworks
• SwiftUI for declarative UI
• RxSwift for asynchronous/event handling
• UserDefaults for lightweight persistence
• Xcode/Swift Package Manager for dependency management