element-ios/Riot/Modules/Room/Views/Title/Thread/ThreadRoomTitleView.swift

150 lines
4.9 KiB
Swift
Raw Normal View History

2021-11-10 22:05:03 +00:00
//
// 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
2021-11-19 13:32:42 +00:00
import Reusable
enum ThreadRoomTitleViewMode {
case allThreads
case specificThread(threadId: String)
}
2021-11-10 22:05:03 +00:00
@objcMembers
class ThreadRoomTitleView: RoomTitleView {
private enum Constants {
static let titleLeadingConstraintOnPortrait: CGFloat = 6
static let titleLeadingConstraintOnLandscape: CGFloat = 18
}
2021-11-19 13:32:42 +00:00
var mode: ThreadRoomTitleViewMode = .allThreads {
2021-11-10 22:05:03 +00:00
didSet {
update()
}
}
2021-11-19 13:32:42 +00:00
@IBOutlet private weak var titleLabel: UILabel!
@IBOutlet private weak var titleLabelLeadingConstraint: NSLayoutConstraint!
2021-11-19 13:32:42 +00:00
@IBOutlet private weak var roomAvatarView: RoomAvatarView!
@IBOutlet private weak var roomEncryptionBadgeView: UIImageView!
@IBOutlet private weak var roomNameLabel: UILabel!
// MARK: - Methods
2022-01-25 20:59:19 +00:00
func configure(withViewModel viewModel: ThreadRoomTitleModel) {
2021-11-19 13:32:42 +00:00
if let avatarViewData = viewModel.roomAvatar {
roomAvatarView.fill(with: avatarViewData)
} else {
roomAvatarView.avatarImageView.image = nil
}
2021-11-19 13:32:42 +00:00
roomEncryptionBadgeView.image = viewModel.roomEncryptionBadge
roomEncryptionBadgeView.isHidden = viewModel.roomEncryptionBadge == nil
roomNameLabel.text = viewModel.roomDisplayName
}
2021-11-10 22:05:03 +00:00
2021-11-19 13:32:42 +00:00
// MARK: - Overrides
2021-11-10 22:05:03 +00:00
override var mxRoom: MXRoom! {
didSet {
2021-11-19 13:32:42 +00:00
update()
}
}
2021-11-10 22:05:03 +00:00
override class func nib() -> UINib! {
2021-11-19 13:32:42 +00:00
return self.nib
2021-11-10 22:05:03 +00:00
}
override func refreshDisplay() {
guard let room = mxRoom else {
// room not initialized yet
return
}
let avatarViewData = AvatarViewData(matrixItemId: room.matrixItemId,
displayName: room.displayName,
avatarUrl: room.mxContentUri,
mediaManager: room.mxSession.mediaManager,
fallbackImage: AvatarFallbackImage.matrixItem(room.matrixItemId,
room.displayName))
2021-11-19 11:19:23 +00:00
2021-11-19 13:32:42 +00:00
let encrpytionBadge: UIImage?
if let summary = room.summary, room.mxSession.crypto != nil {
encrpytionBadge = EncryptionTrustLevelBadgeImageHelper.roomBadgeImage(for: summary.roomEncryptionTrustLevel())
2021-11-19 11:19:23 +00:00
} else {
2021-11-19 13:32:42 +00:00
encrpytionBadge = nil
2021-11-19 11:19:23 +00:00
}
2021-11-19 13:32:42 +00:00
2022-01-25 20:59:19 +00:00
let viewModel = ThreadRoomTitleModel(roomAvatar: avatarViewData,
roomEncryptionBadge: encrpytionBadge,
roomDisplayName: room.displayName)
2021-11-19 13:32:42 +00:00
configure(withViewModel: viewModel)
2021-11-10 22:05:03 +00:00
}
override func awakeFromNib() {
super.awakeFromNib()
update(theme: ThemeService.shared().theme)
2021-11-19 11:19:23 +00:00
registerThemeServiceDidChangeThemeNotification()
2021-11-10 22:05:03 +00:00
}
override func updateLayout(for orientation: UIInterfaceOrientation) {
super.updateLayout(for: orientation)
if orientation.isPortrait {
titleLabelLeadingConstraint.constant = Constants.titleLeadingConstraintOnPortrait
} else {
titleLabelLeadingConstraint.constant = Constants.titleLeadingConstraintOnLandscape
2021-11-10 22:05:03 +00:00
}
}
// MARK: - Private
2021-11-19 11:19:23 +00:00
private func registerThemeServiceDidChangeThemeNotification() {
NotificationCenter.default.addObserver(self,
selector: #selector(themeDidChange),
name: .themeServiceDidChangeTheme,
object: nil)
}
2021-11-10 22:05:03 +00:00
private func update() {
switch mode {
2021-11-19 13:32:42 +00:00
case .allThreads:
titleLabel.text = VectorL10n.threadsTitle
case .specificThread:
titleLabel.text = VectorL10n.roomThreadTitle
2021-11-10 22:05:03 +00:00
}
}
// MARK: - Actions
2021-11-19 11:19:23 +00:00
@objc private func themeDidChange() {
self.update(theme: ThemeService.shared().theme)
2021-11-10 22:05:03 +00:00
}
2021-11-10 22:05:03 +00:00
}
2021-11-19 13:32:42 +00:00
extension ThreadRoomTitleView: NibLoadable {}
2021-11-10 22:05:03 +00:00
extension ThreadRoomTitleView: Themable {
func update(theme: Theme) {
2021-11-19 13:32:42 +00:00
roomAvatarView.backgroundColor = .clear
titleLabel.textColor = theme.colors.primaryContent
roomNameLabel.textColor = theme.colors.secondaryContent
2021-11-10 22:05:03 +00:00
}
}