Skip to content

Commit 0cf9393

Browse files
author
Artur Artikov
committed
Refactor LocationService
1 parent 64e0841 commit 0cf9393

5 files changed

Lines changed: 192 additions & 176 deletions

File tree

core/src/main/kotlin/ru/mobileup/samples/core/location/AndroidLocationService.kt

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,24 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
1111
import kotlinx.coroutines.tasks.await
1212
import kotlinx.coroutines.withTimeout
1313
import ru.mobileup.samples.core.error_handling.LocationNotAvailableException
14+
import kotlin.coroutines.cancellation.CancellationException
1415
import kotlin.time.Duration
15-
import kotlin.time.Duration.Companion.nanoseconds
1616

1717
class AndroidLocationService(
1818
private val context: Context
1919
) : LocationService {
2020

21-
private companion object {
22-
const val DEFAULT_TIMEOUT_SECONDS = 10
23-
}
24-
25-
override suspend fun getCurrentLocation(): GeoCoordinate {
21+
override suspend fun getCurrentLocation(timeout: Duration): GeoCoordinate {
2622
return try {
2723
val fusedLocationProviderClient = LocationServices
2824
.getFusedLocationProviderClient(context)
29-
getLocation(
30-
client = fusedLocationProviderClient,
31-
timeout = DEFAULT_TIMEOUT_SECONDS.nanoseconds
32-
)
25+
getLocation(fusedLocationProviderClient, timeout)
26+
} catch (e: CancellationException) {
27+
throw e
28+
} catch (e: LocationNotAvailableException) {
29+
throw e
3330
} catch (e: Exception) {
34-
throw LocationNotAvailableException(e)
31+
throw LocationNotAvailableException(e, message = null)
3532
}
3633
}
3734

@@ -41,21 +38,19 @@ class AndroidLocationService(
4138
client: FusedLocationProviderClient,
4239
timeout: Duration
4340
): GeoCoordinate {
41+
val cancellationTokenSource = CancellationTokenSource()
4442
return try {
4543
withTimeout(timeout) {
46-
val cancellationTokenSource = CancellationTokenSource()
4744
client
48-
.getCurrentLocation(
49-
PRIORITY_HIGH_ACCURACY,
50-
cancellationTokenSource.token
51-
).await(cancellationTokenSource)
45+
.getCurrentLocation(PRIORITY_HIGH_ACCURACY, cancellationTokenSource.token)
46+
.await(cancellationTokenSource)
5247
.let { location ->
53-
GeoCoordinate(
54-
lat = location.latitude,
55-
lng = location.longitude
56-
)
48+
GeoCoordinate(location.latitude, location.longitude)
5749
}
5850
}
51+
} catch (e: CancellationException) {
52+
cancellationTokenSource.cancel()
53+
throw e
5954
} catch (_: Exception) {
6055
getLastLocation(client)
6156
}
@@ -67,10 +62,7 @@ class AndroidLocationService(
6762
): GeoCoordinate {
6863
val location: Location? = client.lastLocation.await()
6964
return location?.let { location ->
70-
GeoCoordinate(
71-
lat = location.latitude,
72-
lng = location.longitude
73-
)
74-
} ?: throw LocationNotAvailableException(message = "Last location is null")
65+
GeoCoordinate(location.latitude, location.longitude)
66+
} ?: throw LocationNotAvailableException(cause = null, "Last location is null")
7567
}
7668
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package ru.mobileup.samples.core.location
22

33
import ru.mobileup.samples.core.error_handling.LocationNotAvailableException
4+
import kotlin.time.Duration
5+
import kotlin.time.Duration.Companion.seconds
46

57
interface LocationService {
68
@Throws(LocationNotAvailableException::class)
7-
suspend fun getCurrentLocation(): GeoCoordinate
9+
suspend fun getCurrentLocation(timeout: Duration = 10.seconds): GeoCoordinate
810
}

core/src/main/kotlin/ru/mobileup/samples/core/permissions/SinglePermissionResult.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ sealed class SinglePermissionResult {
1313
/**
1414
* Permission has been denied by user
1515
* If [permanently] == true permission was denied permanently (user chose "Never ask again")
16+
* If [automatically] == true permission was denied automatically (user didn't interact with the request)
1617
*/
1718
class Denied(val permanently: Boolean, val automatically: Boolean) : SinglePermissionResult()
1819
}

features/module_graph/modules.dot

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
digraph {
22
audio
3+
bluetooth
34
calendar
45
charts
56
chat
@@ -22,9 +23,11 @@ shared_element_transitions
2223
tutorial
2324
video
2425
work_manager
26+
bluetooth -> charts
2527
photo -> image
2628
photo -> video
2729
root -> audio
30+
root -> bluetooth
2831
root -> calendar
2932
root -> charts
3033
root -> chat

0 commit comments

Comments
 (0)