Emoji picker: Implement Emoji store, view model and view data.

This commit is contained in:
SBiOSoftWhare
2019-07-25 16:37:18 +02:00
parent 48d8d1192e
commit cb57c5a832
7 changed files with 352 additions and 0 deletions
@@ -0,0 +1,69 @@
/*
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
final class EmojiStore {
static let shared = EmojiStore()
// MARK: - Properties
private var emojiCategories: [EmojiCategory] = []
// MARK: - Public
func getAll() -> [EmojiCategory] {
return self.emojiCategories
}
func set(_ emojiCategories: [EmojiCategory]) {
self.emojiCategories = emojiCategories
}
func findEmojiItemsSortedByCategory(with searchText: String) -> [EmojiCategory] {
let initial: [EmojiCategory] = []
let filteredEmojiCategories = emojiCategories.reduce(into: initial) { (filteredEmojiCategories, emojiCategory) in
let filteredEmojiItems = emojiCategory.emojis.filter({ (emojiItem) -> Bool in
// Do not use `String.localizedCaseInsensitiveContains` here as EmojiItem data is not localized for the moment
if emojiItem.name.vc_caseInsensitiveContains(searchText) {
return true
}
if emojiItem.keywords.contains(where: { $0.vc_caseInsensitiveContains(searchText) }) {
return true
}
let shortNamesMatch = emojiItem.shortNames.contains { text -> Bool in
return text.vc_caseInsensitiveContains(searchText)
}
return shortNamesMatch
})
if filteredEmojiItems.isEmpty == false {
let filteredEmojiCategory = EmojiCategory(identifier: emojiCategory.identifier, emojis: filteredEmojiItems)
filteredEmojiCategories.append(filteredEmojiCategory)
}
}
return filteredEmojiCategories
}
}
@@ -0,0 +1,23 @@
/*
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
struct EmojiPickerCategoryViewData {
let identifier: String
let name: String
let emojiViewDataList: [EmojiPickerItemViewData]
}
@@ -0,0 +1,23 @@
/*
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
struct EmojiPickerItemViewData {
let identifier: String
let emoji: String
let isSelected: Bool
}
@@ -0,0 +1,27 @@
// File created from ScreenTemplate
// $ createScreen.sh toto EmojiPicker
/*
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
/// EmojiPickerViewController view actions exposed to view model
enum EmojiPickerViewAction {
case loadData
case cancel
case tap(emojiItemViewData: EmojiPickerItemViewData)
case search(text: String?)
}
@@ -0,0 +1,146 @@
// File created from ScreenTemplate
// $ createScreen.sh toto EmojiPicker
/*
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
final class EmojiPickerViewModel: EmojiPickerViewModelType {
// MARK: - Properties
// MARK: Private
private let session: MXSession
private let roomId: String
private let eventId: String
private let emojiService: EmojiService
private let emojiStore: EmojiStore
private lazy var aggregatedReactionsByEmoji: [String: MXReactionCount] = {
return self.buildAggregatedReactionsByEmoji()
}()
// MARK: Public
weak var viewDelegate: EmojiPickerViewModelViewDelegate?
weak var coordinatorDelegate: EmojiPickerViewModelCoordinatorDelegate?
// MARK: - Setup
init(session: MXSession, roomId: String, eventId: String) {
self.session = session
self.roomId = roomId
self.eventId = eventId
self.emojiService = EmojiService()
self.emojiStore = EmojiStore.shared
}
// MARK: - Public
func process(viewAction: EmojiPickerViewAction) {
switch viewAction {
case .loadData:
self.loadData()
case .tap(emojiItemViewData: let emojiItemViewData):
let emoji = emojiItemViewData.emoji
if emojiItemViewData.isSelected {
self.coordinatorDelegate?.emojiPickerViewModel(self, didRemoveEmoji: emoji, forEventId: self.eventId)
} else {
self.coordinatorDelegate?.emojiPickerViewModel(self, didAddEmoji: emoji, forEventId: self.eventId)
}
case .search(text: let searchText):
self.searchEmojis(with: searchText)
case .cancel:
self.coordinatorDelegate?.emojiPickerViewModelDidCancel(self)
}
}
// MARK: - Private
private func loadData() {
if self.emojiStore.getAll().isEmpty == false {
let emojiCategories = self.emojiStore.getAll()
let emojiCatagoryViewDataList = self.emojiCatagoryViewDataList(from: emojiCategories)
self.update(viewState: .loaded(emojiCategories: emojiCatagoryViewDataList))
} else {
self.update(viewState: .loading)
self.emojiService.getEmojiCategories { response in
switch response {
case .success(let emojiCategories):
self.emojiStore.set(emojiCategories)
let emojiCatagoryViewDataList = self.emojiCatagoryViewDataList(from: emojiCategories)
DispatchQueue.main.async {
self.update(viewState: .loaded(emojiCategories: emojiCatagoryViewDataList))
}
case .failure(let error):
DispatchQueue.main.async {
self.update(viewState: .error(error))
}
}
}
}
}
private func searchEmojis(with searchText: String?) {
let filteredEmojiCategories: [EmojiCategory]
if let searchText = searchText, searchText.isEmpty == false {
filteredEmojiCategories = self.emojiStore.findEmojiItemsSortedByCategory(with: searchText)
} else {
filteredEmojiCategories = self.emojiStore.getAll()
}
let emojiCatagoryViewDataList = self.emojiCatagoryViewDataList(from: filteredEmojiCategories)
self.update(viewState: .loaded(emojiCategories: emojiCatagoryViewDataList))
}
private func update(viewState: EmojiPickerViewState) {
self.viewDelegate?.emojiPickerViewModel(self, didUpdateViewState: viewState)
}
private func emojiCatagoryViewDataList(from emojiCategories: [EmojiCategory]) -> [EmojiPickerCategoryViewData] {
return emojiCategories.map { (emojiCategory) -> EmojiPickerCategoryViewData in
let emojiPickerViewDataList = emojiCategory.emojis.map({ (emojiItem) -> EmojiPickerItemViewData in
let isSelected = self.isUserReacted(with: emojiItem.value)
return EmojiPickerItemViewData(identifier: emojiItem.identifier, emoji: emojiItem.value, isSelected: isSelected)
})
return EmojiPickerCategoryViewData(identifier: emojiCategory.identifier, name: emojiCategory.name, emojiViewDataList: emojiPickerViewDataList)
}
}
private func isUserReacted(with emoji: String) -> Bool {
guard let reactionCount = self.aggregatedReactionsByEmoji[emoji] else {
return false
}
return reactionCount.myUserHasReacted
}
private func buildAggregatedReactionsByEmoji() -> [String: MXReactionCount] {
guard let aggregatedReactions = self.session.aggregations.aggregatedReactions(onEvent: self.eventId, inRoom: self.roomId) else {
return [:]
}
let initial: [String: MXReactionCount] = [:]
return aggregatedReactions.reactions.reduce(into: initial) { (aggregatedReactionsByEmoji, reactionCount) in
aggregatedReactionsByEmoji[reactionCount.reaction] = reactionCount
}
}
}
@@ -0,0 +1,38 @@
// File created from ScreenTemplate
// $ createScreen.sh toto EmojiPicker
/*
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
protocol EmojiPickerViewModelViewDelegate: class {
func emojiPickerViewModel(_ viewModel: EmojiPickerViewModelType, didUpdateViewState viewSate: EmojiPickerViewState)
}
protocol EmojiPickerViewModelCoordinatorDelegate: class {
func emojiPickerViewModel(_ viewModel: EmojiPickerViewModelType, didAddEmoji emoji: String, forEventId eventId: String)
func emojiPickerViewModel(_ viewModel: EmojiPickerViewModelType, didRemoveEmoji emoji: String, forEventId eventId: String)
func emojiPickerViewModelDidCancel(_ viewModel: EmojiPickerViewModelType)
}
/// Protocol describing the view model used by `EmojiPickerViewController`
protocol EmojiPickerViewModelType {
var viewDelegate: EmojiPickerViewModelViewDelegate? { get set }
var coordinatorDelegate: EmojiPickerViewModelCoordinatorDelegate? { get set }
func process(viewAction: EmojiPickerViewAction)
}
@@ -0,0 +1,26 @@
// File created from ScreenTemplate
// $ createScreen.sh toto EmojiPicker
/*
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
/// EmojiPickerViewController view state
enum EmojiPickerViewState {
case loading
case loaded(emojiCategories: [EmojiPickerCategoryViewData])
case error(Error)
}