Skip to content

Commit

Permalink
Code optimization
Browse files Browse the repository at this point in the history
Code optimization
  • Loading branch information
Dhaval2404 committed Mar 22, 2020
1 parent 82ce03e commit eff800c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.github.dhaval2404.imagepicker.constant.ImageProvider
import com.github.dhaval2404.imagepicker.constant.ImageProvider.BOTH
import com.github.dhaval2404.imagepicker.constant.ImageProvider.CAMERA
import com.github.dhaval2404.imagepicker.constant.ImageProvider.GALLERY
import com.github.dhaval2404.imagepicker.listener.ResultListener
import com.github.dhaval2404.imagepicker.util.DialogHelper
import com.github.florent37.inlineactivityresult.kotlin.startForResult
Expand Down Expand Up @@ -40,7 +37,7 @@ open class ImagePicker {

internal const val EXTRA_ERROR = "extra.error"
internal const val EXTRA_FILE_PATH = "extra.file_path"
internal const val EXTRA_GALLERY_MIME_TYPES = "extra.gallery.mime.types"
internal const val EXTRA_MIME_TYPES = "extra.mime_types"

/**
* Use this to use ImagePicker in Activity Class
Expand Down Expand Up @@ -98,7 +95,7 @@ open class ImagePicker {
// Image Provider
private var imageProvider = ImageProvider.BOTH

//mime types restrictions for gallery. by default all mime types are valid
// Mime types restrictions for gallery. by default all mime types are valid
private var mimeTypes: Array<String> = emptyArray()

/*
Expand Down Expand Up @@ -164,7 +161,7 @@ open class ImagePicker {
}

/**
* restrict mime types during gallery fetching, for instance if you do not want GIF images,
* Restrict mime types during gallery fetching, for instance if you do not want GIF images,
* you can use arrayOf("image/png","image/jpeg","image/jpg")
* by default array is empty, which indicates no additional restrictions, just images
* @param mimeTypes
Expand Down Expand Up @@ -322,21 +319,18 @@ open class ImagePicker {
private fun getBundle(): Bundle {
return Bundle().apply {
putSerializable(EXTRA_IMAGE_PROVIDER, imageProvider)

putStringArray(EXTRA_GALLERY_MIME_TYPES, mimeTypes)
putStringArray(EXTRA_MIME_TYPES, mimeTypes)

putBoolean(EXTRA_CROP, crop)

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

putLong(EXTRA_IMAGE_MAX_SIZE, maxSize)
putString(EXTRA_SAVE_DIRECTORY, saveDir)

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

putLong(EXTRA_IMAGE_MAX_SIZE, maxSize)

putString(EXTRA_SAVE_DIRECTORY, saveDir)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,11 @@ class ImagePickerActivity : AppCompatActivity() {
// Retrieve Image Provider
val provider: ImageProvider? =
intent?.getSerializableExtra(ImagePicker.EXTRA_IMAGE_PROVIDER) as ImageProvider?
val mimeTypes: Array<String> = intent?.getStringArrayExtra(ImagePicker.EXTRA_GALLERY_MIME_TYPES) ?: emptyArray()

// Create Gallery/Camera Provider
when (provider) {
ImageProvider.GALLERY -> {
mGalleryProvider = GalleryProvider(this, mimeTypes)
mGalleryProvider = GalleryProvider(this)
// Pick Gallery Image
savedInstanceState ?: mGalleryProvider?.startIntent()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.Manifest
import android.app.Activity
import android.content.Intent
import androidx.core.app.ActivityCompat.requestPermissions
import com.github.dhaval2404.imagepicker.ImagePicker
import com.github.dhaval2404.imagepicker.ImagePickerActivity
import com.github.dhaval2404.imagepicker.R
import com.github.dhaval2404.imagepicker.util.FileUriUtils
Expand All @@ -18,7 +19,7 @@ import java.io.File
* @version 1.0
* @since 04 January 2019
*/
class GalleryProvider(activity: ImagePickerActivity, private val mimeTypes: Array<String>) :
class GalleryProvider(activity: ImagePickerActivity) :
BaseProvider(activity) {

companion object {
Expand All @@ -33,6 +34,15 @@ class GalleryProvider(activity: ImagePickerActivity, private val mimeTypes: Arra
private const val PERMISSION_INTENT_REQ_CODE = 4262
}

//Mime types restrictions for gallery. By default all mime types are valid
private val mimeTypes: Array<String>

init {
val bundle = activity.intent.extras!!

mimeTypes = bundle.getStringArray(ImagePicker.EXTRA_MIME_TYPES) ?: emptyArray()
}

/**
* Start Gallery Capture Intent
*/
Expand All @@ -49,14 +59,14 @@ class GalleryProvider(activity: ImagePickerActivity, private val mimeTypes: Arra
if (!PermissionUtil.isPermissionGranted(this, REQUIRED_PERMISSIONS)) {
requestPermissions(activity, REQUIRED_PERMISSIONS, PERMISSION_INTENT_REQ_CODE)
} else {
startGalleryIntent(mimeTypes)
startGalleryIntent()
}
}

/**
* Start Gallery Intent
*/
private fun startGalleryIntent(mimeTypes: Array<String>) {
private fun startGalleryIntent() {
val galleryIntent = IntentUtils.getGalleryIntent(activity, mimeTypes)
activity.startActivityForResult(galleryIntent, GALLERY_INTENT_REQ_CODE)
}
Expand All @@ -69,7 +79,7 @@ class GalleryProvider(activity: ImagePickerActivity, private val mimeTypes: Arra
// Check again if permission is granted
if (PermissionUtil.isPermissionGranted(this, REQUIRED_PERMISSIONS)) {
// Permission is granted, Start Camera Intent
startGalleryIntent(mimeTypes)
startGalleryIntent()
} else {
// Exit with error message
setError(getString(R.string.permission_gallery_denied))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ object IntentUtils {
*/
private fun getLegacyGalleryPickIntent(mimeTypes: Array<String>): Intent {
// Show Gallery Intent, Will open google photos
val intent = Intent(Intent.ACTION_PICK).applyImageTypes(mimeTypes)
return intent
return Intent(Intent.ACTION_PICK).applyImageTypes(mimeTypes)
}

private fun Intent.applyImageTypes(mimeTypes: Array<String>): Intent {
Expand Down

0 comments on commit eff800c

Please sign in to comment.