diff options
author | Allan Wang <me@allanwang.ca> | 2017-08-01 10:48:37 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-01 10:48:37 -0700 |
commit | 7d894be6de118357ec908d2d171b6152ce67307d (patch) | |
tree | f34f0676e78433f7f58d6a5bad800430f8e767a0 /mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker | |
parent | 48213d0b427c478865c75fee912ff1ae8bbaffb5 (diff) | |
download | kau-3.2.1.tar.gz kau-3.2.1.tar.bz2 kau-3.2.1.zip |
Imagepicker -> mediapicker (#16)3.2.1
* 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 objec
* 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
* Rename imagepicker to mediapicker
* Clean up
* Fix remaining merge conflicts
* Update readme
* Update readme
Diffstat (limited to 'mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker')
9 files changed, 0 insertions, 755 deletions
diff --git a/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/BlurredImageView.kt b/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/BlurredImageView.kt deleted file mode 100644 index 0dcd7a2..0000000 --- a/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/BlurredImageView.kt +++ /dev/null @@ -1,158 +0,0 @@ -package ca.allanwang.kau.imagepicker - -import android.content.Context -import android.graphics.Color -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 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().scaleXY(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) - scaleXY = 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/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/MediaItem.kt b/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/MediaItem.kt deleted file mode 100644 index 4b70638..0000000 --- a/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/MediaItem.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 MediaItem(val data: MediaModel) - : KauIItem<MediaItem, MediaItem.ViewHolder>(R.layout.kau_iitem_image, { ViewHolder(it) }) { - - private var failedToLoad = false - - companion object { - fun bindEvents(fastAdapter: FastAdapter<MediaItem>) { - 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(MediaPickerCore.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/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/MediaItemBasic.kt b/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/MediaItemBasic.kt deleted file mode 100644 index 7a1ebf7..0000000 --- a/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/MediaItemBasic.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 MediaItemBasic(val data: MediaModel) - : KauIItem<MediaItem, MediaItemBasic.ViewHolder>(R.layout.kau_iitem_image_basic, { ViewHolder(it) }) { - - companion object { - @SuppressLint("NewApi") - fun bindEvents(activity: Activity, fastAdapter: FastAdapter<MediaItemBasic>) { - fastAdapter.withSelectable(false) - //add image data and return right away - .withOnClickListener { _, _, item, _ -> - val intent = Intent() - val data = arrayListOf(item.data) - intent.putParcelableArrayListExtra(MEDIA_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(MediaPickerCore.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/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/MediaModel.kt b/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/MediaModel.kt deleted file mode 100644 index c384d48..0000000 --- a/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/MediaModel.kt +++ /dev/null @@ -1,78 +0,0 @@ -package ca.allanwang.kau.imagepicker - -import android.database.Cursor -import android.database.SQLException -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 MediaModel( - val data: String, val mimeType: String, val size: Long, val dateModified: Long, val displayName: String? -) : Parcelable { - - @Throws(SQLException::class) - constructor(@NonNull cursor: Cursor) : this( - cursor.getString(0), - cursor.getString(1), - cursor.getLong(2), - cursor.getLong(3), - cursor.getString(4) - ) - - constructor(parcel: Parcel) : this( - parcel.readString(), - parcel.readString(), - parcel.readLong(), - parcel.readLong(), - parcel.readString()) - - override fun writeToParcel(parcel: Parcel, flags: Int) { - parcel.writeString(this.data) - parcel.writeString(this.mimeType) - parcel.writeLong(this.size) - parcel.writeLong(this.dateModified) - parcel.writeString(this.displayName) - } - - val isGif - get() = mimeType.endsWith("gif") - - val isImage - get() = mimeType.endsWith("image") - - val isVideo - get() = mimeType.endsWith("video") - - val uri: Uri by lazy { Uri.fromFile(File(data)) } - - override fun describeContents(): Int { - return 0 - } - - companion object CREATOR : Parcelable.Creator<MediaModel> { - val projection = arrayOf( - MediaStore.MediaColumns.DATA, - MediaStore.MediaColumns.MIME_TYPE, - MediaStore.MediaColumns.SIZE, - MediaStore.MediaColumns.DATE_MODIFIED, - MediaStore.MediaColumns.DISPLAY_NAME - ) - - override fun createFromParcel(parcel: Parcel): MediaModel { - return MediaModel(parcel) - } - - override fun newArray(size: Int): Array<MediaModel?> { - return arrayOfNulls(size) - } - } - -}
\ No newline at end of file diff --git a/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/MediaPickerActivityBase.kt b/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/MediaPickerActivityBase.kt deleted file mode 100644 index ee68f42..0000000 --- a/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/MediaPickerActivityBase.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 [MediaPickerActivityOverlayBase] - */ -abstract class MediaPickerActivityBase(mediaType: MediaType) : MediaPickerCore<MediaItem>(mediaType) { - - 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@MediaPickerActivityBase, 18)) - } - toolbar.setNavigationOnClickListener { onBackPressed() } - - initializeRecycler(recycler) - - MediaItem.bindEvents(adapter) - adapter.withSelectionListener({ _, _ -> selectionCount.text = adapter.selections.size.toString() }) - - fab.apply { - show() - setIcon(GoogleMaterial.Icon.gmd_send) - setOnClickListener { - val selection = adapter.selectedItems - if (selection.isEmpty()) { - toast(R.string.kau_no_items_selected) - } else { - val intent = Intent() - val data = ArrayList(selection.map { it.data }) - intent.putParcelableArrayListExtra(MEDIA_PICKER_RESULT, data) - setResult(RESULT_OK, intent) - finish() - } - } - hideOnDownwardsScroll(recycler) - } - - loadItems() - } - - override fun converter(model: MediaModel): MediaItem = MediaItem(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() < adapter.getItemCount() - 1) - } - - override fun onStatusChange(loaded: Boolean) { - setToolbarScrollable(loaded) - } - -}
\ No newline at end of file diff --git a/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/MediaPickerActivityOverlayBase.kt b/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/MediaPickerActivityOverlayBase.kt deleted file mode 100644 index 5161c08..0000000 --- a/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/MediaPickerActivityOverlayBase.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 [MediaPickerActivityBase], as all images are one layer deep - * as opposed to three layers deep - */ -@RequiresApi(Build.VERSION_CODES.LOLLIPOP) -abstract class MediaPickerActivityOverlayBase(mediaType: MediaType) : MediaPickerCore<MediaItemBasic>(mediaType) { - - 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) - MediaItemBasic.bindEvents(this, adapter) - - draggable.addExitListener(this, R.transition.kau_image_exit_bottom, R.transition.kau_image_exit_top) - draggable.setOnClickListener { finishAfterTransition() } - - loadItems() - } - - override fun finishAfterTransition() { - recycler.stopScroll() - super.finishAfterTransition() - } - - override fun onStatusChange(loaded: Boolean) { - if (!loaded) toast(R.string.kau_no_items_loaded) - } - - override fun converter(model: MediaModel): MediaItemBasic = MediaItemBasic(model) - - override fun onBackPressed() { - finishAfterTransition() - } -}
\ No newline at end of file diff --git a/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/MediaPickerBinder.kt b/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/MediaPickerBinder.kt deleted file mode 100644 index e423e84..0000000 --- a/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/MediaPickerBinder.kt +++ /dev/null @@ -1,33 +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.kauLaunchMediaPicker(clazz: Class<out MediaPickerCore<*>>, requestCode: Int) { - startActivityForResult(clazz, requestCode, transition = MediaPickerActivityOverlayBase::class.java.isAssignableFrom(clazz)) -} - -/** - * Image picker result - * call under [Activity.onActivityResult] - * and make sure that the requestCode matches first - */ -fun Activity.kauOnMediaPickerResult(resultCode: Int, data: Intent?) = MediaPickerCore.onMediaPickerResult(resultCode, data) - -internal const val LOADER_ID = 42 -internal const val MEDIA_PICKER_RESULT = "media_picker_result" - -internal const val ANIMATION_DURATION = 200L -internal const val ANIMATION_SCALE = 0.95f - diff --git a/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/MediaPickerCore.kt b/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/MediaPickerCore.kt deleted file mode 100644 index 4b922f9..0000000 --- a/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/MediaPickerCore.kt +++ /dev/null @@ -1,182 +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 both pickers - */ -abstract class MediaPickerCore<T : IItem<*, *>>(val mediaType: MediaType) : 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 = MediaPickerCore.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 media from our media picker - * This is used for both single and multiple photo picks - */ - fun onMediaPickerResult(resultCode: Int, data: Intent?): List<MediaModel> { - if (resultCode != Activity.RESULT_OK || data == null || !data.hasExtra(MEDIA_PICKER_RESULT)) - return emptyList() - return data.getParcelableArrayListExtra(MEDIA_PICKER_RESULT) - } - - /** - * Number of loaded items we should cache - * This is arbitrary - */ - const val CACHE_SIZE = 80 - } - - val adapter: 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 if (mediaType != MediaType.VIDEO) extraSpace else super.getExtraLayoutSpace(state) - } - } - setItemViewCacheSize(CACHE_SIZE) - isDrawingCacheEnabled = true - layoutManager = manager - adapter = this@MediaPickerCore.adapter - setHasFixedSize(true) - itemAnimator = KauAnimator(FadeScaleAnimatorAdd(0.8f)) - } - } - - //Sort by descending date - var sortQuery = MediaStore.MediaColumns.DATE_MODIFIED + " DESC" - - override fun onCreateLoader(id: Int, args: Bundle?): Loader<Cursor> { - return CursorLoader(this, mediaType.contentUri, MediaModel.projection, null, null, sortQuery) - } - - /** - * Request read permissions and load all external items - * 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 loadItems() { - 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 onLoadFinished(loader: Loader<Cursor>?, data: Cursor?) { - reset() - if (data == null || !data.moveToFirst()) { - toast(R.string.kau_no_items_found) - onStatusChange(false) - return - } - val items = mutableListOf<T>() - do { - val model = MediaModel(data) - if (!shouldLoad(model)) continue - items.add(converter(model)) - } while (data.moveToNext()) - addItems(items) - } - - abstract fun converter(model: MediaModel): 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>) { - adapter.add(items) - } - - /** - * Clears the adapter to prepare for a new load - */ - open fun reset() { - adapter.clear() - } - - /** - * Optional filter to decide which items get displayed - * Defaults to checking their sizes to filter out - * very small items such as lurking drawables/icons - * - * Returns true if model should be displayed, false otherwise - */ - open fun shouldLoad(model: MediaModel): Boolean = model.size > 10000L - - open fun onStatusChange(loaded: Boolean) {} - -}
\ No newline at end of file diff --git a/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/MediaType.kt b/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/MediaType.kt deleted file mode 100644 index c934e04..0000000 --- a/mediapicker/src/main/kotlin/ca/allanwang/kau/imagepicker/MediaType.kt +++ /dev/null @@ -1,13 +0,0 @@ -package ca.allanwang.kau.imagepicker - -import android.net.Uri -import android.provider.MediaStore -import com.bumptech.glide.load.engine.DiskCacheStrategy - -/** - * Created by Allan Wang on 2017-07-30. - */ -enum class MediaType(val cacheStrategy: DiskCacheStrategy, val contentUri: Uri) { - IMAGE(DiskCacheStrategy.AUTOMATIC, MediaStore.Images.Media.EXTERNAL_CONTENT_URI), - VIDEO(DiskCacheStrategy.AUTOMATIC, MediaStore.Video.Media.EXTERNAL_CONTENT_URI) -}
\ No newline at end of file |