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
|
2021-01-31 19:34:33 +01:00
|
|
|
import androidx.fragment.app.Fragment
|
2018-10-15 19:56:11 +02:00
|
|
|
import com.keylesspalace.tusky.ViewMediaActivity
|
|
|
|
import com.keylesspalace.tusky.entity.Attachment
|
|
|
|
|
2021-01-31 19:34:33 +01:00
|
|
|
abstract class ViewMediaFragment : Fragment() {
|
2022-11-09 19:32:39 +01:00
|
|
|
private var toolbarVisibilityDisposable: Function0<Boolean>? = null
|
2018-10-15 19:56:11 +02:00
|
|
|
|
2020-08-01 21:48:51 +02:00
|
|
|
abstract fun setupMediaView(
|
2021-06-28 21:13:24 +02:00
|
|
|
url: String,
|
|
|
|
previewUrl: String?,
|
|
|
|
description: String?,
|
|
|
|
showingDescription: Boolean
|
2020-08-01 21:48:51 +02:00
|
|
|
)
|
|
|
|
|
2018-10-15 19:56:11 +02:00
|
|
|
abstract fun onToolbarVisibilityChange(visible: Boolean)
|
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"
|
2020-08-01 21:48:51 +02:00
|
|
|
|
2019-08-04 20:22:57 +02:00
|
|
|
@JvmStatic
|
|
|
|
protected val ARG_ATTACHMENT = "attach"
|
|
|
|
@JvmStatic
|
2020-10-22 21:15:46 +02:00
|
|
|
protected val ARG_SINGLE_IMAGE_URL = "singleImageUrl"
|
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,
|
2019-09-05 21:07:01 +02:00
|
|
|
Attachment.Type.GIFV,
|
|
|
|
Attachment.Type.AUDIO -> ViewVideoFragment()
|
2021-06-28 21:13:24 +02: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
|
2020-10-22 21:15:46 +02:00
|
|
|
fun newSingleImageInstance(imageUrl: String): ViewMediaFragment {
|
2018-10-15 19:56:11 +02:00
|
|
|
val arguments = Bundle(2)
|
|
|
|
val fragment = ViewImageFragment()
|
2020-10-22 21:15:46 +02:00
|
|
|
arguments.putString(ARG_SINGLE_IMAGE_URL, imageUrl)
|
2018-10-15 19:56:11 +02:00
|
|
|
arguments.putBoolean(ARG_START_POSTPONED_TRANSITION, true)
|
|
|
|
|
|
|
|
fragment.arguments = arguments
|
|
|
|
return fragment
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 20:17:53 +01:00
|
|
|
abstract fun onTransitionEnd()
|
|
|
|
|
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
|
|
|
|
|
|
|
|
showingDescription = !TextUtils.isEmpty(description)
|
|
|
|
isDescriptionVisible = showingDescription
|
2020-08-01 21:48:51 +02:00
|
|
|
setupMediaView(url, previewUrl, description, showingDescription && mediaActivity.isToolbarVisible)
|
2018-11-01 14:52:22 +01:00
|
|
|
|
2022-11-09 19:32:39 +01:00
|
|
|
toolbarVisibilityDisposable = (activity as ViewMediaActivity)
|
2021-06-28 21:13:24 +02:00
|
|
|
.addToolbarVisibilityListener { isVisible ->
|
|
|
|
onToolbarVisibilityChange(isVisible)
|
|
|
|
}
|
2018-10-15 19:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroyView() {
|
2022-11-09 19:32:39 +01:00
|
|
|
toolbarVisibilityDisposable?.invoke()
|
2018-10-15 19:56:11 +02:00
|
|
|
super.onDestroyView()
|
|
|
|
}
|
|
|
|
}
|