Skip to content
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

Do not crop after pick cancelled #171

Merged
merged 4 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- `Security` in case of vulnerabilities.

## [unreleased x.x.x] -
### Fixed
- After cropping a camera image, cancelling library picker shows again the last cropped image [#162](https://github.com/CanHub/Android-Image-Cropper/issues/162)

## [3.2.1] - 14/07/21
### Fixed
Expand Down
21 changes: 13 additions & 8 deletions cropper/src/main/java/com/canhub/cropper/PickImageContract.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.canhub.cropper

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
Expand All @@ -22,7 +23,6 @@ open class PickImageContract : ActivityResultContract<Boolean, Uri?>() {
protected var context: Context? = null

override fun createIntent(context: Context, input: Boolean): Intent {

this.context = context

return CropImage.getPickImageChooserIntent(
Expand All @@ -36,12 +36,17 @@ open class PickImageContract : ActivityResultContract<Boolean, Uri?>() {
open override fun parseResult(
resultCode: Int,
intent: Intent?
): Uri? {
context?.let {
context = null
return getPickImageResultUriContent(it, intent)
): Uri? =
when (resultCode) {
Activity.RESULT_CANCELED -> {
context = null
null
}
else -> {
context?.let {
context = null
getPickImageResultUriContent(it, intent)
}
}
}
context = null
return null
}
}
24 changes: 24 additions & 0 deletions cropper/src/test/java/com/canhub/cropper/PickImageContractTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,28 @@ class PickImageContractTest {
// THEN
assertEquals(expected, result)
}

@Test
fun `GIVEN pick image contract and previous image selection, WHEN cancelling image pick, THEN no uri should be returned`() {
// GIVEN
var firstFragment: ContractTestFragment? = null
var secondFragment: ContractTestFragment? = null
val firstSelection = "content://testResult".toUri()
with(launchFragmentInContainer { ContractTestFragment(testRegistry) }) {
onFragment { firstFragment = it }
}
firstFragment?.pickImageIntent(true)
firstFragment?.pickImage?.contract?.parseResult(Activity.RESULT_OK, Intent().apply { data = firstSelection })
with(launchFragmentInContainer { ContractTestFragment(testRegistry) }) {
onFragment { secondFragment = it }
}

// WHEN
secondFragment?.pickImageIntent(true)
val resultIntent = Intent()
val result = secondFragment?.pickImage?.contract?.parseResult(Activity.RESULT_CANCELED, resultIntent)

// THEN
assertEquals(null, result)
}
}