-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Convert GeoPointMapActivity to Fragment
#7253
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
base: master
Are you sure you want to change the base?
Changes from all commits
dcbf415
1122489
b1cd2ef
eb1223a
b3dbde2
dde58c1
208aba7
551117c
c4b16a9
a942f59
63ff1ba
33753a7
88504a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package org.odk.collect.androidshared.system | ||
|
|
||
| import android.os.Bundle | ||
| import android.os.Parcelable | ||
| import androidx.core.os.BundleCompat | ||
|
|
||
| object BundleExt { | ||
|
|
||
| inline fun <reified T : Parcelable> Bundle.getParcelableExtraCompat(name: String): T? { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The name |
||
| return BundleCompat.getParcelable(this, name, T::class.java) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package org.odk.collect.android.widgets.geo | ||
|
|
||
| import androidx.lifecycle.ViewModelProvider | ||
| import org.javarosa.core.model.data.GeoPointData | ||
| import org.javarosa.form.api.FormEntryPrompt | ||
| import org.odk.collect.android.utilities.Appearances | ||
| import org.odk.collect.android.utilities.FormEntryPromptUtils | ||
| import org.odk.collect.android.widgets.interfaces.SelectChoiceLoader | ||
| import org.odk.collect.android.widgets.utilities.BindAttributes | ||
| import org.odk.collect.android.widgets.utilities.WidgetAnswerDialogFragment | ||
| import org.odk.collect.geo.GeoUtils.parseGeometryPoint | ||
| import org.odk.collect.geo.GeoUtils.toMapPoint | ||
| import org.odk.collect.geo.geopoint.GeoPointMapFragment | ||
|
|
||
| class GeoPointMapDialogFragment( | ||
| viewModelFactory: ViewModelProvider.Factory | ||
| ) : | ||
| WidgetAnswerDialogFragment<GeoPointMapFragment>( | ||
| GeoPointMapFragment::class, | ||
| viewModelFactory | ||
| ) { | ||
|
|
||
| override fun onCreateFragment( | ||
| prompt: FormEntryPrompt, | ||
| selectChoiceLoader: SelectChoiceLoader | ||
| ): GeoPointMapFragment { | ||
| childFragmentManager.setFragmentResultListener( | ||
| GeoPointMapFragment.REQUEST_GEOPOINT, | ||
| this | ||
| ) { _, result -> | ||
| val geoPoint = result.getString(GeoPointMapFragment.RESULT_GEOPOINT) | ||
|
|
||
| if (geoPoint != null) { | ||
| onAnswer(geoPoint) | ||
| } else { | ||
| dismiss() | ||
| } | ||
| } | ||
|
|
||
| val retainMockAccuracy = | ||
| FormEntryPromptUtils.getBindAttribute(prompt, BindAttributes.ALLOW_MOCK_ACCURACY) | ||
| .toBoolean() | ||
|
|
||
| val inputPoint = when (val answer = prompt.answerValue) { | ||
| is GeoPointData -> answer.toMapPoint() | ||
| null -> null | ||
| else -> throw IllegalArgumentException() | ||
| } | ||
|
|
||
| val draggable = Appearances.hasAppearance(prompt, Appearances.PLACEMENT_MAP) | ||
| return GeoPointMapFragment( | ||
| inputPoint, | ||
| draggable, | ||
| prompt.isReadOnly, | ||
| retainMockAccuracy | ||
| ) | ||
| } | ||
|
|
||
| private fun onAnswer(geoString: String) { | ||
| val answer = if (geoString.isBlank()) { | ||
| null | ||
| } else { | ||
| GeoPointData(parseGeometryPoint(geoString)) | ||
| } | ||
|
|
||
| onAnswer(answer) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,16 +8,14 @@ import org.javarosa.form.api.FormEntryPrompt | |
| import org.odk.collect.android.utilities.Appearances | ||
| import org.odk.collect.android.utilities.ApplicationConstants | ||
| import org.odk.collect.android.utilities.FormEntryPromptUtils | ||
| import org.odk.collect.android.widgets.geo.GeoPointMapDialogFragment | ||
| import org.odk.collect.android.widgets.geo.GeoPolyDialogFragment | ||
| import org.odk.collect.android.widgets.interfaces.GeoDataRequester | ||
| import org.odk.collect.android.widgets.utilities.BindAttributes.ALLOW_MOCK_ACCURACY | ||
| import org.odk.collect.androidshared.ui.DialogFragmentUtils | ||
| import org.odk.collect.geo.Constants.EXTRA_DRAGGABLE_ONLY | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This const is unused now, and we can remove it. |
||
| import org.odk.collect.geo.Constants.EXTRA_READ_ONLY | ||
| import org.odk.collect.geo.Constants.EXTRA_RETAIN_MOCK_ACCURACY | ||
| import org.odk.collect.geo.geopoint.GeoPointActivity | ||
| import org.odk.collect.geo.geopoint.GeoPointMapActivity | ||
| import org.odk.collect.geo.geopoly.GeoPolyUtils.parseGeometry | ||
| import org.odk.collect.permissions.PermissionListener | ||
| import org.odk.collect.permissions.PermissionsProvider | ||
| import java.lang.Boolean.parseBoolean | ||
|
|
@@ -29,7 +27,6 @@ class ActivityGeoDataRequester( | |
|
|
||
| override fun requestGeoPoint( | ||
| prompt: FormEntryPrompt, | ||
| answerText: String?, | ||
| waitingForDataRegistry: WaitingForDataRegistry | ||
| ) { | ||
| permissionsProvider.requestEnabledLocationPermissions( | ||
|
|
@@ -38,50 +35,49 @@ class ActivityGeoDataRequester( | |
| override fun granted() { | ||
| waitingForDataRegistry.waitForData(prompt.index) | ||
|
|
||
| val bundle = Bundle().also { | ||
| val parsedGeometry = parseGeometry(answerText) | ||
| if (parsedGeometry.isNotEmpty()) { | ||
| it.putParcelable( | ||
| GeoPointMapActivity.EXTRA_LOCATION, | ||
| parsedGeometry[0] | ||
| if (isMapsAppearance(prompt)) { | ||
| DialogFragmentUtils.showIfNotShowing( | ||
| GeoPointMapDialogFragment::class.java, | ||
| bundleOf(WidgetAnswerDialogFragment.ARG_FORM_INDEX to prompt.index), | ||
| activity.supportFragmentManager | ||
| ) | ||
| } else { | ||
| val bundle = Bundle().also { | ||
| val accuracyThreshold = | ||
| FormEntryPromptUtils.getAdditionalAttribute( | ||
| prompt, | ||
| "accuracyThreshold" | ||
| ) | ||
| val unacceptableAccuracyThreshold = | ||
| FormEntryPromptUtils.getAdditionalAttribute( | ||
| prompt, | ||
| "unacceptableAccuracyThreshold" | ||
| ) | ||
|
|
||
| it.putFloat( | ||
| GeoPointActivity.EXTRA_ACCURACY_THRESHOLD, | ||
| accuracyThreshold?.toFloatOrNull() ?: DEFAULT_ACCURACY_THRESHOLD | ||
| ) | ||
| } | ||
|
|
||
| val accuracyThreshold = | ||
| FormEntryPromptUtils.getAdditionalAttribute(prompt, "accuracyThreshold") | ||
| val unacceptableAccuracyThreshold = | ||
| FormEntryPromptUtils.getAdditionalAttribute( | ||
| prompt, | ||
| "unacceptableAccuracyThreshold" | ||
| it.putFloat( | ||
| GeoPointActivity.EXTRA_UNACCEPTABLE_ACCURACY_THRESHOLD, | ||
| unacceptableAccuracyThreshold?.toFloatOrNull() | ||
| ?: DEFAULT_UNACCEPTABLE_ACCURACY_THRESHOLD | ||
| ) | ||
|
|
||
| it.putFloat( | ||
| GeoPointActivity.EXTRA_ACCURACY_THRESHOLD, | ||
| accuracyThreshold?.toFloatOrNull() ?: DEFAULT_ACCURACY_THRESHOLD | ||
| ) | ||
|
|
||
| it.putFloat( | ||
| GeoPointActivity.EXTRA_UNACCEPTABLE_ACCURACY_THRESHOLD, | ||
| unacceptableAccuracyThreshold?.toFloatOrNull() | ||
| ?: DEFAULT_UNACCEPTABLE_ACCURACY_THRESHOLD | ||
| ) | ||
| it.putBoolean(EXTRA_RETAIN_MOCK_ACCURACY, getAllowMockAccuracy(prompt)) | ||
| it.putBoolean(EXTRA_READ_ONLY, prompt.isReadOnly) | ||
| } | ||
|
|
||
| it.putBoolean(EXTRA_RETAIN_MOCK_ACCURACY, getAllowMockAccuracy(prompt)) | ||
| it.putBoolean(EXTRA_READ_ONLY, prompt.isReadOnly) | ||
| it.putBoolean(EXTRA_DRAGGABLE_ONLY, hasPlacementMapAppearance(prompt)) | ||
| } | ||
| val intent = Intent(activity, GeoPointActivity::class.java).also { | ||
| it.putExtras(bundle) | ||
| } | ||
|
|
||
| val intent = Intent( | ||
| activity, | ||
| if (isMapsAppearance(prompt)) GeoPointMapActivity::class.java else GeoPointActivity::class.java | ||
| ).also { | ||
| it.putExtras(bundle) | ||
| activity.startActivityForResult( | ||
| intent, | ||
| ApplicationConstants.RequestCodes.LOCATION_CAPTURE | ||
| ) | ||
| } | ||
|
|
||
| activity.startActivityForResult( | ||
| intent, | ||
| ApplicationConstants.RequestCodes.LOCATION_CAPTURE | ||
| ) | ||
| } | ||
| } | ||
| ) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrapping a single extension function in an object adds an extra layer without much benefit. A top-level extension function would be more idiomatic and just as easy to import.