@@ -5,16 +5,28 @@ import androidx.lifecycle.Lifecycle
5
5
import androidx.lifecycle.LifecycleObserver
6
6
import androidx.lifecycle.OnLifecycleEvent
7
7
import androidx.lifecycle.ProcessLifecycleOwner
8
+ import cloud.pace.sdk.api.API
9
+ import cloud.pace.sdk.api.model.Categories
10
+ import cloud.pace.sdk.api.model.Fuel
11
+ import cloud.pace.sdk.api.model.PriceHistory
12
+ import cloud.pace.sdk.api.model.RegionalPrices
13
+ import cloud.pace.sdk.api.request.gasStations.getGasStation
14
+ import cloud.pace.sdk.api.request.metadataFilters.getMetadataFilters
15
+ import cloud.pace.sdk.api.request.priceHistories.getPriceHistory
16
+ import cloud.pace.sdk.api.request.prices.getRegionalPrices
8
17
import cloud.pace.sdk.poikit.database.POIKitDatabase
9
18
import cloud.pace.sdk.poikit.poi.*
10
- import cloud.pace.sdk.poikit.poi.download.*
19
+ import cloud.pace.sdk.poikit.poi.download.GasStationCodes
20
+ import cloud.pace.sdk.poikit.poi.download.GasStationMovedResponse
21
+ import cloud.pace.sdk.poikit.poi.downloadOld.TileDownloader
11
22
import cloud.pace.sdk.poikit.routing.NavigationApiClient
12
23
import cloud.pace.sdk.poikit.routing.NavigationMode
13
24
import cloud.pace.sdk.poikit.routing.NavigationRequest
14
25
import cloud.pace.sdk.poikit.routing.Route
15
26
import cloud.pace.sdk.poikit.search.AddressSearchClient
16
27
import cloud.pace.sdk.poikit.search.AddressSearchRequest
17
28
import cloud.pace.sdk.poikit.search.PhotonResult
29
+ import cloud.pace.sdk.poikit.utils.ApiException
18
30
import cloud.pace.sdk.utils.*
19
31
import com.google.android.gms.maps.model.VisibleRegion
20
32
import io.reactivex.rxjava3.core.Observable
@@ -25,11 +37,7 @@ object POIKit : POIKitKoinComponent, LifecycleObserver {
25
37
26
38
private val database: POIKitDatabase by inject()
27
39
private val navigationApi: NavigationApiClient by inject()
28
- private val poiApi: PoiApiClient by inject()
29
40
private val addressSearchApi: AddressSearchClient by inject()
30
- private val dynamicFilterApi: DynamicFilterApiClient by inject()
31
- private val priceHistoryApi: PriceHistoryApiClient by inject()
32
- private val gasStationApi: GasStationApiClient by inject()
33
41
private val locationProvider: LocationProvider by inject()
34
42
var maxPoiSearchBoxSize = 15000.0
35
43
@@ -82,24 +90,109 @@ object POIKit : POIKitKoinComponent, LifecycleObserver {
82
90
}
83
91
}
84
92
85
- fun getRegionalPrice (latitude : Double , longitude : Double , completion : (Completion <List <RegionalPriceResponse >? >) -> Unit ) {
86
- poiApi.getRegionalPrices(latitude, longitude, completion)
93
+ fun getRegionalPrice (latitude : Float , longitude : Float , completion : (Completion <List <RegionalPrices >? >) -> Unit ) {
94
+ API .prices.getRegionalPrices(latitude, longitude).enqueue {
95
+ onResponse = {
96
+ val body = it.body()
97
+ if (it.isSuccessful && body != null ) {
98
+ completion(Success (body))
99
+ } else {
100
+ completion(Failure (ApiException (it.code(), it.message())))
101
+ }
102
+ }
103
+
104
+ onFailure = {
105
+ completion(Failure (it ? : Exception (" Unknown exception" )))
106
+ }
107
+ }
87
108
}
88
109
89
110
fun searchAddress (request : AddressSearchRequest ): Observable <PhotonResult > {
90
111
return addressSearchApi.searchAddress(request)
91
112
}
92
113
93
- fun getDynamicFilters (latitude : Double , longitude : Double , completion : (Completion <DynamicFilterResponse ?>) -> Unit ) {
94
- dynamicFilterApi.getDynamicFilters(latitude, longitude, completion)
114
+ fun getDynamicFilters (latitude : Double , longitude : Double , completion : (Completion <Categories ?>) -> Unit ) {
115
+ API .metadataFilters.getMetadataFilters(latitude.toFloat(), longitude.toFloat()).enqueue {
116
+ onResponse = {
117
+ val body = it.body()
118
+ if (it.isSuccessful && body != null ) {
119
+ completion(Success (body))
120
+ } else {
121
+ completion(Failure (ApiException (it.code(), it.message())))
122
+ }
123
+ }
124
+
125
+ onFailure = {
126
+ completion(Failure (it ? : Exception (" Unknown exception" )))
127
+ }
128
+ }
95
129
}
96
130
97
- fun getPriceHistory (id : String , fuelType : FuelType , from : Date , to : Date , completion : (Completion <PriceHistoryApiResponse ?>) -> Unit ) {
98
- priceHistoryApi.getPriceHistory(id, fuelType.value, from, to, completion)
131
+ fun getPriceHistory (id : String , fuelType : Fuel , from : Date , to : Date , completion : (Completion <PriceHistory ?>) -> Unit ) {
132
+ API .priceHistories.getPriceHistory(id, fuelType, from, to).enqueue {
133
+ onResponse = {
134
+ val body = it.body()
135
+ if (it.isSuccessful && body != null ) {
136
+ completion(Success (body))
137
+ } else {
138
+ completion(Failure (ApiException (it.code(), it.message())))
139
+ }
140
+ }
141
+
142
+ onFailure = {
143
+ completion(Failure (it ? : Exception (" Unknown exception" )))
144
+ }
145
+ }
99
146
}
100
147
101
148
fun getGasStation (id : String , compileOpeningHours : Boolean , forMovedGasStation : Boolean , completion : (Completion <GasStationMovedResponse >) -> Unit ) {
102
- gasStationApi.getGasStation(id, compileOpeningHours, forMovedGasStation, completion)
149
+ API .gasStations.getGasStation(id, compileOpeningHours).enqueue {
150
+ onResponse = {
151
+ when (it.code()) {
152
+ GasStationCodes .STATUS_MOVED -> {
153
+ val newUuid: String? = it.headers().values(GasStationCodes .HEADER_LOCATION ).first()?.split(" /" )?.last()
154
+ if (newUuid.isNotNullOrEmpty()) {
155
+ completion(Success (GasStationMovedResponse (newUuid, true , null , null )))
156
+ } else {
157
+ completion(Success (GasStationMovedResponse (null , true , null , null )))
158
+ }
159
+ }
160
+ GasStationCodes .STATUS_OK -> {
161
+ val priorResponse = it.raw().priorResponse()
162
+ if (priorResponse != null ) {
163
+ val newUuid = priorResponse.headers().values(GasStationCodes .HEADER_LOCATION ).first()?.split(" /" )?.last()
164
+ if (newUuid.isNotNullOrEmpty()) {
165
+ completion(Success (GasStationMovedResponse (newUuid, true , null , null )))
166
+ } else {
167
+ completion(Success (GasStationMovedResponse (null , false , null , null )))
168
+ }
169
+ } else {
170
+ if (forMovedGasStation)
171
+ completion(
172
+ Success (
173
+ GasStationMovedResponse (
174
+ null , true , it.body()?.latitude?.toDouble(),
175
+ it.body()?.longitude?.toDouble()
176
+ )
177
+ )
178
+ )
179
+ else
180
+ completion(Success (GasStationMovedResponse (null , false , null , null )))
181
+ }
182
+ }
183
+ GasStationCodes .STATUS_NOT_FOUND -> {
184
+ completion(Success (GasStationMovedResponse (null , true , null , null )))
185
+ }
186
+ else -> {
187
+ completion(Failure (Exception (" Server error" )))
188
+ }
189
+ }
190
+ }
191
+
192
+ onFailure = {
193
+ completion(Failure (it ? : Exception (" Unknown exception" )))
194
+ }
195
+ }
103
196
}
104
197
105
198
fun getGasStationLocal (vararg ids : String , completion : (Completion <List <GasStation >>) -> Unit ) {
0 commit comments