refactor camera launcher

Signed-off-by: parneet-guraya <gurayaparneet@gmail.com>
This commit is contained in:
parneet-guraya 2024-10-17 19:30:56 +05:30
parent 6a98923a83
commit 77cd18c560
No known key found for this signature in database
GPG key ID: 63B807C4B2A9064B
8 changed files with 50 additions and 39 deletions

View file

@ -37,7 +37,9 @@ public class BookmarkLocationsFragment extends DaggerFragment {
private ActivityResultLauncher<Intent> cameraPickLauncherForResult = registerForActivityResult(new StartActivityForResult(), private ActivityResultLauncher<Intent> cameraPickLauncherForResult = registerForActivityResult(new StartActivityForResult(),
result -> { result -> {
// TODO handle result from controller contributionController.handleActivityResultWithCallback(requireActivity(),callbacks -> {
contributionController.onPictureReturnedFromCamera(result,requireActivity(),callbacks);
});
}); });
private ActivityResultLauncher<Intent> galleryPickLauncherForResult = registerForActivityResult(new StartActivityForResult(), private ActivityResultLauncher<Intent> galleryPickLauncherForResult = registerForActivityResult(new StartActivityForResult(),
@ -99,7 +101,6 @@ public class BookmarkLocationsFragment extends DaggerFragment {
commonPlaceClickActions, commonPlaceClickActions,
inAppCameraLocationPermissionLauncher, inAppCameraLocationPermissionLauncher,
galleryPickLauncherForResult, galleryPickLauncherForResult,
//TODO[Parry] just a stub, make sure if this is actually the launcher we need!!
cameraPickLauncherForResult cameraPickLauncherForResult
); );

View file

@ -113,7 +113,7 @@ public class ContributionController {
public void onLocationPermissionGranted() { public void onLocationPermissionGranted() {
if (!locationPermissionsHelper.isLocationAccessToAppsTurnedOn()) { if (!locationPermissionsHelper.isLocationAccessToAppsTurnedOn()) {
showLocationOffDialog(activity, R.string.in_app_camera_needs_location, showLocationOffDialog(activity, R.string.in_app_camera_needs_location,
R.string.in_app_camera_location_unavailable); R.string.in_app_camera_location_unavailable, resultLauncher);
} else { } else {
initiateCameraUpload(activity, resultLauncher); initiateCameraUpload(activity, resultLauncher);
} }
@ -138,9 +138,10 @@ public class ContributionController {
* @param activity Activity reference * @param activity Activity reference
* @param dialogTextResource Resource id of text to be shown in dialog * @param dialogTextResource Resource id of text to be shown in dialog
* @param toastTextResource Resource id of text to be shown in toast * @param toastTextResource Resource id of text to be shown in toast
* @param resultLauncher
*/ */
private void showLocationOffDialog(Activity activity, int dialogTextResource, private void showLocationOffDialog(Activity activity, int dialogTextResource,
int toastTextResource) { int toastTextResource, ActivityResultLauncher<Intent> resultLauncher) {
DialogUtil DialogUtil
.showAlertDialog(activity, .showAlertDialog(activity,
activity.getString(R.string.ask_to_turn_location_on), activity.getString(R.string.ask_to_turn_location_on),
@ -151,8 +152,7 @@ public class ContributionController {
() -> { () -> {
Toast.makeText(activity, activity.getString(toastTextResource), Toast.makeText(activity, activity.getString(toastTextResource),
Toast.LENGTH_LONG).show(); Toast.LENGTH_LONG).show();
//TODO [Parry] why do we need this call??? initiateCameraUpload(activity,resultLauncher);
// initiateCameraUpload(activity);
} }
); );
} }
@ -270,6 +270,10 @@ public class ContributionController {
FilePicker.onPictureReturnedFromCustomSelector(result,activity,callbacks); FilePicker.onPictureReturnedFromCustomSelector(result,activity,callbacks);
} }
public void onPictureReturnedFromCamera(ActivityResult result, Activity activity, @NonNull FilePicker.Callbacks callbacks) {
FilePicker.onPictureReturnedFromCamera(result,activity,callbacks);
}
/** /**
* Attaches callback for file picker. * Attaches callback for file picker.
*/ */

View file

@ -114,7 +114,9 @@ public class ContributionsListFragment extends CommonsDaggerSupportFragment impl
private ActivityResultLauncher<Intent> cameraPickLauncherForResult = registerForActivityResult(new StartActivityForResult(), private ActivityResultLauncher<Intent> cameraPickLauncherForResult = registerForActivityResult(new StartActivityForResult(),
result -> { result -> {
// TODO handle result from controller controller.handleActivityResultWithCallback(requireActivity(),callbacks -> {
controller.onPictureReturnedFromCamera(result,requireActivity(),callbacks);
});
}); });
private ActivityResultLauncher<String[]> inAppCameraLocationPermissionLauncher = registerForActivityResult( private ActivityResultLauncher<String[]> inAppCameraLocationPermissionLauncher = registerForActivityResult(

View file

@ -135,7 +135,6 @@ public class FilePicker implements Constants {
*/ */
public static void openCameraForImage(Activity activity, ActivityResultLauncher<Intent> resultLauncher, int type) { public static void openCameraForImage(Activity activity, ActivityResultLauncher<Intent> resultLauncher, int type) {
Intent intent = createCameraForImageIntent(activity, type); Intent intent = createCameraForImageIntent(activity, type);
//TODO[Parry] we're not using the result anyways.
// activity.startActivityForResult(intent, RequestCodes.TAKE_PICTURE); // activity.startActivityForResult(intent, RequestCodes.TAKE_PICTURE);
resultLauncher.launch(intent); resultLauncher.launch(intent);
} }
@ -179,8 +178,7 @@ public class FilePicker implements Constants {
} else if (requestCode == RequestCodes.PICK_PICTURE_FROM_CUSTOM_SELECTOR) { } else if (requestCode == RequestCodes.PICK_PICTURE_FROM_CUSTOM_SELECTOR) {
// onPictureReturnedFromCustomSelector(data, activity, callbacks); // onPictureReturnedFromCustomSelector(data, activity, callbacks);
} else if (requestCode == RequestCodes.TAKE_PICTURE) { } else if (requestCode == RequestCodes.TAKE_PICTURE) {
//TODO[Parry] why handle the result , when not using it in the first place??? // onPictureReturnedFromCamera(activity, callbacks);
onPictureReturnedFromCamera(activity, callbacks);
} }
} else { } else {
if (requestCode == RequestCodes.PICK_PICTURE_FROM_DOCUMENTS) { if (requestCode == RequestCodes.PICK_PICTURE_FROM_DOCUMENTS) {
@ -337,7 +335,8 @@ public class FilePicker implements Constants {
return files; return files;
} }
private static void onPictureReturnedFromCamera(Activity activity, @NonNull FilePicker.Callbacks callbacks) { public static void onPictureReturnedFromCamera(ActivityResult activityResult, Activity activity, @NonNull FilePicker.Callbacks callbacks) {
if(activityResult.getResultCode() == Activity.RESULT_OK){
try { try {
String lastImageUri = PreferenceManager.getDefaultSharedPreferences(activity).getString(KEY_PHOTO_URI, null); String lastImageUri = PreferenceManager.getDefaultSharedPreferences(activity).getString(KEY_PHOTO_URI, null);
if (!TextUtils.isEmpty(lastImageUri)) { if (!TextUtils.isEmpty(lastImageUri)) {
@ -369,6 +368,7 @@ public class FilePicker implements Constants {
callbacks.onImagePickerError(e, FilePicker.ImageSource.CAMERA_IMAGE, restoreType(activity)); callbacks.onImagePickerError(e, FilePicker.ImageSource.CAMERA_IMAGE, restoreType(activity));
} }
} }
}
private static void onVideoReturnedFromCamera(Activity activity, @NonNull FilePicker.Callbacks callbacks) { private static void onVideoReturnedFromCamera(Activity activity, @NonNull FilePicker.Callbacks callbacks) {
try { try {

View file

@ -29,7 +29,7 @@ fun placeAdapterDelegate(
onDirectionsClicked: (Place) -> Unit, onDirectionsClicked: (Place) -> Unit,
onDirectionsLongPressed: () -> Boolean, onDirectionsLongPressed: () -> Boolean,
inAppCameraLocationPermissionLauncher: ActivityResultLauncher<Array<String>>, inAppCameraLocationPermissionLauncher: ActivityResultLauncher<Array<String>>,
resultLauncher: ActivityResultLauncher<Intent>, cameraPickLauncherForResult: ActivityResultLauncher<Intent>,
galleryPickLauncherForResult: ActivityResultLauncher<Intent> galleryPickLauncherForResult: ActivityResultLauncher<Intent>
) = adapterDelegateViewBinding<Place, Place, ItemPlaceBinding>({ layoutInflater, parent -> ) = adapterDelegateViewBinding<Place, Place, ItemPlaceBinding>({ layoutInflater, parent ->
ItemPlaceBinding.inflate(layoutInflater, parent, false) ItemPlaceBinding.inflate(layoutInflater, parent, false)
@ -47,7 +47,7 @@ fun placeAdapterDelegate(
onItemClick?.invoke(item) onItemClick?.invoke(item)
} }
} }
nearbyButtonLayout.cameraButton.setOnClickListener { onCameraClicked(item, inAppCameraLocationPermissionLauncher, resultLauncher) } nearbyButtonLayout.cameraButton.setOnClickListener { onCameraClicked(item, inAppCameraLocationPermissionLauncher, cameraPickLauncherForResult) }
nearbyButtonLayout.cameraButton.setOnLongClickListener { onCameraLongPressed() } nearbyButtonLayout.cameraButton.setOnLongClickListener { onCameraLongPressed() }
nearbyButtonLayout.galleryButton.setOnClickListener { onGalleryClicked(item,galleryPickLauncherForResult) } nearbyButtonLayout.galleryButton.setOnClickListener { onGalleryClicked(item,galleryPickLauncherForResult) }

View file

@ -243,7 +243,9 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
private ActivityResultLauncher<Intent> cameraPickLauncherForResult = registerForActivityResult(new StartActivityForResult(), private ActivityResultLauncher<Intent> cameraPickLauncherForResult = registerForActivityResult(new StartActivityForResult(),
result -> { result -> {
// TODO handle result from controller controller.handleActivityResultWithCallback(requireActivity(),callbacks -> {
controller.onPictureReturnedFromCamera(result,requireActivity(),callbacks);
});
}); });
private ActivityResultLauncher<String[]> inAppCameraLocationPermissionLauncher = registerForActivityResult( private ActivityResultLauncher<String[]> inAppCameraLocationPermissionLauncher = registerForActivityResult(

View file

@ -14,7 +14,7 @@ class PlaceAdapter(
commonPlaceClickActions: CommonPlaceClickActions, commonPlaceClickActions: CommonPlaceClickActions,
inAppCameraLocationPermissionLauncher: ActivityResultLauncher<Array<String>>, inAppCameraLocationPermissionLauncher: ActivityResultLauncher<Array<String>>,
galleryPickLauncherForResult: ActivityResultLauncher<Intent>, galleryPickLauncherForResult: ActivityResultLauncher<Intent>,
resultLauncherStub: ActivityResultLauncher<Intent> cameraPickLauncherForResult: ActivityResultLauncher<Intent>
) : BaseDelegateAdapter<Place>( ) : BaseDelegateAdapter<Place>(
placeAdapterDelegate( placeAdapterDelegate(
bookmarkLocationsDao, bookmarkLocationsDao,
@ -30,7 +30,7 @@ class PlaceAdapter(
commonPlaceClickActions.onDirectionsClicked(), commonPlaceClickActions.onDirectionsClicked(),
commonPlaceClickActions.onDirectionsLongPressed(), commonPlaceClickActions.onDirectionsLongPressed(),
inAppCameraLocationPermissionLauncher, inAppCameraLocationPermissionLauncher,
resultLauncherStub, cameraPickLauncherForResult,
galleryPickLauncherForResult galleryPickLauncherForResult
), ),
areItemsTheSame = { oldItem, newItem -> oldItem.wikiDataEntityId == newItem.wikiDataEntityId }, areItemsTheSame = { oldItem, newItem -> oldItem.wikiDataEntityId == newItem.wikiDataEntityId },

View file

@ -89,7 +89,9 @@ public class SettingsFragment extends PreferenceFragmentCompat {
private ActivityResultLauncher<Intent> cameraPickLauncherForResult = registerForActivityResult(new StartActivityForResult(), private ActivityResultLauncher<Intent> cameraPickLauncherForResult = registerForActivityResult(new StartActivityForResult(),
result -> { result -> {
// TODO handle result from controller contributionController.handleActivityResultWithCallback(requireActivity(),callbacks -> {
contributionController.onPictureReturnedFromCamera(result,requireActivity(),callbacks);
});
}); });
private ActivityResultLauncher<String[]> inAppCameraLocationPermissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() { private ActivityResultLauncher<String[]> inAppCameraLocationPermissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() {