forked from arthurhammer/FrameGrabber
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlerts.swift
More file actions
177 lines (150 loc) · 6.16 KB
/
Alerts.swift
File metadata and controls
177 lines (150 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import UIKit
/// Static factories for UIAlertController alerts.
extension UIAlertController {
static func videoLoadingFailed(okHandler: ((UIAlertAction) -> ())? = nil) -> UIAlertController {
with(
title: Localized.alertVideoLoadFailedTitle,
message: Localized.alertVideoLoadFailedMessage,
okHandler: okHandler
)
}
static func playbackFailed(okHandler: ((UIAlertAction) -> ())? = nil) -> UIAlertController {
with(
title: Localized.alertPlaybackFailedTitle,
message: Localized.alertPlaybackFailedMessage,
okHandler: okHandler
)
}
static func filePickingFailed(okHandler: ((UIAlertAction) -> ())? = nil) -> UIAlertController {
with(
title: Localized.alertFilePickingFailedTitle,
message: Localized.alertFilePickingFailedMessage,
okHandler: okHandler
)
}
static func frameExportFailed(okHandler: ((UIAlertAction) -> ())? = nil) -> UIAlertController {
with(
title: Localized.alertFrameExportFailedTitle,
message: Localized.alertFrameExportFailedMessage,
okHandler: okHandler
)
}
static func savingToPhotosFailed(okHandler: ((UIAlertAction) -> ())? = nil) -> UIAlertController {
with(
title: Localized.alertFrameExportFailedTitle,
message: Localized.alertFrameExportFailedMessage,
okHandler: okHandler
)
}
static func videoRecordingUnavailable(okHandler: ((UIAlertAction) -> ())? = nil) -> UIAlertController {
with(
title: Localized.videoRecordingUnavailableTitle,
message: Localized.videoRecordingUnavailableMessage,
okHandler: okHandler
)
}
/// By default, the open settings action opens the app's Settings.
static func videoRecordingDenied(
okHandler: ((UIAlertAction) -> ())? = nil,
openSettingsHandler: ((UIAlertAction) -> ())? = { _ in UIApplication.shared.openSettings() }
) -> UIAlertController {
let alert = UIAlertController(
title: Localized.videoRecordingDeniedTitle,
message: Localized.videoRecordingDeniedMessage,
preferredStyle: .alert
)
alert.addAction(.openSettings(handler: openSettingsHandler))
alert.addAction(.ok())
return alert
}
static func recordingVideoFailed(okHandler: ((UIAlertAction) -> ())? = nil) -> UIAlertController {
with(
title: Localized.videoRecordingFailedTitle,
message: Localized.videoRecordingFailedMessage,
okHandler: okHandler
)
}
static func mailNotAvailable(contactAddress: String, okHandler: ((UIAlertAction) -> ())? = nil) -> UIAlertController {
with(
title: Localized.alertMailUnavailableTitle,
message: String.localizedStringWithFormat(Localized.alertMailUnavailableMessageFormat, contactAddress),
okHandler: okHandler
)
}
}
// MARK: - Purchase
extension UIAlertController {
static func purchaseNotAllowed(okHandler: ((UIAlertAction) -> ())? = nil) -> UIAlertController {
with(
title: Localized.Purchase.purchaseUnauthorized,
message: Localized.Purchase.purchaseUnauthorizedMessage,
okHandler: okHandler
)
}
static func productNotFetched(okHandler: ((UIAlertAction) -> ())? = nil) -> UIAlertController {
with(
title: Localized.Purchase.purchaseUnavailable,
message: Localized.Purchase.purchaseUnavailableMessage,
okHandler: okHandler
)
}
static func purchaseFailed(error: Error?, okHandler: ((UIAlertAction) -> ())? = nil) -> UIAlertController {
// `error.localizedDescription` does not seem to be localized, so we use a generic
// message.
with(
title: Localized.Purchase.purchaseFailed,
message: Localized.Purchase.purchaseFailedMessage,
okHandler: okHandler
)
}
static func restoreNotAllowed(okHandler: ((UIAlertAction) -> ())? = nil) -> UIAlertController {
with(
title: Localized.Purchase.restoreUnauthorized,
message: Localized.Purchase.restoreUnauthorizedMessage,
okHandler: okHandler
)
}
static func nothingToRestore(okHandler: ((UIAlertAction) -> ())? = nil) -> UIAlertController {
with(
title: Localized.Purchase.nothingToRestore,
message: Localized.Purchase.nothingToRestoreMessage,
okHandler: okHandler
)
}
static func restoreFailed(error: Error, okHandler: ((UIAlertAction) -> ())? = nil) -> UIAlertController {
with(
title: Localized.Purchase.restoreFailed,
message: Localized.Purchase.restoreFailedMessage,
okHandler: okHandler
)
}
}
// MARK: - Utility
extension UIAlertController {
static func with(title: String?, message: String? = nil, preferredStyle: UIAlertController.Style = .alert, okHandler: ((UIAlertAction) -> ())? = nil) -> UIAlertController {
let controller = UIAlertController(title: title, message: message, preferredStyle: preferredStyle)
controller.addAction(.ok(handler: okHandler))
return controller
}
}
extension UIAlertAction {
static func ok(handler: ((UIAlertAction) -> ())? = nil) -> UIAlertAction {
UIAlertAction(title: Localized.okAction, style: .default, handler: handler)
}
static func cancel(handler: ((UIAlertAction) -> ())? = nil) -> UIAlertAction {
UIAlertAction(title: Localized.cancelAction, style: .cancel, handler: handler)
}
static func openSettings(handler: ((UIAlertAction) -> ())? = nil) -> UIAlertAction {
UIAlertAction(title: Localized.openSettingsAction, style: .default, handler: handler)
}
}
extension UIAlertController {
func addActions(_ actions: [UIAlertAction]) {
actions.forEach(addAction)
}
}
extension UIViewController {
func presentAlert(_ controller: UIAlertController, animated: Bool = true) {
present(controller, animated: animated)
}
}