mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
3.2 KiB
Swift
110 lines
3.2 KiB
Swift
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
|
|
|
import UIKit
|
|
import SessionUIKit
|
|
|
|
final class InfoBanner: UIView {
|
|
public struct Info: Equatable, Hashable {
|
|
let message: String
|
|
let backgroundColor: ThemeValue
|
|
let messageFont: UIFont
|
|
let messageTintColor: ThemeValue
|
|
let height: CGFloat
|
|
|
|
// MARK: - Confirmance
|
|
|
|
public static func == (lhs: InfoBanner.Info, rhs: InfoBanner.Info) -> Bool {
|
|
return (
|
|
lhs.message == rhs.message &&
|
|
lhs.backgroundColor == rhs.backgroundColor &&
|
|
lhs.messageFont == rhs.messageFont &&
|
|
lhs.messageTintColor == rhs.messageTintColor &&
|
|
lhs.height == rhs.height
|
|
)
|
|
}
|
|
|
|
public func hash(into hasher: inout Hasher) {
|
|
message.hash(into: &hasher)
|
|
backgroundColor.hash(into: &hasher)
|
|
messageFont.hash(into: &hasher)
|
|
messageTintColor.hash(into: &hasher)
|
|
height.hash(into: &hasher)
|
|
}
|
|
|
|
func with(
|
|
message: String? = nil,
|
|
backgroundColor: ThemeValue? = nil,
|
|
messageFont: UIFont? = nil,
|
|
messageTintColor: ThemeValue? = nil,
|
|
height: CGFloat? = nil
|
|
) -> Info {
|
|
return Info(
|
|
message: message ?? self.message,
|
|
backgroundColor: backgroundColor ?? self.backgroundColor,
|
|
messageFont: messageFont ?? self.messageFont,
|
|
messageTintColor: messageTintColor ?? self.messageTintColor,
|
|
height: height ?? self.height
|
|
)
|
|
}
|
|
}
|
|
|
|
private lazy var label: UILabel = {
|
|
let result: UILabel = UILabel()
|
|
result.textAlignment = .center
|
|
result.lineBreakMode = .byWordWrapping
|
|
result.numberOfLines = 0
|
|
|
|
return result
|
|
}()
|
|
|
|
public var info: Info?
|
|
|
|
// MARK: - Initialization
|
|
|
|
init(info: Info) {
|
|
super.init(frame: CGRect.zero)
|
|
addSubview(label)
|
|
label.pin(to: self)
|
|
self.set(.height, to: info.height)
|
|
self.update(info)
|
|
}
|
|
|
|
override init(frame: CGRect) {
|
|
preconditionFailure("Use init(message:) instead.")
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
preconditionFailure("Use init(coder:) instead.")
|
|
}
|
|
|
|
// MARK: Update
|
|
|
|
private func update(_ info: InfoBanner.Info) {
|
|
self.info = info
|
|
|
|
themeBackgroundColor = info.backgroundColor
|
|
|
|
label.font = info.messageFont
|
|
label.text = info.message
|
|
label.themeTextColor = info.messageTintColor
|
|
}
|
|
|
|
public func update(
|
|
message: String? = nil,
|
|
backgroundColor: ThemeValue? = nil,
|
|
messageFont: UIFont? = nil,
|
|
messageTintColor: ThemeValue? = nil,
|
|
height: CGFloat? = nil
|
|
) {
|
|
if let updatedInfo = self.info?.with(
|
|
message: message,
|
|
backgroundColor: backgroundColor,
|
|
messageFont: messageFont,
|
|
messageTintColor: messageTintColor,
|
|
height: height
|
|
) {
|
|
self.update(updatedInfo)
|
|
}
|
|
}
|
|
}
|