2019-05-20 19:52:07 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
@objc final class BubbleReactionsViewModel: NSObject, BubbleReactionsViewModelType {
|
2019-07-03 16:05:20 +00:00
|
|
|
|
|
|
|
// MARK: - Constants
|
|
|
|
|
|
|
|
private enum Constants {
|
|
|
|
static let maxItemsWhenLimited: Int = 8
|
|
|
|
}
|
|
|
|
|
2019-05-20 19:52:07 +00:00
|
|
|
// MARK: - Properties
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
private let aggregatedReactions: MXAggregatedReactions
|
|
|
|
private let eventId: String
|
2019-07-03 16:05:20 +00:00
|
|
|
private let showAll: Bool
|
2019-05-20 19:52:07 +00:00
|
|
|
|
|
|
|
// MARK: Public
|
|
|
|
|
|
|
|
@objc weak var viewModelDelegate: BubbleReactionsViewModelDelegate?
|
|
|
|
weak var viewDelegate: BubbleReactionsViewModelViewDelegate?
|
|
|
|
|
|
|
|
// MARK: - Setup
|
|
|
|
|
|
|
|
@objc init(aggregatedReactions: MXAggregatedReactions,
|
2019-07-03 16:05:20 +00:00
|
|
|
eventId: String,
|
|
|
|
showAll: Bool) {
|
2019-05-20 19:52:07 +00:00
|
|
|
self.aggregatedReactions = aggregatedReactions
|
|
|
|
self.eventId = eventId
|
2019-07-03 16:05:20 +00:00
|
|
|
self.showAll = showAll
|
2019-05-20 19:52:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Public
|
|
|
|
|
|
|
|
func process(viewAction: BubbleReactionsViewAction) {
|
|
|
|
switch viewAction {
|
|
|
|
case .loadData:
|
2019-07-03 16:05:20 +00:00
|
|
|
self.loadData()
|
2019-05-20 19:52:07 +00:00
|
|
|
case .tapReaction(let index):
|
|
|
|
guard index < self.aggregatedReactions.reactions.count else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let reactionCount = self.aggregatedReactions.reactions[index]
|
|
|
|
if reactionCount.myUserHasReacted {
|
|
|
|
self.viewModelDelegate?.bubbleReactionsViewModel(self, didRemoveReaction: reactionCount, forEventId: self.eventId)
|
|
|
|
} else {
|
|
|
|
self.viewModelDelegate?.bubbleReactionsViewModel(self, didAddReaction: reactionCount, forEventId: self.eventId)
|
|
|
|
}
|
|
|
|
case .addNewReaction:
|
|
|
|
break
|
2019-07-03 16:05:20 +00:00
|
|
|
case .tapShowAction(.showAll):
|
|
|
|
self.viewModelDelegate?.bubbleReactionsViewModel(self, didShowAllTappedForEventId: self.eventId)
|
|
|
|
case .tapShowAction(.showLess):
|
|
|
|
self.viewModelDelegate?.bubbleReactionsViewModel(self, didShowLessTappedForEventId: self.eventId)
|
2019-07-30 13:44:51 +00:00
|
|
|
case .longPress:
|
|
|
|
self.viewModelDelegate?.bubbleReactionsViewModel(self, didLongPressForEventId: self.eventId)
|
2019-05-20 19:52:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-03 16:05:20 +00:00
|
|
|
|
|
|
|
func loadData() {
|
|
|
|
var reactions = self.aggregatedReactions.reactions
|
|
|
|
var showAllButtonState: BubbleReactionsViewState.ShowAllButtonState = .none
|
|
|
|
|
|
|
|
// Limit displayed reactions if required
|
|
|
|
if reactions.count > Constants.maxItemsWhenLimited {
|
|
|
|
if self.showAll == true {
|
|
|
|
showAllButtonState = .showLess
|
|
|
|
} else {
|
|
|
|
reactions = Array(reactions[0..<Constants.maxItemsWhenLimited])
|
|
|
|
showAllButtonState = .showAll
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let reactionsViewData = reactions.map { (reactionCount) -> BubbleReactionViewData in
|
|
|
|
return BubbleReactionViewData(emoji: reactionCount.reaction, countString: "\(reactionCount.count)", isCurrentUserReacted: reactionCount.myUserHasReacted)
|
|
|
|
}
|
|
|
|
|
|
|
|
self.viewDelegate?.bubbleReactionsViewModel(self, didUpdateViewState: .loaded(reactionsViewData: reactionsViewData, showAllButtonState: showAllButtonState))
|
|
|
|
}
|
2019-05-20 19:52:07 +00:00
|
|
|
}
|