-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #164 from SwEnt-Group13/feat/map-user-gps-location
Feat/map: user gps location
- Loading branch information
Showing
13 changed files
with
429 additions
and
28 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
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
48 changes: 48 additions & 0 deletions
48
app/src/main/java/com/android/unio/model/map/MapViewModel.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,48 @@ | ||
package com.android.unio.model.map | ||
|
||
import android.content.Context | ||
import android.content.pm.PackageManager | ||
import android.util.Log | ||
import androidx.core.content.ContextCompat | ||
import androidx.lifecycle.ViewModel | ||
import com.google.android.gms.location.FusedLocationProviderClient | ||
import com.google.android.gms.maps.model.LatLng | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
|
||
@HiltViewModel | ||
class MapViewModel | ||
@Inject | ||
constructor(private val fusedLocationClient: FusedLocationProviderClient) : ViewModel() { | ||
|
||
/** State flow that holds the user's location. */ | ||
private val _userLocation = MutableStateFlow<LatLng?>(null) | ||
val userLocation: StateFlow<LatLng?> = _userLocation.asStateFlow() | ||
|
||
/** Fetches the user's location and updates the [_userLocation] state flow. */ | ||
fun fetchUserLocation(context: Context) { | ||
if (hasLocationPermissions(context)) { | ||
try { | ||
fusedLocationClient.lastLocation.addOnSuccessListener { location -> | ||
location?.let { _userLocation.value = LatLng(it.latitude, it.longitude) } | ||
} | ||
} catch (e: SecurityException) { | ||
Log.e("MapViewModel", "Permission for location access was revoked: ${e.localizedMessage}") | ||
} | ||
} else { | ||
Log.e("MapViewModel", "Location permission is not granted.") | ||
} | ||
} | ||
|
||
fun hasLocationPermissions(context: Context): Boolean { | ||
return ContextCompat.checkSelfPermission( | ||
context, android.Manifest.permission.ACCESS_FINE_LOCATION) == | ||
PackageManager.PERMISSION_GRANTED || | ||
ContextCompat.checkSelfPermission( | ||
context, android.Manifest.permission.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
Oops, something went wrong.