Skip to content

Commit

Permalink
Merge pull request #4 from icerockdev/develop
Browse files Browse the repository at this point in the history
Release 0.1.1
  • Loading branch information
Alex009 authored Dec 30, 2019
2 parents 7ef8c19 + 71da954 commit 8ca2e3c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
51 changes: 48 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This is a Kotlin Multiplatform library that provides geolocation to common code.
- [License](#license)

## Features
- **** - ;
- **Geolocation tracking** - track user geolocation from common code;

## Requirements
- Gradle version 5.4.1+
Expand All @@ -26,6 +26,7 @@ This is a Kotlin Multiplatform library that provides geolocation to common code.
## Versions
- kotlin 1.3.61
- 0.1.0
- 0.1.1

## Installation
root build.gradle
Expand All @@ -40,7 +41,7 @@ allprojects {
project build.gradle
```groovy
dependencies {
commonMainApi("dev.icerock.moko:geo:0.1.0")
commonMainApi("dev.icerock.moko:geo:0.1.1")
}
```

Expand All @@ -50,7 +51,51 @@ enableFeaturePreview("GRADLE_METADATA")
```

## Usage
TODO
in common code:
```kotlin
class TrackerViewModel(
val locationTracker: LocationTracker
) : ViewModel() {

init {
viewModelScope.launch {
locationTracker.getLocationsFlow()
.distinctUntilChanged()
.collect { println("new location: $it") }
}
}

fun onStartPressed() {
viewModelScope.launch { locationTracker.startTracking() }
}

fun onStopPressed() {
locationTracker.stopTracking()
}
}
```

In Android:
```kotlin
// create ViewModel
val locationTracker = LocationTracker(
permissionsController = PermissionsController(applicationContext = applicationContext)
)
val viewModel = TrackerViewModel(locationTracker)

// bind tracker to lifecycle
viewModel.locationTracker.bind(lifecycle, this, supportFragmentManager)
```

In iOS:
```swift
let viewModel = TrackerViewModel(
locationTracker: LocationTracker(
permissionsController: PermissionsController(),
accuracy: kCLLocationAccuracyBest
)
)
```

## Samples
Please see more examples in the [sample directory](sample).
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object Versions {

object MultiPlatform {
const val coroutines = "1.3.3"
const val mokoGeo = "0.1.0"
const val mokoGeo = "0.1.1"
const val mokoParcelize = "0.2.0"
const val mokoPermissions = "0.3.0"
const val mokoMvvm = "0.4.0"
Expand Down
12 changes: 10 additions & 2 deletions geo/src/iosMain/kotlin/dev/icerock/moko/geo/LocationTracker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import platform.CoreLocation.kCLLocationAccuracyBest
import platform.Foundation.NSError
import platform.Foundation.NSLog
import platform.darwin.NSObject
import kotlin.native.ref.WeakReference

actual class LocationTracker(
actual val permissionsController: PermissionsController,
accuracy: CLLocationAccuracy = kCLLocationAccuracyBest
) {
private val tracker = Tracker()
private val tracker = Tracker(this)
private val locationManager = CLLocationManager().apply {
delegate = tracker
desiredAccuracy = accuracy
Expand Down Expand Up @@ -60,9 +61,16 @@ actual class LocationTracker(
}
}

inner class Tracker: NSObject(), CLLocationManagerDelegateProtocol {
private class Tracker(
locationTracker: LocationTracker
) : NSObject(), CLLocationManagerDelegateProtocol {
private val locationTracker = WeakReference(locationTracker)

override fun locationManager(manager: CLLocationManager, didUpdateLocations: List<*>) {
val locations = didUpdateLocations as List<CLLocation>
val locationTracker = locationTracker.get() ?: return
val trackerScope = locationTracker.trackerScope
val locationsChannel = locationTracker.locationsChannel

locations.forEach { location ->
val latLng = location.coordinate().useContents {
Expand Down

0 comments on commit 8ca2e3c

Please # to comment.