Create ThreadSummaryView

This commit is contained in:
ismailgulek
2021-11-09 16:50:02 +03:00
parent 9a7095ea4b
commit 8cfbf9c4c7
3 changed files with 203 additions and 2 deletions
@@ -0,0 +1,110 @@
//
// 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
import Reusable
@objc
protocol ThreadSummaryViewDelegate: AnyObject {
func tappedThreadSummaryView(_ summaryView: ThreadSummaryView, for thread: MXThread)
}
/// A view to display a summary for an `MXThread` generated by the `MXThreadingService`.
@objcMembers
class ThreadSummaryView: UIView {
private enum Constants {
static let viewHeight: CGFloat = 32
static let cornerRadius: CGFloat = 4
}
@IBOutlet private weak var iconView: UIImageView!
@IBOutlet private weak var numberOfRepliesLabel: UILabel!
@IBOutlet private weak var lastMessageAvatarView: UserAvatarView!
@IBOutlet private weak var lastMessageContentLabel: UILabel!
private(set) var thread: MXThread! {
didSet {
configure()
}
}
weak var delegate: ThreadSummaryViewDelegate?
// 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
}
static func contentViewHeight(forThread thread: MXThread, fitting maxWidth: CGFloat) -> CGFloat {
return Constants.viewHeight
}
private func configure() {
clipsToBounds = true
layer.cornerRadius = Constants.cornerRadius
guard let thread = thread else { return }
numberOfRepliesLabel.text = String(thread.numberOfReplies)
guard let lastMessage = thread.lastMessage 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,
lastMessageSender?.displayname)
let avatarViewData = AvatarViewData(matrixItemId: lastMessage.sender,
displayName: lastMessageSender?.displayname,
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)
}
}
// MARK: - Action
}
extension ThreadSummaryView: NibLoadable {}
extension ThreadSummaryView: Themable {
func update(theme: Theme) {
backgroundColor = theme.colors.system
numberOfRepliesLabel.textColor = theme.colors.secondaryContent
lastMessageContentLabel.textColor = theme.colors.secondaryContent
}
}
@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="18122" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<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="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="iN0-l3-epB" customClass="ThreadSummaryView" customModule="Riot" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="414" height="32"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="TFL-sS-eJc">
<rect key="frame" x="8" y="4" width="398" height="24"/>
<subviews>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="room_context_menu_reply_in_thread" translatesAutoresizingMaskIntoConstraints="NO" id="vva-PV-3Ya">
<rect key="frame" x="1" y="3" width="18" height="18"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="width" secondItem="vva-PV-3Ya" secondAttribute="height" multiplier="1:1" id="972-WJ-2Zq"/>
</constraints>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="252" verticalHuggingPriority="251" text="1" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="GcG-W8-9LR">
<rect key="frame" x="25" y="0.0" width="6" height="24"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<fontDescription key="fontDescription" type="system" pointSize="12"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="9wW-1f-f69" customClass="UserAvatarView" customModule="Riot" customModuleProvider="target">
<rect key="frame" x="39" y="0.0" width="24" height="24"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="width" secondItem="9wW-1f-f69" secondAttribute="height" multiplier="1:1" id="V4H-JA-w4O"/>
</constraints>
</view>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="DVT-JI-3kw">
<rect key="frame" x="71" y="0.0" width="319" height="24"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<fontDescription key="fontDescription" type="system" pointSize="12"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="vva-PV-3Ya" firstAttribute="top" secondItem="TFL-sS-eJc" secondAttribute="top" constant="3" id="3JV-P7-s7n"/>
<constraint firstAttribute="bottom" secondItem="DVT-JI-3kw" secondAttribute="bottom" id="ArM-9P-35J"/>
<constraint firstAttribute="bottom" secondItem="GcG-W8-9LR" secondAttribute="bottom" id="FI4-bk-goz"/>
<constraint firstItem="9wW-1f-f69" firstAttribute="top" secondItem="TFL-sS-eJc" secondAttribute="top" id="GBe-gi-Iwc"/>
<constraint firstItem="DVT-JI-3kw" firstAttribute="top" secondItem="TFL-sS-eJc" secondAttribute="top" id="MSs-PD-tov"/>
<constraint firstItem="GcG-W8-9LR" firstAttribute="leading" secondItem="vva-PV-3Ya" secondAttribute="trailing" constant="6" id="PhI-J3-Ycb"/>
<constraint firstItem="GcG-W8-9LR" firstAttribute="top" secondItem="TFL-sS-eJc" secondAttribute="top" id="Twp-gS-w3u"/>
<constraint firstAttribute="bottom" secondItem="9wW-1f-f69" secondAttribute="bottom" id="VG5-XU-DAK"/>
<constraint firstAttribute="trailing" secondItem="DVT-JI-3kw" secondAttribute="trailing" constant="8" id="bX2-Ha-8bf"/>
<constraint firstItem="DVT-JI-3kw" firstAttribute="leading" secondItem="9wW-1f-f69" secondAttribute="trailing" constant="8" id="qGg-0A-C6M"/>
<constraint firstItem="9wW-1f-f69" firstAttribute="leading" secondItem="GcG-W8-9LR" secondAttribute="trailing" constant="8" id="s2V-X9-cyI"/>
<constraint firstAttribute="bottom" secondItem="vva-PV-3Ya" secondAttribute="bottom" constant="3" id="smY-cv-CoE"/>
<constraint firstItem="vva-PV-3Ya" firstAttribute="leading" secondItem="TFL-sS-eJc" secondAttribute="leading" constant="1" id="vyh-e4-Vy3"/>
</constraints>
</view>
</subviews>
<viewLayoutGuide key="safeArea" id="vUN-kp-3ea"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<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"/>
<constraint firstItem="TFL-sS-eJc" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" constant="8" id="lbH-ay-p0c"/>
<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>
</resources>
</document>