diff options
Diffstat (limited to 'mediapicker/src/main/kotlin')
8 files changed, 95 insertions, 62 deletions
diff --git a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/BlurredImageView.kt b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/BlurredImageView.kt index 739bf47..e430dff 100644 --- a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/BlurredImageView.kt +++ b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/BlurredImageView.kt @@ -28,7 +28,7 @@ import ca.allanwang.kau.utils.scaleXY import ca.allanwang.kau.utils.setBackgroundColorRes import ca.allanwang.kau.utils.setIcon import ca.allanwang.kau.utils.visible -import com.mikepenz.google_material_typeface_library.GoogleMaterial +import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial import jp.wasabeef.blurry.internal.BlurFactor import jp.wasabeef.blurry.internal.BlurTask import kotlinx.android.synthetic.main.kau_blurred_imageview.view.* @@ -48,7 +48,9 @@ class BlurredImageView @JvmOverloads constructor( defStyleAttr: Int = 0 ) : FrameLayout(context, attrs, defStyleAttr), MeasureSpecContract by MeasureSpecDelegate() { - private var blurred = false + var isBlurred = false + private set + val imageBase: ImageView get() = image_base init { @@ -69,20 +71,19 @@ class BlurredImageView @JvmOverloads constructor( image_foreground.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) + private fun View.scaleAnimate(scale: Float) = + animate().scaleXY(scale).setDuration(ANIMATION_DURATION) - fun isBlurred(): Boolean { - return blurred - } + private fun View.alphaAnimate(alpha: Float) = + animate().alpha(alpha).setDuration(ANIMATION_DURATION) /** * 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 + if (isBlurred) return + isBlurred = true val factor = BlurFactor() factor.width = width factor.height = height @@ -100,7 +101,7 @@ class BlurredImageView @JvmOverloads constructor( * is still asynchronous and takes time */ fun blurInstantly() { - blurred = true + isBlurred = true clearAnimation() val factor = BlurFactor() factor.width = width @@ -117,8 +118,8 @@ class BlurredImageView @JvmOverloads constructor( * Animate view back to original state and remove drawable when finished */ fun removeBlur() { - if (!blurred) return - blurred = false + if (!isBlurred) return + isBlurred = false scaleAnimate(1.0f).start() image_blur.alphaAnimate(0f).withEndAction { image_blur.setImageDrawable(null) }.start() image_foreground.alphaAnimate(0f).start() @@ -128,7 +129,7 @@ class BlurredImageView @JvmOverloads constructor( * Clear all animations and unblur the image */ fun removeBlurInstantly() { - blurred = false + isBlurred = false clearAnimation() scaleX = 1.0f scaleX = 1.0f @@ -143,9 +144,9 @@ class BlurredImageView @JvmOverloads constructor( * @return true if new state is blurred; false otherwise */ fun toggleBlur(): Boolean { - if (blurred) removeBlur() + if (isBlurred) removeBlur() else blur() - return blurred + return isBlurred } /** @@ -162,7 +163,7 @@ class BlurredImageView @JvmOverloads constructor( */ fun fullReset() { reset() - fullAction({ it.visible().background = null }) + fullAction { it.visible().background = null } image_foreground.setBackgroundColorRes(R.color.kau_blurred_image_selection_overlay) image_foreground.setIcon(GoogleMaterial.Icon.gmd_check, 30, Color.WHITE) } diff --git a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaActionItem.kt b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaActionItem.kt index 55b448e..552bf0f 100644 --- a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaActionItem.kt +++ b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaActionItem.kt @@ -42,6 +42,10 @@ class MediaActionItem( R.id.kau_item_media_action ) { + override var isSelectable: Boolean + get() = false + set(_) {} + override fun bindView(holder: MediaItemBasic.ViewHolder, payloads: MutableList<Any>) { super.bindView(holder, payloads) holder.image.apply { diff --git a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaItem.kt b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaItem.kt index 5d47590..88a304f 100644 --- a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaItem.kt +++ b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaItem.kt @@ -52,7 +52,7 @@ class MediaItem(val data: MediaModel) : override var isSelectable: Boolean get() = !failedToLoad - set(value) {} + set(_) {} override fun bindView(holder: ViewHolder, payloads: MutableList<Any>) { super.bindView(holder, payloads) diff --git a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaItemBasic.kt b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaItemBasic.kt index 73647b8..873289f 100644 --- a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaItemBasic.kt +++ b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaItemBasic.kt @@ -27,29 +27,32 @@ 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 +import com.mikepenz.fastadapter.select.getSelectExtension /** * 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) }), + KauIItem<MediaItemBasic.ViewHolder>(R.layout.kau_iitem_image_basic, { ViewHolder(it) }), GlideContract by GlideDelegate() { companion object { @SuppressLint("NewApi") fun bindEvents(activity: Activity, fastAdapter: FastAdapter<MediaItemBasic>) { - fastAdapter.withSelectable(false) - //add image data and return right away - .withOnClickListener { _, _, item, _ -> - activity.finish(arrayListOf(item.data)) - true - } + fastAdapter.getSelectExtension().isSelectable = true + //add image data and return right away + fastAdapter.onClickListener = { _, _, item, _ -> + activity.finish(arrayListOf(item.data)) + true + } } } - override fun isSelectable(): Boolean = false + override var isSelectable: Boolean + get() = false + set(_) {} - override fun bindView(holder: ViewHolder, payloads: List<Any>) { + override fun bindView(holder: ViewHolder, payloads: MutableList<Any>) { super.bindView(holder, payloads) glide(holder.itemView) .load(data.data) diff --git a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaModel.kt b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaModel.kt index edd6199..1ba8041 100644 --- a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaModel.kt +++ b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaModel.kt @@ -85,6 +85,8 @@ data class MediaModel( } companion object CREATOR : Parcelable.Creator<MediaModel> { + @Suppress("DEPRECATION") + // TODO verify data deprecation val projection = arrayOf( MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.MIME_TYPE, diff --git a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerActivityBase.kt b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerActivityBase.kt index 5e5d1ed..c7cff55 100644 --- a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerActivityBase.kt +++ b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerActivityBase.kt @@ -26,7 +26,9 @@ import ca.allanwang.kau.utils.setIcon import ca.allanwang.kau.utils.toDrawable import ca.allanwang.kau.utils.toast import com.google.android.material.appbar.AppBarLayout -import com.mikepenz.google_material_typeface_library.GoogleMaterial +import com.mikepenz.fastadapter.ISelectionListener +import com.mikepenz.fastadapter.select.selectExtension +import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial import kotlinx.android.synthetic.main.kau_activity_image_picker.* /** @@ -46,21 +48,37 @@ abstract class MediaPickerActivityBase( setContentView(R.layout.kau_activity_image_picker) - kau_selection_count.setCompoundDrawables(null, null, GoogleMaterial.Icon.gmd_image.toDrawable(this, 18), null) + kau_selection_count.setCompoundDrawables( + null, + null, + GoogleMaterial.Icon.gmd_image.toDrawable(this, 18), + null + ) setSupportActionBar(kau_toolbar) supportActionBar?.apply { setDisplayHomeAsUpEnabled(true) setDisplayShowHomeEnabled(true) - setHomeAsUpIndicator(GoogleMaterial.Icon.gmd_close.toDrawable(this@MediaPickerActivityBase, 18)) + setHomeAsUpIndicator( + GoogleMaterial.Icon.gmd_close.toDrawable( + this@MediaPickerActivityBase, + 18 + ) + ) } kau_toolbar.setNavigationOnClickListener { onBackPressed() } initializeRecycler(kau_recyclerview) - MediaItem.bindEvents(adapter.fastAdapter) - adapter.fastAdapter.withSelectionListener { _, _ -> - kau_selection_count.text = adapter.selectionSize.toString() + adapter.fastAdapter!!.let { + MediaItem.bindEvents(it) + it.selectExtension { + selectionListener = object : ISelectionListener<MediaItem> { + override fun onSelectionChanged(item: MediaItem?, selected: Boolean) { + kau_selection_count.text = adapter.selectionSize.toString() + } + } + } } kau_fab.apply { diff --git a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerActivityOverlayBase.kt b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerActivityOverlayBase.kt index a7ee63d..df47688 100644 --- a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerActivityOverlayBase.kt +++ b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerActivityOverlayBase.kt @@ -39,7 +39,7 @@ abstract class MediaPickerActivityOverlayBase( super.onCreate(savedInstanceState) setContentView(R.layout.kau_activity_image_picker_overlay) initializeRecycler(kau_recyclerview) - MediaItemBasic.bindEvents(this, adapter.fastAdapter) + MediaItemBasic.bindEvents(this, adapter.fastAdapter!!) kau_draggable.addExitListener(this, R.transition.kau_image_exit_bottom, R.transition.kau_image_exit_top) kau_draggable.setOnClickListener { finishAfterTransition() } diff --git a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerCore.kt b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerCore.kt index c928e04..116f511 100644 --- a/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerCore.kt +++ b/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerCore.kt @@ -46,13 +46,10 @@ import com.bumptech.glide.Glide import com.bumptech.glide.RequestManager import com.mikepenz.fastadapter.IItem import com.mikepenz.fastadapter.adapters.ItemAdapter -import com.mikepenz.iconics.IconicsDrawable -import com.mikepenz.iconics.colorInt -import com.mikepenz.iconics.paddingPx -import com.mikepenz.iconics.sizePx +import com.mikepenz.iconics.dsl.ExperimentalIconicsDSL +import com.mikepenz.iconics.dsl.iconicsDrawable import com.mikepenz.iconics.typeface.IIcon import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial -import com.mikepenz.iconics.utils.toIconicsColor import kotlinx.coroutines.CancellationException import java.io.File @@ -91,16 +88,19 @@ abstract class MediaPickerCore<T : IItem<*>>( /** * Create error tile for a given item */ + @ExperimentalIconicsDSL fun getErrorDrawable(context: Context) = getIconDrawable(context, GoogleMaterial.Icon.gmd_error, accentColor) + @ExperimentalIconicsDSL fun getIconDrawable(context: Context, iicon: IIcon, color: Int): Drawable { - val sizePx = MediaPickerCore.computeViewSize(context) - return IconicsDrawable(context, iicon) - .sizePx(sizePx) - .backgroundColor(color.toIconicsColor()) - .paddingPx(sizePx / 3) - .colorInt(Color.WHITE) + val sizePx = computeViewSize(context) + return context.iconicsDrawable(iicon) { + size = sizePx(sizePx) + backgroundColor = colorInt(color) + padding = sizePx(sizePx / 3) + this.color = colorInt(Color.WHITE) + } } var accentColor: Int = 0xff666666.toInt() @@ -110,12 +110,12 @@ abstract class MediaPickerCore<T : IItem<*>>( * 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 - ) - ) + if (resultCode != Activity.RESULT_OK || + data?.hasExtra(MEDIA_PICKER_RESULT) != true + ) { return emptyList() - return data.getParcelableArrayListExtra(MEDIA_PICKER_RESULT) + } + return data.getParcelableArrayListExtra(MEDIA_PICKER_RESULT) ?: emptyList() } /** @@ -278,8 +278,8 @@ abstract class MediaPickerCore<T : IItem<*>>( override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { if (resultCode != RESULT_OK) { - if (tempPath != null) { - val f = File(tempPath) + tempPath?.let { + val f = File(it) if (f.exists()) f.delete() tempPath = null } @@ -295,14 +295,18 @@ abstract class MediaPickerCore<T : IItem<*>>( private fun onCameraResult(data: Intent?) { val f: File - if (tempPath != null) { - f = File(tempPath) - tempPath = null - } else if (data?.data != null) { - f = File(data.data!!.path) - } else { - KL.d { "Media camera no file found" } - return + val tempPath = tempPath + val dataPath = data?.data?.path + when { + tempPath != null -> { + f = File(tempPath) + this.tempPath = null + } + dataPath != null -> f = File(dataPath) + else -> { + KL.d { "Media camera no file found" } + return + } } if (f.exists()) { KL.v { "Media camera path found: ${f.absolutePath}" } @@ -315,9 +319,10 @@ abstract class MediaPickerCore<T : IItem<*>>( private fun onPickerResult(data: Intent?) { val items = mutableListOf<Uri>() - if (data?.data != null) { - KL.v { "Media picker data uri: ${data.data!!.path}" } - items.add(data.data!!) + val _data = data?.data + if (_data != null) { + KL.v { "Media picker data uri: ${_data.path}" } + items.add(_data) } else if (data != null) { val clip = data.clipData if (clip != null) { |