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
|
|
|
|
|
|
2020-06-05 15:32:10 -07:00
|
|
|
extension TimeInterval
|
|
|
|
|
{
|
|
|
|
|
static let shortToastViewDuration = 4.0
|
|
|
|
|
static let longToastViewDuration = 8.0
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-19 16:42:40 -07:00
|
|
|
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-06-05 15:32:10 -07:00
|
|
|
self.preferredDuration = .shortToastViewDuration
|
2020-01-24 14:14:08 -08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-06-05 15:32:10 -07:00
|
|
|
self.preferredDuration = .longToastViewDuration
|
2020-01-24 14:14:08 -08:00
|
|
|
}
|
|
|
|
|
|
2019-07-24 12:23:54 -07:00
|
|
|
super.init(text: text, detailText: detailedText)
|
|
|
|
|
|
2020-08-27 15:24:26 -07:00
|
|
|
self.isAccessibilityElement = true
|
|
|
|
|
|
2020-03-19 15:02:35 -07:00
|
|
|
self.layoutMargins = UIEdgeInsets(top: 8, left: 16, bottom: 10, right: 16)
|
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.
|
2020-03-19 15:02:35 -07:00
|
|
|
stackView.spacing = (detailedText != nil) ? 4.0 : 0.0
|
2020-03-11 13:35:14 -07:00
|
|
|
}
|
2020-01-24 14:14:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
convenience init(error: Error)
|
|
|
|
|
{
|
2020-06-05 15:32:10 -07:00
|
|
|
var error = error as NSError
|
|
|
|
|
var underlyingError = error.userInfo[NSUnderlyingErrorKey] as? NSError
|
|
|
|
|
|
|
|
|
|
var preferredDuration: TimeInterval?
|
|
|
|
|
|
|
|
|
|
if
|
|
|
|
|
let unwrappedUnderlyingError = underlyingError,
|
|
|
|
|
error.domain == AltServerErrorDomain && error.code == ALTServerError.Code.underlyingError.rawValue
|
|
|
|
|
{
|
|
|
|
|
// Treat underlyingError as the primary error.
|
|
|
|
|
|
|
|
|
|
error = unwrappedUnderlyingError
|
|
|
|
|
underlyingError = nil
|
|
|
|
|
|
|
|
|
|
preferredDuration = .longToastViewDuration
|
|
|
|
|
}
|
2020-03-11 13:35:14 -07:00
|
|
|
|
|
|
|
|
let text: String
|
|
|
|
|
let detailText: String?
|
|
|
|
|
|
|
|
|
|
if let failure = error.localizedFailure
|
|
|
|
|
{
|
|
|
|
|
text = failure
|
2020-06-05 15:32:10 -07:00
|
|
|
detailText = error.localizedFailureReason ?? error.localizedRecoverySuggestion ?? underlyingError?.localizedDescription ?? error.localizedDescription
|
2020-03-11 13:35:14 -07:00
|
|
|
}
|
|
|
|
|
else if let reason = error.localizedFailureReason
|
2020-01-24 14:14:08 -08:00
|
|
|
{
|
2020-03-11 13:35:14 -07:00
|
|
|
text = reason
|
2020-06-05 15:32:10 -07:00
|
|
|
detailText = error.localizedRecoverySuggestion ?? underlyingError?.localizedDescription
|
2020-01-24 14:14:08 -08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-03-11 13:35:14 -07:00
|
|
|
text = error.localizedDescription
|
2020-08-27 16:23:50 -07:00
|
|
|
detailText = underlyingError?.localizedDescription ?? error.localizedRecoverySuggestion
|
2020-01-24 14:14:08 -08:00
|
|
|
}
|
2020-03-11 13:35:14 -07:00
|
|
|
|
|
|
|
|
self.init(text: text, detailText: detailText)
|
2020-06-05 15:32:10 -07:00
|
|
|
|
|
|
|
|
if let preferredDuration = preferredDuration
|
|
|
|
|
{
|
|
|
|
|
self.preferredDuration = preferredDuration
|
|
|
|
|
}
|
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.
|
2020-03-19 15:02:35 -07:00
|
|
|
let minimumHeight = self.textLabel.font.lineHeight.rounded() + 18
|
2020-03-11 13:35:14 -07:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-27 15:24:26 -07:00
|
|
|
override func show(in view: UIView, duration: TimeInterval)
|
|
|
|
|
{
|
|
|
|
|
super.show(in: view, duration: duration)
|
|
|
|
|
|
|
|
|
|
let announcement = (self.textLabel.text ?? "") + ". " + (self.detailTextLabel.text ?? "")
|
|
|
|
|
self.accessibilityLabel = announcement
|
|
|
|
|
|
|
|
|
|
// Minimum 0.75 delay to prevent announcement being cut off by VoiceOver.
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.75) {
|
|
|
|
|
UIAccessibility.post(notification: .announcement, argument: announcement)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-24 14:14:08 -08:00
|
|
|
override func show(in view: UIView)
|
|
|
|
|
{
|
|
|
|
|
self.show(in: view, duration: self.preferredDuration)
|
|
|
|
|
}
|
2019-07-19 16:42:40 -07:00
|
|
|
}
|