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 pathNewIssueTableViewController.swift
241 lines (197 loc) · 7.93 KB
/
NewIssueTableViewController.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//
// NewIssueTableViewController.swift
// Freetime
//
// Created by Sherlock, James on 22/09/2017.
// Copyright © 2017 Ryan Nystrom. All rights reserved.
//
import UIKit
import Squawk
import GitHubAPI
enum IssueSignatureType {
case bugReport
case sentWithGitHawk
var addition: String {
switch self {
case .bugReport:
#if TESTFLIGHT
let testFlight = "true"
#else
let testFlight = "false"
#endif
return [
"\n",
"<details>",
"<summary>Bug Report Dump (Auto-generated)</summary>",
"<pre>",
Bundle.main.prettyVersionString,
"Device: \(UIDevice.current.modelName) (iOS \(UIDevice.current.systemVersion))",
"TestFlight: \(testFlight)",
"</pre>",
"</details>"
].joined(separator: "\n")
case .sentWithGitHawk:
return Signature.signed(text: "")
}
}
}
protocol NewIssueTableViewControllerDelegate: class {
func didDismissAfterCreatingIssue(model: IssueDetailsModel)
}
final class NewIssueTableViewController: UITableViewController, UITextFieldDelegate {
weak var delegate: NewIssueTableViewControllerDelegate?
@IBOutlet var titleField: UITextField!
@IBOutlet var bodyField: UITextView!
private var client: GithubClient!
private var owner: String!
private var repo: String!
private var signature: IssueSignatureType?
private let textActionsController = TextActionsController()
private var titleText: String? {
guard let raw = titleField.text?.trimmingCharacters(in: .whitespacesAndNewlines) else { return nil }
if raw.isEmpty { return nil }
return raw
}
private var bodyText: String? {
let raw = bodyField.text.trimmingCharacters(in: .whitespacesAndNewlines)
if raw.isEmpty { return nil }
return raw
}
static func create(client: GithubClient,
owner: String,
repo: String,
signature: IssueSignatureType? = nil) -> NewIssueTableViewController? {
let storyboard = UIStoryboard(name: "NewIssue", bundle: nil)
let viewController = storyboard.instantiateInitialViewController() as? NewIssueTableViewController
viewController?.hidesBottomBarWhenPushed = true
viewController?.client = client
viewController?.owner = owner
viewController?.repo = repo
viewController?.signature = signature
return viewController
}
override func viewDidLoad() {
super.viewDidLoad()
// Add send button
setRightBarItemIdle()
// Add cancel button
navigationItem.leftBarButtonItem = UIBarButtonItem(
title: Constants.Strings.cancel,
style: .plain,
target: self,
action: #selector(onCancel)
)
// Make the return button move on to description field
titleField.delegate = self
titleField.font = Styles.Text.body.preferredFont
// Setup markdown input view
bodyField.githawkConfigure(inset: false)
setupInputView()
// Update title to use localization
title = Constants.Strings.newIssue
titleField.becomeFirstResponder()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
view.endEditing(false)
}
// MARK: Accessibility
override func accessibilityPerformMagicTap() -> Bool {
guard titleText != nil else { return false }
onSend()
return true
}
// MARK: Private API
func setRightBarItemIdle() {
navigationItem.rightBarButtonItem = UIBarButtonItem(
title: NSLocalizedString("Submit", comment: ""),
style: .done,
target: self,
action: #selector(onSend)
)
navigationItem.rightBarButtonItem?.isEnabled = titleText != nil
}
/// Attempts to sends the current forms information to GitHub, on success will redirect the user to the new issue
@objc func onSend() {
if let bodyText = bodyText {
self.finishSend()
} else {
let submitAlert = UIAlertController(title: "Please Provide Description", message: "Are you certain you want to submit this issue without a description?", preferredStyle: UIAlertControllerStyle.alert)
submitAlert.addAction(UIAlertAction(title: "Submit", style: .default, handler: { (action: UIAlertAction!) in
self.finishSend()
}))
submitAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
submitAlert .dismiss(animated: true, completion: nil)
}))
present(submitAlert, animated: true, completion: nil)
}
}
func finishSend() {
guard let titleText = titleText else {
Squawk.showIssueError(message: NSLocalizedString("An issue title is required. Please add a title and try again.", comment: "Invalid title when sending new issue"), view: bodyField)
return
}
titleField.resignFirstResponder()
bodyField.resignFirstResponder()
setRightBarItemSpinning()
let signature = self.signature?.addition ?? ""
client.client.send(V3CreateIssueRequest(
owner: owner,
repo: repo,
title: titleText,
body: (bodyText ?? "") + signature)
) { [weak self] result in
guard let strongSelf = self else { return }
strongSelf.setRightBarItemIdle()
switch result {
case .success(let response):
let model = IssueDetailsModel(owner: strongSelf.owner, repo: strongSelf.repo, number: response.data.number)
let delegate = strongSelf.delegate
strongSelf.dismiss(animated: trueUnlessReduceMotionEnabled, completion: {
delegate?.didDismissAfterCreatingIssue(model: model)
})
case .failure(let error):
Squawk.show(error: error)
}
}
}
/// Ensures there are no unsaved changes before dismissing the view controller. Will prompt user if unsaved changes.
@objc func onCancel() {
titleField.resignFirstResponder()
bodyField.resignFirstResponder()
cancelAction_onCancel(
texts: [titleText, bodyText],
title: NSLocalizedString("Unsaved Changes", comment: "New Issue - Cancel w/ Unsaved Changes Title"),
message: NSLocalizedString("Are you sure you want to discard this new issue? Your message will be lost.",
comment: "New Issue - Cancel w/ Unsaved Changes Message")
)
}
func setupInputView() {
let getMarkdownBlock = { [weak self] () -> (String) in
return self?.bodyField.text ?? ""
}
let actions = IssueTextActionsView.forMarkdown(
viewController: self,
getMarkdownBlock: getMarkdownBlock,
repo: repo,
owner: owner,
addBorder: true,
supportsImageUpload: true,
showSendButton: false
)
textActionsController.configure(client: client, textView: bodyField, actions: actions)
textActionsController.viewController = self
bodyField.inputAccessoryView = actions
}
// MARK: UITextFieldDelegate
/// Called when the user taps return on the title field, moves their cursor to the body
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
bodyField.becomeFirstResponder()
return false
}
// MARK: Actions
/// Called when editing changed on the title field, enable/disable submit button based on title and body
@IBAction func titleFieldEditingChanged(_ sender: Any) {
navigationItem.rightBarButtonItem?.isEnabled = titleText != nil
}
}