mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-20 00:24:43 +02:00
Move tile action button to common
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
//
|
||||
// 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 UIKit
|
||||
|
||||
enum CallTileActionButtonStyle {
|
||||
case positive
|
||||
case negative
|
||||
case custom(bgColor: UIColor, tintColor: UIColor)
|
||||
}
|
||||
|
||||
class CallTileActionButton: UIButton {
|
||||
|
||||
// MARK: - Constants
|
||||
|
||||
private enum Constants {
|
||||
static let cornerRadius: CGFloat = 8.0
|
||||
static let fontSize: CGFloat = 13.0
|
||||
static let contentEdgeInsets: UIEdgeInsets = UIEdgeInsets(top: 8, left: 12, bottom: 8, right: 12)
|
||||
static let spaceBetweenImageAndTitle: CGFloat = 8
|
||||
static let imageSize: CGSize = CGSize(width: 16, height: 16)
|
||||
}
|
||||
|
||||
private var theme: Theme = ThemeService.shared().theme {
|
||||
didSet {
|
||||
updateStyle()
|
||||
}
|
||||
}
|
||||
|
||||
var style: CallTileActionButtonStyle = .positive {
|
||||
didSet {
|
||||
updateStyle()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Setup
|
||||
|
||||
override init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
self.commonInit()
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
super.init(coder: coder)
|
||||
self.commonInit()
|
||||
}
|
||||
|
||||
private func commonInit() {
|
||||
self.contentEdgeInsets = Constants.contentEdgeInsets
|
||||
self.layer.masksToBounds = true
|
||||
self.titleLabel?.font = UIFont.systemFont(ofSize: Constants.fontSize)
|
||||
self.layer.cornerRadius = Constants.cornerRadius
|
||||
setImage(image(for: .normal)?.vc_resized(with: Constants.imageSize)?.withRenderingMode(.alwaysTemplate), for: .normal)
|
||||
updateStyle()
|
||||
}
|
||||
|
||||
private func updateStyle() {
|
||||
switch style {
|
||||
case .positive:
|
||||
self.vc_setBackgroundColor(theme.tintColor, for: .normal)
|
||||
self.tintColor = theme.baseTextPrimaryColor
|
||||
case .negative:
|
||||
self.vc_setBackgroundColor(theme.noticeColor, for: .normal)
|
||||
self.tintColor = theme.baseTextPrimaryColor
|
||||
case .custom(let bgColor, let tintColor):
|
||||
self.vc_setBackgroundColor(bgColor, for: .normal)
|
||||
self.tintColor = tintColor
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Overrides
|
||||
|
||||
override func setImage(_ image: UIImage?, for state: UIControl.State) {
|
||||
super.setImage(image?.vc_resized(with: Constants.imageSize)?.withRenderingMode(.alwaysTemplate),
|
||||
for: state)
|
||||
}
|
||||
|
||||
override var intrinsicContentSize: CGSize {
|
||||
var result = super.intrinsicContentSize
|
||||
result.width += Constants.spaceBetweenImageAndTitle
|
||||
return result
|
||||
}
|
||||
|
||||
override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
|
||||
var result = super.imageRect(forContentRect: contentRect)
|
||||
result.origin.x -= Constants.spaceBetweenImageAndTitle/2
|
||||
return result
|
||||
}
|
||||
|
||||
override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
|
||||
var result = super.titleRect(forContentRect: contentRect)
|
||||
result.origin.x += Constants.spaceBetweenImageAndTitle/2
|
||||
return result
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension CallTileActionButton: Themable {
|
||||
|
||||
func update(theme: Theme) {
|
||||
self.theme = theme
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user