mirror of
https://github.com/mayokunadeniyi/Instant-Weather.git
synced 2026-03-13 08:10:17 +08:00
Chore: add air location dependency
This commit is contained in:
6
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
6
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="SameParameterValue" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||
</profile>
|
||||
</component>
|
||||
@@ -149,4 +149,7 @@ dependencies {
|
||||
|
||||
//Elastic view
|
||||
implementation "com.github.skydoves:elasticviews:$elastic_version"
|
||||
|
||||
//Air Location
|
||||
implementation "com.github.mumayank:AirLocation:$air_location_version"
|
||||
}
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
package com.mayokunadeniyi.instantweather.utils
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.location.Location
|
||||
import androidx.lifecycle.LiveData
|
||||
import com.mayokunadeniyi.instantweather.data.model.LocationModel
|
||||
import com.google.android.gms.location.LocationCallback
|
||||
import com.google.android.gms.location.LocationRequest
|
||||
import com.google.android.gms.location.LocationResult
|
||||
import com.google.android.gms.location.LocationServices
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
class LocationLiveData(context: Context) : LiveData<LocationModel>() {
|
||||
|
||||
private var fusedLocationClient = LocationServices.getFusedLocationProviderClient(context)
|
||||
|
||||
override fun onInactive() {
|
||||
super.onInactive()
|
||||
fusedLocationClient.removeLocationUpdates(locationCallback)
|
||||
}
|
||||
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
override fun onActive() {
|
||||
super.onActive()
|
||||
fusedLocationClient.lastLocation
|
||||
.addOnSuccessListener { location: Location? ->
|
||||
location?.also {
|
||||
setLocationData(it)
|
||||
}
|
||||
}
|
||||
startLocationUpdates()
|
||||
}
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
private fun startLocationUpdates() {
|
||||
fusedLocationClient.requestLocationUpdates(
|
||||
locationRequest,
|
||||
locationCallback,
|
||||
null
|
||||
)
|
||||
}
|
||||
|
||||
private val locationCallback = object : LocationCallback() {
|
||||
override fun onLocationResult(locationResult: LocationResult?) {
|
||||
locationResult ?: return
|
||||
for (location in locationResult.locations) {
|
||||
setLocationData(location)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun setLocationData(location: Location) {
|
||||
value = LocationModel(
|
||||
longitude = location.longitude,
|
||||
latitude = location.latitude
|
||||
)
|
||||
}
|
||||
|
||||
companion object {
|
||||
val locationRequest: LocationRequest = LocationRequest.create().apply {
|
||||
interval = TimeUnit.SECONDS.toMillis(60)
|
||||
fastestInterval = TimeUnit.SECONDS.toMillis(30)
|
||||
maxWaitTime = TimeUnit.MINUTES.toMillis(2)
|
||||
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -57,4 +57,5 @@ ext {
|
||||
algolia_version = '2.3.1'
|
||||
paging_version = '2.1.2'
|
||||
elastic_version = '2.0.7'
|
||||
air_location_version = '1.3.1'
|
||||
}
|
||||
Reference in New Issue
Block a user