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.os.Bundle
|
2018-11-01 14:52:22 +01:00
|
|
|
import android.text.TextUtils
|
|
|
|
import android.widget.TextView
|
2019-08-04 20:22:57 +02:00
|
|
|
import com.keylesspalace.tusky.SharedElementTransitionListener
|
2018-10-15 19:56:11 +02:00
|
|
|
|
|
|
|
import com.keylesspalace.tusky.ViewMediaActivity
|
|
|
|
import com.keylesspalace.tusky.entity.Attachment
|
2018-11-01 14:52:22 +01:00
|
|
|
import com.keylesspalace.tusky.util.visible
|
2018-10-15 19:56:11 +02:00
|
|
|
|
2019-08-04 20:22:57 +02:00
|
|
|
abstract class ViewMediaFragment : BaseFragment(), SharedElementTransitionListener {
|
2018-10-15 19:56:11 +02:00
|
|
|
private var toolbarVisibiltyDisposable: Function0<Boolean>? = null
|
|
|
|
|
2019-08-04 20:22:57 +02:00
|
|
|
abstract fun setupMediaView(url: String, previewUrl: String?)
|
2018-10-15 19:56:11 +02:00
|
|
|
abstract fun onToolbarVisibilityChange(visible: Boolean)
|
2019-08-04 20:22:57 +02:00
|
|
|
abstract val descriptionView: TextView
|
2018-11-01 14:52:22 +01:00
|
|
|
|
|
|
|
protected var showingDescription = false
|
|
|
|
protected var isDescriptionVisible = false
|
2018-10-15 19:56:11 +02:00
|
|
|
|
|
|
|
companion object {
|
2019-08-04 20:22:57 +02:00
|
|
|
@JvmStatic
|
|
|
|
protected val ARG_START_POSTPONED_TRANSITION = "startPostponedTransition"
|
|
|
|
@JvmStatic
|
|
|
|
protected val ARG_ATTACHMENT = "attach"
|
|
|
|
@JvmStatic
|
|
|
|
protected val ARG_AVATAR_URL = "avatarUrl"
|
2018-10-15 19:56:11 +02:00
|
|
|
|
|
|
|
@JvmStatic
|
|
|
|
fun newInstance(attachment: Attachment, shouldStartPostponedTransition: Boolean): ViewMediaFragment {
|
|
|
|
val arguments = Bundle(2)
|
|
|
|
arguments.putParcelable(ARG_ATTACHMENT, attachment)
|
|
|
|
arguments.putBoolean(ARG_START_POSTPONED_TRANSITION, shouldStartPostponedTransition)
|
|
|
|
|
|
|
|
val fragment = when (attachment.type) {
|
|
|
|
Attachment.Type.IMAGE -> ViewImageFragment()
|
|
|
|
Attachment.Type.VIDEO,
|
|
|
|
Attachment.Type.GIFV -> ViewVideoFragment()
|
2018-11-17 16:17:24 +01:00
|
|
|
else -> ViewImageFragment() // it probably won't show anything, but its better than crashing
|
2018-10-15 19:56:11 +02:00
|
|
|
}
|
|
|
|
fragment.arguments = arguments
|
|
|
|
return fragment
|
|
|
|
}
|
|
|
|
|
|
|
|
@JvmStatic
|
|
|
|
fun newAvatarInstance(avatarUrl: String): ViewMediaFragment {
|
|
|
|
val arguments = Bundle(2)
|
|
|
|
val fragment = ViewImageFragment()
|
|
|
|
arguments.putString(ARG_AVATAR_URL, avatarUrl)
|
|
|
|
arguments.putBoolean(ARG_START_POSTPONED_TRANSITION, true)
|
|
|
|
|
|
|
|
fragment.arguments = arguments
|
|
|
|
return fragment
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-04 20:22:57 +02:00
|
|
|
protected fun finalizeViewSetup(url: String, previewUrl: String?, description: String?) {
|
2018-11-01 14:52:22 +01:00
|
|
|
val mediaActivity = activity as ViewMediaActivity
|
2019-08-04 20:22:57 +02:00
|
|
|
setupMediaView(url, previewUrl)
|
2018-11-01 14:52:22 +01:00
|
|
|
|
|
|
|
descriptionView.text = description ?: ""
|
|
|
|
showingDescription = !TextUtils.isEmpty(description)
|
|
|
|
isDescriptionVisible = showingDescription
|
|
|
|
|
2019-08-04 20:22:57 +02:00
|
|
|
descriptionView.visible(showingDescription && mediaActivity.isToolbarVisible)
|
2018-11-01 14:52:22 +01:00
|
|
|
|
2019-08-04 20:22:57 +02:00
|
|
|
toolbarVisibiltyDisposable = (activity as ViewMediaActivity)
|
|
|
|
.addToolbarVisibilityListener { isVisible ->
|
|
|
|
onToolbarVisibilityChange(isVisible)
|
|
|
|
}
|
2018-10-15 19:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroyView() {
|
|
|
|
toolbarVisibiltyDisposable?.invoke()
|
|
|
|
super.onDestroyView()
|
|
|
|
}
|
|
|
|
}
|