Skip to content

Commit

Permalink
Refactoring: Better spacing. (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
vanniktech authored Oct 23, 2022
1 parent 3ec7695 commit 50f3cda
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 8 deletions.
2 changes: 2 additions & 0 deletions cropper/src/main/kotlin/com/canhub/cropper/BitmapUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down Expand Up @@ -854,6 +855,7 @@ internal object BitmapUtils {
if (mMaxTextureSize == 0) {
mMaxTextureSize = maxTextureSize
}

if (mMaxTextureSize > 0) {
while (
height / inSampleSize > mMaxTextureSize ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -87,6 +88,7 @@ class CropImageIntentChooser(
}
allIntents.addAll(galleryIntents)
}

val target = if (allIntents.isEmpty()) {
Intent()
} else {
Expand Down
5 changes: 5 additions & 0 deletions cropper/src/main/kotlin/com/canhub/cropper/CropImageView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -1770,6 +1774,7 @@ class CropImageView @JvmOverloads constructor(
options = bundle.parcelable(CropImage.CROP_IMAGE_EXTRA_OPTIONS)
}
}

if (options == null) {
options = CropImageOptions()
if (attrs != null) {
Expand Down
6 changes: 6 additions & 0 deletions cropper/src/main/kotlin/com/canhub/cropper/CropOverlayView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ internal class CropOverlayView @JvmOverloads constructor(
} else {
null
}

internal fun getNewPaintWithFill(color: Int): Paint {
val borderPaint = Paint()
borderPaint.color = color
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

/**
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

/**
Expand All @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class CropImageContractTest {
dispatchResult(requestCode, Activity.RESULT_CANCELED, Intent())
}
}

with(launchFragmentInContainer { ContractTestFragment(testRegistry) }) {
onFragment { fragment = it }
}
Expand Down Expand Up @@ -62,6 +63,7 @@ class CropImageContractTest {
dispatchResult(requestCode, Activity.RESULT_CANCELED, Intent())
}
}

with(launchFragmentInContainer { ContractTestFragment(testRegistry) }) {
onFragment { fragment = it }
}
Expand Down Expand Up @@ -98,6 +100,7 @@ class CropImageContractTest {
dispatchResult(requestCode, CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE, intent)
}
}

with(launchFragmentInContainer { ContractTestFragment(testRegistry) }) {
onFragment { fragment = it }
}
Expand Down Expand Up @@ -157,6 +160,7 @@ class CropImageContractTest {
setCropMenuCropButtonTitle("Test Button Title")
setCropMenuCropButtonIcon(R.drawable.ic_rotate_left_24)
}

val testRegistry = object : ActivityResultRegistry() {
override fun <I, O> onLaunch(
requestCode: Int,
Expand All @@ -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)
Expand Down Expand Up @@ -203,6 +208,7 @@ class CropImageContractTest {
dispatchResult(requestCode, CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE, intent)
}
}

with(launchFragmentInContainer { ContractTestFragment(testRegistry) }) {
onFragment { fragment = it }
}
Expand Down

0 comments on commit 50f3cda

Please sign in to comment.