Skip to content

Commit 1cb459b

Browse files
committed
added strategy
1 parent 72e1b63 commit 1cb459b

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ public protocol ILocationManagerViewModel: ObservableObject{
6767
@MainActor
6868
var locations : [CLLocation] { get }
6969
70+
/// Strategy for publishing locations Default value is .keepAll
71+
/// .keepLast is optional
72+
var strategy : LMViewModel.Strategy { get }
73+
7074
/// Start streaming locations
7175
func start() async throws
7276
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// Strategy.swift
3+
//
4+
//
5+
// Created by Igor on 10.02.2023.
6+
//
7+
8+
import Foundation
9+
10+
@available(iOS 14.0, watchOS 7.0, *)
11+
public extension LMViewModel{
12+
13+
/// Strategy for publishing locations
14+
enum Strategy{
15+
16+
case keepAll
17+
18+
case keepLast
19+
20+
/// Check if the strategy keep all streamed values
21+
var isKeepLast: Bool{
22+
self == .keepLast
23+
}
24+
25+
/// Check if the strategy keep only the last one
26+
var isKeepAll: Bool{
27+
self == .keepAll
28+
}
29+
30+
}
31+
}

Sources/d3-async-location/protocol/ILocationManagerViewModel.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public protocol ILocationManagerViewModel: ObservableObject{
1515
@MainActor
1616
var locations : [CLLocation] { get }
1717

18+
/// Strategy for publishing locations Default value is .keepAll
19+
var strategy : LMViewModel.Strategy { get }
20+
1821
/// Start streaming locations
1922
func start() async throws
2023

Sources/d3-async-location/viewmodel/LMViewModel.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public final class LMViewModel: ILocationManagerViewModel{
1717

1818
// MARK: - Public
1919

20+
/// Strategy for publishing locations Default value is .keepAll
21+
public let strategy : Strategy
22+
2023
/// List of locations Subscribe different Views to locations publisher to feed them
2124
/// or create a proxy to manipulate with the flow like filtering, dropping, mapping etc
2225
@MainActor @Published public private(set) var locations : [CLLocation] = []
@@ -37,15 +40,20 @@ public final class LMViewModel: ILocationManagerViewModel{
3740
// MARK: - Life circle
3841

3942
/// - Parameters:
43+
/// - strategy: Strategy for publishing locations Default value is .keepAll
4044
/// - accuracy: The accuracy of a geographical coordinate.
4145
/// - activityType: Constants indicating the type of activity associated with location updates.
4246
/// - distanceFilter: A distance in meters from an existing location.
4347
/// - backgroundUpdates: A Boolean value that indicates whether the app receives location updates when running in the background
44-
public init(accuracy : CLLocationAccuracy? = nil,
48+
public init(
49+
strategy : Strategy = .keepLast,
50+
accuracy : CLLocationAccuracy? = nil,
4551
activityType: CLActivityType? = nil,
4652
distanceFilter: CLLocationDistance? = nil,
4753
backgroundUpdates : Bool = false){
4854

55+
self.strategy = strategy
56+
4957
manager = .init(accuracy, activityType, distanceFilter, backgroundUpdates)
5058
}
5159

@@ -96,7 +104,11 @@ public final class LMViewModel: ILocationManagerViewModel{
96104
/// - Parameter coordinate: data
97105
@MainActor
98106
private func add(_ coordinate : CLLocation) {
99-
locations.append(coordinate)
107+
if strategy.isKeepAll || locations.isEmpty{
108+
locations.append(coordinate)
109+
}else if strategy.isKeepLast{
110+
locations[0] = coordinate
111+
}
100112
}
101113

102114
/// Set state

0 commit comments

Comments
 (0)