2018-10-15 19:56:11 +02:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Tusky 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 Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky.fragment
|
|
|
|
|
|
|
|
import android.animation.Animator
|
|
|
|
import android.animation.AnimatorListenerAdapter
|
|
|
|
import android.content.Context
|
2019-04-16 21:39:12 +02:00
|
|
|
import android.graphics.drawable.Drawable
|
2018-10-15 19:56:11 +02:00
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import android.widget.ImageView
|
2018-11-01 14:52:22 +01:00
|
|
|
import android.widget.TextView
|
2019-04-16 21:39:12 +02:00
|
|
|
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
|
2018-10-15 19:56:11 +02:00
|
|
|
|
|
|
|
import com.github.chrisbanes.photoview.PhotoViewAttacher
|
|
|
|
import com.keylesspalace.tusky.R
|
|
|
|
import com.keylesspalace.tusky.entity.Attachment
|
|
|
|
import com.keylesspalace.tusky.util.hide
|
2018-11-01 14:52:22 +01:00
|
|
|
import com.keylesspalace.tusky.util.visible
|
2018-10-15 19:56:11 +02:00
|
|
|
import kotlinx.android.synthetic.main.activity_view_media.*
|
|
|
|
import kotlinx.android.synthetic.main.fragment_view_image.*
|
|
|
|
|
|
|
|
class ViewImageFragment : ViewMediaFragment() {
|
|
|
|
interface PhotoActionsListener {
|
|
|
|
fun onBringUp()
|
|
|
|
fun onDismiss()
|
|
|
|
fun onPhotoTap()
|
|
|
|
}
|
|
|
|
|
|
|
|
private lateinit var attacher: PhotoViewAttacher
|
|
|
|
private lateinit var photoActionsListener: PhotoActionsListener
|
|
|
|
private lateinit var toolbar: View
|
2019-04-16 21:39:12 +02:00
|
|
|
override lateinit var descriptionView: TextView
|
2018-10-15 19:56:11 +02:00
|
|
|
|
|
|
|
override fun onAttach(context: Context) {
|
|
|
|
super.onAttach(context)
|
|
|
|
photoActionsListener = context as PhotoActionsListener
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun setupMediaView(url: String) {
|
2018-11-01 14:52:22 +01:00
|
|
|
descriptionView = mediaDescription
|
2018-12-17 15:25:35 +01:00
|
|
|
photoView.transitionName = url
|
2018-10-15 19:56:11 +02:00
|
|
|
attacher = PhotoViewAttacher(photoView)
|
|
|
|
|
|
|
|
// Clicking outside the photo closes the viewer.
|
2018-11-16 13:31:03 +01:00
|
|
|
attacher.setOnOutsidePhotoTapListener { photoActionsListener.onDismiss() }
|
2018-10-15 19:56:11 +02:00
|
|
|
|
2018-11-16 13:31:03 +01:00
|
|
|
attacher.setOnClickListener { onMediaTap() }
|
2018-10-15 19:56:11 +02:00
|
|
|
|
|
|
|
/* A vertical swipe motion also closes the viewer. This is especially useful when the photo
|
|
|
|
* mostly fills the screen so clicking outside is difficult. */
|
|
|
|
attacher.setOnSingleFlingListener { _, _, velocityX, velocityY ->
|
|
|
|
var result = false
|
|
|
|
if (Math.abs(velocityY) > Math.abs(velocityX)) {
|
|
|
|
photoActionsListener.onDismiss()
|
|
|
|
result = true
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2019-04-16 21:39:12 +02:00
|
|
|
loadImageFromNetwork(url, photoView)
|
2018-10-15 19:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
|
|
|
toolbar = activity!!.toolbar
|
|
|
|
return inflater.inflate(R.layout.fragment_view_image, container, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
|
|
|
val arguments = this.arguments!!
|
|
|
|
val attachment = arguments.getParcelable<Attachment>(ARG_ATTACHMENT)
|
|
|
|
val url: String?
|
2019-04-16 21:39:12 +02:00
|
|
|
var description: String? = null
|
2018-10-15 19:56:11 +02:00
|
|
|
|
|
|
|
if (attachment != null) {
|
|
|
|
url = attachment.url
|
2018-11-01 14:52:22 +01:00
|
|
|
description = attachment.description
|
2018-10-15 19:56:11 +02:00
|
|
|
} else {
|
|
|
|
url = arguments.getString(ARG_AVATAR_URL)
|
|
|
|
if (url == null) {
|
|
|
|
throw IllegalArgumentException("attachment or avatar url has to be set")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 14:52:22 +01:00
|
|
|
finalizeViewSetup(url, description)
|
2018-10-15 19:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun onMediaTap() {
|
|
|
|
photoActionsListener.onPhotoTap()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onToolbarVisibilityChange(visible: Boolean) {
|
|
|
|
if (photoView == null || !userVisibleHint) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
isDescriptionVisible = showingDescription && visible
|
|
|
|
val alpha = if (isDescriptionVisible) 1.0f else 0.0f
|
|
|
|
descriptionView.animate().alpha(alpha)
|
|
|
|
.setListener(object : AnimatorListenerAdapter() {
|
|
|
|
override fun onAnimationEnd(animation: Animator) {
|
2018-11-01 14:52:22 +01:00
|
|
|
descriptionView.visible(isDescriptionVisible)
|
2018-10-15 19:56:11 +02:00
|
|
|
animation.removeListener(this)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.start()
|
|
|
|
}
|
|
|
|
|
2019-04-16 21:39:12 +02:00
|
|
|
override fun onDestroyView() {
|
|
|
|
Glide.with(this).clear(photoView)
|
|
|
|
super.onDestroyView()
|
2018-10-15 19:56:11 +02:00
|
|
|
}
|
|
|
|
|
2019-04-16 21:39:12 +02:00
|
|
|
private fun loadImageFromNetwork(url: String, photoView: ImageView) =
|
|
|
|
//Request image from the any cache
|
|
|
|
Glide.with(this)
|
|
|
|
.load(url)
|
|
|
|
.dontAnimate()
|
|
|
|
.onlyRetrieveFromCache(true)
|
|
|
|
.error(
|
|
|
|
//Request image from the network on fail load image from cache
|
|
|
|
Glide.with(this)
|
|
|
|
.load(url)
|
|
|
|
.centerInside()
|
|
|
|
.addListener(ImageRequestListener(false))
|
|
|
|
)
|
|
|
|
.centerInside()
|
|
|
|
.addListener(ImageRequestListener(true))
|
|
|
|
.into(photoView)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param isCacheRequest - is this listener for request image from cache or from the network
|
|
|
|
*/
|
|
|
|
private inner class ImageRequestListener(private val isCacheRequest: Boolean) : RequestListener<Drawable> {
|
|
|
|
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
|
|
|
|
if (isCacheRequest) //Complete the transition on failed image from cache
|
|
|
|
completeTransition()
|
|
|
|
else
|
|
|
|
progressBar?.hide() //Hide progress bar only on fail request from internet
|
|
|
|
return false
|
|
|
|
}
|
2018-10-15 19:56:11 +02:00
|
|
|
|
2019-04-16 21:39:12 +02:00
|
|
|
override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
|
|
|
|
progressBar?.hide() //Always hide the progress bar on success
|
|
|
|
resource?.let {
|
|
|
|
target?.onResourceReady(resource, null)
|
|
|
|
if (isCacheRequest) completeTransition() //Complete transition on cache request only, because transition already completed on Network request
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2018-10-15 19:56:11 +02:00
|
|
|
}
|
|
|
|
|
2019-04-16 21:39:12 +02:00
|
|
|
private fun completeTransition() {
|
2018-10-15 19:56:11 +02:00
|
|
|
attacher.update()
|
|
|
|
photoActionsListener.onBringUp()
|
|
|
|
}
|
|
|
|
}
|