BayernMessenger/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/factory/EncryptedItemFactory.kt

89 lines
4.3 KiB
Kotlin
Raw Normal View History

2019-05-16 08:23:57 +00:00
/*
* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.riotx.features.home.room.detail.timeline.factory
2019-05-16 08:23:57 +00:00
2019-06-18 10:45:24 +00:00
import android.view.View
2019-05-16 08:23:57 +00:00
import im.vector.matrix.android.api.session.crypto.MXCryptoError
import im.vector.matrix.android.api.session.events.model.EventType
import im.vector.matrix.android.api.session.room.timeline.TimelineEvent
import im.vector.riotx.R
import im.vector.riotx.core.epoxy.VectorEpoxyModel
import im.vector.riotx.core.resources.ColorProvider
import im.vector.riotx.core.resources.StringProvider
import im.vector.riotx.core.utils.DebouncedClickListener
import im.vector.riotx.features.home.AvatarRenderer
import im.vector.riotx.features.home.room.detail.timeline.TimelineEventController
import im.vector.riotx.features.home.room.detail.timeline.item.MessageTextItem_
import im.vector.riotx.features.home.room.detail.timeline.util.MessageInformationDataFactory
import me.gujun.android.span.span
2019-06-27 13:25:01 +00:00
import javax.inject.Inject
2019-05-16 08:23:57 +00:00
// This class handles timeline events who haven't been successfully decrypted
2019-06-27 13:25:01 +00:00
class EncryptedItemFactory @Inject constructor(private val messageInformationDataFactory: MessageInformationDataFactory,
private val colorProvider: ColorProvider,
private val stringProvider: StringProvider,
private val avatarRenderer: AvatarRenderer) {
2019-06-18 10:45:24 +00:00
fun create(event: TimelineEvent,
nextEvent: TimelineEvent?,
2019-06-20 14:27:43 +00:00
highlight: Boolean,
2019-06-18 10:45:24 +00:00
callback: TimelineEventController.Callback?): VectorEpoxyModel<*>? {
event.root.eventId ?: return null
2019-05-16 08:23:57 +00:00
return when {
EventType.ENCRYPTED == event.root.getClearType() -> {
val cryptoError = event.root.mCryptoError
val errorDescription =
2019-07-08 09:18:27 +00:00
if (cryptoError == MXCryptoError.ErrorType.UNKNOWN_INBOUND_SESSION_ID) {
stringProvider.getString(R.string.notice_crypto_error_unkwown_inbound_session_id)
} else {
2019-07-08 09:18:27 +00:00
// TODO i18n
cryptoError?.name
}
2019-05-16 08:23:57 +00:00
val message = stringProvider.getString(R.string.encrypted_message).takeIf { cryptoError == null }
?: stringProvider.getString(R.string.notice_crypto_unable_to_decrypt, errorDescription)
val spannableStr = span(message) {
textStyle = "italic"
textColor = colorProvider.getColorFromAttribute(R.attr.riotx_text_secondary)
}
// TODO This is not correct format for error, change it
val informationData = messageInformationDataFactory.create(event, nextEvent)
return MessageTextItem_()
.message(spannableStr)
.avatarRenderer(avatarRenderer)
2019-07-12 08:16:43 +00:00
.colorProvider(colorProvider)
.informationData(informationData)
2019-06-20 14:27:43 +00:00
.highlighted(highlight)
2019-06-18 10:45:24 +00:00
.avatarCallback(callback)
2019-06-20 14:27:43 +00:00
.urlClickCallback(callback)
2019-06-18 10:45:24 +00:00
.cellClickListener(
DebouncedClickListener(View.OnClickListener { view ->
callback?.onEncryptedMessageClicked(informationData, view)
}))
.longClickListener { view ->
return@longClickListener callback?.onEventLongClicked(informationData, null, view)
2019-07-05 12:28:28 +00:00
?: false
2019-06-18 10:45:24 +00:00
}
2019-05-16 08:23:57 +00:00
}
else -> null
2019-05-16 08:23:57 +00:00
}
}
}