-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
216 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
app/src/main/java/com/obrekht/coffeeshops/app/utils/DistanceFormatter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.obrekht.coffeeshops.app.utils | ||
|
||
import android.content.Context | ||
import com.obrekht.coffeeshops.R | ||
import java.text.NumberFormat | ||
|
||
fun Long.formatDistance(context: Context): String { | ||
val value = when { | ||
this > 1000 -> this / 1000.0 | ||
else -> this | ||
} | ||
val unit = when { | ||
this > 1000 -> context.getString(R.string.unit_short_kilometers) | ||
else -> context.getString(R.string.unit_short_meters) | ||
} | ||
return NumberFormat.getNumberInstance().apply { | ||
maximumFractionDigits = 1 | ||
}.format(value) + " $unit" | ||
} |
16 changes: 16 additions & 0 deletions
16
app/src/main/java/com/obrekht/coffeeshops/app/utils/PermissionExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.obrekht.coffeeshops.app.utils | ||
|
||
import android.Manifest.permission.ACCESS_COARSE_LOCATION | ||
import android.content.Context | ||
import android.content.pm.PackageManager | ||
import androidx.core.content.ContextCompat | ||
|
||
fun Context.hasLocationPermission(): Boolean { | ||
return ContextCompat.checkSelfPermission( | ||
this, | ||
ACCESS_COARSE_LOCATION | ||
) == PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission( | ||
this, | ||
ACCESS_COARSE_LOCATION | ||
) == PackageManager.PERMISSION_GRANTED | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
app/src/main/java/com/obrekht/coffeeshops/geolocation/data/di/GeoLocationRepositoryModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.obrekht.coffeeshops.geolocation.data.di | ||
|
||
import com.obrekht.coffeeshops.geolocation.data.repository.GeoLocationRepository | ||
import com.obrekht.coffeeshops.geolocation.data.repository.PlayServicesGeoLocationRepository | ||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
abstract class GeoLocationRepositoryModule { | ||
|
||
@Singleton | ||
@Binds | ||
abstract fun bindGeoLocationRepository( | ||
repository: PlayServicesGeoLocationRepository | ||
): GeoLocationRepository | ||
} |
12 changes: 12 additions & 0 deletions
12
...rc/main/java/com/obrekht/coffeeshops/geolocation/data/repository/GeoLocationRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.obrekht.coffeeshops.geolocation.data.repository | ||
|
||
import android.location.Location | ||
import kotlinx.coroutines.flow.StateFlow | ||
|
||
interface GeoLocationRepository { | ||
|
||
val currentLocation: StateFlow<Location> | ||
|
||
fun refreshCurrentLocation(withLastLocation: Boolean = false) | ||
fun getDistanceToLocation(latitude: Double, longitude: Double): Float? | ||
} |
64 changes: 64 additions & 0 deletions
64
.../com/obrekht/coffeeshops/geolocation/data/repository/PlayServicesGeoLocationRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.obrekht.coffeeshops.geolocation.data.repository | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Context | ||
import android.location.Location | ||
import com.google.android.gms.location.LocationServices | ||
import com.google.android.gms.location.Priority | ||
import com.google.android.gms.tasks.CancellationTokenSource | ||
import com.obrekht.coffeeshops.app.utils.hasLocationPermission | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import javax.inject.Inject | ||
|
||
class PlayServicesGeoLocationRepository @Inject constructor( | ||
@ApplicationContext private val context: Context | ||
): GeoLocationRepository { | ||
|
||
private val _currentLocation = MutableStateFlow(Location(null)) | ||
override val currentLocation = _currentLocation.asStateFlow() | ||
|
||
private val fusedLocationClient = LocationServices.getFusedLocationProviderClient(context) | ||
|
||
init { | ||
refreshCurrentLocation(true) | ||
} | ||
|
||
@SuppressLint("MissingPermission") | ||
override fun refreshCurrentLocation(withLastLocation: Boolean) { | ||
if (!context.hasLocationPermission()) return | ||
|
||
if (withLastLocation) { | ||
fusedLocationClient.lastLocation.addOnSuccessListener { location: Location? -> | ||
location?.let { | ||
_currentLocation.value = it | ||
} | ||
} | ||
} | ||
|
||
fusedLocationClient.getCurrentLocation( | ||
Priority.PRIORITY_HIGH_ACCURACY, | ||
CancellationTokenSource().token | ||
).addOnSuccessListener { location: Location? -> | ||
location?.let { | ||
_currentLocation.value = it | ||
} | ||
} | ||
} | ||
|
||
override fun getDistanceToLocation(latitude: Double, longitude: Double): Float? { | ||
val location = _currentLocation.value | ||
if (location.provider == null) { | ||
return null | ||
} | ||
|
||
val results = FloatArray(3) | ||
Location.distanceBetween( | ||
location.latitude, location.longitude, | ||
latitude, longitude, | ||
results | ||
) | ||
return results[0] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters