From 5ec00376fa6be5530cdd16cd5228f868f5680798 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Mon, 1 Jul 2019 23:32:45 -0700 Subject: Add swipe to dismiss --- .../kotlin/com/pitchedapps/frost/StartActivity.kt | 16 +++-- .../pitchedapps/frost/activities/ImageActivity.kt | 84 +++++++++++++++++++--- .../kotlin/com/pitchedapps/frost/utils/Prefs.kt | 1 - .../kotlin/com/pitchedapps/frost/utils/Showcase.kt | 2 - .../com/pitchedapps/frost/views/DragFrame.kt | 40 +++++++++++ 5 files changed, 126 insertions(+), 17 deletions(-) create mode 100644 app/src/main/kotlin/com/pitchedapps/frost/views/DragFrame.kt (limited to 'app/src/main/kotlin/com') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt index 8aebb575..8d2649ff 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt @@ -16,6 +16,7 @@ */ package com.pitchedapps.frost +import android.content.Intent import android.graphics.Color import android.os.Build import android.os.Bundle @@ -25,9 +26,11 @@ import android.widget.TextView import ca.allanwang.kau.internal.KauBaseActivity import ca.allanwang.kau.utils.buildIsLollipopAndUp import ca.allanwang.kau.utils.setIcon +import ca.allanwang.kau.utils.startActivity import ca.allanwang.kau.utils.string import com.mikepenz.google_material_typeface_library.GoogleMaterial import com.pitchedapps.frost.activities.LoginActivity +import com.pitchedapps.frost.activities.MainActivity import com.pitchedapps.frost.activities.SelectorActivity import com.pitchedapps.frost.db.CookieDao import com.pitchedapps.frost.db.CookieEntity @@ -40,6 +43,7 @@ import com.pitchedapps.frost.db.saveTabs import com.pitchedapps.frost.db.selectAll import com.pitchedapps.frost.facebook.FbCookie import com.pitchedapps.frost.utils.BiometricUtils +import com.pitchedapps.frost.utils.EXTRA_COOKIES import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.Prefs import com.pitchedapps.frost.utils.launchImageActivity @@ -95,12 +99,12 @@ class StartActivity : KauBaseActivity() { cookies.isEmpty() -> launchNewTask() // Has cookies but no selected account Prefs.userId == -1L -> launchNewTask(cookies) - else -> launchImageActivity("https://images.pexels.com/photos/374870/pexels-photo-374870.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500") -// else -> startActivity(intentBuilder = { -// putParcelableArrayListExtra(EXTRA_COOKIES, cookies) -// flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP or -// Intent.FLAG_ACTIVITY_SINGLE_TOP -// }) +// else -> launchImageActivity("https://images.pexels.com/photos/374870/pexels-photo-374870.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500") + else -> startActivity(intentBuilder = { + putParcelableArrayListExtra(EXTRA_COOKIES, cookies) + flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP or + Intent.FLAG_ACTIVITY_SINGLE_TOP + }) } } catch (e: Exception) { L._e(e) { "Load start failed" } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt index 661b4526..c7d31032 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt @@ -23,11 +23,13 @@ import android.graphics.Color import android.os.Bundle import android.os.Environment import android.view.View +import androidx.customview.widget.ViewDragHelper import ca.allanwang.kau.internal.KauBaseActivity import ca.allanwang.kau.logging.KauLoggerExtension import ca.allanwang.kau.mediapicker.scanMedia import ca.allanwang.kau.permissions.PERMISSION_WRITE_EXTERNAL_STORAGE import ca.allanwang.kau.permissions.kauRequestPermissions +import ca.allanwang.kau.utils.adjustAlpha import ca.allanwang.kau.utils.colorToForeground import ca.allanwang.kau.utils.copyFromInputStream import ca.allanwang.kau.utils.fadeOut @@ -75,6 +77,8 @@ import java.io.IOException import java.text.SimpleDateFormat import java.util.Date import java.util.Locale +import kotlin.math.abs +import kotlin.math.max /** * Created by Allan Wang on 2017-07-15. @@ -103,6 +107,8 @@ class ImageActivity : KauBaseActivity() { value.update(image_fab) } + private lateinit var dragHelper: ViewDragHelper + companion object { /** * Cache folder to store images @@ -135,6 +141,9 @@ class ImageActivity : KauBaseActivity() { )}_${Math.abs(imageUrl.hashCode())}" } + private val baseBackgroundColor = if (Prefs.blackMediaBg) Color.BLACK + else Prefs.bgColor.withMinAlpha(235) + private fun loadError(e: Throwable) { errorRef = e e.logFrostEvent("Image load error") @@ -158,12 +167,10 @@ class ImageActivity : KauBaseActivity() { result } - val layout = if (!imageText.isNullOrBlank()) R.layout.activity_image else R.layout.activity_image_textless + val layout = + if (!imageText.isNullOrBlank()) R.layout.activity_image else R.layout.activity_image_textless setContentView(layout) - image_container.setBackgroundColor( - if (Prefs.blackMediaBg) Color.BLACK - else Prefs.bgColor.withMinAlpha(235) - ) + image_container.setBackgroundColor(baseBackgroundColor) image_text?.setTextColor(if (Prefs.blackMediaBg) Color.WHITE else Prefs.textColor) image_text?.setBackgroundColor( (if (Prefs.blackMediaBg) Color.BLACK else Prefs.bgColor) @@ -171,7 +178,8 @@ class ImageActivity : KauBaseActivity() { ) image_text?.text = imageText image_progress.tint(if (Prefs.blackMediaBg) Color.WHITE else Prefs.accentColor) - image_panel?.addPanelSlideListener(object : SlidingUpPanelLayout.SimplePanelSlideListener() { + image_panel?.addPanelSlideListener(object : + SlidingUpPanelLayout.SimplePanelSlideListener() { override fun onPanelSlide(panel: View, slideOffset: Float) { if (slideOffset == 0f && !image_fab.isShown) image_fab.show() else if (slideOffset != 0f && image_fab.isShown) image_fab.hide() @@ -179,7 +187,8 @@ class ImageActivity : KauBaseActivity() { } }) image_fab.setOnClickListener { fabAction.onClick(this) } - image_photo.setOnImageEventListener(object : SubsamplingScaleImageView.DefaultOnImageEventListener() { + image_photo.setOnImageEventListener(object : + SubsamplingScaleImageView.DefaultOnImageEventListener() { override fun onImageLoadError(e: Exception) { loadError(e) } @@ -194,14 +203,73 @@ class ImageActivity : KauBaseActivity() { image_photo.setImage(ImageSource.uri(frostUriFromFile(tempFile))) fabAction = FabStates.DOWNLOAD image_photo.animate().alpha(1f).scaleXY(1f).start() + dragHelper = ViewDragHelper.create(image_drag, ViewDragCallback()).apply { + setEdgeTrackingEnabled(ViewDragHelper.EDGE_TOP or ViewDragHelper.EDGE_BOTTOM) + } + image_drag.dragHelper = dragHelper + } + } + + private inner class ViewDragCallback : ViewDragHelper.Callback() { + private var scrollPercent: Float = 0f + private var scrollThreshold = 0.5f + private var scrollToTop = false + + override fun tryCaptureView(view: View, i: Int): Boolean { + return true + } + + override fun getViewHorizontalDragRange(child: View): Int = 0 + + override fun getViewVerticalDragRange(child: View): Int = child.height + + override fun onViewPositionChanged( + changedView: View, + left: Int, + top: Int, + dx: Int, + dy: Int + ) { + super.onViewPositionChanged(changedView, left, top, dx, dy) + //make sure that we are using the proper axis + scrollPercent = abs(top.toFloat() / image_container.height) + scrollToTop = top < 0 + val multiplier = max(1f - scrollPercent, 0f) + image_fab.alpha = multiplier + image_panel?.alpha = multiplier + image_container.setBackgroundColor(baseBackgroundColor.adjustAlpha(multiplier)) + + if (scrollPercent >= 1) { + if (!isFinishing) { + finish() + overridePendingTransition(0, 0) + } + } } + + override fun onViewReleased(releasedChild: View, xvel: Float, yvel: Float) { + val overScrolled = scrollPercent > scrollThreshold + val maxOffset = releasedChild.height + 10 + val finalTop = when { + scrollToTop && (overScrolled || yvel < -dragHelper.minVelocity) -> -maxOffset + !scrollToTop && (overScrolled || yvel > dragHelper.minVelocity) -> maxOffset + else -> 0 + } + dragHelper.settleCapturedViewAt(0, finalTop) + image_drag.invalidate() + } + + override fun clampViewPositionHorizontal(child: View, left: Int, dx: Int): Int = 0 + + override fun clampViewPositionVertical(child: View, top: Int, dy: Int): Int = top } @Throws(IOException::class) private fun createPublicMediaFile(): File { val timeStamp = SimpleDateFormat(TIME_FORMAT, Locale.getDefault()).format(Date()) val imageFileName = "${IMG_TAG}_${timeStamp}_" - val storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + val storageDir = + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) val frostDir = File(storageDir, IMG_TAG) if (!frostDir.exists()) frostDir.mkdirs() return File.createTempFile(imageFileName, IMG_EXTENSION, frostDir) diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt index 7656a081..51eb856b 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt @@ -19,7 +19,6 @@ package com.pitchedapps.frost.utils import android.graphics.Color import ca.allanwang.kau.kotlin.lazyResettable import ca.allanwang.kau.kpref.KPref -import ca.allanwang.kau.kpref.kpref import ca.allanwang.kau.utils.colorToForeground import ca.allanwang.kau.utils.isColorVisibleOn import ca.allanwang.kau.utils.withAlpha diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/Showcase.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/Showcase.kt index 27016018..df48bfbc 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Showcase.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Showcase.kt @@ -17,8 +17,6 @@ package com.pitchedapps.frost.utils import ca.allanwang.kau.kpref.KPref -import ca.allanwang.kau.kpref.kpref -import ca.allanwang.kau.kpref.kprefSingle /** * Created by Allan Wang on 2017-07-03. diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/DragFrame.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/DragFrame.kt new file mode 100644 index 00000000..db9b901b --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/DragFrame.kt @@ -0,0 +1,40 @@ +package com.pitchedapps.frost.views + +import android.annotation.SuppressLint +import android.content.Context +import android.util.AttributeSet +import android.view.MotionEvent +import android.widget.FrameLayout +import androidx.core.view.ViewCompat +import androidx.customview.widget.ViewDragHelper + +class DragFrame @JvmOverloads constructor( + context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 +) : FrameLayout(context, attrs, defStyleAttr) { + var dragHelper: ViewDragHelper? = null + + override fun onInterceptTouchEvent(event: MotionEvent): Boolean { + return try { + dragHelper?.shouldInterceptTouchEvent(event) ?: false + } catch (e: Exception) { + false + } + } + + @SuppressLint("ClickableViewAccessibility") + override fun onTouchEvent(event: MotionEvent): Boolean { + try { + dragHelper?.processTouchEvent(event) ?: return false + } catch (e: Exception) { + return false + } + return true + } + + override fun computeScroll() { + super.computeScroll() + if (dragHelper?.continueSettling(true) == true) { + ViewCompat.postInvalidateOnAnimation(this) + } + } +} \ No newline at end of file -- cgit v1.2.3