2021-03-25 21:15:18 +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 UIKit
|
|
|
|
|
|
|
|
@objcMembers
|
2021-04-08 10:11:36 +00:00
|
|
|
class RoomActionsBar: UIScrollView, Themable {
|
2021-03-25 21:15:18 +00:00
|
|
|
// MARK: - Properties
|
|
|
|
|
|
|
|
var itemSpacing: CGFloat = 20 {
|
|
|
|
didSet {
|
|
|
|
self.setNeedsLayout()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-08 10:11:36 +00:00
|
|
|
var actionItems: [RoomActionItem] = [] {
|
2021-03-25 21:15:18 +00:00
|
|
|
didSet {
|
2021-04-08 10:11:36 +00:00
|
|
|
var actionButtons: [UIButton] = []
|
2021-03-25 21:15:18 +00:00
|
|
|
for (index, item) in actionItems.enumerated() {
|
|
|
|
let button = UIButton(type: .custom)
|
|
|
|
button.setImage(item.image, for: .normal)
|
|
|
|
button.addTarget(self, action: #selector(buttonAction(_:)), for: .touchUpInside)
|
|
|
|
button.tintColor = ThemeService.shared().theme.tintColor
|
|
|
|
button.tag = index
|
|
|
|
actionButtons.append(button)
|
|
|
|
addSubview(button)
|
|
|
|
}
|
|
|
|
self.actionButtons = actionButtons
|
|
|
|
self.lastBounds = .zero
|
|
|
|
self.setNeedsLayout()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-08 10:11:36 +00:00
|
|
|
private var actionButtons: [UIButton] = [] {
|
2021-03-25 21:15:18 +00:00
|
|
|
willSet {
|
|
|
|
for button in actionButtons {
|
|
|
|
button.removeFromSuperview()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var lastBounds = CGRect.zero
|
|
|
|
|
|
|
|
// MARK: - Lifecycle
|
|
|
|
|
|
|
|
override init(frame: CGRect) {
|
|
|
|
super.init(frame: frame)
|
2021-04-08 08:47:57 +00:00
|
|
|
setupView()
|
2021-03-25 21:15:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
super.init(coder: coder)
|
2021-04-08 08:48:15 +00:00
|
|
|
setupView()
|
2021-03-25 21:15:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func layoutSubviews() {
|
|
|
|
super.layoutSubviews()
|
|
|
|
|
|
|
|
guard lastBounds != self.bounds else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
lastBounds = self.bounds
|
|
|
|
|
|
|
|
var currentX: CGFloat = 0
|
|
|
|
for button in actionButtons {
|
2021-03-29 21:45:47 +00:00
|
|
|
button.transform = CGAffineTransform.identity
|
2021-03-25 21:15:18 +00:00
|
|
|
button.frame = CGRect(x: currentX, y: 0, width: self.bounds.height, height: self.bounds.height)
|
|
|
|
currentX = button.frame.maxX + itemSpacing
|
|
|
|
}
|
|
|
|
|
|
|
|
self.contentSize = CGSize(width: currentX - itemSpacing, height: self.bounds.height)
|
|
|
|
}
|
|
|
|
|
2021-04-08 10:11:36 +00:00
|
|
|
// MARK: - Themable
|
2021-03-25 21:15:18 +00:00
|
|
|
|
2021-04-08 10:11:36 +00:00
|
|
|
func update(theme: Theme) {
|
2021-03-25 21:15:18 +00:00
|
|
|
for button in actionButtons {
|
2021-04-08 10:11:36 +00:00
|
|
|
button.tintColor = theme.tintColor
|
2021-03-25 21:15:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-08 10:11:36 +00:00
|
|
|
// MARK: - Business methods
|
|
|
|
|
2021-03-25 21:15:18 +00:00
|
|
|
func animate(showIn: Bool, completion: ((Bool) -> Void)? = nil) {
|
|
|
|
if showIn {
|
|
|
|
for button in actionButtons {
|
|
|
|
button.transform = CGAffineTransform(translationX: 0, y: self.bounds.height)
|
|
|
|
}
|
|
|
|
for (index, button) in actionButtons.enumerated() {
|
2021-04-02 22:03:57 +00:00
|
|
|
UIView.animate(withDuration: 0.3, delay: 0.05 * Double(index), usingSpringWithDamping: 0.45, initialSpringVelocity: 11, options: .curveEaseInOut) {
|
2021-03-25 21:15:18 +00:00
|
|
|
button.transform = CGAffineTransform.identity
|
|
|
|
} completion: { (finished) in
|
|
|
|
completion?(finished)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (index, button) in actionButtons.enumerated() {
|
2021-04-02 22:03:57 +00:00
|
|
|
UIView.animate(withDuration: 0.25, delay: 0.05 * Double(index), options: .curveEaseInOut) {
|
2021-03-25 21:15:18 +00:00
|
|
|
button.transform = CGAffineTransform(translationX: 0, y: self.bounds.height)
|
|
|
|
} completion: { (finished) in
|
2021-04-02 22:03:57 +00:00
|
|
|
if index == self.actionButtons.count - 1 {
|
|
|
|
completion?(finished)
|
|
|
|
}
|
2021-03-25 21:15:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Private methods
|
|
|
|
|
|
|
|
@objc private func buttonAction(_ sender: UIButton) {
|
2021-04-08 08:47:42 +00:00
|
|
|
actionItems[sender.tag].action()
|
2021-03-25 21:15:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func setupView() {
|
|
|
|
self.showsHorizontalScrollIndicator = false
|
|
|
|
}
|
|
|
|
}
|