Skip to content

Commit

Permalink
null safety operator added in replacement of requireActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
Nabin Khatiwada authored and Nabin Khatiwada committed Jul 12, 2020
1 parent 15e3bef commit 812e2c1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class LocationManager internal constructor(
}

@ExperimentalSharedPrefs
/** This is currently is experimental state so it may not work. */
fun getLastUpdatedLocation(): LocationModel? {
return prefs?.getLocationModel()
}
Expand Down
43 changes: 29 additions & 14 deletions library/src/main/java/com/androidbolts/library/gps/GpsManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.androidbolts.library.R
import com.androidbolts.library.utils.LocationConstants
import com.androidbolts.library.utils.LocationConstants.REQUEST_CHECK_SETTINGS
import com.androidbolts.library.utils.LocationConstants.TIME_OUT_NONE
import com.androidbolts.library.utils.orElse
import com.androidbolts.library.utils.showLoadingDialog
import com.google.android.gms.common.api.ApiException
import com.google.android.gms.common.api.ResolvableApiException
Expand Down Expand Up @@ -79,17 +80,22 @@ internal class GpsManager internal constructor() : GpsProvider() {
private fun initFusedAndSettingClient() {
when {
getFragment() != null -> {
mFusedLocationClient =
LocationServices.getFusedLocationProviderClient(getFragment()?.activity!!)
mSettingsClient =
LocationServices.getSettingsClient(getFragment()!!.requireActivity())
val activity = getFragment()?.activity
activity?.let {
mFusedLocationClient =
LocationServices.getFusedLocationProviderClient(activity)
mSettingsClient =
LocationServices.getSettingsClient(activity)
}.orElse {
Log.i("Activity:", activity.toString())
}
}
getActivity() != null -> {
mFusedLocationClient =
LocationServices.getFusedLocationProviderClient(getActivity()!!)
mSettingsClient = LocationServices.getSettingsClient(getActivity()!!)
}
else -> Log.d("LocationManager", "Host is invalid.")
else -> Log.i("LocationManager", "Host is invalid.")
}
}

Expand Down Expand Up @@ -163,15 +169,24 @@ internal class GpsManager internal constructor() : GpsProvider() {
try {
val rae = it as ResolvableApiException
when {
getFragment() != null -> rae.startResolutionForResult(
getFragment()?.requireActivity(),
REQUEST_CHECK_SETTINGS
)
getActivity() != null -> rae.startResolutionForResult(
getActivity(),
REQUEST_CHECK_SETTINGS
)
else -> Log.d("Invalid host", "Host is invalid.")
getFragment() != null -> {
val activity = getFragment()?.activity
activity?.let { fragmentActivity ->
rae.startResolutionForResult(
fragmentActivity,
REQUEST_CHECK_SETTINGS
)
}
}
getActivity() != null -> {
rae.startResolutionForResult(
getActivity(),
REQUEST_CHECK_SETTINGS
)
}
else -> {
Log.d("Invalid host", "Host is invalid.")
}
}
} catch (sie: IntentSender.SendIntentException) {
Log.i("LocationManager", "PendingIntent unable to execute request.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ abstract class GpsProvider {

@Nullable
protected fun getActivity(): Activity? {
return if (weakContextProcessor.get() == null) null else weakContextProcessor.get()!!.activity
return if (weakContextProcessor.get() == null) null else weakContextProcessor.get()?.activity
}

@Nullable
protected fun getFragment(): Fragment? {
return if (weakContextProcessor.get() == null) null else weakContextProcessor.get()!!.fragment
return if (weakContextProcessor.get() == null) null else weakContextProcessor.get()?.fragment
}

fun getLocationListener(): LocationListener? {
Expand Down

0 comments on commit 812e2c1

Please sign in to comment.