Post

Replies

Boosts

Views

Activity

Reply to how to update SwiftUI Map via MapCamera approach with data from @Observable? (see code)
Here you go // // ContentView.swift // ForumMapsQuestion // // Created by SB on 2024-01-05. // import SwiftUI import MapKit struct ContentView: View { @StateObject var locationMgr = NewLocationManager() @State private var mapCamPos: MapCameraPosition = .automatic var body: some View { ZStack { Map(position: $mapCamPos) .onReceive(locationMgr.$direction) { direction in mapCamPos = .camera(MapCamera( centerCoordinate: self.locationMgr.location.coordinate, distance: 800, heading: direction )) } .onReceive(locationMgr.$location) { location in mapCamPos = .camera(MapCamera( centerCoordinate: location.coordinate, distance: 800, heading: self.locationMgr.direction )) } VStack (alignment: .leading) { VStack { Text("Location from observable: \(locationMgr.location.description)") Text("Direction from observable: \(locationMgr.direction)") } .padding() .background(.gray) .clipShape(RoundedRectangle(cornerSize: CGSize(width: 20, height: 10))) .opacity(0.7) Spacer() } } } } #Preview { ContentView() } final class NewLocationManager: NSObject, ObservableObject, CLLocationManagerDelegate { @Published var location: CLLocation = CLLocation(coordinate: CLLocationCoordinate2D(latitude: 51.500685, longitude: -0.124570), altitude: .zero, horizontalAccuracy: .zero, verticalAccuracy: .zero, timestamp: Date.now) @Published var direction: CLLocationDirection = .zero private let locationManager = CLLocationManager() override init() { super.init() locationManager.startUpdatingLocation() locationManager.delegate = self Task { [weak self] in try? await self?.requestAuthorization() } } func requestAuthorization() async throws { if locationManager.authorizationStatus == .notDetermined { locationManager.requestWhenInUseAuthorization() } } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { locations.forEach { [weak self] location in Task { @MainActor [weak self] in self?.location = location } } } func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) { Task { @MainActor [weak self] in self?.direction = newHeading.trueHeading } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’24
Reply to SwiftUI against SwiftData is too fragile and error prone
Then, you're using the wrong hammer for the nail in question. There are many SwitfUI sample apps available that you can use as a reference for the architecture employed by Apple, such as MVC and MVVM. The language is designed to be semicolonless unless multiple statements are being packed into one line, which then calls for a semicolon. This sounds like your first go at SwiftUI and SwiftData. My recommendation will be to study the SwiftData Sample applications such as BackyardbirdsbuildingAnAppWithSwiftData, to see how Apple approached the issue.
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’23
Reply to How to prevent singleton base class getting re-initialised
import SwiftUI @main struct BlueTest1App: App { @State private var manager = BLEManager() var body: some Scene { WindowGroup { ContentView(bleManager: manager) } } } OR import SwiftUI @main struct BlueTest1App: App { @State private var manager = BLEManager() var body: some Scene { WindowGroup { ContentView() .envitonmentObject(manager) } } } struct ContentView: View { @EnvironmentObject private var bleManager: BLEManager ... }
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’23