Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom error handling for the ITMGeolocationManager #95

Merged
merged 5 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading