Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.inova.algym.utils
import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.os.Bundle
import androidx.core.content.ContextCompat
import com.jakewharton.rxrelay2.PublishRelay
class LocationHelper2(val context: Context) {
val relay = PublishRelay.create<LocationStates>()
fun requestLocationInfo() {
"requestLocationInfo".log("LocationHelper2")
if (ContextCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED
) {
// get location manager
val locationManager = getLocationManager()
if (locationManager == null) {
"CONTEXT_NULLABLE".log("LocationHelper2")
relay.accept(LocationStates.CONTEXT_NULLABLE)
} else {
// check location provider states
val isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
val isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
when {
isGPSEnabled -> {
"isGPSEnabled".log("LocationHelper2")
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0f,
listener
)
}
isNetworkEnabled -> {
"isNetworkEnabled".log("LocationHelper2")
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
0,
0f,
listener
)
}
else -> {
"both PROVIDER_DISABLED".log("LocationHelper2")
relay.accept(LocationStates.ProviderDisabled)
}
}
}
} else {
"PERMISION_DENIED".log("LocationHelper2")
relay.accept(LocationStates.PERMISION_DENIED)
}
}
fun gotoEnableProvider() {
}
private fun getLocationManager(): LocationManager? =
context.getSystemService(Context.LOCATION_SERVICE) as? LocationManager
val listener: LocationListener by lazy {
object : LocationListener {
override fun onLocationChanged(location: Location?) {
"onLocationChanged".log("LocationHelper2")
location?.let {
relay.accept(LocationStates.Success(it))
getLocationManager()?.removeUpdates(listener)
}
}
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
"onStatusChanged $status".log("LocationHelper2")
getLocationManager()?.removeUpdates(listener)
}
override fun onProviderEnabled(provider: String?) {
"onProviderEnabled $provider".log("LocationHelper2")
getLocationManager()?.removeUpdates(listener)
}
override fun onProviderDisabled(provider: String?) {
"onProviderDisabled".log("LocationHelper2")
getLocationManager()?.removeUpdates(listener)
relay.accept(LocationStates.ProviderDisabled)
}
}
}
}
@MahmoudMabrok

This comment has been minimized.

Copy link
Owner Author

@MahmoudMabrok MahmoudMabrok commented May 7, 2020

  val helper = LocationHelper2(requireContext())
        helper.requestLocationInfo()
        helper.relay.bind()
            .subscribe {
                when (it) {
                    is LocationStates.Success -> {
                        "Success ${it.location}".log("UserHotdelasFragment")
                    }

                    is LocationStates.PERMISION_DENIED -> {
                        "PERMISION_DENIED".log("UserHotdelasFragment")

                    }
                    is LocationStates.ProviderDisabled -> {
                        "PROVIDER_DISABLED".log("UserHotdelasFragment")

                    }
                    is LocationStates.CONTEXT_NULLABLE -> {
                        "CONTEXT_NULLABLE".log("UserHotdelasFragment")

                    }
                }
            }
            .disposedBy(bag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment