Add expectedWidth to MUToast to have width control on iPad (and center the toast + clean a lonely warning)

This commit is contained in:
Damien 2020-03-27 19:05:17 +01:00
parent 8237cb4669
commit ed4f02acc3
3 changed files with 52 additions and 39 deletions

View File

@ -40,6 +40,13 @@ extension ToastsVC: MUButtonDelegate {
func didTap(button: MUButton) {
counter += 1
let expectedWidth: CGFloat?
if UIDevice.current.userInterfaceIdiom == .pad {
expectedWidth = UIScreen.main.bounds.width * 0.25
} else {
expectedWidth = nil
}
switch button {
case topInfoButton:
let toast = MUToast()
@ -51,6 +58,7 @@ extension ToastsVC: MUButtonDelegate {
toast.cornerRadius = 10.0
toast.spacing = 0.0
toast.icon = #imageLiteral(resourceName: "walkthrough_sleep")
toast.expectedWidth = expectedWidth
toast.show(in: self)
case topWarningButton:
let toast = MUToast()
@ -63,11 +71,13 @@ extension ToastsVC: MUButtonDelegate {
toast.cornerRadius = 0.0
toast.spacing = 2.0
toast.icon = #imageLiteral(resourceName: "walkthrough_sleep")
toast.expectedWidth = expectedWidth
toast.show(in: self)
case topAlertButton:
let toast = redToast()
toast.title = "LONG ALERT TITLE"
toast.detail = "ID: \(counter) You have to read this now!"
toast.expectedWidth = expectedWidth
toast.show(in: self, onTap: {
print("Tapped")
toast.hide(completion: { succeed in
@ -87,13 +97,27 @@ extension ToastsVC: MUButtonDelegate {
toast.cornerRadius = 10.0
toast.spacing = 0.0
toast.icon = #imageLiteral(resourceName: "walkthrough_sleep")
toast.expectedWidth = expectedWidth
toast.show(in: self)
case bottomWarningButton:
print("TODO")
let toast = MUToast()
toast.displayPosition = .bottom
toast.displayPriority = .warning
toast.title = "WARNING"
toast.detail = "ID: \(counter) Long description dgfe dfgdfjhgdf fg dfg ffdfg dfgfdg dfgdfgdfjgshl fd fkgdflkgd dfgj dfjg"
toast.titleColor = .black
toast.detailColor = .black
toast.backgroundColor = .orange
toast.cornerRadius = 0.0
toast.spacing = 2.0
toast.icon = #imageLiteral(resourceName: "walkthrough_sleep")
toast.expectedWidth = expectedWidth
toast.show(in: self)
case bottomAlertButton:
let toast = redToast()
toast.displayPosition = .bottom
toast.title = "Shutdown in 2 minutes"
toast.expectedWidth = expectedWidth
toast.show(in: self)
default:
print("INVALID BUTTON")

View File

@ -247,9 +247,7 @@ open class MUBarChart: MUNibView {
addBars(for: data, in: radiusView, bkgView: bkgView)
datasStackView.addArrangedSubview(bkgView)
guard showTotalValue, type == .stacked else {
return
}
guard showTotalValue, type == .stacked else { return }
addTotalValueLabel(to: bkgView, stackTo: radiusView, value: data.totalValue)
}

View File

@ -11,7 +11,7 @@ import Neumann
/// Class that define a card (title, description, indicator and image) with screen animation.
@IBDesignable
open class MUToast: MUNibView { // swiftlint:disable:this type_body_length
open class MUToast: MUNibView {
@IBOutlet private var markerView: UIView!
@IBOutlet private var imageView: UIImageView!
@IBOutlet private var header: MUHeader!
@ -214,10 +214,13 @@ open class MUToast: MUNibView { // swiftlint:disable:this type_body_length
// MARK: - Toast layout
/// The toasts horizontal padding.
@IBInspectable open var horizontalPadding: CGFloat = 16.0
@IBInspectable open dynamic var horizontalPadding: CGFloat = 16.0
/// The toasts vertical padding.
@IBInspectable open var verticalPadding: CGFloat = 16.0
@IBInspectable open dynamic var verticalPadding: CGFloat = 16.0
/// The toasts expected width. If nil it will take the safeArea.width.
open dynamic var expectedWidth: CGFloat?
// MARK: - Buttons layout
@ -253,9 +256,7 @@ open class MUToast: MUNibView { // swiftlint:disable:this type_body_length
add(in: vc)
showingAnimation(completion: { [weak self] _ in
guard let displayDuration = self?.displayDuration, displayDuration > 0 else {
return
}
guard let displayDuration = self?.displayDuration, displayDuration > 0 else { return }
DispatchQueue.main.asyncAfter(deadline: .now() + displayDuration, execute: {
self?.hidingAnimation(completion)
@ -305,7 +306,8 @@ open class MUToast: MUNibView { // swiftlint:disable:this type_body_length
imageWidth.constant = icon == nil ? 0.0 : iconWidth
}
private func expectedSize(in width: CGFloat) -> CGSize {
private func expectedSize() -> CGSize {
guard let width = expectedWidth else { return .zero }
var size = CGSize(width: width - 2.0 * horizontalPadding, height: 0.0)
var headerWidth = size.width - 2.0 * headerHorizontalPadding
@ -329,7 +331,10 @@ open class MUToast: MUNibView { // swiftlint:disable:this type_body_length
private func add(in vc: UIViewController) {
let safeArea = vc.areaFrame
let size = expectedSize(in: safeArea.width)
if expectedWidth == nil {
expectedWidth = safeArea.width
}
let size = expectedSize()
let origin: CGFloat
if displayPosition == .top {
@ -340,36 +345,24 @@ open class MUToast: MUNibView { // swiftlint:disable:this type_body_length
transform = CGAffineTransform(translationX: 0.0, y: vc.view.frame.height - origin)
}
let toastView: UIView?
switch displayPriority {
case .alert:
break // Default case on top of all views
toastView = nil // Default case on top of all views
case .warning:
if let toastView = getLowestToast(in: vc.view, priorities: [.alert]) {
vc.view.insertAutolayoutSubview(self,
belowSubview: toastView,
top: origin,
height: nil,
leading: safeArea.origin.x + horizontalPadding,
width: size.width / vc.view.bounds.width)
return
}
toastView = getLowestToast(in: vc.view, priorities: [.alert])
case .info:
if let toastView = getLowestToast(in: vc.view, priorities: [.alert, .warning]) {
vc.view.insertAutolayoutSubview(self,
belowSubview: toastView,
top: origin,
height: nil,
leading: safeArea.origin.x + horizontalPadding,
width: size.width / vc.view.bounds.width)
return
}
toastView = getLowestToast(in: vc.view, priorities: [.alert, .warning])
}
vc.view.addAutolayoutSubview(self,
top: origin,
height: nil,
leading: safeArea.origin.x + horizontalPadding,
width: size.width / vc.view.bounds.width)
let leading = safeArea.origin.x + safeArea.size.width * 0.5 - size.width * 0.5
let width = size.width / vc.view.bounds.width
if let toastView = toastView {
vc.view.insertAutolayoutSubview(self, belowSubview: toastView,
top: origin, height: nil, leading: leading, width: width)
} else {
vc.view.addAutolayoutSubview(self, top: origin, height: nil, leading: leading, width: width)
}
}
private func getLowestToast(in vcView: UIView, priorities: [MUToastPriority]) -> UIView? {
@ -401,9 +394,7 @@ open class MUToast: MUNibView { // swiftlint:disable:this type_body_length
delay: 0,
options: .curveEaseOut,
animations: { [weak self] in
guard let strongSelf = self else {
return
}
guard let strongSelf = self else { return }
strongSelf.transform = strongSelf.hideTransform
}, completion: { [weak self] completed in
self?.onTapBlock = nil