diff options
author | Allan Wang <me@allanwang.ca> | 2017-07-31 23:02:01 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-31 23:02:01 -0700 |
commit | 48213d0b427c478865c75fee912ff1ae8bbaffb5 (patch) | |
tree | 7aef1d8400fc3403ee5a40aba945f33a95319359 /imagepicker/src/main/kotlin/ca/allanwang | |
parent | 8a4e9fd44dfbcf58aa7ab63167dcbdf8752db7d0 (diff) | |
download | kau-48213d0b427c478865c75fee912ff1ae8bbaffb5.tar.gz kau-48213d0b427c478865c75fee912ff1ae8bbaffb5.tar.bz2 kau-48213d0b427c478865c75fee912ff1ae8bbaffb5.zip |
Major update to core and kotterknife; create mediapicker (#15)
* Readme
* Fix kau direction bits
* Truly support transparent ripples
* Update changelog
* Test rect as base
* Replace fab transition with generic fade scale transition
* Add scalexy func
* Add scaleXY
* Add arguments to fadeScaleTransition
* Clean up ink indicator
* Create setOnSingleTapListener
* Fix lint and add rndColor
* Create kotterknife resettables
* Add readme and missing object
* Create lazy resettable registered
* Update core docs
* Opt for separate class for resettable registry
* Clean up resettable registry
* Rename functions
* Add ripple callback listener
* Adjust kprefactivity desc color
* Add more transitions
* Add delete keys option
* Add instrumentation tests
* switch id
* Revert automatic instrumental tests
* Generify imagepickercore and prepare video alternative
* Create working video picker
* Address possible null issue
* Update searchview
* Make layouts public
* Add changelog test
* Update logo link
* Add custom color gif
Diffstat (limited to 'imagepicker/src/main/kotlin/ca/allanwang')
8 files changed, 0 insertions, 744 deletions
diff --git a/imagepicker/src/main/kotlin/ca/allanwang/kau/imagepicker/BlurredImageView.kt b/imagepicker/src/main/kotlin/ca/allanwang/kau/imagepicker/BlurredImageView.kt deleted file mode 100644 index 2ce00ba..0000000 --- a/imagepicker/src/main/kotlin/ca/allanwang/kau/imagepicker/BlurredImageView.kt +++ /dev/null @@ -1,161 +0,0 @@ -package ca.allanwang.kau.imagepicker - -import android.content.Context -import android.graphics.Color -import android.support.annotation.StyleRes -import android.util.AttributeSet -import android.view.View -import android.widget.FrameLayout -import android.widget.ImageView -import ca.allanwang.kau.ui.views.MeasureSpecContract -import ca.allanwang.kau.ui.views.MeasureSpecDelegate -import ca.allanwang.kau.utils.* -import com.mikepenz.google_material_typeface_library.GoogleMaterial -import com.mikepenz.iconics.IconicsDrawable -import jp.wasabeef.blurry.internal.BlurFactor -import jp.wasabeef.blurry.internal.BlurTask - -/** - * Created by Allan Wang on 2017-07-14. - * - * ImageView that can be blurred and selected - * The frame is composed of three layers: the base, the blur, and the foreground - * Images should be placed in the base view, and the blur view should not be touched - * as the class will handle it - * The foreground by default contains a white checkmark, but can be customized or hidden depending on the situation - */ -class BlurredImageView @JvmOverloads constructor( - context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 -) : FrameLayout(context, attrs, defStyleAttr), MeasureSpecContract by MeasureSpecDelegate() { - - private var blurred = false - val imageBase: ImageView by bindView(R.id.image_base) - internal val imageBlur: ImageView by bindView(R.id.image_blur) - val imageForeground: ImageView by bindView(R.id.image_foreground) - - init { - inflate(R.layout.kau_blurred_imageview, true) - initAttrs(context, attrs) - imageForeground.setIcon(GoogleMaterial.Icon.gmd_check, 30) - } - - override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { - val result = onMeasure(this, widthMeasureSpec, heightMeasureSpec) - super.onMeasure(result.first, result.second) - } - - override fun clearAnimation() { - super.clearAnimation() - imageBase.clearAnimation() - imageBlur.clearAnimation() - imageForeground.clearAnimation() - } - - private fun View.scaleAnimate(scale: Float) = animate().scaleX(scale).scaleY(scale).setDuration(ANIMATION_DURATION) - private fun View.alphaAnimate(alpha: Float) = animate().alpha(alpha).setDuration(ANIMATION_DURATION) - - - fun isBlurred(): Boolean { - return blurred - } - - /** - * Applies a blur and fills the blur image asynchronously - * When ready, scales the image down and shows the blur & foreground - */ - fun blur() { - if (blurred) return - blurred = true - val factor = BlurFactor() - factor.width = width - factor.height = height - BlurTask(imageBase, factor) { - imageBlur.setImageDrawable(it) - scaleAnimate(ANIMATION_SCALE).start() - imageBlur.alphaAnimate(1f).start() - imageForeground.alphaAnimate(1f).start() - }.execute() - } - - /** - * Clears animations and blurs the image without further animations - * This method is relatively instantaneous, as retrieving the blurred image - * is still asynchronous and takes time - */ - fun blurInstantly() { - blurred = true - clearAnimation() - val factor = BlurFactor() - factor.width = width - factor.height = height - BlurTask(imageBase, factor) { drawable -> - imageBlur.setImageDrawable(drawable) - scaleX = ANIMATION_SCALE - scaleY = ANIMATION_SCALE - imageBlur.alpha = 1f - imageForeground.alpha = 1f - }.execute() - } - - /** - * Animate view back to original state and remove drawable when finished - */ - fun removeBlur() { - if (!blurred) return - blurred = false - scaleAnimate(1.0f).start() - imageBlur.alphaAnimate(0f).withEndAction { imageBlur.setImageDrawable(null) }.start() - imageForeground.alphaAnimate(0f).start() - } - - - /** - * Clear all animations and unblur the image - */ - fun removeBlurInstantly() { - blurred = false - clearAnimation() - scaleX = 1.0f - scaleX = 1.0f - imageBlur.alpha = 0f - imageBlur.setImageDrawable(null) - imageForeground.alpha = 0f - } - - /** - * Switch blur state and apply transition - * - * @return true if new state is blurred; false otherwise - */ - fun toggleBlur(): Boolean { - if (blurred) removeBlur() - else blur() - return blurred - } - - /** - * Clears all of the blur effects to restore the original states - * If views were modified in other ways, this method won't affect it - */ - fun reset() { - removeBlurInstantly() - imageBase.setImageDrawable(null) - } - - /** - * Reset most of possible changes to the view - */ - fun fullReset() { - reset() - fullAction({ it.visible().background = null }) - imageForeground.setBackgroundColorRes(R.color.kau_blurred_image_selection_overlay) - imageForeground.setIcon(GoogleMaterial.Icon.gmd_check, 30, Color.WHITE) - } - - private fun fullAction(action: (View) -> Unit) { - action(this) - action(imageBase) - action(imageBlur) - action(imageForeground) - } -}
\ No newline at end of file diff --git a/imagepicker/src/main/kotlin/ca/allanwang/kau/imagepicker/ImageItem.kt b/imagepicker/src/main/kotlin/ca/allanwang/kau/imagepicker/ImageItem.kt deleted file mode 100644 index ffeb560..0000000 --- a/imagepicker/src/main/kotlin/ca/allanwang/kau/imagepicker/ImageItem.kt +++ /dev/null @@ -1,69 +0,0 @@ -package ca.allanwang.kau.imagepicker - -import android.graphics.drawable.Drawable -import android.support.v7.widget.RecyclerView -import android.view.View -import ca.allanwang.kau.iitems.KauIItem -import ca.allanwang.kau.utils.bindView -import com.bumptech.glide.Glide -import com.bumptech.glide.load.DataSource -import com.bumptech.glide.load.engine.GlideException -import com.bumptech.glide.request.RequestListener -import com.bumptech.glide.request.target.Target -import com.mikepenz.fastadapter.FastAdapter - -/** - * Created by Allan Wang on 2017-07-04. - */ -class ImageItem(val data: ImageModel) - : KauIItem<ImageItem, ImageItem.ViewHolder>(R.layout.kau_iitem_image, { ViewHolder(it) }) { - - private var failedToLoad = false - - companion object { - fun bindEvents(fastAdapter: FastAdapter<ImageItem>) { - fastAdapter.withMultiSelect(true) - .withSelectable(true) - //adapter selector occurs before the on click event - .withOnClickListener { v, _, item, _ -> - val image = v as BlurredImageView - if (item.isSelected) image.blur() - else image.removeBlur() - true - } - } - } - - override fun isSelectable(): Boolean = !failedToLoad - - override fun bindView(holder: ViewHolder, payloads: List<Any>?) { - super.bindView(holder, payloads) - Glide.with(holder.itemView) - .load(data.data) - .listener(object : RequestListener<Drawable> { - override fun onLoadFailed(e: GlideException?, model: Any, target: Target<Drawable>, isFirstResource: Boolean): Boolean { - failedToLoad = true - holder.container.imageBase.setImageDrawable(ImagePickerCore.getErrorDrawable(holder.itemView.context)) - return true; - } - - override fun onResourceReady(resource: Drawable, model: Any, target: Target<Drawable>, dataSource: DataSource, isFirstResource: Boolean): Boolean { - holder.container.imageBase.setImageDrawable(resource) - if (isSelected) holder.container.blurInstantly() - return true; - } - }) - .into(holder.container.imageBase) - } - - override fun unbindView(holder: ViewHolder) { - super.unbindView(holder) - Glide.with(holder.itemView).clear(holder.container.imageBase) - holder.container.removeBlurInstantly() - failedToLoad = false - } - - class ViewHolder(v: View) : RecyclerView.ViewHolder(v) { - val container: BlurredImageView by bindView(R.id.kau_image) - } -}
\ No newline at end of file diff --git a/imagepicker/src/main/kotlin/ca/allanwang/kau/imagepicker/ImageItemBasic.kt b/imagepicker/src/main/kotlin/ca/allanwang/kau/imagepicker/ImageItemBasic.kt deleted file mode 100644 index bc2a4e1..0000000 --- a/imagepicker/src/main/kotlin/ca/allanwang/kau/imagepicker/ImageItemBasic.kt +++ /dev/null @@ -1,71 +0,0 @@ -package ca.allanwang.kau.imagepicker - -import android.annotation.SuppressLint -import android.app.Activity -import android.content.Intent -import android.graphics.drawable.Drawable -import android.support.v7.app.AppCompatActivity -import android.support.v7.widget.RecyclerView -import android.view.View -import ca.allanwang.kau.iitems.KauIItem -import ca.allanwang.kau.ui.views.MeasuredImageView -import ca.allanwang.kau.utils.bindView -import ca.allanwang.kau.utils.buildIsLollipopAndUp -import com.bumptech.glide.Glide -import com.bumptech.glide.load.DataSource -import com.bumptech.glide.load.engine.GlideException -import com.bumptech.glide.request.RequestListener -import com.bumptech.glide.request.target.Target -import com.mikepenz.fastadapter.FastAdapter - -/** - * Created by Allan Wang on 2017-07-04. - */ -class ImageItemBasic(val data: ImageModel) - : KauIItem<ImageItem, ImageItemBasic.ViewHolder>(R.layout.kau_iitem_image_basic, { ViewHolder(it) }) { - - companion object { - @SuppressLint("NewApi") - fun bindEvents(activity: Activity, fastAdapter: FastAdapter<ImageItemBasic>) { - fastAdapter.withSelectable(false) - //add image data and return right away - .withOnClickListener { _, _, item, _ -> - val intent = Intent() - val data = arrayListOf(item.data) - intent.putParcelableArrayListExtra(IMAGE_PICKER_RESULT, data) - activity.setResult(AppCompatActivity.RESULT_OK, intent) - if (buildIsLollipopAndUp) activity.finishAfterTransition() - else activity.finish() - true - } - } - } - - override fun isSelectable(): Boolean = false - - override fun bindView(holder: ViewHolder, payloads: List<Any>?) { - super.bindView(holder, payloads) - Glide.with(holder.itemView) - .load(data.data) - .listener(object : RequestListener<Drawable> { - override fun onLoadFailed(e: GlideException?, model: Any, target: Target<Drawable>, isFirstResource: Boolean): Boolean { - holder.image.setImageDrawable(ImagePickerCore.getErrorDrawable(holder.itemView.context)) - return true; - } - - override fun onResourceReady(resource: Drawable, model: Any, target: Target<Drawable>, dataSource: DataSource, isFirstResource: Boolean): Boolean { - return false - } - }) - .into(holder.image) - } - - override fun unbindView(holder: ViewHolder) { - super.unbindView(holder) - Glide.with(holder.itemView).clear(holder.image) - } - - class ViewHolder(v: View) : RecyclerView.ViewHolder(v) { - val image: MeasuredImageView by bindView(R.id.kau_image) - } -}
\ No newline at end of file diff --git a/imagepicker/src/main/kotlin/ca/allanwang/kau/imagepicker/ImageModel.kt b/imagepicker/src/main/kotlin/ca/allanwang/kau/imagepicker/ImageModel.kt deleted file mode 100644 index 202805c..0000000 --- a/imagepicker/src/main/kotlin/ca/allanwang/kau/imagepicker/ImageModel.kt +++ /dev/null @@ -1,58 +0,0 @@ -package ca.allanwang.kau.imagepicker - -import android.database.Cursor -import android.net.Uri -import android.os.Parcel -import android.os.Parcelable -import android.provider.MediaStore -import android.support.annotation.NonNull -import java.io.File - - -/** - * Created by Allan Wang on 2017-07-14. - */ - -data class ImageModel(val size: Long, val dateModified: Long, val data: String, val displayName: String) : Parcelable { - - constructor(@NonNull cursor: Cursor) : this( - cursor.getLong(MediaStore.Images.Media.SIZE), - cursor.getLong(MediaStore.Images.Media.DATE_MODIFIED), - cursor.getString(MediaStore.Images.Media.DATA), - cursor.getString(MediaStore.Images.Media.DISPLAY_NAME) - ) - - constructor(parcel: Parcel) : this( - parcel.readLong(), - parcel.readLong(), - parcel.readString(), - parcel.readString()) - - - override fun writeToParcel(parcel: Parcel, flags: Int) { - parcel.writeLong(this.size) - parcel.writeLong(this.dateModified) - parcel.writeString(this.data) - parcel.writeString(this.displayName) - } - - val uri: Uri by lazy { Uri.fromFile(File(data)) } - - override fun describeContents(): Int { - return 0 - } - - companion object CREATOR : Parcelable.Creator<ImageModel> { - override fun createFromParcel(parcel: Parcel): ImageModel { - return ImageModel(parcel) - } - - override fun newArray(size: Int): Array<ImageModel?> { - return arrayOfNulls(size) - } - } - -} - -private fun Cursor.getString(name: String) = getString(getColumnIndex(name)) -private fun Cursor.getLong(name: String) = getLong(getColumnIndex(name))
\ No newline at end of file diff --git a/imagepicker/src/main/kotlin/ca/allanwang/kau/imagepicker/ImagePickerActivityBase.kt b/imagepicker/src/main/kotlin/ca/allanwang/kau/imagepicker/ImagePickerActivityBase.kt deleted file mode 100644 index 3a9809c..0000000 --- a/imagepicker/src/main/kotlin/ca/allanwang/kau/imagepicker/ImagePickerActivityBase.kt +++ /dev/null @@ -1,100 +0,0 @@ -package ca.allanwang.kau.imagepicker - -import android.content.Intent -import android.database.Cursor -import android.os.Bundle -import android.support.design.widget.AppBarLayout -import android.support.design.widget.CoordinatorLayout -import android.support.design.widget.FloatingActionButton -import android.support.v4.content.Loader -import android.support.v7.widget.LinearLayoutManager -import android.support.v7.widget.RecyclerView -import android.support.v7.widget.Toolbar -import android.widget.TextView -import ca.allanwang.kau.utils.* -import com.mikepenz.google_material_typeface_library.GoogleMaterial - -/** - * Created by Allan Wang on 2017-07-04. - * - * Base activity for selecting images from storage - * Images are blurred when selected, and multiple images can be selected at a time. - * Having three layered images makes this slightly slower than [ImagePickerActivityOverlayBase] - */ -abstract class ImagePickerActivityBase : ImagePickerCore<ImageItem>() { - - val coordinator: CoordinatorLayout by bindView(R.id.kau_coordinator) - val toolbar: Toolbar by bindView(R.id.kau_toolbar) - val selectionCount: TextView by bindView(R.id.kau_selection_count) - val recycler: RecyclerView by bindView(R.id.kau_recyclerview) - val fab: FloatingActionButton by bindView(R.id.kau_fab) - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - - setContentView(R.layout.kau_activity_image_picker) - - selectionCount.setCompoundDrawables(null, null, GoogleMaterial.Icon.gmd_image.toDrawable(this, 18), null) - - setSupportActionBar(toolbar) - supportActionBar?.apply { - setDisplayHomeAsUpEnabled(true) - setDisplayShowHomeEnabled(true) - setHomeAsUpIndicator(GoogleMaterial.Icon.gmd_close.toDrawable(this@ImagePickerActivityBase, 18)) - } - toolbar.setNavigationOnClickListener { onBackPressed() } - - initializeRecycler(recycler) - - ImageItem.bindEvents(imageAdapter) - imageAdapter.withSelectionListener({ _, _ -> selectionCount.text = imageAdapter.selections.size.toString() }) - - fab.apply { - show() - setIcon(GoogleMaterial.Icon.gmd_send) - setOnClickListener { - val selection = imageAdapter.selectedItems - if (selection.isEmpty()) { - toast(R.string.kau_no_images_selected) - } else { - val intent = Intent() - val data = ArrayList(selection.map { it.data }) - intent.putParcelableArrayListExtra(IMAGE_PICKER_RESULT, data) - setResult(RESULT_OK, intent) - finish() - } - } - hideOnDownwardsScroll(recycler) - } - - loadImages() - } - - override fun converter(model: ImageModel): ImageItem = ImageItem(model) - - /** - * Decide whether the toolbar can hide itself - * We typically want this behaviour unless we don't have enough images - * to fill the entire screen. In that case we don't want the recyclerview to be scrollable - * which means the toolbar shouldn't scroll either - - * @param scrollable true if scroll flags are enabled, false otherwise - */ - private fun setToolbarScrollable(scrollable: Boolean) { - val params = toolbar.layoutParams as AppBarLayout.LayoutParams - if (scrollable) - params.scrollFlags = AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS or AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL - else - params.scrollFlags = 0 - } - - override fun onLoadFinished(loader: Loader<Cursor>?, data: Cursor?) { - super.onLoadFinished(loader, data) - setToolbarScrollable((recycler.layoutManager as LinearLayoutManager).findLastCompletelyVisibleItemPosition() < imageAdapter.getItemCount() - 1) - } - - override fun onStatusChange(loaded: Boolean) { - setToolbarScrollable(loaded) - } - -}
\ No newline at end of file diff --git a/imagepicker/src/main/kotlin/ca/allanwang/kau/imagepicker/ImagePickerActivityOverlayBase.kt b/imagepicker/src/main/kotlin/ca/allanwang/kau/imagepicker/ImagePickerActivityOverlayBase.kt deleted file mode 100644 index ed8eee4..0000000 --- a/imagepicker/src/main/kotlin/ca/allanwang/kau/imagepicker/ImagePickerActivityOverlayBase.kt +++ /dev/null @@ -1,51 +0,0 @@ -package ca.allanwang.kau.imagepicker - -import android.os.Build -import android.os.Bundle -import android.support.annotation.RequiresApi -import android.support.v7.widget.RecyclerView -import ca.allanwang.kau.ui.widgets.ElasticDragDismissFrameLayout -import ca.allanwang.kau.utils.bindView -import ca.allanwang.kau.utils.toast - -/** - * Created by Allan Wang on 2017-07-23. - * - * Base activity for selecting images from storage - * This variant is an overlay and selects one image only before returning directly - * It is more efficient than [ImagePickerActivityBase], as all images are one layer deep - * as opposed to three layers deep - */ -@RequiresApi(Build.VERSION_CODES.LOLLIPOP) -abstract class ImagePickerActivityOverlayBase : ImagePickerCore<ImageItemBasic>() { - - val draggable: ElasticDragDismissFrameLayout by bindView(R.id.kau_draggable) - val recycler: RecyclerView by bindView(R.id.kau_recyclerview) - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setContentView(R.layout.kau_activity_image_picker_overlay) - initializeRecycler(recycler) - ImageItemBasic.bindEvents(this, imageAdapter) - - draggable.addExitListener(this, R.transition.kau_image_exit_bottom, R.transition.kau_image_exit_top) - draggable.setOnClickListener { finishAfterTransition() } - - loadImages() - } - - override fun finishAfterTransition() { - recycler.stopScroll() - super.finishAfterTransition() - } - - override fun onStatusChange(loaded: Boolean) { - if (!loaded) toast(R.string.kau_no_images_loaded) - } - - override fun converter(model: ImageModel): ImageItemBasic = ImageItemBasic(model) - - override fun onBackPressed() { - finishAfterTransition() - } -}
\ No newline at end of file diff --git a/imagepicker/src/main/kotlin/ca/allanwang/kau/imagepicker/ImagePickerBinder.kt b/imagepicker/src/main/kotlin/ca/allanwang/kau/imagepicker/ImagePickerBinder.kt deleted file mode 100644 index db8d113..0000000 --- a/imagepicker/src/main/kotlin/ca/allanwang/kau/imagepicker/ImagePickerBinder.kt +++ /dev/null @@ -1,34 +0,0 @@ -package ca.allanwang.kau.imagepicker - -import android.app.Activity -import android.content.Intent -import ca.allanwang.kau.utils.startActivityForResult - -/** - * Created by Allan Wang on 2017-07-21. - * - * Extension functions for interacting with the image picker - * as well as internal constants - */ - -/** - * Image picker launchers - */ -fun Activity.kauLaunchImagePicker(clazz: Class<out ImagePickerCore<*>>, requestCode: Int) { -// startActivityForResult(clazz, requestCode, true) - startActivityForResult(clazz, requestCode, transition = ImagePickerActivityOverlayBase::class.java.isAssignableFrom(clazz)) -} - -/** - * Image picker result - * call under [Activity.onActivityResult] - * and make sure that the requestCode matches first - */ -fun Activity.kauOnImagePickerResult(resultCode: Int, data: Intent?) = ImagePickerCore.onImagePickerResult(resultCode, data) - -internal const val LOADER_ID = 42 -internal const val IMAGE_PICKER_RESULT = "image_picker_result" - -internal const val ANIMATION_DURATION = 200L -internal const val ANIMATION_SCALE = 0.95f - diff --git a/imagepicker/src/main/kotlin/ca/allanwang/kau/imagepicker/ImagePickerCore.kt b/imagepicker/src/main/kotlin/ca/allanwang/kau/imagepicker/ImagePickerCore.kt deleted file mode 100644 index f197a5e..0000000 --- a/imagepicker/src/main/kotlin/ca/allanwang/kau/imagepicker/ImagePickerCore.kt +++ /dev/null @@ -1,200 +0,0 @@ -package ca.allanwang.kau.imagepicker - -import android.Manifest -import android.app.Activity -import android.content.Context -import android.content.Intent -import android.database.Cursor -import android.graphics.Color -import android.graphics.drawable.Drawable -import android.os.Bundle -import android.provider.MediaStore -import android.support.v4.app.LoaderManager -import android.support.v4.content.CursorLoader -import android.support.v4.content.Loader -import android.support.v7.app.AppCompatActivity -import android.support.v7.widget.GridLayoutManager -import android.support.v7.widget.RecyclerView -import ca.allanwang.kau.animators.FadeScaleAnimatorAdd -import ca.allanwang.kau.animators.KauAnimator -import ca.allanwang.kau.permissions.kauRequestPermissions -import ca.allanwang.kau.utils.dimenPixelSize -import ca.allanwang.kau.utils.toast -import com.mikepenz.fastadapter.IItem -import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter -import com.mikepenz.google_material_typeface_library.GoogleMaterial -import com.mikepenz.iconics.IconicsDrawable - -/** - * Created by Allan Wang on 2017-07-23. - * - * Container for the main logic behind the image pickers - */ -abstract class ImagePickerCore<T : IItem<*, *>> : AppCompatActivity(), LoaderManager.LoaderCallbacks<Cursor> { - - companion object { - /** - * Given the dimensions of our device and a minimum image size, - * Computer the optimal column count for our grid layout - * - * @return column count - */ - fun computeColumnCount(context: Context): Int { - val minImageSizePx = context.dimenPixelSize(R.dimen.kau_image_minimum_size) - val screenWidthPx = context.resources.displayMetrics.widthPixels - return screenWidthPx / minImageSizePx - } - - /** - * Compute our resulting image size - */ - fun computeViewSize(context: Context): Int { - val screenWidthPx = context.resources.displayMetrics.widthPixels - return screenWidthPx / computeColumnCount(context) - } - - /** - * Create error tile for a given item - */ - fun getErrorDrawable(context: Context): Drawable { - val sizePx = ImagePickerCore.computeViewSize(context) - return IconicsDrawable(context, GoogleMaterial.Icon.gmd_error) - .sizePx(sizePx) - .backgroundColor(accentColor) - .paddingPx(sizePx / 3) - .color(Color.WHITE) - } - - var accentColor: Int = 0xff666666.toInt() - - /** - * Helper method to retrieve the images from our iamge picker - * This is used for both single and multiple photo picks - */ - fun onImagePickerResult(resultCode: Int, data: Intent?): List<ImageModel> { - if (resultCode != Activity.RESULT_OK || data == null || !data.hasExtra(IMAGE_PICKER_RESULT)) - return emptyList() - return data.getParcelableArrayListExtra(IMAGE_PICKER_RESULT) - } - - /** - * Number of loaded images we should cache - * This is arbitrary - */ - const val CACHE_SIZE = 80 - - /** - * We know that Glide takes a while to initially fetch the images - * My as well make it look pretty - */ - const val INITIAL_LOAD_DELAY = 600L - } - - val imageAdapter: FastItemAdapter<T> = FastItemAdapter() - - /** - * Further improve preloading by extending the layout space - */ - val extraSpace: Int by lazy { resources.displayMetrics.heightPixels } - - fun initializeRecycler(recycler: RecyclerView) { - recycler.apply { - val manager = object : GridLayoutManager(context, computeColumnCount(context)) { - override fun getExtraLayoutSpace(state: RecyclerView.State?): Int { - return extraSpace - } - } - setItemViewCacheSize(CACHE_SIZE) - isDrawingCacheEnabled = true - layoutManager = manager - adapter = imageAdapter - setHasFixedSize(true) - itemAnimator = object : KauAnimator(FadeScaleAnimatorAdd(0.8f)) { - override fun startDelay(holder: RecyclerView.ViewHolder, duration: Long, factor: Float): Long { - return super.startDelay(holder, duration, factor) + INITIAL_LOAD_DELAY - } - } - } - } - - //Sort by descending date - var sortQuery = MediaStore.Images.Media.DATE_MODIFIED + " DESC" - - /** - * Request read permissions and load all external images - * The result will be filtered through {@link #onLoadFinished(Loader, Cursor)} - * Call this to make sure that we request permissions each time - * The adapter will be cleared on each successful call - */ - open fun loadImages() { - kauRequestPermissions(Manifest.permission.READ_EXTERNAL_STORAGE) { - granted, _ -> - if (granted) { - supportLoaderManager.initLoader(LOADER_ID, null, this) - onStatusChange(true) - } else { - toast(R.string.kau_permission_denied) - onStatusChange(false) - } - } - } - - override fun onCreateLoader(id: Int, args: Bundle?): Loader<Cursor> { - val columns = arrayOf( - MediaStore.Images.Media._ID, - MediaStore.Images.Media.TITLE, - MediaStore.Images.Media.DATA, - MediaStore.Images.Media.SIZE, - MediaStore.Images.Media.DISPLAY_NAME, - MediaStore.Images.Media.DATE_MODIFIED - ) - return CursorLoader(this, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, sortQuery) - } - - override fun onLoadFinished(loader: Loader<Cursor>?, data: Cursor?) { - reset() - if (data == null || !data.moveToFirst()) { - toast(R.string.kau_no_images_found) - onStatusChange(false) - return - } - val items = mutableListOf<T>() - do { - val model = ImageModel(data) - if (!shouldLoad(model)) continue - items.add(converter(model)) - } while (data.moveToNext()) - addItems(items) - } - - abstract fun converter(model: ImageModel): T - - override fun onLoaderReset(loader: Loader<Cursor>?) = reset() - - /** - * Called at the end of [onLoadFinished] - * when the adapter should add the items - */ - open fun addItems(items: List<T>) { - imageAdapter.add(items) - } - - /** - * Clears the adapter to prepare for a new load - */ - open fun reset() { - imageAdapter.clear() - } - - /** - * Optional filter to decide which images get displayed - * Defaults to checking their sizes to filter out - * very small images such as lurking drawables/icons - * - * Returns true if model should be displayed, false otherwise - */ - open fun shouldLoad(model: ImageModel): Boolean = model.size > 10000L - - open fun onStatusChange(loaded: Boolean) {} - -}
\ No newline at end of file |