Skip to content

Commit 3f6f42b

Browse files
authored
Refactoring: CropImageView - use CropImageOptions constructor & Image Option Validation is now part of init. (#506)
1 parent 201d1ee commit 3f6f42b

File tree

3 files changed

+59
-203
lines changed

3 files changed

+59
-203
lines changed

cropper/src/main/kotlin/com/canhub/cropper/CropImageContract.kt

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,14 @@ import androidx.activity.result.contract.ActivityResultContract
1212
* If you do not provide an [CropImageContractOptions.uri] in the input the user will be asked to pick an image before cropping.
1313
*/
1414
class CropImageContract : ActivityResultContract<CropImageContractOptions, CropImageView.CropResult>() {
15-
override fun createIntent(context: Context, input: CropImageContractOptions): Intent {
16-
input.cropImageOptions.validate()
17-
return Intent(context, CropImageActivity::class.java).apply {
18-
putExtra(
19-
CropImage.CROP_IMAGE_EXTRA_BUNDLE,
20-
Bundle(2).apply {
21-
putParcelable(CropImage.CROP_IMAGE_EXTRA_SOURCE, input.uri)
22-
putParcelable(CropImage.CROP_IMAGE_EXTRA_OPTIONS, input.cropImageOptions)
23-
},
24-
)
25-
}
15+
override fun createIntent(context: Context, input: CropImageContractOptions) = Intent(context, CropImageActivity::class.java).apply {
16+
putExtra(
17+
CropImage.CROP_IMAGE_EXTRA_BUNDLE,
18+
Bundle(2).apply {
19+
putParcelable(CropImage.CROP_IMAGE_EXTRA_SOURCE, input.uri)
20+
putParcelable(CropImage.CROP_IMAGE_EXTRA_OPTIONS, input.cropImageOptions)
21+
},
22+
)
2623
}
2724

2825
override fun parseResult(

cropper/src/main/kotlin/com/canhub/cropper/CropImageOptions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ import kotlinx.parcelize.Parcelize
104104
@JvmField @ColorInt var toolbarBackButtonColor: Int? = null,
105105
@JvmField @ColorInt var toolbarTintColor: Int? = null,
106106
) : Parcelable {
107-
fun validate() {
107+
init {
108108
require(maxZoom >= 0) { "Cannot set max zoom to a number < 1" }
109109
require(touchRadius >= 0) { "Cannot set touch radius value to a number <= 0 " }
110110
require(!(initialCropWindowPaddingRatio < 0 || initialCropWindowPaddingRatio >= 0.5)) { "Cannot set initial crop window padding value to a number < 0 or >= 0.5" }

cropper/src/main/kotlin/com/canhub/cropper/CropImageView.kt

Lines changed: 50 additions & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,202 +1789,61 @@ class CropImageView @JvmOverloads constructor(
17891789
}
17901790

17911791
init {
1792-
var options: CropImageOptions? = null
1793-
val intent = if (context is Activity) context.intent else null
1794-
if (intent != null) {
1795-
val bundle = intent.getBundleExtra(CropImage.CROP_IMAGE_EXTRA_BUNDLE)
1796-
if (bundle != null) {
1797-
options = bundle.parcelable(CropImage.CROP_IMAGE_EXTRA_OPTIONS)
1798-
}
1799-
}
1800-
1801-
if (options == null) {
1802-
options = CropImageOptions()
1803-
if (attrs != null) {
1804-
val ta = context.obtainStyledAttributes(attrs, R.styleable.CropImageView, 0, 0)
1792+
val options = (context as? Activity)?.intent?.getBundleExtra(CropImage.CROP_IMAGE_EXTRA_BUNDLE)?.parcelable(CropImage.CROP_IMAGE_EXTRA_OPTIONS)
1793+
?: if (attrs != null) {
1794+
val a = context.obtainStyledAttributes(attrs, R.styleable.CropImageView, 0, 0)
1795+
val default = CropImageOptions()
18051796
try {
1806-
options.fixAspectRatio = ta.getBoolean(
1807-
R.styleable.CropImageView_cropFixAspectRatio,
1808-
options.fixAspectRatio,
1809-
)
1810-
options.aspectRatioX = ta.getInteger(
1811-
R.styleable.CropImageView_cropAspectRatioX,
1812-
options.aspectRatioX,
1813-
)
1814-
options.aspectRatioY = ta.getInteger(
1815-
R.styleable.CropImageView_cropAspectRatioY,
1816-
options.aspectRatioY,
1817-
)
1818-
options.scaleType = ScaleType.values()[
1819-
ta.getInt(
1820-
R.styleable.CropImageView_cropScaleType,
1821-
options.scaleType.ordinal,
1822-
),
1823-
]
1824-
options.autoZoomEnabled = ta.getBoolean(
1825-
R.styleable.CropImageView_cropAutoZoomEnabled,
1826-
options.autoZoomEnabled,
1827-
)
1828-
options.multiTouchEnabled = ta.getBoolean(
1829-
R.styleable.CropImageView_cropMultiTouchEnabled,
1830-
options.multiTouchEnabled,
1831-
)
1832-
options.centerMoveEnabled = ta.getBoolean(
1833-
R.styleable.CropImageView_cropCenterMoveEnabled,
1834-
options.centerMoveEnabled,
1835-
)
1836-
options.maxZoom =
1837-
ta.getInteger(R.styleable.CropImageView_cropMaxZoom, options.maxZoom)
1838-
options.cropShape = CropShape.values()[
1839-
ta.getInt(
1840-
R.styleable.CropImageView_cropShape,
1841-
options.cropShape.ordinal,
1842-
),
1843-
]
1844-
options.cornerShape = CropCornerShape.values()[
1845-
ta.getInt(
1846-
R.styleable.CropImageView_cornerShape,
1847-
options.cornerShape.ordinal,
1848-
),
1849-
]
1850-
options.cropCornerRadius = ta.getDimension(
1851-
R.styleable.CropImageView_cropCornerRadius,
1852-
options.cropCornerRadius,
1853-
)
1854-
options.guidelines = Guidelines.values()[
1855-
ta.getInt(
1856-
R.styleable.CropImageView_cropGuidelines,
1857-
options.guidelines.ordinal,
1858-
),
1859-
]
1860-
options.snapRadius = ta.getDimension(
1861-
R.styleable.CropImageView_cropSnapRadius,
1862-
options.snapRadius,
1863-
)
1864-
options.touchRadius = ta.getDimension(
1865-
R.styleable.CropImageView_cropTouchRadius,
1866-
options.touchRadius,
1867-
)
1868-
options.initialCropWindowPaddingRatio = ta.getFloat(
1869-
R.styleable.CropImageView_cropInitialCropWindowPaddingRatio,
1870-
options.initialCropWindowPaddingRatio,
1871-
)
1872-
options.circleCornerFillColorHexValue = ta.getInteger(
1873-
R.styleable.CropImageView_cropCornerCircleFillColor,
1874-
options.circleCornerFillColorHexValue,
1875-
)
1876-
options.borderLineThickness = ta.getDimension(
1877-
R.styleable.CropImageView_cropBorderLineThickness,
1878-
options.borderLineThickness,
1879-
)
1880-
options.borderLineColor = ta.getInteger(
1881-
R.styleable.CropImageView_cropBorderLineColor,
1882-
options.borderLineColor,
1883-
)
1884-
options.borderCornerThickness = ta.getDimension(
1885-
R.styleable.CropImageView_cropBorderCornerThickness,
1886-
options.borderCornerThickness,
1797+
isSaveBitmapToInstanceState = a.getBoolean(R.styleable.CropImageView_cropSaveBitmapToInstanceState, isSaveBitmapToInstanceState)
1798+
1799+
CropImageOptions(
1800+
scaleType = ScaleType.values()[a.getInt(R.styleable.CropImageView_cropScaleType, default.scaleType.ordinal)],
1801+
cropShape = CropShape.values()[a.getInt(R.styleable.CropImageView_cropShape, default.cropShape.ordinal)],
1802+
cornerShape = CropCornerShape.values()[a.getInt(R.styleable.CropImageView_cornerShape, default.cornerShape.ordinal)],
1803+
guidelines = Guidelines.values()[a.getInt(R.styleable.CropImageView_cropGuidelines, default.guidelines.ordinal)],
1804+
aspectRatioX = a.getInteger(R.styleable.CropImageView_cropAspectRatioX, default.aspectRatioX),
1805+
aspectRatioY = a.getInteger(R.styleable.CropImageView_cropAspectRatioY, default.aspectRatioY),
1806+
autoZoomEnabled = a.getBoolean(R.styleable.CropImageView_cropAutoZoomEnabled, default.autoZoomEnabled),
1807+
multiTouchEnabled = a.getBoolean(R.styleable.CropImageView_cropMultiTouchEnabled, default.multiTouchEnabled),
1808+
centerMoveEnabled = a.getBoolean(R.styleable.CropImageView_cropCenterMoveEnabled, default.centerMoveEnabled),
1809+
cropCornerRadius = a.getDimension(R.styleable.CropImageView_cropCornerRadius, default.cropCornerRadius),
1810+
snapRadius = a.getDimension(R.styleable.CropImageView_cropSnapRadius, default.snapRadius),
1811+
touchRadius = a.getDimension(R.styleable.CropImageView_cropTouchRadius, default.touchRadius),
1812+
initialCropWindowPaddingRatio = a.getFloat(R.styleable.CropImageView_cropInitialCropWindowPaddingRatio, default.initialCropWindowPaddingRatio),
1813+
circleCornerFillColorHexValue = a.getInteger(R.styleable.CropImageView_cropCornerCircleFillColor, default.circleCornerFillColorHexValue),
1814+
borderLineThickness = a.getDimension(R.styleable.CropImageView_cropBorderLineThickness, default.borderLineThickness),
1815+
borderLineColor = a.getInteger(R.styleable.CropImageView_cropBorderLineColor, default.borderLineColor),
1816+
borderCornerThickness = a.getDimension(R.styleable.CropImageView_cropBorderCornerThickness, default.borderCornerThickness),
1817+
borderCornerOffset = a.getDimension(R.styleable.CropImageView_cropBorderCornerOffset, default.borderCornerOffset),
1818+
borderCornerLength = a.getDimension(R.styleable.CropImageView_cropBorderCornerLength, default.borderCornerLength),
1819+
borderCornerColor = a.getInteger(R.styleable.CropImageView_cropBorderCornerColor, default.borderCornerColor),
1820+
guidelinesThickness = a.getDimension(R.styleable.CropImageView_cropGuidelinesThickness, default.guidelinesThickness),
1821+
guidelinesColor = a.getInteger(R.styleable.CropImageView_cropGuidelinesColor, default.guidelinesColor),
1822+
backgroundColor = a.getInteger(R.styleable.CropImageView_cropBackgroundColor, default.backgroundColor),
1823+
minCropWindowWidth = a.getDimension(R.styleable.CropImageView_cropMinCropWindowWidth, default.minCropWindowWidth.toFloat()).toInt(),
1824+
minCropWindowHeight = a.getDimension(R.styleable.CropImageView_cropMinCropWindowHeight, default.minCropWindowHeight.toFloat()).toInt(),
1825+
minCropResultWidth = a.getFloat(R.styleable.CropImageView_cropMinCropResultWidthPX, default.minCropResultWidth.toFloat()).toInt(),
1826+
minCropResultHeight = a.getFloat(R.styleable.CropImageView_cropMinCropResultHeightPX, default.minCropResultHeight.toFloat()).toInt(),
1827+
maxCropResultWidth = a.getFloat(R.styleable.CropImageView_cropMaxCropResultWidthPX, default.maxCropResultWidth.toFloat()).toInt(),
1828+
maxCropResultHeight = a.getFloat(R.styleable.CropImageView_cropMaxCropResultHeightPX, default.maxCropResultHeight.toFloat()).toInt(),
1829+
flipHorizontally = a.getBoolean(R.styleable.CropImageView_cropFlipHorizontally, default.flipHorizontally),
1830+
flipVertically = a.getBoolean(R.styleable.CropImageView_cropFlipHorizontally, default.flipVertically),
1831+
cropperLabelTextSize = a.getDimension(R.styleable.CropImageView_cropperLabelTextSize, default.cropperLabelTextSize),
1832+
cropperLabelTextColor = a.getInteger(R.styleable.CropImageView_cropperLabelTextColor, default.cropperLabelTextColor),
1833+
showCropLabel = a.getBoolean(R.styleable.CropImageView_cropShowLabel, default.showCropLabel),
1834+
maxZoom = a.getInteger(R.styleable.CropImageView_cropMaxZoom, default.maxZoom),
1835+
showCropOverlay = a.getBoolean(R.styleable.CropImageView_cropShowCropOverlay, default.showCropOverlay),
1836+
showProgressBar = a.getBoolean(R.styleable.CropImageView_cropShowProgressBar, default.showProgressBar),
1837+
cropperLabelText = a.getString(R.styleable.CropImageView_cropperLabelText),
1838+
fixAspectRatio = a.getBoolean(R.styleable.CropImageView_cropFixAspectRatio, default.fixAspectRatio) || a.hasValue(R.styleable.CropImageView_cropAspectRatioX) && a.hasValue(R.styleable.CropImageView_cropAspectRatioX),
18871839
)
1888-
options.borderCornerOffset = ta.getDimension(
1889-
R.styleable.CropImageView_cropBorderCornerOffset,
1890-
options.borderCornerOffset,
1891-
)
1892-
options.borderCornerLength = ta.getDimension(
1893-
R.styleable.CropImageView_cropBorderCornerLength,
1894-
options.borderCornerLength,
1895-
)
1896-
options.borderCornerColor = ta.getInteger(
1897-
R.styleable.CropImageView_cropBorderCornerColor,
1898-
options.borderCornerColor,
1899-
)
1900-
options.guidelinesThickness = ta.getDimension(
1901-
R.styleable.CropImageView_cropGuidelinesThickness,
1902-
options.guidelinesThickness,
1903-
)
1904-
options.guidelinesColor = ta.getInteger(
1905-
R.styleable.CropImageView_cropGuidelinesColor,
1906-
options.guidelinesColor,
1907-
)
1908-
options.backgroundColor = ta.getInteger(
1909-
R.styleable.CropImageView_cropBackgroundColor,
1910-
options.backgroundColor,
1911-
)
1912-
options.showCropOverlay = ta.getBoolean(
1913-
R.styleable.CropImageView_cropShowCropOverlay,
1914-
mShowCropOverlay,
1915-
)
1916-
options.showProgressBar = ta.getBoolean(
1917-
R.styleable.CropImageView_cropShowProgressBar,
1918-
mShowProgressBar,
1919-
)
1920-
options.borderCornerThickness = ta.getDimension(
1921-
R.styleable.CropImageView_cropBorderCornerThickness,
1922-
options.borderCornerThickness,
1923-
)
1924-
options.minCropWindowWidth = ta.getDimension(
1925-
R.styleable.CropImageView_cropMinCropWindowWidth,
1926-
options.minCropWindowWidth.toFloat(),
1927-
).toInt()
1928-
options.minCropWindowHeight = ta.getDimension(
1929-
R.styleable.CropImageView_cropMinCropWindowHeight,
1930-
options.minCropWindowHeight.toFloat(),
1931-
).toInt()
1932-
options.minCropResultWidth = ta.getFloat(
1933-
R.styleable.CropImageView_cropMinCropResultWidthPX,
1934-
options.minCropResultWidth.toFloat(),
1935-
).toInt()
1936-
options.minCropResultHeight = ta.getFloat(
1937-
R.styleable.CropImageView_cropMinCropResultHeightPX,
1938-
options.minCropResultHeight.toFloat(),
1939-
).toInt()
1940-
options.maxCropResultWidth = ta.getFloat(
1941-
R.styleable.CropImageView_cropMaxCropResultWidthPX,
1942-
options.maxCropResultWidth.toFloat(),
1943-
).toInt()
1944-
options.maxCropResultHeight = ta.getFloat(
1945-
R.styleable.CropImageView_cropMaxCropResultHeightPX,
1946-
options.maxCropResultHeight.toFloat(),
1947-
).toInt()
1948-
options.flipHorizontally = ta.getBoolean(
1949-
R.styleable.CropImageView_cropFlipHorizontally,
1950-
options.flipHorizontally,
1951-
)
1952-
options.flipVertically = ta.getBoolean(
1953-
R.styleable.CropImageView_cropFlipHorizontally,
1954-
options.flipVertically,
1955-
)
1956-
options.cropperLabelTextSize = ta.getDimension(
1957-
R.styleable.CropImageView_cropperLabelTextSize,
1958-
options.cropperLabelTextSize,
1959-
)
1960-
options.cropperLabelTextColor = ta.getInteger(
1961-
R.styleable.CropImageView_cropperLabelTextColor,
1962-
options.cropperLabelTextColor,
1963-
)
1964-
options.cropperLabelText = ta.getString(
1965-
R.styleable.CropImageView_cropperLabelText,
1966-
)
1967-
options.showCropLabel = ta.getBoolean(
1968-
R.styleable.CropImageView_cropShowLabel,
1969-
options.showCropLabel,
1970-
)
1971-
isSaveBitmapToInstanceState = ta.getBoolean(
1972-
R.styleable.CropImageView_cropSaveBitmapToInstanceState,
1973-
isSaveBitmapToInstanceState,
1974-
)
1975-
// if aspect ratio is set then set fixed to true
1976-
if (ta.hasValue(R.styleable.CropImageView_cropAspectRatioX) &&
1977-
ta.hasValue(R.styleable.CropImageView_cropAspectRatioX) &&
1978-
!ta.hasValue(R.styleable.CropImageView_cropFixAspectRatio)
1979-
) {
1980-
options.fixAspectRatio = true
1981-
}
19821840
} finally {
1983-
ta.recycle()
1841+
a.recycle()
19841842
}
1843+
} else {
1844+
CropImageOptions()
19851845
}
1986-
}
1987-
options.validate()
1846+
19881847
mScaleType = options.scaleType
19891848
mAutoZoomEnabled = options.autoZoomEnabled
19901849
mMaxZoom = options.maxZoom

0 commit comments

Comments
 (0)