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 2 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 customErrorHandler: ((String) -> Unit)? = null) {
tcobbs-bentley marked this conversation as resolved.
Show resolved Hide resolved
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, customErrorHandler)
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, customErrorHandler)
addLifecycleObserver(fragment)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,17 @@ fun Context.checkFineLocationPermission() =
*/
internal class ITMGeolocationRequester private constructor(resultCaller: ActivityResultCaller) {
private lateinit var context: Context
private var customErrorHandler: ((String) -> Unit)? = null

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()
if (customErrorHandler != null) {
toddsouthenbentley marked this conversation as resolved.
Show resolved Hide resolved
customErrorHandler?.invoke(context.getString(R.string.itm_location_permissions_error_toast_text))
} else {
Toast.makeText(context, context.getString(R.string.itm_location_permissions_error_toast_text), Toast.LENGTH_LONG).show()
}
}
}

Expand All @@ -73,8 +78,9 @@ 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, customErrorHandler: ((String) -> Unit)? = null) : this(activity as ActivityResultCaller) {
this.context = activity
this.customErrorHandler = customErrorHandler
activity.lifecycle.addObserver(object: DefaultLifecycleObserver {
override fun onDestroy(owner: LifecycleOwner) {
unregister()
Expand All @@ -95,7 +101,8 @@ 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, customErrorHandler: ((String) -> Unit)? = null) : this(fragment as ActivityResultCaller) {
this.customErrorHandler = customErrorHandler
fragment.lifecycle.addObserver(object: DefaultLifecycleObserver {
override fun onStart(owner: LifecycleOwner) {
context = fragment.activity ?: fragment.requireContext()
Expand Down