Skip to content

Commit

Permalink
Add custom error handling for the ITMGeolocationManager (#95)
Browse files Browse the repository at this point in the history
* Added custom error handler for geolocation permission errors

* Renamed variable to  customErrorHandler

* Added Context variable to the customErrorHandler function

* changed to errorHandler, typealias, default errorHandler function

* private val errorHandler, invoke removal
  • Loading branch information
cezaris13 authored Aug 1, 2024
1 parent 7518d20 commit 5f29ccb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import kotlin.math.*
*
* @param context: The [Context] that is used for interacting with Android.
*/
class ITMGeolocationManager(private var context: Context) {
class ITMGeolocationManager(private var context: Context, private val errorHandler: ErrorHandler? = null) {
private val geolocationJsInterface = object {
@JavascriptInterface
fun getCurrentPosition(positionId: Int) {
Expand Down Expand Up @@ -183,7 +183,7 @@ class ITMGeolocationManager(private var context: Context) {
*/
fun associateWithActivity(activity: ComponentActivity) {
context = activity
requester = ITMGeolocationRequester(activity)
requester = ITMGeolocationRequester(activity, errorHandler)
addLifecycleObserver(activity)
}

Expand All @@ -193,7 +193,7 @@ class ITMGeolocationManager(private var context: Context) {
* @param fragment The [Fragment] to associate with.
*/
fun associateWithFragment(fragment: Fragment) {
requester = ITMGeolocationRequester(fragment)
requester = ITMGeolocationRequester(fragment, errorHandler)
addLifecycleObserver(fragment)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import com.google.android.gms.tasks.Task
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.tasks.await

typealias ErrorHandler = (Context, String) -> Unit

/**
* [Context] extension function that checks that ACCESS_FINE_LOCATION has been granted.
*
Expand All @@ -45,15 +47,16 @@ fun Context.checkFineLocationPermission() =
* or Fragment.
*
* @param resultCaller The [ActivityResultCaller] to associate with.
* @param errorHandler The error handler to use when displaying errors. Defaults to a Toast.
*/
internal class ITMGeolocationRequester private constructor(resultCaller: ActivityResultCaller) {
internal class ITMGeolocationRequester private constructor(resultCaller: ActivityResultCaller, private val errorHandler: ErrorHandler) {
private lateinit var context: Context

private val requestPermission = resultCaller.registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
requestPermissionsTask?.complete(isGranted)
requestPermissionsTask = null
if (!isGranted) {
Toast.makeText(context, context.getString(R.string.itm_location_permissions_error_toast_text), Toast.LENGTH_LONG).show()
errorHandler(context, context.getString(R.string.itm_location_permissions_error_toast_text))
}
}

Expand All @@ -73,7 +76,7 @@ internal class ITMGeolocationRequester private constructor(resultCaller: Activit
/**
* Constructor using a [ComponentActivity] as the [ActivityResultCaller] and [Context].
*/
constructor(activity: ComponentActivity): this(activity as ActivityResultCaller) {
constructor(activity: ComponentActivity, errorHandler: ErrorHandler?) : this(activity as ActivityResultCaller, errorHandler ?: ::defaultErrorHandler) {
this.context = activity
activity.lifecycle.addObserver(object: DefaultLifecycleObserver {
override fun onDestroy(owner: LifecycleOwner) {
Expand All @@ -95,7 +98,7 @@ internal class ITMGeolocationRequester private constructor(resultCaller: Activit
* Constructor using a [Fragment] as the as the [ActivityResultCaller], and the fragment's
* activity or context will be used as the [Context].
*/
constructor(fragment: Fragment): this(fragment as ActivityResultCaller) {
constructor(fragment: Fragment, errorHandler: ErrorHandler?) : this(fragment as ActivityResultCaller, errorHandler ?: ::defaultErrorHandler) {
fragment.lifecycle.addObserver(object: DefaultLifecycleObserver {
override fun onStart(owner: LifecycleOwner) {
context = fragment.activity ?: fragment.requireContext()
Expand Down Expand Up @@ -153,6 +156,13 @@ internal class ITMGeolocationRequester private constructor(resultCaller: Activit
"Location permission denied"
)
}

/**
* The default error handler for the [ITMGeolocationRequester].
*/
private fun defaultErrorHandler(context: Context, message: String) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show()
}
}

/**
Expand Down

0 comments on commit 5f29ccb

Please sign in to comment.