chats: implement attachment onClick

This commit is contained in:
Alibek Omarov 2020-10-05 17:25:49 +03:00
parent db18d55619
commit daabaf2bdb
3 changed files with 47 additions and 11 deletions

View File

@ -79,7 +79,16 @@ class ViewMediaActivity : BaseActivity(), ViewImageFragment.PhotoActionsListener
return intent
}
fun newAvatarIntent(context: Context, url: String): Intent {
@JvmStatic
fun newIntent(context: Context?, attachment: Attachment): Intent {
val intent = Intent(context, ViewMediaActivity::class.java)
intent.putParcelableArrayListExtra(EXTRA_ATTACHMENTS,
arrayListOf(AttachmentViewData(attachment, null, null)))
intent.putExtra(EXTRA_ATTACHMENT_INDEX, 0)
return intent
}
fun newAvatarIntent(context: Context?, url: String): Intent {
val intent = Intent(context, ViewMediaActivity::class.java)
intent.putExtra(EXTRA_AVATAR_URL, url)
return intent
@ -171,6 +180,11 @@ class ViewMediaActivity : BaseActivity(), ViewImageFragment.PhotoActionsListener
override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
menu?.findItem(R.id.action_share_media)?.isEnabled = !isCreating
if(attachments != null) {
val isStatus = attachments!!.any { it.statusId != null && it.statusUrl != null }
menu?.findItem(R.id.action_open_status)?.isVisible = isStatus
}
return true
}

View File

@ -20,10 +20,6 @@ import android.widget.Toast
import androidx.activity.viewModels
import androidx.annotation.StringRes
import androidx.annotation.VisibleForTesting
import com.keylesspalace.tusky.BottomSheetActivity
import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.BuildConfig
import com.keylesspalace.tusky.ViewTagActivity
import com.keylesspalace.tusky.di.Injectable
import com.keylesspalace.tusky.di.ViewModelFactory
import com.keylesspalace.tusky.entity.Chat
@ -35,9 +31,11 @@ import com.keylesspalace.tusky.repository.ChatRepository
import com.keylesspalace.tusky.viewdata.ChatMessageViewData
import androidx.arch.core.util.Function
import androidx.core.app.ActivityCompat
import androidx.core.app.ActivityOptionsCompat
import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider
import androidx.core.net.toUri
import androidx.core.view.ViewCompat
import androidx.core.view.inputmethod.InputConnectionCompat
import androidx.core.view.inputmethod.InputContentInfoCompat
import androidx.lifecycle.Lifecycle
@ -49,11 +47,13 @@ import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.snackbar.Snackbar
import com.keylesspalace.tusky.*
import com.keylesspalace.tusky.adapter.*
import com.keylesspalace.tusky.appstore.*
import com.keylesspalace.tusky.components.common.*
import com.keylesspalace.tusky.components.compose.ComposeActivity
import com.keylesspalace.tusky.components.compose.dialog.makeCaptionDialog
import com.keylesspalace.tusky.entity.Attachment
import com.keylesspalace.tusky.repository.Placeholder
import com.keylesspalace.tusky.repository.TimelineRequestMode
import com.keylesspalace.tusky.service.MessageToSend
@ -862,13 +862,13 @@ class ChatActivity: BottomSheetActivity(),
FetchEnd.TOP -> {
updateMessages(msgs, fullFetch)
val pos = msgs.indexOfFirst { it.isRight() }
val last = msgs.indexOfFirst { it.isRight() }
mastodonApi.markChatAsRead(chatId, msgs[pos].asRight().id)
mastodonApi.markChatAsRead(chatId, msgs[last].asRight().id)
.observeOn(AndroidSchedulers.mainThread())
.autoDispose(this, Lifecycle.Event.ON_DESTROY)
.subscribe({
Log.d(TAG, "Marked new messages as read up to ${msgs[pos].asRight().id}")
Log.d(TAG, "Marked new messages as read up to ${msgs[last].asRight().id}")
}, {
Log.d(TAG, "Failed to mark messages as read", it)
})
@ -1039,6 +1039,28 @@ class ChatActivity: BottomSheetActivity(),
startActivity(intent)
}
override fun onViewMedia(position: Int, view: View?) {
val attachment = msgs[position].asRight().attachment!!
when(attachment.type) {
Attachment.Type.GIFV, Attachment.Type.VIDEO, Attachment.Type.AUDIO, Attachment.Type.IMAGE -> {
val intent = ViewMediaActivity.newIntent(this, attachment)
if(view != null) {
val url = attachment.url
ViewCompat.setTransitionName(view, url)
val options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, view, url)
startActivity(intent, options.toBundle())
} else {
startActivity(intent)
}
}
Attachment.Type.UNKNOWN -> {
viewUrl(attachment.url)
}
}
}
companion object {
private const val MEDIA_PICK_RESULT = 1
private const val MEDIA_TAKE_PHOTO_RESULT = 2

View File

@ -8,8 +8,8 @@ import kotlinx.android.parcel.Parcelize
@Parcelize
data class AttachmentViewData(
val attachment: Attachment,
val statusId: String,
val statusUrl: String
val statusId: String?,
val statusUrl: String?
) : Parcelable {
companion object {
@JvmStatic
@ -27,4 +27,4 @@ data class AttachmentViewData(
}
}
}
}