Skip to content

Commit

Permalink
show loading function added in location manager, fix location fetch i…
Browse files Browse the repository at this point in the history
…ssue in fragment
  • Loading branch information
nawinkhatiwada committed Dec 15, 2019
1 parent d8ea33d commit cd7c1b1
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ abstract class BaseFragment : Fragment(), LocationListener {

fun initLocationManager(): LocationManager? {
locationManager = LocationManager.Builder(requireActivity().applicationContext)
.showLoading(true)
.showLoading(false)
.setListener(this)
.setFragment(this)
.setRequestTimeOut(LocationConstants.TIME_OUT_LONG)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import com.androidbolts.library.LocationManager
import com.androidbolts.library.gps.GpsProvider
Expand All @@ -27,18 +28,25 @@ class LocationFragment private constructor() : BaseFragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_location, container, false)
}
val view = inflater.inflate(R.layout.fragment_location, container, false)
val btnGetLocation = view.findViewById<Button>(R.id.btn_get_loc)
locationManager = initLocationManager()
lifecycle.addObserver(locationManager!!)

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
getLocation()
btnGetLocation.setOnClickListener {
getLocation()
}
return view
}


private fun getLocation() {
locationManager = initLocationManager()
lifecycle.addObserver(locationManager!!)
locationManager?.getLocation()
locationManager?.let {
if (!it.isLoadingSet()) {
locationManager?.setShowLoading(true)
}
locationManager?.getLocation()
}
}

override fun onLocationChanged(location: Location?) {
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/layout/fragment_location.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@
android:textStyle="bold"
tools:text="Hello World!" />

<Button
android:id="@+id/btn_get_loc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get location"/>

</LinearLayout>
65 changes: 38 additions & 27 deletions library/src/main/java/com/androidbolts/library/LocationManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.androidbolts.library

import android.app.Activity
import android.content.Context
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
Expand All @@ -12,17 +11,19 @@ import com.androidbolts.library.gps.GpsProvider
import com.androidbolts.library.permissions.PermissionListener
import com.androidbolts.library.permissions.PermissionProvider
import com.androidbolts.library.utils.ContextProcessor
import com.androidbolts.library.utils.ExperimentalSharedPrefs
import com.androidbolts.library.utils.LocationConstants

class LocationManager private constructor(
private val locationListener: LocationListener?,
contextProcessor: ContextProcessor,
timeOut: Long = LocationConstants.TIME_OUT_NONE,
showLoading: Boolean
): PermissionListener, LifecycleObserver {
) : PermissionListener, LifecycleObserver {
private var permissionManager = PermissionProvider.getPermissionManager()
private var gpsProvider: GpsProvider
private var prefs: PreferenceManager?=null
private var prefs: PreferenceManager? = null

init {
this.permissionManager.setListener(this)
this.permissionManager.setContextProcessor(contextProcessor)
Expand All @@ -36,7 +37,7 @@ class LocationManager private constructor(
this.gpsProvider.setPrefs(this.prefs)
}

class Builder constructor(context: Context) {
class Builder constructor(context: Context) {

private lateinit var locationListener: LocationListener
private var timeOut: Long = LocationConstants.TIME_OUT_NONE
Expand All @@ -47,24 +48,25 @@ class LocationManager private constructor(
return this
}

fun setActivity(activity: Activity) : Builder{
this.contextProcessor.activity = activity
return this
}
fun setActivity(activity: Activity): Builder {
this.contextProcessor.activity = activity
return this
}

fun setFragment(fragment: Fragment) : Builder {
this.contextProcessor.fragment = fragment
return this
}
fun setFragment(fragment: Fragment): Builder {
this.contextProcessor.fragment = fragment
return this
}

fun setRequestTimeOut(timeOut: Long):Builder {
fun setRequestTimeOut(timeOut: Long): Builder {
this.timeOut = timeOut
return this
}
fun showLoading(show: Boolean):Builder{
this.showLoading = show
return this
}

fun showLoading(show: Boolean): Builder {
this.showLoading = show
return this
}

fun build(): LocationManager {
return LocationManager(locationListener, contextProcessor, timeOut, showLoading)
Expand All @@ -76,49 +78,58 @@ class LocationManager private constructor(
}

private fun askForPermission() {
if(permissionManager.hasPermission()){
if (permissionManager.hasPermission()) {
permissionGranted(true)
}else{
} else {
permissionManager.requestPermissions()
}
}

private fun permissionGranted(alreadyHadPermission:Boolean){
private fun permissionGranted(alreadyHadPermission: Boolean) {
locationListener?.onPermissionGranted(alreadyHadPermission)
gpsProvider.get()
}

private fun onPermissionGrantedFailed(){
private fun onPermissionGrantedFailed() {
locationListener?.let {
locationListener.onPermissionDenied()
}
}

@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun onResume(){
fun onResume() {
locationListener?.let {
if(permissionManager.hasPermission()) {
if (permissionManager.hasPermission()) {
gpsProvider.onResume()
}
}
}

@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
internal fun onPause(){
internal fun onPause() {
locationListener?.let {
gpsProvider.onPause()
}
}

@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
internal fun onDestroy(){
internal fun onDestroy() {
locationListener?.let {
gpsProvider.onDestroy()
}
}

fun setShowLoading(showLoading: Boolean) {
this.gpsProvider.setShowLoading(showLoading)
}

fun isLoadingSet(): Boolean {
return this.gpsProvider.isLoadingSet()
}

@ExperimentalSharedPrefs
fun getLastUpdatedLocation(): LocationModel? {
return prefs?.getLocationModel()
return prefs?.getLocationModel()
}

override fun onPermissionGranted() {
Expand All @@ -134,6 +145,6 @@ class LocationManager private constructor(
permissions: Array<out String>,
grantResults: IntArray
) {
permissionManager.onPermissionResult(requestCode, permissions,grantResults)
permissionManager.onPermissionResult(requestCode, permissions, grantResults)
}
}
29 changes: 20 additions & 9 deletions library/src/main/java/com/androidbolts/library/gps/GpsManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@ import com.google.android.gms.location.LocationServices
import com.google.android.gms.location.LocationSettingsRequest
import com.google.android.gms.location.LocationSettingsStatusCodes
import com.google.android.gms.location.SettingsClient
import java.lang.ref.WeakReference

internal class GpsManager private constructor() : GpsProvider() {
private var mFusedLocationClient: FusedLocationProviderClient? = null
private var mSettingsClient: SettingsClient? = null
private var mLocationRequest: LocationRequest? = null
private var mLocationRequest: LocationRequest = LocationRequest()
private var mLocationCallback: LocationCallback? = null
private var mCurrentLocation: Location? = null
private var mLocationSettingsRequest: LocationSettingsRequest? = null
private var dialog: AlertDialog? = null
private var mRequestingLocationUpdates: Boolean = false


companion object {
private var gpsManager: GpsManager? = null
fun getGpsManager(): GpsManager {
Expand Down Expand Up @@ -72,7 +74,13 @@ internal class GpsManager private constructor() : GpsProvider() {
}

private fun getLocation() {
if (mSettingsClient == null && mFusedLocationClient == null) {
initFusedAndSettingClient()
}
setupLocationBasic()
}

private fun initFusedAndSettingClient() {
when {
getFragment() != null -> {
mFusedLocationClient =
Expand All @@ -87,7 +95,6 @@ internal class GpsManager private constructor() : GpsProvider() {
}
else -> Log.d("LocationManager", "Host is invalid.")
}
setupLocationBasic()
}

private fun setupLocationBasic() {
Expand All @@ -100,11 +107,10 @@ internal class GpsManager private constructor() : GpsProvider() {
if (mCurrentLocation == null || getFragment() != null || getActivity() != null) {
showDialog()
}
mLocationRequest = LocationRequest()
mLocationRequest?.interval = LocationConstants.UPDATE_INTERVAL_IN_MILLISECONDS
mLocationRequest?.fastestInterval =
mLocationRequest.interval = LocationConstants.UPDATE_INTERVAL_IN_MILLISECONDS
mLocationRequest.fastestInterval =
LocationConstants.FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS
mLocationRequest?.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
mLocationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}

private fun createLocationCallback() {
Expand Down Expand Up @@ -135,17 +141,22 @@ internal class GpsManager private constructor() : GpsProvider() {

private fun buildLocationSettingsRequest() {
val builder = LocationSettingsRequest.Builder()
mLocationRequest?.let { locReq ->
mLocationRequest.let { locReq ->
builder.addLocationRequest(locReq)
}
mLocationSettingsRequest = builder.build()
}

@SuppressLint("MissingPermission")
private fun startLocationUpdates() {
if (mLocationSettingsRequest == null) {
buildLocationSettingsRequest()
}

if (mSettingsClient == null || mFusedLocationClient == null) {
initFusedAndSettingClient()
}
mSettingsClient?.checkLocationSettings(mLocationSettingsRequest)?.addOnSuccessListener {
//remove if the task is already running
// stopLocationUpdates()
mFusedLocationClient?.requestLocationUpdates(
mLocationRequest,
mLocationCallback, Looper.myLooper()
Expand Down
30 changes: 15 additions & 15 deletions library/src/main/java/com/androidbolts/library/gps/GpsProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.androidbolts.library.gps

import android.app.Activity
import android.content.Context
import android.content.SharedPreferences
import androidx.annotation.Nullable
import androidx.fragment.app.Fragment
import com.androidbolts.library.LocationListener
Expand All @@ -13,10 +12,10 @@ import java.lang.ref.WeakReference

abstract class GpsProvider {
private lateinit var weakContextProcessor: WeakReference<ContextProcessor>
private var locationListener: LocationListener?=null
private var showDialog:Boolean = false
private var timeOut:Long = TIME_OUT_NONE
private var prefs: PreferenceManager ?=null
private var locationListener: LocationListener? = null
private var showDialog: Boolean = false
private var timeOut: Long = TIME_OUT_NONE
private var prefs: PreferenceManager? = null
abstract fun onResume()
abstract fun onPause()
abstract fun onDestroy()
Expand All @@ -26,16 +25,16 @@ abstract class GpsProvider {
this.weakContextProcessor = WeakReference(contextProcessor)
}

fun setLocationListener(locationListener: LocationListener?){
fun setLocationListener(locationListener: LocationListener?) {
this.locationListener = locationListener
}

fun setShowLoading(show:Boolean){
fun setShowLoading(show: Boolean) {
this.showDialog = show
}

@Nullable
protected fun getContext():Context? {
protected fun getContext(): Context? {
return this.weakContextProcessor.get()?.context
}

Expand All @@ -49,29 +48,30 @@ abstract class GpsProvider {
return if (weakContextProcessor.get() == null) null else weakContextProcessor.get()!!.fragment
}

fun getLocationListener():LocationListener?{
fun getLocationListener(): LocationListener? {
return this.locationListener
}

fun isLoadingSet(): Boolean{
fun isLoadingSet(): Boolean {
return this.showDialog
}

fun setTimeOut(timeOut: Long){
if(timeOut < 0){
fun setTimeOut(timeOut: Long) {
if (timeOut < 0) {
throw Exception("Timeout can't be negative value.")
}
this.timeOut = timeOut
}

fun getTimeOut():Long{
fun getTimeOut(): Long {
return this.timeOut
}

internal fun setPrefs(prefs: PreferenceManager?){
internal fun setPrefs(prefs: PreferenceManager?) {
this.prefs = prefs
}
internal fun getPrefs(): PreferenceManager?{

internal fun getPrefs(): PreferenceManager? {
return this.prefs
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.androidbolts.library.utils

@Experimental
@Retention(AnnotationRetention.BINARY)
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
annotation class ExperimentalSharedPrefs

0 comments on commit cd7c1b1

Please sign in to comment.