2019-07-19 16:42:40 -07:00
|
|
|
//
|
|
|
|
|
// ToastView.swift
|
|
|
|
|
// AltStore
|
|
|
|
|
//
|
|
|
|
|
// Created by Riley Testut on 7/19/19.
|
|
|
|
|
// Copyright © 2019 Riley Testut. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Roxas
|
|
|
|
|
|
|
|
|
|
class ToastView: RSTToastView
|
|
|
|
|
{
|
2020-01-24 14:14:08 -08:00
|
|
|
var preferredDuration: TimeInterval
|
|
|
|
|
|
2019-07-24 12:23:54 -07:00
|
|
|
override init(text: String, detailText detailedText: String?)
|
|
|
|
|
{
|
2020-01-24 14:14:08 -08:00
|
|
|
if detailedText == nil
|
|
|
|
|
{
|
2020-03-11 13:35:14 -07:00
|
|
|
self.preferredDuration = 4.0
|
2020-01-24 14:14:08 -08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
self.preferredDuration = 8.0
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-24 12:23:54 -07:00
|
|
|
super.init(text: text, detailText: detailedText)
|
|
|
|
|
|
2020-03-11 13:35:14 -07:00
|
|
|
self.layoutMargins = UIEdgeInsets(top: 8, left: 12, bottom: 8, right: 12)
|
2020-01-24 14:14:08 -08:00
|
|
|
self.setNeedsLayout()
|
2020-03-11 13:35:14 -07:00
|
|
|
|
|
|
|
|
if let stackView = self.textLabel.superview as? UIStackView
|
|
|
|
|
{
|
|
|
|
|
// RSTToastView does not expose stack view containing labels,
|
|
|
|
|
// so we access it indirectly as the labels' superview.
|
|
|
|
|
stackView.spacing = 4.0
|
|
|
|
|
}
|
2020-01-24 14:14:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
convenience init(error: Error)
|
|
|
|
|
{
|
2020-03-11 13:35:14 -07:00
|
|
|
let error = error as NSError
|
|
|
|
|
|
|
|
|
|
let text: String
|
|
|
|
|
let detailText: String?
|
|
|
|
|
|
|
|
|
|
if let failure = error.localizedFailure
|
|
|
|
|
{
|
|
|
|
|
text = failure
|
|
|
|
|
detailText = error.localizedFailureReason ?? error.localizedRecoverySuggestion
|
|
|
|
|
}
|
|
|
|
|
else if let reason = error.localizedFailureReason
|
2020-01-24 14:14:08 -08:00
|
|
|
{
|
2020-03-11 13:35:14 -07:00
|
|
|
text = reason
|
|
|
|
|
detailText = error.localizedRecoverySuggestion
|
2020-01-24 14:14:08 -08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-03-11 13:35:14 -07:00
|
|
|
text = error.localizedDescription
|
|
|
|
|
detailText = nil
|
2020-01-24 14:14:08 -08:00
|
|
|
}
|
2020-03-11 13:35:14 -07:00
|
|
|
|
|
|
|
|
self.init(text: text, detailText: detailText)
|
2019-07-24 12:23:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
required init(coder aDecoder: NSCoder) {
|
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-19 16:42:40 -07:00
|
|
|
override func layoutSubviews()
|
|
|
|
|
{
|
|
|
|
|
super.layoutSubviews()
|
|
|
|
|
|
2020-03-11 13:35:14 -07:00
|
|
|
// Rough calculation to determine height of ToastView with one-line textLabel.
|
|
|
|
|
let minimumHeight = self.textLabel.font.lineHeight.rounded() + 20
|
|
|
|
|
self.layer.cornerRadius = minimumHeight/2
|
2019-07-19 16:42:40 -07:00
|
|
|
}
|
2020-01-24 14:14:08 -08:00
|
|
|
|
|
|
|
|
func show(in viewController: UIViewController)
|
|
|
|
|
{
|
|
|
|
|
self.show(in: viewController.navigationController?.view ?? viewController.view, duration: self.preferredDuration)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func show(in view: UIView)
|
|
|
|
|
{
|
|
|
|
|
self.show(in: view, duration: self.preferredDuration)
|
|
|
|
|
}
|
2019-07-19 16:42:40 -07:00
|
|
|
}
|