Create BubbleReactionsView

This commit is contained in:
SBiOSoftWhare 2019-05-20 21:54:23 +02:00
parent 2caf4e326f
commit 4ababf957e
2 changed files with 182 additions and 0 deletions

View file

@ -0,0 +1,136 @@
/*
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
import MatrixSDK
import Reusable
@objcMembers
final class BubbleReactionsView: UIView, NibOwnerLoadable {
// MARK: - Constants
private enum Constants {
static let minimumInteritemSpacing: CGFloat = 6.0
static let minimumLineSpacing: CGFloat = 2.0
}
// MARK: - Properties
// MARK: Outlets
@IBOutlet private weak var collectionView: UICollectionView!
// MARK: Private
private var reactionsViewData: [BubbleReactionViewData] = []
private var theme: Theme?
// MARK: Public
// Do not use `BubbleReactionsViewModelType` here due to Objective-C incompatibily
var viewModel: BubbleReactionsViewModel? {
didSet {
self.viewModel?.viewDelegate = self
self.viewModel?.process(viewAction: .loadData)
}
}
// MARK: - Setup
private func commonInit() {
self.collectionView.isScrollEnabled = false
self.collectionView.delegate = self
self.collectionView.dataSource = self
if let collectionViewFlowLayout = self.collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
collectionViewFlowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
collectionViewFlowLayout.minimumInteritemSpacing = Constants.minimumInteritemSpacing
collectionViewFlowLayout.minimumLineSpacing = Constants.minimumLineSpacing
}
self.collectionView.register(cellType: BubbleReactionViewCell.self)
self.collectionView.reloadData()
}
convenience init() {
self.init(frame: CGRect.zero)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.loadNibContent()
self.commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
self.loadNibContent()
self.commonInit()
}
// MARK: - Public
func update(theme: Theme) {
self.theme = theme
self.collectionView.reloadData()
}
func fill(reactionsViewData: [BubbleReactionViewData]) {
self.reactionsViewData = reactionsViewData
self.collectionView.reloadData()
}
}
// MARK: - UICollectionViewDataSource
extension BubbleReactionsView: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.reactionsViewData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: BubbleReactionViewCell = collectionView.dequeueReusableCell(for: indexPath)
if let theme = self.theme {
cell.update(theme: theme)
}
let viewData = self.reactionsViewData[indexPath.row]
cell.fill(viewData: viewData)
return cell
}
}
// MARK: - UICollectionViewDelegate
extension BubbleReactionsView: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.viewModel?.process(viewAction: .tapReaction(index: indexPath.row))
}
}
// MARK: - BubbleReactionsViewModelViewDelegate
extension BubbleReactionsView: BubbleReactionsViewModelViewDelegate {
func bubbleReactionsViewModel(_ viewModel: BubbleReactionsViewModel, didUpdateViewState viewState: BubbleReactionsViewState) {
switch viewState {
case .loaded(reactionsViewData: let reactionsViewData):
self.fill(reactionsViewData: reactionsViewData)
}
}
}

View file

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina6_1" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="BubbleReactionsView" customModule="Riot" customModuleProvider="target">
<connections>
<outlet property="collectionView" destination="uTe-bw-bWE" id="EGc-ca-YSM"/>
</connections>
</placeholder>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="iN0-l3-epB">
<rect key="frame" x="0.0" y="0.0" width="422" height="136"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<collectionView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" dataMode="none" translatesAutoresizingMaskIntoConstraints="NO" id="uTe-bw-bWE" customClass="AutosizedCollectionView" customModule="Riot" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="422" height="136"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="0.0" minimumInteritemSpacing="0.0" id="qeb-Ii-egn">
<size key="itemSize" width="1" height="1"/>
<size key="headerReferenceSize" width="0.0" height="0.0"/>
<size key="footerReferenceSize" width="0.0" height="0.0"/>
<inset key="sectionInset" minX="0.0" minY="0.0" maxX="0.0" maxY="0.0"/>
</collectionViewFlowLayout>
</collectionView>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="uTe-bw-bWE" firstAttribute="top" secondItem="iN0-l3-epB" secondAttribute="top" id="NbI-pU-tZb"/>
<constraint firstAttribute="bottom" secondItem="uTe-bw-bWE" secondAttribute="bottom" id="lwj-Lm-yDr"/>
<constraint firstAttribute="trailing" secondItem="uTe-bw-bWE" secondAttribute="trailing" id="rP3-ME-Y83"/>
<constraint firstItem="uTe-bw-bWE" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" id="uSd-DZ-bOf"/>
</constraints>
<nil key="simulatedTopBarMetrics"/>
<nil key="simulatedBottomBarMetrics"/>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<point key="canvasLocation" x="-423" y="11"/>
</view>
</objects>
</document>