From 50f3cdaabc945296ed31561226b6cd51db822027 Mon Sep 17 00:00:00 2001 From: Niklas Baudy Date: Sun, 23 Oct 2022 21:31:43 +0200 Subject: [PATCH] Refactoring: Better spacing. (#486) --- .../kotlin/com/canhub/cropper/BitmapUtils.kt | 2 + .../com/canhub/cropper/CropImageActivity.kt | 2 + .../canhub/cropper/CropImageIntentChooser.kt | 2 + .../com/canhub/cropper/CropImageView.kt | 5 +++ .../com/canhub/cropper/CropOverlayView.kt | 6 +++ .../canhub/cropper/CropWindowMoveHandler.kt | 39 +++++++++++++++---- .../canhub/cropper/CropImageContractTest.kt | 6 +++ 7 files changed, 54 insertions(+), 8 deletions(-) diff --git a/cropper/src/main/kotlin/com/canhub/cropper/BitmapUtils.kt b/cropper/src/main/kotlin/com/canhub/cropper/BitmapUtils.kt index 2a6ab667..2dc6a135 100644 --- a/cropper/src/main/kotlin/com/canhub/cropper/BitmapUtils.kt +++ b/cropper/src/main/kotlin/com/canhub/cropper/BitmapUtils.kt @@ -100,6 +100,7 @@ internal object BitmapUtils { ExifInterface.ORIENTATION_ROTATE_270 -> 270 else -> 0 } + val flipHorizontally = orientationAttributeInt == ExifInterface.ORIENTATION_FLIP_HORIZONTAL || orientationAttributeInt == ExifInterface.ORIENTATION_TRANSPOSE val flipVertically = orientationAttributeInt == ExifInterface.ORIENTATION_FLIP_VERTICAL || @@ -854,6 +855,7 @@ internal object BitmapUtils { if (mMaxTextureSize == 0) { mMaxTextureSize = maxTextureSize } + if (mMaxTextureSize > 0) { while ( height / inSampleSize > mMaxTextureSize || diff --git a/cropper/src/main/kotlin/com/canhub/cropper/CropImageActivity.kt b/cropper/src/main/kotlin/com/canhub/cropper/CropImageActivity.kt index 20508636..f74beb33 100644 --- a/cropper/src/main/kotlin/com/canhub/cropper/CropImageActivity.kt +++ b/cropper/src/main/kotlin/com/canhub/cropper/CropImageActivity.kt @@ -243,6 +243,7 @@ open class CropImageActivity : AppCompatActivity(), OnSetImageUriCompleteListene menu.findItem(R.id.crop_image_menu_crop).title = cropImageOptions.cropMenuCropButtonTitle } + var cropIcon: Drawable? = null try { if (cropImageOptions.cropMenuCropButtonIcon != 0) { @@ -252,6 +253,7 @@ open class CropImageActivity : AppCompatActivity(), OnSetImageUriCompleteListene } catch (e: Exception) { Log.w("AIC", "Failed to read menu crop drawable", e) } + if (cropImageOptions.activityMenuIconColor != 0) { updateMenuItemIconColor( menu, diff --git a/cropper/src/main/kotlin/com/canhub/cropper/CropImageIntentChooser.kt b/cropper/src/main/kotlin/com/canhub/cropper/CropImageIntentChooser.kt index de738370..ebb39451 100644 --- a/cropper/src/main/kotlin/com/canhub/cropper/CropImageIntentChooser.kt +++ b/cropper/src/main/kotlin/com/canhub/cropper/CropImageIntentChooser.kt @@ -79,6 +79,7 @@ class CropImageIntentChooser( if (!isExplicitCameraPermissionRequired(activity) && includeCamera) { allIntents.addAll(getCameraIntents(activity, packageManager)) } + if (includeGallery) { var galleryIntents = getGalleryIntents(packageManager, Intent.ACTION_GET_CONTENT) if (galleryIntents.isEmpty()) { @@ -87,6 +88,7 @@ class CropImageIntentChooser( } allIntents.addAll(galleryIntents) } + val target = if (allIntents.isEmpty()) { Intent() } else { diff --git a/cropper/src/main/kotlin/com/canhub/cropper/CropImageView.kt b/cropper/src/main/kotlin/com/canhub/cropper/CropImageView.kt index 8ca9af62..ac452f37 100644 --- a/cropper/src/main/kotlin/com/canhub/cropper/CropImageView.kt +++ b/cropper/src/main/kotlin/com/canhub/cropper/CropImageView.kt @@ -1045,6 +1045,7 @@ class CropImageView @JvmOverloads constructor( if (imageUri == null && originalBitmap == null && mImageResource < 1) { return super.onSaveInstanceState() } + val bundle = Bundle() val loadedImageUri = if (isSaveBitmapToInstanceState && imageUri == null && mImageResource < 1) { @@ -1056,17 +1057,20 @@ class CropImageView @JvmOverloads constructor( } else { imageUri } + if (loadedImageUri != null && originalBitmap != null) { val key = UUID.randomUUID().toString() BitmapUtils.mStateBitmap = Pair(key, WeakReference(originalBitmap)) bundle.putString("LOADED_IMAGE_STATE_BITMAP_KEY", key) } + if (bitmapLoadingWorkerJob != null) { val task = bitmapLoadingWorkerJob!!.get() if (task != null) { bundle.putParcelable("LOADING_IMAGE_URI", task.uri) } } + bundle.putParcelable("instanceState", super.onSaveInstanceState()) bundle.putParcelable("LOADED_IMAGE_URI", loadedImageUri) bundle.putInt("LOADED_IMAGE_RESOURCE", mImageResource) @@ -1770,6 +1774,7 @@ class CropImageView @JvmOverloads constructor( options = bundle.parcelable(CropImage.CROP_IMAGE_EXTRA_OPTIONS) } } + if (options == null) { options = CropImageOptions() if (attrs != null) { diff --git a/cropper/src/main/kotlin/com/canhub/cropper/CropOverlayView.kt b/cropper/src/main/kotlin/com/canhub/cropper/CropOverlayView.kt index f5c79689..ba4d619e 100644 --- a/cropper/src/main/kotlin/com/canhub/cropper/CropOverlayView.kt +++ b/cropper/src/main/kotlin/com/canhub/cropper/CropOverlayView.kt @@ -64,6 +64,7 @@ internal class CropOverlayView @JvmOverloads constructor( } else { null } + internal fun getNewPaintWithFill(color: Int): Paint { val borderPaint = Paint() borderPaint.color = color @@ -569,21 +570,25 @@ internal class CropOverlayView @JvmOverloads constructor( rect.left -= adj rect.right += adj } + if (rect.height() < mCropWindowHandler.getMinCropHeight()) { val adj = (mCropWindowHandler.getMinCropHeight() - rect.height()) / 2 rect.top -= adj rect.bottom += adj } + if (rect.width() > mCropWindowHandler.getMaxCropWidth()) { val adj = (rect.width() - mCropWindowHandler.getMaxCropWidth()) / 2 rect.left += adj rect.right -= adj } + if (rect.height() > mCropWindowHandler.getMaxCropHeight()) { val adj = (rect.height() - mCropWindowHandler.getMaxCropHeight()) / 2 rect.top += adj rect.bottom -= adj } + calculateBounds(rect) if (mCalcBounds.width() > 0 && mCalcBounds.height() > 0) { @@ -597,6 +602,7 @@ internal class CropOverlayView @JvmOverloads constructor( if (rect.right > rightLimit) rect.right = rightLimit if (rect.bottom > bottomLimit) rect.bottom = bottomLimit } + if (isFixAspectRatio && abs(rect.width() - rect.height() * mTargetAspectRatio) > 0.1) { if (rect.width() > rect.height() * mTargetAspectRatio) { val adj = abs(rect.height() * mTargetAspectRatio - rect.width()) / 2 diff --git a/cropper/src/main/kotlin/com/canhub/cropper/CropWindowMoveHandler.kt b/cropper/src/main/kotlin/com/canhub/cropper/CropWindowMoveHandler.kt index 4f8e5ce2..e93e1df5 100644 --- a/cropper/src/main/kotlin/com/canhub/cropper/CropWindowMoveHandler.kt +++ b/cropper/src/main/kotlin/com/canhub/cropper/CropWindowMoveHandler.kt @@ -191,6 +191,7 @@ internal class CropWindowMoveHandler( dx /= 1.05f mTouchOffset.x -= dx / 2 } + if (rect.top + dy < 0 || rect.bottom + dy > viewHeight || rect.top + dy < bounds.top || rect.bottom + dy > bounds.bottom) { dy /= 1.05f mTouchOffset.y -= dy / 2 @@ -549,13 +550,21 @@ internal class CropWindowMoveHandler( /** Check if edges have gone out of bounds (including snap margin), and fix if needed. */ private fun snapEdgesToBounds(edges: RectF, bounds: RectF, margin: Float) { - if (edges.left < bounds.left + margin) edges.offset(bounds.left - edges.left, 0f) + if (edges.left < bounds.left + margin) { + edges.offset(bounds.left - edges.left, 0f) + } - if (edges.top < bounds.top + margin) edges.offset(0f, bounds.top - edges.top) + if (edges.top < bounds.top + margin) { + edges.offset(0f, bounds.top - edges.top) + } - if (edges.right > bounds.right - margin) edges.offset(bounds.right - edges.right, 0f) + if (edges.right > bounds.right - margin) { + edges.offset(bounds.right - edges.right, 0f) + } - if (edges.bottom > bounds.bottom - margin) edges.offset(0f, bounds.bottom - edges.bottom) + if (edges.bottom > bounds.bottom - margin) { + edges.offset(0f, bounds.bottom - edges.bottom) + } } /** @@ -580,6 +589,7 @@ internal class CropWindowMoveHandler( newLeft /= 1.05f mTouchOffset.x -= newLeft / 1.1f } + if (newLeft < bounds.left) mTouchOffset.x -= (newLeft - bounds.left) / 2f if (newLeft - bounds.left < snapMargin) newLeft = bounds.left @@ -651,6 +661,7 @@ internal class CropWindowMoveHandler( newRight = viewWidth + (newRight - viewWidth) / 1.05f mTouchOffset.x -= (newRight - viewWidth) / 1.1f } + if (newRight > bounds.right) mTouchOffset.x -= (newRight - bounds.right) / 2f // If close to the edge if (bounds.right - newRight < snapMargin) newRight = bounds.right @@ -718,6 +729,7 @@ internal class CropWindowMoveHandler( newTop /= 1.05f mTouchOffset.y -= newTop / 1.1f } + if (newTop < bounds.top) mTouchOffset.y -= (newTop - bounds.top) / 2f if (newTop - bounds.top < snapMargin) newTop = bounds.top @@ -785,6 +797,7 @@ internal class CropWindowMoveHandler( newBottom = viewHeight + (newBottom - viewHeight) / 1.05f mTouchOffset.y -= (newBottom - viewHeight) / 1.1f } + if (newBottom > bounds.bottom) mTouchOffset.y -= (newBottom - bounds.bottom) / 2f if (bounds.bottom - newBottom < snapMargin) newBottom = bounds.bottom @@ -867,8 +880,13 @@ internal class CropWindowMoveHandler( */ private fun adjustLeftRightByAspectRatio(rect: RectF, bounds: RectF, aspectRatio: Float) { rect.inset((rect.width() - rect.height() * aspectRatio) / 2, 0f) - if (rect.left < bounds.left) rect.offset(bounds.left - rect.left, 0f) - if (rect.right > bounds.right) rect.offset(bounds.right - rect.right, 0f) + if (rect.left < bounds.left) { + rect.offset(bounds.left - rect.left, 0f) + } + + if (rect.right > bounds.right) { + rect.offset(bounds.right - rect.right, 0f) + } } /** @@ -877,7 +895,12 @@ internal class CropWindowMoveHandler( */ private fun adjustTopBottomByAspectRatio(rect: RectF, bounds: RectF, aspectRatio: Float) { rect.inset(0f, (rect.height() - rect.width() / aspectRatio) / 2) - if (rect.top < bounds.top) rect.offset(0f, bounds.top - rect.top) - if (rect.bottom > bounds.bottom) rect.offset(0f, bounds.bottom - rect.bottom) + if (rect.top < bounds.top) { + rect.offset(0f, bounds.top - rect.top) + } + + if (rect.bottom > bounds.bottom) { + rect.offset(0f, bounds.bottom - rect.bottom) + } } } diff --git a/cropper/src/test/kotlin/com/canhub/cropper/CropImageContractTest.kt b/cropper/src/test/kotlin/com/canhub/cropper/CropImageContractTest.kt index 4a8bbde8..8ed9de3b 100644 --- a/cropper/src/test/kotlin/com/canhub/cropper/CropImageContractTest.kt +++ b/cropper/src/test/kotlin/com/canhub/cropper/CropImageContractTest.kt @@ -34,6 +34,7 @@ class CropImageContractTest { dispatchResult(requestCode, Activity.RESULT_CANCELED, Intent()) } } + with(launchFragmentInContainer { ContractTestFragment(testRegistry) }) { onFragment { fragment = it } } @@ -62,6 +63,7 @@ class CropImageContractTest { dispatchResult(requestCode, Activity.RESULT_CANCELED, Intent()) } } + with(launchFragmentInContainer { ContractTestFragment(testRegistry) }) { onFragment { fragment = it } } @@ -98,6 +100,7 @@ class CropImageContractTest { dispatchResult(requestCode, CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE, intent) } } + with(launchFragmentInContainer { ContractTestFragment(testRegistry) }) { onFragment { fragment = it } } @@ -157,6 +160,7 @@ class CropImageContractTest { setCropMenuCropButtonTitle("Test Button Title") setCropMenuCropButtonIcon(R.drawable.ic_rotate_left_24) } + val testRegistry = object : ActivityResultRegistry() { override fun onLaunch( requestCode: Int, @@ -170,6 +174,7 @@ class CropImageContractTest { with(launchFragmentInContainer { ContractTestFragment(testRegistry) }) { onFragment { fragment -> cropImageIntent = fragment.cropImageIntent(options) } } + val bundle = cropImageIntent?.getBundleExtra(CropImage.CROP_IMAGE_EXTRA_BUNDLE) // THEN assertEquals(expectedClassName, cropImageIntent?.component?.className) @@ -203,6 +208,7 @@ class CropImageContractTest { dispatchResult(requestCode, CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE, intent) } } + with(launchFragmentInContainer { ContractTestFragment(testRegistry) }) { onFragment { fragment = it } }