Refactor summary view to be configurable via a view model

This commit is contained in:
ismailgulek
2021-11-18 17:46:14 +03:00
parent ee13724f64
commit 968db1161d
4 changed files with 74 additions and 38 deletions
@@ -28,6 +28,7 @@ class ThreadSummaryView: UIView {
private enum Constants {
static let viewHeight: CGFloat = 32
static let viewDefaultWidth: CGFloat = 320
static let cornerRadius: CGFloat = 4
}
@@ -36,11 +37,7 @@ class ThreadSummaryView: UIView {
@IBOutlet private weak var lastMessageAvatarView: UserAvatarView!
@IBOutlet private weak var lastMessageContentLabel: UILabel!
private(set) var thread: MXThread! {
didSet {
configure()
}
}
private(set) var thread: MXThread!
private lazy var tapGestureRecognizer: UITapGestureRecognizer = {
return UITapGestureRecognizer(target: self, action: #selector(tapped(_:)))
@@ -50,31 +47,49 @@ class ThreadSummaryView: UIView {
// MARK: - Setup
static func instantiate(withThread thread: MXThread) -> ThreadSummaryView {
let view = ThreadSummaryView.loadFromNib()
view.thread = thread
view.update(theme: ThemeService.shared().theme)
view.translatesAutoresizingMaskIntoConstraints = false
return view
init(withThread thread: MXThread) {
self.thread = thread
super.init(frame: CGRect(origin: .zero,
size: CGSize(width: Constants.viewDefaultWidth,
height: Constants.viewHeight)))
loadNibContent()
update(theme: ThemeService.shared().theme)
configure()
}
static func contentViewHeight(forThread thread: MXThread, fitting maxWidth: CGFloat) -> CGFloat {
return Constants.viewHeight
}
required init?(coder: NSCoder) {
super.init(coder: coder)
loadNibContent()
}
@nonobjc func configure(withViewModel viewModel: ThreadSummaryViewModel) {
numberOfRepliesLabel.text = String(viewModel.numberOfReplies)
if let avatar = viewModel.lastMessageSenderAvatar {
lastMessageAvatarView.fill(with: avatar)
} else {
lastMessageAvatarView.avatarImageView.image = nil
}
lastMessageContentLabel.text = viewModel.lastMessageText
}
private func configure() {
clipsToBounds = true
layer.cornerRadius = Constants.cornerRadius
addGestureRecognizer(tapGestureRecognizer)
guard let thread = thread else { return }
numberOfRepliesLabel.text = String(thread.numberOfReplies)
guard let lastMessage = thread.lastMessage else {
guard let thread = thread,
let lastMessage = thread.lastMessage,
let session = thread.session,
let eventFormatter = session.roomSummaryUpdateDelegate as? MXKEventFormatter,
let room = session.room(withRoomId: lastMessage.roomId) else {
lastMessageAvatarView.avatarImageView.image = nil
lastMessageContentLabel.text = nil
return
}
guard let session = thread.session else { return }
let lastMessageSender = session.user(withUserId: lastMessage.sender)
let fallbackImage = AvatarFallbackImage.matrixItem(lastMessage.sender,
@@ -84,17 +99,16 @@ class ThreadSummaryView: UIView {
avatarUrl: lastMessageSender?.avatarUrl,
mediaManager: session.mediaManager,
fallbackImage: fallbackImage)
lastMessageAvatarView.fill(with: avatarViewData)
guard let eventFormatter = session.roomSummaryUpdateDelegate as? MXKEventFormatter,
let room = session.room(withRoomId: lastMessage.roomId) else {
return
}
room.state { [weak self] roomState in
guard let self = self else { return }
let formatterError = UnsafeMutablePointer<MXKEventFormatterError>.allocate(capacity: 1)
self.lastMessageContentLabel.text = eventFormatter.string(from: lastMessage, with: roomState, error: formatterError)
let lastMessageText = eventFormatter.string(from: lastMessage, with: roomState, error: formatterError)
let viewModel = ThreadSummaryViewModel(numberOfReplies: thread.numberOfReplies,
lastMessageSenderAvatar: avatarViewData,
lastMessageText: lastMessageText)
self.configure(withViewModel: viewModel)
}
}
@@ -107,7 +121,9 @@ class ThreadSummaryView: UIView {
}
}
extension ThreadSummaryView: NibLoadable {}
// extension ThreadSummaryView: NibLoadable {}
extension ThreadSummaryView: NibOwnerLoadable {}
extension ThreadSummaryView: Themable {
@@ -5,13 +5,19 @@
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="18093"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="ThreadSummaryView" customModule="Riot" customModuleProvider="target">
<connections>
<outlet property="iconView" destination="vva-PV-3Ya" id="e1B-Zp-pni"/>
<outlet property="lastMessageAvatarView" destination="9wW-1f-f69" id="xiI-t8-Q56"/>
<outlet property="lastMessageContentLabel" destination="DVT-JI-3kw" id="O6N-ev-FRz"/>
<outlet property="numberOfRepliesLabel" destination="GcG-W8-9LR" id="hzP-Ea-C6l"/>
</connections>
</placeholder>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="iN0-l3-epB" customClass="ThreadSummaryView" customModule="Riot" customModuleProvider="target">
<view contentMode="scaleToFill" id="iN0-l3-epB">
<rect key="frame" x="0.0" y="0.0" width="414" height="32"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
@@ -66,7 +72,7 @@
</view>
</subviews>
<viewLayoutGuide key="safeArea" id="vUN-kp-3ea"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="bottom" secondItem="TFL-sS-eJc" secondAttribute="bottom" constant="4" id="GJq-Pw-T0A"/>
<constraint firstItem="vUN-kp-3ea" firstAttribute="trailing" secondItem="TFL-sS-eJc" secondAttribute="trailing" constant="8" id="RR6-Uu-dD9"/>
@@ -74,19 +80,10 @@
<constraint firstItem="TFL-sS-eJc" firstAttribute="top" secondItem="iN0-l3-epB" secondAttribute="top" constant="4" id="yqe-iO-Cz5"/>
</constraints>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<connections>
<outlet property="iconView" destination="vva-PV-3Ya" id="BGw-QZ-0YO"/>
<outlet property="lastMessageAvatarView" destination="9wW-1f-f69" id="mCe-43-Ne3"/>
<outlet property="lastMessageContentLabel" destination="DVT-JI-3kw" id="ONf-CO-FNC"/>
<outlet property="numberOfRepliesLabel" destination="GcG-W8-9LR" id="7bl-c4-wk0"/>
</connections>
<point key="canvasLocation" x="-23.188405797101453" y="-179.46428571428569"/>
</view>
</objects>
<resources>
<image name="room_context_menu_reply_in_thread" width="24" height="24"/>
<systemColor name="systemBackgroundColor">
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</systemColor>
<image name="room_context_menu_reply_in_thread" width="18" height="18"/>
</resources>
</document>
@@ -0,0 +1,23 @@
//
// Copyright 2021 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
struct ThreadSummaryViewModel {
var numberOfReplies: Int
var lastMessageSenderAvatar: AvatarViewDataProtocol?
var lastMessageText: String?
}