diff --git a/app/build.gradle b/app/build.gradle index 642b4a8..9e14a99 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -11,7 +11,7 @@ android { minSdk 21 targetSdk 32 versionCode 5 - versionName "0.3.1" + versionName "0.4.0" ndk { abiFilters 'armeabi-v7a','arm64-v8a','x86','x86_64' } diff --git a/app/src/main/java/com/ethosa/ktc/ui/adapters/AlbumAdapter.kt b/app/src/main/java/com/ethosa/ktc/ui/adapters/AlbumAdapter.kt index cf0cdba..6cba624 100644 --- a/app/src/main/java/com/ethosa/ktc/ui/adapters/AlbumAdapter.kt +++ b/app/src/main/java/com/ethosa/ktc/ui/adapters/AlbumAdapter.kt @@ -1,15 +1,14 @@ package com.ethosa.ktc.ui.adapters import android.animation.ObjectAnimator +import android.animation.PropertyValuesHolder import android.annotation.SuppressLint import android.app.Dialog import android.graphics.Color import android.graphics.drawable.ColorDrawable +import android.os.Build import android.util.DisplayMetrics -import android.view.LayoutInflater -import android.view.MotionEvent -import android.view.View -import android.view.ViewGroup +import android.view.* import android.widget.ImageView import androidx.appcompat.app.AppCompatActivity import androidx.constraintlayout.widget.ConstraintLayout @@ -17,6 +16,8 @@ import androidx.recyclerview.widget.RecyclerView import com.bumptech.glide.Glide import com.ethosa.ktc.R import com.ethosa.ktc.databinding.LayoutAlbumImageBinding +import kotlin.math.abs + /** * Provides RecyclerView.Adapter behavior for album photos. @@ -28,8 +29,19 @@ class AlbumAdapter( private val dialog = Dialog(activity) private var animator = ObjectAnimator() + private var animX = PropertyValuesHolder.ofFloat("x", 0f) + private var animY = PropertyValuesHolder.ofFloat("y", 0f) private var img: ImageView? = null private var root: ConstraintLayout? = null + private var pos: Int = -1 + + companion object { + private const val MAX_DIM_AMOUNT = 0.95f + private const val HIDE_OFFSET = 400 + private const val CHANGE_IMAGE_OFFSET = 200 + private const val Y_PRIORITY = 3 + private const val DIM_STEP = 0.1f + } /** * Provides RecyclerView.ViewHolder behavior. @@ -49,9 +61,9 @@ class AlbumAdapter( /** * Builds photo */ - override fun onBindViewHolder(holder: ViewHolder, pos: Int) { + override fun onBindViewHolder(holder: ViewHolder, position: Int) { val binding = holder.binding - val image = items[pos] + val image = items[position] // Download image Glide.with(binding.root) .load(image) @@ -59,21 +71,42 @@ class AlbumAdapter( // Show photo dialog. binding.root.setOnClickListener { - // Load image - root!!.x = 0f - root!!.y = 0f - Glide.with(dialog.context) - .load(image) - .into(img!!) - // Resize dialog - val metrics = DisplayMetrics() - @Suppress("DEPRECATION") - activity.windowManager.defaultDisplay.getMetrics(metrics) - dialog.window?.setLayout(metrics.widthPixels, metrics.heightPixels) - dialog.show() + showImage(image, position) } } + private fun changeImage(inc: Int = 1) { + pos += inc + root!!.x = root!!.width.toFloat() * inc + Glide.with(dialog.context) + .load(items[pos]) + .into(img!!) + animator = ObjectAnimator.ofPropertyValuesHolder(root!!, animX) + animator.duration = 600 + animator.start() + } + + private fun showImage(imgUrl: String, position: Int) { + // Load image + root!!.x = 0f + root!!.y = 0f + pos = position + Glide.with(dialog.context) + .load(imgUrl) + .into(img!!) + // Resize dialog + val metrics = DisplayMetrics() + @Suppress("DEPRECATION") + activity.windowManager.defaultDisplay.getMetrics(metrics) + dialog.window!!.attributes.dimAmount = MAX_DIM_AMOUNT + dialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND) + dialog.window?.setLayout(metrics.widthPixels, metrics.heightPixels) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + dialog.window!!.attributes.blurBehindRadius = 5 + } + dialog.show() + } + /** * @return image urls count */ @@ -82,33 +115,53 @@ class AlbumAdapter( /** * Provides dialog behavior */ - @SuppressLint("ClickableViewAccessibility") + @SuppressLint("ClickableViewAccessibility", "ObjectAnimatorBinding") private fun setupDialog() { // set content view dialog.window?.setContentView(R.layout.layout_album_photo) dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) root = dialog.findViewById(R.id.album_photo_root) img = dialog.findViewById(R.id.album_photo) + var touchX = 0f var touchY = 0f var lastMotionEvent = 0 - val hideHeight = 400 // Provides touch root!!.setOnTouchListener { _, motionEvent -> when (motionEvent.action) { MotionEvent.ACTION_DOWN -> { touchY = motionEvent.y + touchX = motionEvent.x } + // Move img on X MotionEvent.ACTION_MOVE -> { - root!!.y += motionEvent.y - touchY + val tmpX = motionEvent.x - touchX + val tmpY = motionEvent.y - touchY + if (lastMotionEvent == MotionEvent.ACTION_DOWN) { + if (abs(tmpX) > abs(tmpY)* Y_PRIORITY) + touchY = 0f + else + touchX = 0f + } + else if(touchY != 0f) { + root!!.y += tmpY + dialog.window!!.attributes.dimAmount -= abs(tmpY * DIM_STEP) + } + else if(touchX != 0f) + root!!.x += tmpX } + // Hide and change image. MotionEvent.ACTION_UP -> { if (lastMotionEvent == MotionEvent.ACTION_DOWN) { dialog.dismiss() - } else if (root!!.y < -hideHeight || root!!.y > root!!.width - hideHeight) { + } else if (root!!.y < -HIDE_OFFSET || root!!.y > root!!.width - HIDE_OFFSET) { dialog.dismiss() + } else if (root!!.x < -CHANGE_IMAGE_OFFSET && pos < itemCount-1) { + changeImage() + } else if (root!!.x > CHANGE_IMAGE_OFFSET && pos > 0) { + changeImage(-1) } else { - animator = ObjectAnimator.ofFloat(root!!, "y", 0f) + animator = ObjectAnimator.ofPropertyValuesHolder(root!!, animX, animY) animator.duration = 600 animator.start() } diff --git a/app/src/main/res/layout/layout_album_photo.xml b/app/src/main/res/layout/layout_album_photo.xml index 34db00d..2d85f5d 100644 --- a/app/src/main/res/layout/layout_album_photo.xml +++ b/app/src/main/res/layout/layout_album_photo.xml @@ -4,8 +4,7 @@ xmlns:tools="http://schemas.android.com/tools" android:id="@+id/album_photo_root" android:layout_width="match_parent" - android:layout_height="match_parent" - android:background="@color/background_photo"> + android:layout_height="match_parent"> #150524 #1A1430 @color/background_secondary - #B51A1430 #D7BBF2 #E6CFFD #2F1D38 diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index bfb6a5b..42530bc 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -3,7 +3,6 @@ #fcfefe #ECF2F8 #1E2832 - #B5101119 #6979B4 #6076AC #8E95AF