session-ios/Signal/src/views/ReminderView.swift

132 lines
3.9 KiB
Swift
Raw Normal View History

2017-05-05 18:39:21 +02:00
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
2017-05-05 18:39:21 +02:00
//
import Foundation
class ReminderView: UIView {
2017-05-05 18:39:21 +02:00
let TAG = "[ReminderView]"
2017-05-05 18:39:21 +02:00
let label = UILabel()
static let defaultTapAction = {
Logger.debug("[ReminderView] tapped.")
2017-05-05 18:39:21 +02:00
}
var tapAction: () -> Void
var text: String? {
get {
return label.text
}
set(newText) {
label.text = newText
}
}
enum ReminderViewMode {
// Nags are urgent interactive prompts, bidding for the user's attention.
case nag
// Explanations are not interactive or urgent.
case explanation
}
let mode: ReminderViewMode
2017-05-05 18:39:21 +02:00
@available(*, unavailable, message:"use other constructor instead.")
required init?(coder aDecoder: NSCoder) {
fatalError("\(#function) is unimplemented.")
2017-05-05 18:39:21 +02:00
}
@available(*, unavailable, message:"use other constructor instead.")
2017-05-05 18:39:21 +02:00
override init(frame: CGRect) {
fatalError("\(#function) is unimplemented.")
}
2017-05-05 18:39:21 +02:00
private init(mode: ReminderViewMode,
text: String, tapAction: @escaping () -> Void) {
self.mode = mode
self.tapAction = tapAction
super.init(frame: .zero)
self.text = text
2017-05-05 18:39:21 +02:00
setupSubviews()
}
@objc public class func nag(text: String, tapAction: @escaping () -> Void) -> ReminderView {
return ReminderView(mode: .nag, text: text, tapAction: tapAction)
}
@objc public class func explanation(text: String) -> ReminderView {
return ReminderView(mode: .explanation, text: text, tapAction: ReminderView.defaultTapAction)
2017-05-05 18:39:21 +02:00
}
func setupSubviews() {
switch (mode) {
case .nag:
self.backgroundColor = UIColor.ows_reminderYellow
case .explanation:
self.backgroundColor = UIColor(rgbHex: 0xf5f5f5)
}
2017-05-05 18:39:21 +02:00
self.clipsToBounds = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(gestureRecognizer:)))
self.addGestureRecognizer(tapGesture)
let container = UIView()
2017-05-05 18:39:21 +02:00
self.addSubview(container)
container.autoPinWidthToSuperview(withMargin: 16)
switch (mode) {
case .nag:
container.autoPinHeightToSuperview(withMargin: 16)
case .explanation:
container.autoPinHeightToSuperview(withMargin: 12)
}
// Margin: top and bottom 12 left and right 16.
2017-05-05 18:39:21 +02:00
// Label
switch (mode) {
case .nag:
label.font = UIFont.ows_regularFont(withSize: 14)
case .explanation:
label.font = UIFont.ows_dynamicTypeSubheadline
}
2017-05-05 18:39:21 +02:00
container.addSubview(label)
label.textColor = UIColor.black.withAlphaComponent(0.9)
2017-05-05 18:39:21 +02:00
label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping
label.autoPinLeadingToSuperviewMargin()
label.autoPinEdge(toSuperviewEdge: .top)
2017-05-05 18:39:21 +02:00
label.autoPinEdge(toSuperviewEdge: .bottom)
guard mode == .nag else {
label.autoPinTrailingToSuperviewMargin()
return
}
2017-05-05 18:39:21 +02:00
// Icon
let iconName = (self.isRTL() ? "system_disclosure_indicator_rtl" : "system_disclosure_indicator")
2018-05-04 19:34:11 +02:00
guard let iconImage = UIImage(named: iconName) else {
owsFail("\(logTag) missing icon.")
return
}
let iconView = UIImageView(image: iconImage.withRenderingMode(.alwaysTemplate))
2017-05-05 18:39:21 +02:00
iconView.contentMode = .scaleAspectFit
iconView.tintColor = UIColor.black.withAlphaComponent(0.6)
container.addSubview(iconView)
iconView.autoPinLeading(toTrailingEdgeOf: label, offset: 28)
iconView.autoPinTrailingToSuperviewMargin()
2017-05-05 18:39:21 +02:00
iconView.autoVCenterInSuperview()
iconView.autoSetDimension(.width, toSize: 13)
}
func handleTap(gestureRecognizer: UIGestureRecognizer) {
tapAction()
}
}