This repository was archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathAlertAction.swift
132 lines (104 loc) · 4.83 KB
/
AlertAction.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
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
//
// AlertActions.swift
// Freetime
//
// Created by Ivan Magda on 25/09/2017.
// Copyright © 2017 Ryan Nystrom. All rights reserved.
//
import UIKit
typealias AlertActionBlock = (UIAlertAction) -> Void
// MARK: AlertActions -
struct AlertAction {
// MARK: Properties
let rootViewController: UIViewController?
let title: String?
let style: UIAlertActionStyle
enum AlertShareType {
case shareUrl
case shareContent
case shareFilePath
case shareFileName
case `default`
var localizedString: String {
switch self {
case .shareUrl: return NSLocalizedString("Share URL", comment: "")
case .shareContent: return NSLocalizedString("Share Content", comment: "")
case .shareFilePath: return NSLocalizedString("Copy Path", comment: "")
case .shareFileName: return NSLocalizedString("Copy Name", comment: "")
case .default: return NSLocalizedString("Share", comment: "")
}
}
}
// MARK: Init
init(_ builder: AlertActionBuilder) {
rootViewController = builder.rootViewController
title = builder.title
style = builder.style ?? .default
}
// MARK: Public
func get(_ handler: AlertActionBlock? = nil) -> UIAlertAction {
return UIAlertAction(title: self.title, style: self.style, handler: handler)
}
func share(_ items: [Any],
activities: [UIActivity]?,
type: AlertShareType = .default,
buildActivityBlock: ((UIActivityViewController) -> Void)?) -> UIAlertAction {
return UIAlertAction(title: type.localizedString, style: .default) { _ in
let activityController = UIActivityViewController(activityItems: items, applicationActivities: activities)
buildActivityBlock?(activityController)
self.rootViewController?.present(activityController, animated: trueUnlessReduceMotionEnabled)
}
}
func view(client: GithubClient, repo: RepositoryDetails, icon: UIImage) -> UIAlertAction {
return UIAlertAction(title: repo.name, image: icon, style: .default) { _ in
self.rootViewController?.route_push(to: RepositoryViewController(
client: client,
repo: repo
))
}
}
func view(owner: String, icon: UIImage) -> UIAlertAction {
return UIAlertAction(title: "@\(owner)", image: icon, style: .default) { _ in
guard let url = URLBuilder.github().add(path: owner).url else { return }
self.rootViewController?.presentSafari(url: url)
}
}
func newIssue(issueController: NewIssueTableViewController) -> UIAlertAction {
return UIAlertAction(title: Constants.Strings.newIssue, style: .default) { _ in
let nav = UINavigationController(rootViewController: issueController)
nav.modalPresentationStyle = .formSheet
self.rootViewController?.route_present(to: nav)
}
}
// MARK: Static
static func cancel(_ handler: AlertActionBlock? = nil) -> UIAlertAction {
return UIAlertAction(title: Constants.Strings.cancel, style: .cancel, handler: handler)
}
static func ok(_ handler: AlertActionBlock? = nil) -> UIAlertAction {
return UIAlertAction(title: Constants.Strings.ok, style: .default, handler: handler)
}
static func no(_ handler: AlertActionBlock? = nil) -> UIAlertAction {
return UIAlertAction(title: Constants.Strings.no, style: .cancel, handler: handler)
}
static func yes(_ handler: AlertActionBlock? = nil) -> UIAlertAction {
return UIAlertAction(title: Constants.Strings.yes, style: .default, handler: handler)
}
static func goBack(_ handler: AlertActionBlock? = nil) -> UIAlertAction {
return UIAlertAction(title: NSLocalizedString("Go back", comment: ""), style: .cancel, handler: handler)
}
static func discard(_ handler: AlertActionBlock? = nil) -> UIAlertAction {
return UIAlertAction(title: NSLocalizedString("Discard", comment: ""), style: .destructive, handler: handler)
}
static func keep(_ handler: AlertActionBlock? = nil) -> UIAlertAction {
return UIAlertAction(title: NSLocalizedString("Keep", comment: ""), style: .default, handler: handler)
}
static func delete(_ handler: AlertActionBlock? = nil) -> UIAlertAction {
return UIAlertAction(title: NSLocalizedString("Delete", comment: ""), style: .destructive, handler: handler)
}
static func login(_ handler: AlertActionBlock? = nil) -> UIAlertAction {
return UIAlertAction(title: Constants.Strings.signin, style: .default, handler: handler)
}
static func clearAll(_ handler: AlertActionBlock? = nil) -> UIAlertAction {
return UIAlertAction(title: Constants.Strings.clearAll, style: .destructive, handler: handler)
}
}