Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change ObservableObject to Observable Macros #56

Merged
merged 5 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Examples/OTPKitDemo/OTPKitDemo/MapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import OTPKit
import SwiftUI

struct MapView: View {
@EnvironmentObject private var tripPlanner: TripPlannerService
@Environment(TripPlannerService.self) private var tripPlanner

var body: some View {
TripPlannerExtensionView {
Map(position: $tripPlanner.currentCameraPosition, interactionModes: .all) {
Map(position: tripPlanner.currentCameraPositionBinding, interactionModes: .all) {
tripPlanner.generateMarkers()
tripPlanner.generateMapPolyline()
.stroke(.blue, lineWidth: 5)
Expand All @@ -26,7 +26,6 @@ struct MapView: View {
}
}
}
.environmentObject(tripPlanner)
}
}

Expand All @@ -38,5 +37,4 @@ struct MapView: View {
)

return MapView()
.environmentObject(planner)
}
5 changes: 4 additions & 1 deletion Examples/OTPKitDemo/OTPKitDemo/OTPKitDemoApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ struct OTPKitDemoApp: App {
searchCompleter: MKLocalSearchCompleter()
)

let sheetEnvironment = OriginDestinationSheetEnvironment()

var body: some Scene {
WindowGroup {
MapView()
.environmentObject(tripPlannerService)
.environment(tripPlannerService)
.environment(sheetEnvironment)
}
}
}
8 changes: 5 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ let package = Package(
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "OTPKit",
targets: ["OTPKit"])
targets: ["OTPKit"]
)
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
Expand All @@ -28,7 +29,8 @@ let package = Package(
// Copy Tests/ExampleTests/Resources directories as-is.
// Use to retain directory structure.
// Will be at top level in bundle.
.process("Resources"),
]),
.process("Resources")
]
)
]
)
3 changes: 1 addition & 2 deletions Sources/OTPKit/Features/MapExtension/MapMarkingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI
/// View for Map Marking Mode
/// User able to add Marking directly from the map
public struct MapMarkingView: View {
@EnvironmentObject private var tripPlanner: TripPlannerService
@Environment(TripPlannerService.self) private var tripPlanner

public init() {}
public var body: some View {
Expand Down Expand Up @@ -54,5 +54,4 @@ public struct MapMarkingView: View {

#Preview {
MapMarkingView()
.environmentObject(PreviewHelpers.buildTripPlannerService())
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,25 @@ import SwiftUI

/// OriginDestinationSheetEnvironment responsible for manage the environment of `OriginDestination` features
/// - sheetState: responsible for managing shown sheet in `OriginDestinationView`
/// - selectedValue: responsible for managing selected value when user taped the list in `OriginDestinationSheetView`
public final class OriginDestinationSheetEnvironment: ObservableObject {
@Published public var isSheetOpened = false
@Published public var selectedValue: String = ""
/// - selectedValue: responsible for managing selected value when user taped the list in `OriginDestinationSheetView
@Observable
public final class OriginDestinationSheetEnvironment {
public var isSheetOpened = false
public var selectedValue: String = ""

// This responsible for showing favorite locations and recent locations in sheets
@Published public var favoriteLocations: [Location] = []
@Published public var recentLocations: [Location] = []
public var favoriteLocations: [Location] = []
public var recentLocations: [Location] = []

/// Selected detail favorite locations that will be shown in `FavoriteLocationDetailSheet`
@Published public var selectedDetailFavoriteLocation: Location?
public var selectedDetailFavoriteLocation: Location?

public var isSheetOpenedBinding: Binding<Bool> {
Binding(
get: { self.isSheetOpened },
set: { newValue in self.isSheetOpened = newValue }
)
}

// Public initializer
public init() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import SwiftUI
/// OriginDestinationView is the main view for setting up Origin/Destination in OTPKit.
/// It consists a list of Origin and Destination along with the `MapKit`
public struct OriginDestinationView: View {
@EnvironmentObject private var sheetEnvironment: OriginDestinationSheetEnvironment
@EnvironmentObject private var tripPlanner: TripPlannerService
@Environment(OriginDestinationSheetEnvironment.self) private var sheetEnvironment
@Environment(TripPlannerService.self) private var tripPlanner
@State private var isSheetOpened = false

// Public Initializer
Expand Down Expand Up @@ -63,5 +63,4 @@ public struct OriginDestinationView: View {

#Preview {
OriginDestinationView()
.environmentObject(PreviewHelpers.buildTripPlannerService())
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import SwiftUI
/// Users can search and add their favorite locations
public struct AddFavoriteLocationsSheet: View {
@Environment(\.dismiss) var dismiss
@EnvironmentObject private var sheetEnvironment: OriginDestinationSheetEnvironment
@EnvironmentObject private var tripPlanner: TripPlannerService
@Environment(OriginDestinationSheetEnvironment.self) private var sheetEnvironment
@Environment(TripPlannerService.self) private var tripPlanner

@State private var search = ""

Expand Down Expand Up @@ -108,5 +108,4 @@ public struct AddFavoriteLocationsSheet: View {

#Preview {
AddFavoriteLocationsSheet()
.environmentObject(PreviewHelpers.buildTripPlannerService())
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftUI
/// Users can see the details and delete the location sheet
public struct FavoriteLocationDetailSheet: View {
@Environment(\.dismiss) private var dismiss
@EnvironmentObject private var sheetEnvironment: OriginDestinationSheetEnvironment
@Environment(OriginDestinationSheetEnvironment.self) private var sheetEnvironment

@State private var isShowErrorAlert = false
@State private var errorMessage = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import SwiftUI
/// Show all the lists of favorite locations
public struct MoreFavoriteLocationsSheet: View {
@Environment(\.dismiss) private var dismiss
@EnvironmentObject private var sheetEnvironment: OriginDestinationSheetEnvironment
@EnvironmentObject private var tripPlanner: TripPlannerService
@Environment(OriginDestinationSheetEnvironment.self) private var sheetEnvironment
@Environment(TripPlannerService.self) private var tripPlanner

@State private var isDetailSheetOpened = false

Expand All @@ -36,13 +36,11 @@ public struct MoreFavoriteLocationsSheet: View {
}
.sheet(isPresented: $isDetailSheetOpened, content: {
FavoriteLocationDetailSheet()
.environmentObject(sheetEnvironment)
})
}
}
}

#Preview {
MoreFavoriteLocationsSheet()
.environmentObject(PreviewHelpers.buildTripPlannerService())
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftUI
public struct MoreRecentLocationsSheet: View {
@Environment(\.dismiss) var dismiss

@EnvironmentObject private var sheetEnvironment: OriginDestinationSheetEnvironment
@Environment(OriginDestinationSheetEnvironment.self) private var sheetEnvironment

public var body: some View {
VStack {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import SwiftUI
///
public struct OriginDestinationSheetView: View {
@Environment(\.dismiss) var dismiss
@EnvironmentObject var sheetEnvironment: OriginDestinationSheetEnvironment
@EnvironmentObject private var tripPlanner: TripPlannerService
@Environment(OriginDestinationSheetEnvironment.self) private var sheetEnvironment
@Environment(TripPlannerService.self) private var tripPlanner

@State private var search: String = ""

Expand Down Expand Up @@ -221,18 +221,15 @@ public struct OriginDestinationSheetView: View {
switch presentation {
case .addFavoriteSheet:
AddFavoriteLocationsSheet()
.environmentObject(sheetEnvironment)
.environmentObject(tripPlanner)

case .moreFavoritesSheet:
MoreFavoriteLocationsSheet()
.environmentObject(sheetEnvironment)
.environmentObject(tripPlanner)

case .favoriteDetailsSheet:
FavoriteLocationDetailSheet()
.environmentObject(sheetEnvironment)

case .moreRecentsSheet:
MoreRecentLocationsSheet()
.environmentObject(sheetEnvironment)
}
}
.alert(isPresented: $isShowErrorAlert) {
Expand All @@ -248,6 +245,4 @@ public struct OriginDestinationSheetView: View {

#Preview {
OriginDestinationSheetView()
.environmentObject(OriginDestinationSheetEnvironment())
.environmentObject(PreviewHelpers.buildTripPlannerService())
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import MapKit
import SwiftUI

public struct DirectionSheetView: View {
@EnvironmentObject private var tripPlanner: TripPlannerService
@Environment(TripPlannerService.self) private var tripPlanner
@Environment(\.dismiss) private var dismiss
@Binding var sheetDetent: PresentationDetent
@State private var scrollToItem: String?
Expand Down Expand Up @@ -108,5 +108,4 @@ public struct DirectionSheetView: View {

#Preview {
DirectionSheetView(sheetDetent: .constant(.fraction(0.2)))
.environmentObject(PreviewHelpers.buildTripPlannerService())
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

public struct TripPlannerSheetView: View {
@EnvironmentObject private var tripPlanner: TripPlannerService
@Environment(TripPlannerService.self) private var tripPlanner
@Environment(\.dismiss) var dismiss

public init() {}
Expand Down Expand Up @@ -99,5 +99,4 @@ public struct TripPlannerSheetView: View {

#Preview {
TripPlannerSheetView()
.environmentObject(PreviewHelpers.buildTripPlannerService())
}
3 changes: 1 addition & 2 deletions Sources/OTPKit/Features/TripPlanner/TripPlannerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

public struct TripPlannerView: View {
@EnvironmentObject private var tripPlanner: TripPlannerService
@Environment(TripPlannerService.self) private var tripPlanner

public init(text: String) {
self.text = text
Expand Down Expand Up @@ -46,5 +46,4 @@ public struct TripPlannerView: View {

#Preview {
TripPlannerView(text: "43 minutes, departs at 4:15 PM")
.environmentObject(PreviewHelpers.buildTripPlannerService())
}
1 change: 0 additions & 1 deletion Sources/OTPKit/Network/RestAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import Foundation

/// An actor representing a REST API client for making network requests
public actor RestAPI {

/// Initializes a new instance of RestAPI
///
/// - Parameters:
Expand Down
59 changes: 42 additions & 17 deletions Sources/OTPKit/Services/TripPlannerService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,25 @@ import Foundation
import MapKit
import SwiftUI

// swiftlint:disable file_length
/// Services to manage all functions related to trip planning
public final class TripPlannerService: NSObject, ObservableObject {
@Observable
public final class TripPlannerService: NSObject {
// MARK: - Properties

private let apiClient: RestAPI

// Trip Planner
@Published public var planResponse: OTPResponse?
@Published public var isFetchingResponse = false
@Published public var tripPlannerErrorMessage: String?
@Published public var selectedItinerary: Itinerary?
@Published public var isStepsViewPresented = false
public var planResponse: OTPResponse?
public var isFetchingResponse = false
public var tripPlannerErrorMessage: String?
public var selectedItinerary: Itinerary?
public var isStepsViewPresented = false

// Origin Destination
@Published public var originDestinationState: OriginDestinationState = .origin
@Published public var originCoordinate: CLLocationCoordinate2D?
@Published public var destinationCoordinate: CLLocationCoordinate2D?
public var originDestinationState: OriginDestinationState = .origin
public var originCoordinate: CLLocationCoordinate2D?
public var destinationCoordinate: CLLocationCoordinate2D?

// Location Search
private let searchCompleter: MKLocalSearchCompleter
Expand All @@ -34,24 +36,46 @@ public final class TripPlannerService: NSObject, ObservableObject {
private var currentRegion: MKCoordinateRegion?
private var searchTask: Task<Void, Never>?

@Published var completions = [Location]()
var completions = [Location]()

// Map Extension
@Published public var selectedMapPoint: [String: MarkerItem?] = [
public var selectedMapPoint: [String: MarkerItem?] = [
"origin": nil,
"destination": nil
]

@Published public var isMapMarkingMode = false
@Published public var currentCameraPosition: MapCameraPosition = .userLocation(fallback: .automatic)
public var isMapMarkingMode = false
public var currentCameraPosition: MapCameraPosition = .userLocation(fallback: .automatic)

@Published public var originName = "Origin"
@Published public var destinationName = "Destination"
public var originName = "Origin"
public var destinationName = "Destination"

// User Location
@Published var currentLocation: Location?
var currentLocation: Location?
private let locationManager: CLLocationManager

// View Bindings
public var isStepsViewPresentedBinding: Binding<Bool> {
Binding(
get: { self.isStepsViewPresented },
set: { _ in }
)
}

public var isPlanResponsePresentedBinding: Binding<Bool> {
Binding(
get: { self.planResponse != nil && self.isStepsViewPresented == false },
set: { _ in }
)
}

public var currentCameraPositionBinding: Binding<MapCameraPosition> {
Binding(
get: { self.currentCameraPosition },
set: { _ in }
)
}

// MARK: - Initialization

/// Initializes a new instance of TripPlannerService
Expand Down Expand Up @@ -365,7 +389,6 @@ extension TripPlannerService: CLLocationManagerDelegate {
// MARK: - Service Extension

extension TripPlannerService {

/// Formats a coordinate into a string representation
///
/// - Parameter coordinate: The coordinate to format
Expand Down Expand Up @@ -399,3 +422,5 @@ extension TripPlannerService {
return dateFormatter.string(from: currentDate)
}
}

// swiftlint:enable file_length
Loading
Loading