Support AddedByMe in reaction + context menu reflect agree/like state

This commit is contained in:
Valere 2019-05-16 10:48:33 +02:00
parent a9a1fe2aa0
commit 6fe455dac0
6 changed files with 48 additions and 15 deletions

View File

@ -45,6 +45,7 @@ internal object EventMapper {
}
fun map(eventEntity: EventEntity): Event {
//TODO proxy the event to only parse unsigned data when accessed?
var ud = if (eventEntity.unsignedData.isNullOrBlank()) null
else MoshiProvider.providesMoshi().adapter(UnsignedData::class.java).fromJson(eventEntity.unsignedData)
return Event(

View File

@ -104,7 +104,7 @@ internal class SessionModule(private val sessionParams: SessionParams) {
}
scope(DefaultSession.SCOPE) {
EventRelationsAggregationUpdater()
EventRelationsAggregationUpdater(get())
}
scope(DefaultSession.SCOPE) {

View File

@ -1,5 +1,6 @@
package im.vector.matrix.android.internal.session.room
import im.vector.matrix.android.api.auth.data.Credentials
import im.vector.matrix.android.api.session.events.model.*
import im.vector.matrix.android.api.session.room.model.annotation.ReactionContent
import im.vector.matrix.android.internal.database.model.EventAnnotationsSummaryEntity
@ -10,7 +11,7 @@ import io.realm.Realm
import timber.log.Timber
internal class EventRelationsAggregationUpdater {
internal class EventRelationsAggregationUpdater(private val credentials: Credentials) {
fun update(realm: Realm, roomId: String, events: List<Event>?) {
events?.forEach { event ->
@ -66,12 +67,14 @@ internal class EventRelationsAggregationUpdater {
sum.key = reaction
sum.firstTimestamp = event.originServerTs ?: 0
sum.count = 1
sum.addedByMe = sum.addedByMe || (credentials.userId == event.sender)
eventSummary.reactionsSummary.add(sum)
} else {
//is this a known event (is possible? pagination?)
if (!sum.sourceEvents.contains(eventId)) {
sum.count += 1
sum.sourceEvents.add(eventId)
sum.addedByMe = sum.addedByMe || (credentials.userId == event.sender)
}
}

View File

@ -89,7 +89,7 @@ class MessageActionsBottomSheet : BaseMvRxBottomSheetDialog() {
var quickReactionFragment = cfm.findFragmentByTag("QuickReaction") as? QuickReactionFragment
if (quickReactionFragment == null) {
quickReactionFragment = QuickReactionFragment.newInstance()
quickReactionFragment = QuickReactionFragment.newInstance(arguments!!.get(MvRx.KEY_ARG) as ParcelableArgs)
cfm.beginTransaction()
.replace(R.id.bottom_sheet_quick_reaction_container, quickReactionFragment, "QuickReaction")
.commit()

View File

@ -25,6 +25,7 @@ import androidx.transition.TransitionManager
import butterknife.BindView
import butterknife.ButterKnife
import com.airbnb.mvrx.BaseMvRxFragment
import com.airbnb.mvrx.MvRx
import com.airbnb.mvrx.fragmentViewModel
import com.airbnb.mvrx.withState
import im.vector.riotredesign.R
@ -62,10 +63,10 @@ class QuickReactionFragment : BaseMvRxFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
quickReact1Text.text = viewModel.agreePositive
quickReact2Text.text = viewModel.agreeNegative
quickReact3Text.text = viewModel.likePositive
quickReact4Text.text = viewModel.likeNegative
quickReact1Text.text = QuickReactionViewModel.agreePositive
quickReact2Text.text = QuickReactionViewModel.agreeNegative
quickReact3Text.text = QuickReactionViewModel.likePositive
quickReact4Text.text = QuickReactionViewModel.likeNegative
//configure click listeners
quickReact1Text.setOnClickListener {
@ -127,8 +128,12 @@ class QuickReactionFragment : BaseMvRxFragment() {
}
companion object {
fun newInstance(): QuickReactionFragment {
return QuickReactionFragment()
fun newInstance(pa: MessageActionsBottomSheet.ParcelableArgs): QuickReactionFragment {
val args = Bundle()
args.putParcelable(MvRx.KEY_ARG, pa)
val fragment = QuickReactionFragment()
fragment.arguments = args
return fragment
}
}
}

View File

@ -18,7 +18,9 @@ package im.vector.riotredesign.features.home.room.detail.timeline.action
import com.airbnb.mvrx.MvRxState
import com.airbnb.mvrx.MvRxViewModelFactory
import com.airbnb.mvrx.ViewModelContext
import im.vector.matrix.android.api.session.Session
import im.vector.riotredesign.core.platform.VectorViewModel
import org.koin.android.ext.android.get
/**
* Quick reactions state, it's a toggle with 3rd state
@ -37,11 +39,6 @@ data class QuickReactionState(val agreeTrigleState: TriggleState, val likeTriggl
*/
class QuickReactionViewModel(initialState: QuickReactionState) : VectorViewModel<QuickReactionState>(initialState) {
val agreePositive = "👍"
val agreeNegative = "👎"
val likePositive = "😀"
val likeNegative = "😞"
fun toggleAgree(isFirst: Boolean) = withState {
if (isFirst) {
@ -99,10 +96,37 @@ class QuickReactionViewModel(initialState: QuickReactionState) : VectorViewModel
companion object : MvRxViewModelFactory<QuickReactionViewModel, QuickReactionState> {
val agreePositive = "👍"
val agreeNegative = "👎"
val likePositive = "😀"
val likeNegative = "😞"
override fun initialState(viewModelContext: ViewModelContext): QuickReactionState? {
// Args are accessible from the context.
// val foo = vieWModelContext.args<MyArgs>.foo
return QuickReactionState(TriggleState.NONE, TriggleState.NONE)
val currentSession = viewModelContext.activity.get<Session>()
val parcel = viewModelContext.args as MessageActionsBottomSheet.ParcelableArgs
val event = currentSession.getRoom(parcel.roomId)?.getTimeLineEvent(parcel.eventId)
?: return null
var agreeTriggle: TriggleState = TriggleState.NONE
var likeTriggle: TriggleState = TriggleState.NONE
event.annotations?.reactionsSummary?.forEach {
//it.addedByMe
if (it.addedByMe) {
if (agreePositive == it.key) {
agreeTriggle = TriggleState.FIRST
} else if (agreeNegative == it.key) {
agreeTriggle = TriggleState.SECOND
}
if (likePositive == it.key) {
likeTriggle = TriggleState.FIRST
} else if (likeNegative == it.key) {
likeTriggle = TriggleState.SECOND
}
}
}
return QuickReactionState(agreeTriggle, likeTriggle)
}
}
}