mirror of
https://github.com/element-hq/element-android
synced 2024-11-27 03:48:12 +03:00
Support AddedByMe in reaction + context menu reflect agree/like state
This commit is contained in:
parent
a9a1fe2aa0
commit
6fe455dac0
6 changed files with 48 additions and 15 deletions
|
@ -45,6 +45,7 @@ internal object EventMapper {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun map(eventEntity: EventEntity): Event {
|
fun map(eventEntity: EventEntity): Event {
|
||||||
|
//TODO proxy the event to only parse unsigned data when accessed?
|
||||||
var ud = if (eventEntity.unsignedData.isNullOrBlank()) null
|
var ud = if (eventEntity.unsignedData.isNullOrBlank()) null
|
||||||
else MoshiProvider.providesMoshi().adapter(UnsignedData::class.java).fromJson(eventEntity.unsignedData)
|
else MoshiProvider.providesMoshi().adapter(UnsignedData::class.java).fromJson(eventEntity.unsignedData)
|
||||||
return Event(
|
return Event(
|
||||||
|
|
|
@ -104,7 +104,7 @@ internal class SessionModule(private val sessionParams: SessionParams) {
|
||||||
}
|
}
|
||||||
|
|
||||||
scope(DefaultSession.SCOPE) {
|
scope(DefaultSession.SCOPE) {
|
||||||
EventRelationsAggregationUpdater()
|
EventRelationsAggregationUpdater(get())
|
||||||
}
|
}
|
||||||
|
|
||||||
scope(DefaultSession.SCOPE) {
|
scope(DefaultSession.SCOPE) {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package im.vector.matrix.android.internal.session.room
|
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.events.model.*
|
||||||
import im.vector.matrix.android.api.session.room.model.annotation.ReactionContent
|
import im.vector.matrix.android.api.session.room.model.annotation.ReactionContent
|
||||||
import im.vector.matrix.android.internal.database.model.EventAnnotationsSummaryEntity
|
import im.vector.matrix.android.internal.database.model.EventAnnotationsSummaryEntity
|
||||||
|
@ -10,7 +11,7 @@ import io.realm.Realm
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
|
||||||
|
|
||||||
internal class EventRelationsAggregationUpdater {
|
internal class EventRelationsAggregationUpdater(private val credentials: Credentials) {
|
||||||
|
|
||||||
fun update(realm: Realm, roomId: String, events: List<Event>?) {
|
fun update(realm: Realm, roomId: String, events: List<Event>?) {
|
||||||
events?.forEach { event ->
|
events?.forEach { event ->
|
||||||
|
@ -66,12 +67,14 @@ internal class EventRelationsAggregationUpdater {
|
||||||
sum.key = reaction
|
sum.key = reaction
|
||||||
sum.firstTimestamp = event.originServerTs ?: 0
|
sum.firstTimestamp = event.originServerTs ?: 0
|
||||||
sum.count = 1
|
sum.count = 1
|
||||||
|
sum.addedByMe = sum.addedByMe || (credentials.userId == event.sender)
|
||||||
eventSummary.reactionsSummary.add(sum)
|
eventSummary.reactionsSummary.add(sum)
|
||||||
} else {
|
} else {
|
||||||
//is this a known event (is possible? pagination?)
|
//is this a known event (is possible? pagination?)
|
||||||
if (!sum.sourceEvents.contains(eventId)) {
|
if (!sum.sourceEvents.contains(eventId)) {
|
||||||
sum.count += 1
|
sum.count += 1
|
||||||
sum.sourceEvents.add(eventId)
|
sum.sourceEvents.add(eventId)
|
||||||
|
sum.addedByMe = sum.addedByMe || (credentials.userId == event.sender)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -89,7 +89,7 @@ class MessageActionsBottomSheet : BaseMvRxBottomSheetDialog() {
|
||||||
|
|
||||||
var quickReactionFragment = cfm.findFragmentByTag("QuickReaction") as? QuickReactionFragment
|
var quickReactionFragment = cfm.findFragmentByTag("QuickReaction") as? QuickReactionFragment
|
||||||
if (quickReactionFragment == null) {
|
if (quickReactionFragment == null) {
|
||||||
quickReactionFragment = QuickReactionFragment.newInstance()
|
quickReactionFragment = QuickReactionFragment.newInstance(arguments!!.get(MvRx.KEY_ARG) as ParcelableArgs)
|
||||||
cfm.beginTransaction()
|
cfm.beginTransaction()
|
||||||
.replace(R.id.bottom_sheet_quick_reaction_container, quickReactionFragment, "QuickReaction")
|
.replace(R.id.bottom_sheet_quick_reaction_container, quickReactionFragment, "QuickReaction")
|
||||||
.commit()
|
.commit()
|
||||||
|
|
|
@ -25,6 +25,7 @@ import androidx.transition.TransitionManager
|
||||||
import butterknife.BindView
|
import butterknife.BindView
|
||||||
import butterknife.ButterKnife
|
import butterknife.ButterKnife
|
||||||
import com.airbnb.mvrx.BaseMvRxFragment
|
import com.airbnb.mvrx.BaseMvRxFragment
|
||||||
|
import com.airbnb.mvrx.MvRx
|
||||||
import com.airbnb.mvrx.fragmentViewModel
|
import com.airbnb.mvrx.fragmentViewModel
|
||||||
import com.airbnb.mvrx.withState
|
import com.airbnb.mvrx.withState
|
||||||
import im.vector.riotredesign.R
|
import im.vector.riotredesign.R
|
||||||
|
@ -62,10 +63,10 @@ class QuickReactionFragment : BaseMvRxFragment() {
|
||||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
super.onViewCreated(view, savedInstanceState)
|
super.onViewCreated(view, savedInstanceState)
|
||||||
|
|
||||||
quickReact1Text.text = viewModel.agreePositive
|
quickReact1Text.text = QuickReactionViewModel.agreePositive
|
||||||
quickReact2Text.text = viewModel.agreeNegative
|
quickReact2Text.text = QuickReactionViewModel.agreeNegative
|
||||||
quickReact3Text.text = viewModel.likePositive
|
quickReact3Text.text = QuickReactionViewModel.likePositive
|
||||||
quickReact4Text.text = viewModel.likeNegative
|
quickReact4Text.text = QuickReactionViewModel.likeNegative
|
||||||
|
|
||||||
//configure click listeners
|
//configure click listeners
|
||||||
quickReact1Text.setOnClickListener {
|
quickReact1Text.setOnClickListener {
|
||||||
|
@ -127,8 +128,12 @@ class QuickReactionFragment : BaseMvRxFragment() {
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun newInstance(): QuickReactionFragment {
|
fun newInstance(pa: MessageActionsBottomSheet.ParcelableArgs): QuickReactionFragment {
|
||||||
return QuickReactionFragment()
|
val args = Bundle()
|
||||||
|
args.putParcelable(MvRx.KEY_ARG, pa)
|
||||||
|
val fragment = QuickReactionFragment()
|
||||||
|
fragment.arguments = args
|
||||||
|
return fragment
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -18,7 +18,9 @@ package im.vector.riotredesign.features.home.room.detail.timeline.action
|
||||||
import com.airbnb.mvrx.MvRxState
|
import com.airbnb.mvrx.MvRxState
|
||||||
import com.airbnb.mvrx.MvRxViewModelFactory
|
import com.airbnb.mvrx.MvRxViewModelFactory
|
||||||
import com.airbnb.mvrx.ViewModelContext
|
import com.airbnb.mvrx.ViewModelContext
|
||||||
|
import im.vector.matrix.android.api.session.Session
|
||||||
import im.vector.riotredesign.core.platform.VectorViewModel
|
import im.vector.riotredesign.core.platform.VectorViewModel
|
||||||
|
import org.koin.android.ext.android.get
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Quick reactions state, it's a toggle with 3rd state
|
* 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) {
|
class QuickReactionViewModel(initialState: QuickReactionState) : VectorViewModel<QuickReactionState>(initialState) {
|
||||||
|
|
||||||
val agreePositive = "👍"
|
|
||||||
val agreeNegative = "👎"
|
|
||||||
val likePositive = "😀"
|
|
||||||
val likeNegative = "😞"
|
|
||||||
|
|
||||||
|
|
||||||
fun toggleAgree(isFirst: Boolean) = withState {
|
fun toggleAgree(isFirst: Boolean) = withState {
|
||||||
if (isFirst) {
|
if (isFirst) {
|
||||||
|
@ -99,10 +96,37 @@ class QuickReactionViewModel(initialState: QuickReactionState) : VectorViewModel
|
||||||
|
|
||||||
companion object : MvRxViewModelFactory<QuickReactionViewModel, QuickReactionState> {
|
companion object : MvRxViewModelFactory<QuickReactionViewModel, QuickReactionState> {
|
||||||
|
|
||||||
|
val agreePositive = "👍"
|
||||||
|
val agreeNegative = "👎"
|
||||||
|
val likePositive = "😀"
|
||||||
|
val likeNegative = "😞"
|
||||||
|
|
||||||
override fun initialState(viewModelContext: ViewModelContext): QuickReactionState? {
|
override fun initialState(viewModelContext: ViewModelContext): QuickReactionState? {
|
||||||
// Args are accessible from the context.
|
// Args are accessible from the context.
|
||||||
// val foo = vieWModelContext.args<MyArgs>.foo
|
// 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue