diff options
author | Allan Wang <me@allanwang.ca> | 2019-07-01 23:55:47 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-01 23:55:47 -0700 |
commit | 312f956b8a50f48075ebfee580dd4545e1c630f3 (patch) | |
tree | 137ba0b497fe61b89b49030cef2743b92c300676 /app/src/main/kotlin/com/pitchedapps | |
parent | 39babe71b65cdad17b3d055f147d95e52a811e4a (diff) | |
parent | 03e16c98c7b7bf69a57d0cb3e19df807f3b355fa (diff) | |
download | frost-312f956b8a50f48075ebfee580dd4545e1c630f3.tar.gz frost-312f956b8a50f48075ebfee580dd4545e1c630f3.tar.bz2 frost-312f956b8a50f48075ebfee580dd4545e1c630f3.zip |
Merge pull request #1459 from AllanWang/image-swipe
Image swipe
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps')
6 files changed, 159 insertions, 14 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt index cf8acdd3..61b4a194 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt @@ -86,7 +86,12 @@ class StartActivity : KauBaseActivity() { FbCookie.switchBackUser() val cookies = ArrayList(cookieDao.selectAll()) L.i { "Cookies loaded at time ${System.currentTimeMillis()}" } - L._d { "Cookies: ${cookies.joinToString("\t", transform = CookieEntity::toSensitiveString)}" } + L._d { + "Cookies: ${cookies.joinToString( + "\t", + transform = CookieEntity::toSensitiveString + )}" + } loadAssets() authDefer.await() when { @@ -112,7 +117,8 @@ class StartActivity : KauBaseActivity() { */ private suspend fun migrate() = withContext(Dispatchers.IO) { if (cookieDao.selectAll().isNotEmpty()) return@withContext - val cookies = (select from CookieModel::class).queryList().map { CookieEntity(it.id, it.name, it.cookie) } + val cookies = (select from CookieModel::class).queryList() + .map { CookieEntity(it.id, it.name, it.cookie) } if (cookies.isNotEmpty()) { cookieDao.save(cookies) L._d { "Migrated cookies ${cookieDao.selectAll()}" } 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 814ce778..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(222) - ) + 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..c2f36536 --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/DragFrame.kt @@ -0,0 +1,58 @@ +/* + * Copyright 2019 Allan Wang + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +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) + } + } +} diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/SwipeRefreshLayout.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/SwipeRefreshLayout.kt index 2154ce2d..5c7c58f1 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/SwipeRefreshLayout.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/SwipeRefreshLayout.kt @@ -1,3 +1,19 @@ +/* + * Copyright 2019 Allan Wang + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ package com.pitchedapps.frost.views import android.content.Context @@ -84,4 +100,4 @@ class SwipeRefreshLayout @JvmOverloads constructor(context: Context, attrs: Attr * Alias for adding on refresh listener */ interface OnRefreshListener : SwipeRefreshLayout.OnRefreshListener -}
\ No newline at end of file +} |