/
Wireframe.swift
94 lines (79 loc) · 2.59 KB
/
Wireframe.swift
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
//
// Wireframe.swift
// RxExample
//
// Created by Krunoslav Zaher on 4/3/15.
// Copyright © 2015 Krunoslav Zaher. All rights reserved.
//
import Foundation
#if !RX_NO_MODULE
import RxSwift
#endif
#if os(iOS)
import UIKit
#elseif os(macOS)
import Cocoa
#endif
enum RetryResult {
case retry
case cancel
}
protocol Wireframe {
func open(url: URL)
func promptFor<Action: CustomStringConvertible>(_ message: String, cancelAction: Action, actions: [Action]) -> Observable<Action>
}
class DefaultWireframe: Wireframe {
static let sharedInstance = DefaultWireframe()
func open(url: URL) {
#if os(iOS)
UIApplication.shared.openURL(url)
#elseif os(macOS)
NSWorkspace.shared().open(url)
#endif
}
#if os(iOS)
private static func rootViewController() -> UIViewController {
// cheating, I know
return UIApplication.shared.keyWindow!.rootViewController!
}
#endif
static func presentAlert(_ message: String) {
#if os(iOS)
let alertView = UIAlertController(title: "RxExample", message: message, preferredStyle: .alert)
alertView.addAction(UIAlertAction(title: "OK", style: .cancel) { _ in
})
rootViewController().present(alertView, animated: true, completion: nil)
#endif
}
func promptFor<Action : CustomStringConvertible>(_ message: String, cancelAction: Action, actions: [Action]) -> Observable<Action> {
#if os(iOS)
return Observable.create { observer in
let alertView = UIAlertController(title: "RxExample", message: message, preferredStyle: .alert)
alertView.addAction(UIAlertAction(title: cancelAction.description, style: .cancel) { _ in
observer.on(.next(cancelAction))
})
for action in actions {
alertView.addAction(UIAlertAction(title: action.description, style: .default) { _ in
observer.on(.next(action))
})
}
DefaultWireframe.rootViewController().present(alertView, animated: true, completion: nil)
return Disposables.create {
alertView.dismiss(animated:false, completion: nil)
}
}
#elseif os(macOS)
return Observable.error(NSError(domain: "Unimplemented", code: -1, userInfo: nil))
#endif
}
}
extension RetryResult : CustomStringConvertible {
var description: String {
switch self {
case .retry:
return "Retry"
case .cancel:
return "Cancel"
}
}
}