session-ios/Session/Shared/ReminderView.swift

122 lines
3.6 KiB
Swift
Raw Normal View History

2017-05-05 18:39:21 +02:00
//
2019-03-30 15:50:52 +01:00
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
2017-05-05 18:39:21 +02:00
//
import Foundation
import SessionUIKit
2017-05-05 18:39:21 +02:00
class ReminderView: UIView {
2017-05-05 18:39:21 +02:00
let label = UILabel()
2018-06-19 18:18:06 +02:00
typealias Action = () -> Void
2017-05-05 18:39:21 +02:00
2018-06-19 18:19:19 +02:00
var tapAction: Action?
2017-05-05 18:39:21 +02:00
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) {
2018-08-27 16:21:03 +02:00
notImplemented()
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) {
2018-08-27 16:21:03 +02:00
notImplemented()
}
2017-05-05 18:39:21 +02:00
private init(mode: ReminderViewMode,
2018-06-19 18:18:06 +02:00
text: String, tapAction: Action?) {
self.mode = mode
self.tapAction = tapAction
super.init(frame: .zero)
self.text = text
2017-05-05 18:39:21 +02:00
setupSubviews()
}
2018-06-19 18:18:06 +02:00
@objc public class func nag(text: String, tapAction: Action?) -> ReminderView {
return ReminderView(mode: .nag, text: text, tapAction: tapAction)
}
@objc public class func explanation(text: String) -> ReminderView {
2018-06-19 18:18:06 +02:00
return ReminderView(mode: .explanation, text: text, tapAction: nil)
2017-05-05 18:39:21 +02:00
}
func setupSubviews() {
2018-09-21 17:35:55 +02:00
let textColor: UIColor
let iconColor: UIColor
2019-03-30 15:50:52 +01:00
switch mode {
case .nag:
self.backgroundColor = UIColor.ows_reminderYellow
2018-09-21 17:35:55 +02:00
textColor = UIColor.ows_gray90
iconColor = UIColor.ows_gray60
case .explanation:
2018-07-13 00:22:25 +02:00
// TODO: Theme, review with design.
self.backgroundColor = Colors.unimportant
textColor = Colors.text
iconColor = Colors.separator
}
2017-05-05 18:39:21 +02:00
self.clipsToBounds = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(gestureRecognizer:)))
self.addGestureRecognizer(tapGesture)
2018-06-22 19:00:19 +02:00
let container = UIStackView()
container.axis = .horizontal
container.alignment = .center
container.isLayoutMarginsRelativeArrangement = true
2017-05-05 18:39:21 +02:00
self.addSubview(container)
2018-06-22 19:00:19 +02:00
container.layoutMargins = UIEdgeInsets(top: 12, left: 16, bottom: 12, right: 16)
container.ows_autoPinToSuperviewEdges()
2017-05-05 18:39:21 +02:00
// Label
2018-06-20 21:15:33 +02:00
label.font = UIFont.ows_dynamicTypeSubheadline
2018-06-22 19:00:19 +02:00
container.addArrangedSubview(label)
2018-09-21 17:35:55 +02:00
label.textColor = textColor
2017-05-05 18:39:21 +02:00
label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping
2017-05-05 18:39:21 +02:00
2018-06-19 18:12:37 +02:00
// Show the disclosure indicator if this reminder has a tap action.
2018-06-22 19:00:19 +02:00
if tapAction != nil {
// Icon
2018-06-29 23:00:22 +02:00
let iconName = (CurrentAppContext().isRTL ? "system_disclosure_indicator_rtl" : "system_disclosure_indicator")
2018-06-22 19:00:19 +02:00
guard let iconImage = UIImage(named: iconName) else {
2018-08-27 16:27:48 +02:00
owsFailDebug("missing icon.")
2018-06-22 19:00:19 +02:00
return
}
let iconView = UIImageView(image: iconImage.withRenderingMode(.alwaysTemplate))
iconView.contentMode = .scaleAspectFit
2018-09-21 17:35:55 +02:00
iconView.tintColor = iconColor
2018-06-22 19:00:19 +02:00
iconView.autoSetDimension(.width, toSize: 13)
container.addArrangedSubview(iconView)
2018-05-04 19:34:11 +02:00
}
2017-05-05 18:39:21 +02:00
}
2018-05-25 18:54:25 +02:00
@objc func handleTap(gestureRecognizer: UIGestureRecognizer) {
2018-06-19 18:18:06 +02:00
guard gestureRecognizer.state == .recognized else {
return
}
tapAction?()
2017-05-05 18:39:21 +02:00
}
}