Skip to content

Commit

Permalink
Multiplatform: add nearby api
Browse files Browse the repository at this point in the history
  • Loading branch information
joreilly committed Jun 2, 2019
1 parent 82b457d commit 6ccd66f
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 164 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ fastlane/metadata/android/screenshots.html
app/release

api-*.json

ios/GalwayBusApp/GalwayBusApp.xcworkspace/xcuserdata
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,41 @@ import kotlinx.coroutines.launch

class GalwayBusRepository {

private val galwayBusApi = GalwayBusApi()

suspend fun fetchBusStops(): List<BusStop> {
val galwayBusApi = GalwayBusApi()
return galwayBusApi.fetchBusStops()
}

fun fetchBusStops(success: (List<BusStop>) -> Unit) {
GlobalScope.launch(ApplicationDispatcher) {
val galwayBusApi = GalwayBusApi()
success(galwayBusApi.fetchBusStops())
success(fetchBusStops())
}
}


suspend fun fetchBusRoutes(): List<BusRoute> {
val galwayBusApi = GalwayBusApi()
val busRoutes = galwayBusApi.fetchBusRoutes()
return transformBusRouteMapToList(busRoutes)
}

fun fetchBusRoutes(success: (List<BusRoute>) -> Unit) {
GlobalScope.launch(ApplicationDispatcher) {
val galwayBusApi = GalwayBusApi()
val busRoutes = galwayBusApi.fetchBusRoutes()
success(transformBusRouteMapToList(busRoutes))
success(fetchBusRoutes())
}
}

suspend fun getNearestStops(latitude: Double, longitude: Double): List<BusStop> {
return galwayBusApi.getNearestStops(latitude, longitude)
}

fun getNearestStops(latitude: Double, longitude: Double, success: (List<BusStop>) -> Unit) {
GlobalScope.launch(ApplicationDispatcher) {
success(getNearestStops(latitude, longitude))
}
}


private fun transformBusRouteMapToList(busRoutesMap: Map<String, BusRoute>): List<BusRoute> {
val busRouteList = mutableListOf<BusRoute>()
busRoutesMap.values.forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.ktor.client.HttpClient
import io.ktor.client.features.json.JsonFeature
import io.ktor.client.features.json.serializer.KotlinxSerializer
import io.ktor.client.request.get
import io.ktor.client.request.parameter
import io.ktor.client.request.url
import kotlinx.serialization.KSerializer
import kotlinx.serialization.internal.StringSerializer
Expand All @@ -27,7 +28,6 @@ class GalwayBusApi() {
setMapper(BusStop::class, BusStop.serializer())
//setListMapper(BusStop::class, BusStop.serializer())
}

}
}
}
Expand All @@ -44,7 +44,15 @@ class GalwayBusApi() {
val jsonArrayString = client.get<String> {
url("$baseUrl/stops.json")
}
return Json.nonstrict.parse(BusStop.serializer().list, jsonArrayString)
}

suspend fun getNearestStops(latitude: Double, longitude: Double): List<BusStop> {
val jsonArrayString = client.get<String> {
url("$baseUrl/stops/nearby.json")
parameter("latitude", latitude)
parameter("longitude", longitude)
}
return Json.nonstrict.parse(BusStop.serializer().list, jsonArrayString)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,13 @@ class GalwayBusApplication : Application() {
// exploring multiplatform kotlin
val repo = GalwayBusRepository()
GlobalScope.launch {
val stops = repo.fetchBusStops()
val stops = repo.getNearestStops(53.2743394, -9.0514163)
Logger.d(stops)
}

repo.fetchBusRoutes {
Logger.d(it)
}


}

Logger.i("GalwayBusApplication init completed")
}

Expand Down
Binary file not shown.

This file was deleted.

5 changes: 5 additions & 0 deletions ios/GalwayBusApp/GalwayBusApp/StopsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class FirstViewController: UIViewController, UITableViewDataSource, UITableViewD
self.tableView.reloadData()
return KotlinUnit()
})


g.getNearestStops(latitude: 53.2743394, longitude: -9.0514163, success: { data in
return KotlinUnit()
})
}


Expand Down

0 comments on commit 6ccd66f

Please sign in to comment.