Skip to content

Commit

Permalink
Code Cleanup
Browse files Browse the repository at this point in the history
Code Cleanup
  • Loading branch information
Dhaval Patel committed May 13, 2019
1 parent 8cddf09 commit fedf52b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ open class ImagePicker {

internal const val EXTRA_ERROR = "extra.error"
internal const val EXTRA_FILE_PATH = "extra.file_path"
internal const val EXTRA_CAMERA = "extra.camera"

/**
* Use this to use ImagePicker in Activity Class
Expand Down Expand Up @@ -127,20 +126,20 @@ open class ImagePicker {
}

/**
* Only Capture image using Camera
* Only Capture image using Camera.
*/
@Deprecated("Please use provider(ImageProvider.CAMERA) instead")
//@Deprecated("Please use provider(ImageProvider.CAMERA) instead")
fun cameraOnly(): Builder {
imageProvider = ImageProvider.CAMERA
this.imageProvider = ImageProvider.CAMERA
return this
}

/**
* Only Pick image from gallery
* Only Pick image from gallery.
*/
@Deprecated("Please use provider(ImageProvider.GALLERY) instead")
//@Deprecated("Please use provider(ImageProvider.GALLERY) instead")
fun galleryOnly(): Builder {
imageProvider = ImageProvider.GALLERY
this.imageProvider = ImageProvider.GALLERY
return this
}

Expand Down Expand Up @@ -221,8 +220,8 @@ open class ImagePicker {
private fun showImageProviderDialog(reqCode: Int) {
DialogHelper.showChooseAppDialog(activity, object : ResultListener<ImageProvider> {
override fun onResult(t: ImageProvider?) {
if (t != null) {
imageProvider = t
t?.let {
imageProvider = it
startActivity(reqCode)
}
}
Expand All @@ -239,32 +238,39 @@ open class ImagePicker {
imageProvider = t
startActivity(completionHandler)
} else {
completionHandler?.invoke(Activity.RESULT_CANCELED, Intent())
val intent = ImagePickerActivity.getCancelledIntent(activity)
completionHandler?.invoke(Activity.RESULT_CANCELED, intent)
}
}
})
}

/**
* Start ImagePickerActivity with given Argument
* Get Bundle for ImagePickerActivity
*/
private fun startActivity(completionHandler: ((resultCode: Int, data: Intent?) -> Unit)? = null) {
private fun getBundle(): Bundle {
val bundle = Bundle()
bundle.putSerializable(EXTRA_IMAGE_PROVIDER, imageProvider)

try {
val bundle = Bundle()
bundle.putSerializable(EXTRA_IMAGE_PROVIDER, imageProvider)
//bundle.putBoolean(EXTRA_ASK_PERMISSION, askPermission)
bundle.putFloat(EXTRA_CROP_X, cropX)
bundle.putFloat(EXTRA_CROP_Y, cropY)

bundle.putFloat(EXTRA_CROP_X, cropX)
bundle.putFloat(EXTRA_CROP_Y, cropY)
bundle.putInt(EXTRA_MAX_WIDTH, maxWidth)
bundle.putInt(EXTRA_MAX_HEIGHT, maxHeight)

bundle.putInt(EXTRA_MAX_WIDTH, maxWidth)
bundle.putInt(EXTRA_MAX_HEIGHT, maxHeight)
bundle.putLong(EXTRA_IMAGE_MAX_SIZE, maxSize)

bundle.putLong(EXTRA_IMAGE_MAX_SIZE, maxSize)
return bundle
}

/**
* Start ImagePickerActivity with given Argument
*/
private fun startActivity(completionHandler: ((resultCode: Int, data: Intent?) -> Unit)? = null) {

try {
val intent = Intent(activity, ImagePickerActivity::class.java)
intent.putExtras(bundle)
intent.putExtras(getBundle())
if (fragment != null) {

fragment?.startForResult(intent) { result ->
Expand All @@ -275,7 +281,7 @@ open class ImagePicker {
} else {
(activity as AppCompatActivity).startForResult(intent) { result ->
completionHandler?.invoke(result.resultCode, result.data)
}?.onFailed { result ->
}.onFailed { result ->
completionHandler?.invoke(result.resultCode, result.data)
}
}
Expand All @@ -293,20 +299,8 @@ open class ImagePicker {
* Start ImagePickerActivity with given Argument
*/
private fun startActivity(reqCode: Int) {
val bundle = Bundle()
bundle.putSerializable(EXTRA_IMAGE_PROVIDER, imageProvider)
//bundle.putBoolean(EXTRA_ASK_PERMISSION, askPermission)

bundle.putFloat(EXTRA_CROP_X, cropX)
bundle.putFloat(EXTRA_CROP_Y, cropY)

bundle.putInt(EXTRA_MAX_WIDTH, maxWidth)
bundle.putInt(EXTRA_MAX_HEIGHT, maxHeight)

bundle.putLong(EXTRA_IMAGE_MAX_SIZE, maxSize)

val intent = Intent(activity, ImagePickerActivity::class.java)
intent.putExtras(bundle)
intent.putExtras(getBundle())
if (fragment != null) {
fragment?.startActivityForResult(intent, reqCode)
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.dhaval2404.imagepicker

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
Expand All @@ -23,6 +24,14 @@ class ImagePickerActivity : FragmentActivity() {

companion object {
private const val TAG = "image_picker"

internal fun getCancelledIntent(context: Context): Intent {
val intent = Intent()
val message = context.getString(R.string.error_task_cancelled)
intent.putExtra(ImagePicker.EXTRA_ERROR, message)
return intent
}

}

private var mGalleryProvider: GalleryProvider? = null
Expand Down Expand Up @@ -86,8 +95,7 @@ class ImagePickerActivity : FragmentActivity() {
* Handle Activity Back Press
*/
override fun onBackPressed() {
setResultCancel(false) //needs to be set before onBackPressed or else won't work
super.onBackPressed()
setResultCancel()
}

/**
Expand Down Expand Up @@ -164,10 +172,9 @@ class ImagePickerActivity : FragmentActivity() {
/**
* User has cancelled the task
*/
fun setResultCancel(finish: Boolean = true) {
setResult(Activity.RESULT_CANCELED, Intent())
if (finish)
finish()
fun setResultCancel() {
setResult(Activity.RESULT_CANCELED, getCancelledIntent(this))
finish()
}

/**
Expand Down
1 change: 1 addition & 0 deletions imagepicker/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<string name="error_failed_pick_gallery_image">Failed to pick Gallery image</string>
<string name="error_failed_to_crop_image">Failed to crop image</string>
<string name="error_failed_to_compress_image">Failed to compress image</string>
<string name="error_task_cancelled">Task Cancelled</string>


</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.github.dhaval2404.imagepicker.ImagePicker
import com.github.dhaval2404.imagepicker.constant.ImageProvider
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_camera_only.*
import kotlinx.android.synthetic.main.content_gallery_only.*
Expand Down Expand Up @@ -44,7 +45,7 @@ class MainActivity : AppCompatActivity() {

fab_add_camera_photo.setOnClickListener {
ImagePicker.with(this)
.cameraOnly() //User can only capture image using Camera(Optional)
.provider(ImageProvider.CAMERA) //Default will be ImageProvider.BOTH
.compress(1024) //Final image size will be less than 1 MB(Optional)
.start(CAMERA_IMAGE_REQ_CODE)
}
Expand Down

0 comments on commit fedf52b

Please sign in to comment.