mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-20 16:42:44 +02:00
b21a89584b
Fix Steve's comment
250 lines
8.6 KiB
Swift
250 lines
8.6 KiB
Swift
/*
|
|
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.
|
|
*/
|
|
|
|
import UIKit
|
|
|
|
final class ReactionsMenuViewModel: ReactionsMenuViewModelType {
|
|
|
|
// MARK: - Properties
|
|
|
|
// MARK: Private
|
|
private let session: MXSession // TODO: To remove. Only required for reactUsingHack()
|
|
private let aggregations: MXAggregations
|
|
private let roomId: String
|
|
private let eventId: String
|
|
|
|
// MARK: Public
|
|
|
|
private(set) var isAgreeButtonSelected: Bool = false
|
|
private(set) var isDisagreeButtonSelected: Bool = false
|
|
private(set) var isLikeButtonSelected: Bool = false
|
|
private(set) var isDislikeButtonSelected: Bool = false
|
|
|
|
weak var viewDelegate: ReactionsMenuViewModelDelegate?
|
|
weak var coordinatorDelegate: ReactionsMenuViewModelCoordinatorDelegate?
|
|
|
|
// MARK: - Setup
|
|
|
|
init(aggregations: MXAggregations, roomId: String, eventId: String, session: MXSession) {
|
|
self.aggregations = aggregations
|
|
self.roomId = roomId
|
|
self.eventId = eventId
|
|
self.session = session
|
|
|
|
self.loadData()
|
|
self.listenToDataUpdate()
|
|
}
|
|
|
|
// MARK: - Public
|
|
|
|
func process(viewAction: ReactionsMenuViewAction) {
|
|
var reaction: ReactionsMenuReaction?
|
|
var newState: Bool?
|
|
|
|
switch viewAction {
|
|
case .toggleReaction(let menuReaction):
|
|
reaction = menuReaction
|
|
|
|
switch menuReaction {
|
|
case .agree:
|
|
newState = !self.isAgreeButtonSelected
|
|
case .disagree:
|
|
newState = !self.isDisagreeButtonSelected
|
|
case .like:
|
|
newState = !self.isLikeButtonSelected
|
|
case .dislike:
|
|
newState = !self.isDislikeButtonSelected
|
|
}
|
|
}
|
|
|
|
guard let theReaction = reaction, let theNewState = newState else {
|
|
return
|
|
}
|
|
|
|
self.react(withReaction: theReaction, selected: theNewState)
|
|
}
|
|
|
|
// MARK: - Private
|
|
|
|
private func resetData() {
|
|
self.isAgreeButtonSelected = false
|
|
self.isDisagreeButtonSelected = false
|
|
self.isLikeButtonSelected = false
|
|
self.isDislikeButtonSelected = false
|
|
}
|
|
|
|
private func loadData() {
|
|
guard let reactionCounts = self.aggregations.aggregatedReactions(onEvent: self.eventId, inRoom: self.roomId)?.reactions else {
|
|
return
|
|
}
|
|
|
|
self.resetData()
|
|
reactionCounts.forEach { (reactionCount) in
|
|
if reactionCount.myUserHasReacted {
|
|
if let reaction = ReactionsMenuReaction(rawValue: reactionCount.reaction) {
|
|
switch reaction {
|
|
case .agree:
|
|
self.isAgreeButtonSelected = true
|
|
case .disagree:
|
|
self.isDisagreeButtonSelected = true
|
|
case .like:
|
|
self.isLikeButtonSelected = true
|
|
case .dislike:
|
|
self.isDislikeButtonSelected = true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
self.viewDelegate?.reactionsMenuViewModelDidUpdate(self)
|
|
}
|
|
|
|
private func listenToDataUpdate() {
|
|
self.aggregations.listenToReactionCountUpdate(inRoom: self.roomId) { [weak self] (changes) in
|
|
|
|
guard let sself = self else {
|
|
return
|
|
}
|
|
|
|
if changes[sself.eventId] != nil {
|
|
sself.loadData()
|
|
}
|
|
}
|
|
}
|
|
|
|
private func react(withReaction reaction: ReactionsMenuReaction, selected: Bool) {
|
|
if selected {
|
|
self.aggregations.sendReaction(reaction.rawValue, toEvent: self.eventId, inRoom: self.roomId, success: {[weak self] _ in
|
|
|
|
guard let sself = self else {
|
|
return
|
|
}
|
|
|
|
sself.coordinatorDelegate?.reactionsMenuViewModel(sself, didReactionComplete: reaction.rawValue, isAddReaction: true)
|
|
|
|
}, failure: {[weak self] (error) in
|
|
print("[ReactionsMenuViewModel] react: Error: \(error)")
|
|
|
|
guard let sself = self else {
|
|
return
|
|
}
|
|
|
|
// The server does not support support reaction yet
|
|
// Use a fallback mechanism
|
|
// TODO: To remove once the feature has landed on matrix.org homeserver
|
|
if let mxError = MXError(nsError: error) {
|
|
if mxError.errcode == kMXErrCodeStringUnrecognized {
|
|
sself.reactUsingHack(withReaction: reaction)
|
|
return
|
|
}
|
|
}
|
|
|
|
sself.coordinatorDelegate?.reactionsMenuViewModel(sself, didReactionFailedWithError: error, reaction: reaction.rawValue, isAddReaction: true)
|
|
})
|
|
} else {
|
|
|
|
self.aggregations.unReact(onReaction: reaction.rawValue, toEvent: self.eventId, inRoom: self.roomId, success: {[weak self] in
|
|
|
|
guard let sself = self else {
|
|
return
|
|
}
|
|
|
|
sself.coordinatorDelegate?.reactionsMenuViewModel(sself, didReactionComplete: reaction.rawValue, isAddReaction: false)
|
|
|
|
}, failure: {[weak self] (error) in
|
|
print("[ReactionsMenuViewModel] react: Error: \(error)")
|
|
|
|
guard let sself = self else {
|
|
return
|
|
}
|
|
|
|
sself.coordinatorDelegate?.reactionsMenuViewModel(sself, didReactionFailedWithError: error, reaction: reaction.rawValue, isAddReaction: false)
|
|
})
|
|
}
|
|
|
|
self.coordinatorDelegate?.reactionsMenuViewModel(self, didSendReaction: reaction.rawValue, isAddReaction: !selected)
|
|
|
|
if selected {
|
|
self.ensure3StateButtons(withReaction: reaction)
|
|
}
|
|
}
|
|
|
|
// We can like, dislike, be indifferent but we cannot like & dislike at the same time
|
|
private func ensure3StateButtons(withReaction reaction: ReactionsMenuReaction) {
|
|
var unreaction: ReactionsMenuReaction?
|
|
|
|
switch reaction {
|
|
case .agree:
|
|
if isDisagreeButtonSelected {
|
|
unreaction = .disagree
|
|
}
|
|
case .disagree:
|
|
if isAgreeButtonSelected {
|
|
unreaction = .agree
|
|
}
|
|
case .like:
|
|
if isDislikeButtonSelected {
|
|
unreaction = .dislike
|
|
}
|
|
case .dislike:
|
|
if isLikeButtonSelected {
|
|
unreaction = .like
|
|
}
|
|
}
|
|
|
|
if let unreaction = unreaction {
|
|
self.react(withReaction: unreaction, selected: false)
|
|
}
|
|
}
|
|
|
|
/// reactUsingHack directly sends a `m.reaction` room message instead of using the `/send_relation` api.
|
|
///
|
|
/// TODO: To remove once the feature has landed on matrix.org homeserver
|
|
///
|
|
/// - Parameter reaction: the reaction
|
|
private func reactUsingHack(withReaction reaction: ReactionsMenuReaction) {
|
|
guard let room = self.session.room(withRoomId: self.roomId) else {
|
|
print("[ReactionsMenuViewModel] reactUsingHack: Error: Unknown room: \(self.roomId)")
|
|
return
|
|
}
|
|
|
|
print("[ReactionsMenuViewModel] reactUsingHack")
|
|
|
|
let reactionContent = [
|
|
"m.relates_to": [
|
|
"rel_type": "m.annotation",
|
|
"event_id": self.eventId,
|
|
"key": reaction.rawValue]
|
|
]
|
|
|
|
var nilEvent: MXEvent?
|
|
room.sendEvent(.reaction, content: reactionContent, localEcho: &nilEvent, completion: { [weak self] ( completion) in
|
|
guard let sself = self else {
|
|
return
|
|
}
|
|
switch completion {
|
|
case .success:
|
|
print("[ReactionsMenuViewModel] reactUsingHack: Success")
|
|
sself.coordinatorDelegate?.reactionsMenuViewModel(sself, didReactionComplete: reaction.rawValue, isAddReaction: true)
|
|
|
|
case.failure(let error):
|
|
print("[ReactionsMenuViewModel] reactUsingHack: Error: \(error)")
|
|
sself.coordinatorDelegate?.reactionsMenuViewModel(sself, didReactionFailedWithError: error, reaction: reaction.rawValue, isAddReaction: true)
|
|
}
|
|
})
|
|
}
|
|
}
|