This repository was archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathFrontViewController.swift
592 lines (509 loc) · 28.5 KB
/
FrontViewController.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
//
// FrontViewController.swift
// reddift
//
// Created by sonson on 2015/09/02.
// Copyright © 2015年 sonson. All rights reserved.
//
import reddift
import UIKit
class FrontViewController: UITableViewController, UIViewControllerPreviewingDelegate, UITextFieldDelegate, UIViewControllerTransitioningDelegate, ImageViewAnimator {
@IBOutlet var titleTextField: UITextField?
var searchController = SearchController(style: .plain)
var cellar: LinkContainerCellar = LinkContainerCellar()
@IBOutlet var rightListButton: UIBarButtonItem?
@IBOutlet var leftAccountButton: UIBarButtonItem?
var refresh = UIRefreshControl()
var searchControllerViewBottomSpaceConstraint: NSLayoutConstraint?
// func hideThumbnail(thumbnail: Thumbnail?) {
// let imageIncludingCells = self.tableView.visibleCells.flatMap({$0 as? ImageViewAnimator})
// if let thumbnail = thumbnail {
// for i in 0..<imageIncludingCells.count {
// if let imageView = imageIncludingCells[i].targetImageView(thumbnail: thumbnail) {
// imageView.isHidden = true
// } else {
// imageView.isHidden = false
// }
// }
// } else {
// for i in 0..<imageIncludingCells.count {
// if let imageView = imageIncludingCells[i].targetImageView(thumbnail: thumbnail) {
// imageView.isHidden = true
// } else {
// imageView.isHidden = false
// }
// }
// }
// }
func targetImageView(thumbnail: Thumbnail) -> UIImageView? {
let imageIncludingCells = self.tableView.visibleCells.compactMap({$0 as? ImageViewAnimator})
for i in 0..<imageIncludingCells.count {
if let imageView = imageIncludingCells[i].targetImageView(thumbnail: thumbnail) {
return imageView
}
}
return nil
}
func imageViews() -> [UIImageView] {
return self.tableView.visibleCells
.compactMap({$0 as? ImageViewAnimator})
.flatMap({$0.imageViews()})
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return ImageViewPageDismissAnimator()
}
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return ImageViewPageModalAnimator()
}
func showBars() {
// UIView.animateWithDuration(1,
// animations: { () -> Void in
// self.navigationController?.navigationBar.alpha = 1
// self.navigationController?.toolbar.alpha = 1
// }) { (success) -> Void in
// }
}
// MARK: - action
@objc func didChangeThumbnailPage(notification: Notification) {
if let userInfo = notification.userInfo, let thumbnail = userInfo["thumbnail"] as? Thumbnail {
if let index = cellar.containers.index(where: { $0.link.id == thumbnail.parentID }) {
let rect = tableView.rectForRow(at: IndexPath(row: index, section: 0))
tableView.scrollRectToVisible(rect, animated: false)
if let cell = tableView.cellForRow(at: IndexPath(row: index, section: 0)) as? ImageViewAnimator {
if let imageView = cell.targetImageView(thumbnail: thumbnail) {
let rect = tableView.convert(imageView.bounds, from: imageView)
tableView.contentOffset = CGPoint(x: 0, y: rect.origin.y - 64)
}
}
}
self.imageViews().forEach({$0.isHidden = false})
}
}
@objc func didTapActionNotification(notification: Notification) {
if let userInfo = notification.userInfo, let link = userInfo["link"] as? Link, let contents = userInfo["contents"] as? LinkContainable {
let controller = UIAlertController(title: link.title, message: link.url, preferredStyle: .actionSheet)
let shareAction = UIAlertAction(title: "Share", style: .default, handler: { (_) -> Void in
let sharedURL = URL(string: link.url)!
let activityController = UIActivityViewController(activityItems: [sharedURL], applicationActivities: nil)
self.present(activityController, animated: true, completion: nil)
})
controller.addAction(shareAction)
let reportAction = UIAlertAction(title: "Report", style: .default, handler: { (_) -> Void in
contents.report()
})
controller.addAction(reportAction)
let hideAction = UIAlertAction(title: "Hide", style: .destructive, handler: { (_) -> Void in
contents.hide()
})
controller.addAction(hideAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: { (_) -> Void in
})
controller.addAction(cancelAction)
self.present(controller, animated: true, completion: nil)
}
}
@objc func didTapCommentNotification(notification: Notification) {
if let userInfo = notification.userInfo, let link = userInfo["link"] as? Link {
let commentViewController = CommentViewController(nibName: nil, bundle: nil)
commentViewController.link = link
self.navigationController?.pushViewController(commentViewController, animated: true)
}
}
@objc func didTapNameNotification(notification: Notification) {
if let userInfo = notification.userInfo, let name = userInfo["name"] as? String {
let controller = UserViewController.controller(name)
self.present(controller, animated: true, completion: nil)
}
}
@objc func didTapTitleNotification(notification: Notification) {
if let userInfo = notification.userInfo, let link = userInfo["link"] as? Link, let url = URL(string: link.url) {
let controller = WebViewController(nibName: nil, bundle: nil)
controller.url = url
let nav = UINavigationController(rootViewController: controller)
self.present(nav, animated: true, completion: nil)
}
}
@objc func didTapThumbnailNotification(notification: Notification) {
if let userInfo = notification.userInfo,
let _ = userInfo["link"] as? Link,
let thumbnail = userInfo["thumbnail"] as? Thumbnail,
let _ = userInfo["view"] as? UIImageView {
let controller = createImageViewPageControllerWith(cellar.thumbnails, openThumbnail: thumbnail)
controller.modalPresentationStyle = .custom
controller.transitioningDelegate = self
self.present(controller, animated: true, completion: nil)
}
}
@objc func refreshDidChange(sender: AnyObject) {
print("refreshDidChange")
refresh.attributedTitle = NSAttributedString(string: cellar.loadingMessage())
DispatchQueue.main.async {
self.cellar.load(atTheBeginning: true)
}
}
@objc func closeSearchController(sender: AnyObject) {
titleTextField?.resignFirstResponder()
self.navigationController?.isToolbarHidden = false
self.navigationItem.leftBarButtonItem = self.leftAccountButton
self.navigationItem.rightBarButtonItem = self.rightListButton
if searchController.view.superview == self.navigationController?.view {
searchController.close(sender: self)
searchControllerViewBottomSpaceConstraint = nil
searchController.removeFromParentViewController()
}
}
@IBAction func compose(sender: AnyObject) {
// cellar.containers
if let c = cellar as? SubredditCellar {
let nav = PostCommentViewController.controller(with: c.subreddit)
self.present(nav, animated: true, completion: nil)
}
}
@IBAction func openTab(sender: AnyObject) {
// let rect = self.view.frame
//
// self.navigationController?.navigationBar.alpha = 0
// self.navigationController?.toolbar.alpha = 0
// self.tableView.showsVerticalScrollIndicator = false
// UIGraphicsBeginImageContextWithOptions(CGSize(width:rect.size.width, height:rect.size.height), false, 0.0)
// let context = UIGraphicsGetCurrentContext()
// context!.translate(x: 0.0, y: 0.0)
// self.navigationController?.view.layer.render(in: context!)
// let image = UIGraphicsGetImageFromCurrentImageContext()
// TabManager.sharedInstance.setScreenshot(image: image!)
// UIGraphicsEndImageContext()
//
// self.navigationController?.navigationBar.alpha = 1
// self.navigationController?.toolbar.alpha = 1
// self.tableView.showsVerticalScrollIndicator = true
//
// let nav = TabSelectViewController.sharedNavigationController
// if let vc = nav.topViewController as? TabSelectViewController {
// vc.startAnimationWhenWillAppearWithSnapshotView(view: self.navigationController!.view.snapshotView(afterScreenUpdates: true)!)
// }
// self.present(nav, animated: false, completion: { () -> Void in
// if let vc = nav.topViewController as? TabSelectViewController {
// vc.startAnimationWhenDidAppear()
// }
// })
}
// MARK: - Notification
@objc func openSubreddit(notification: NSNotification) {
if let userInfo = notification.userInfo, let subreddit = userInfo[SearchController.subredditKey] as? String {
cellar = SubredditCellar(subreddit: subreddit, width: self.view.frame.size.width, fontSize: 18)
self.tableView.reloadData()
cellar.load(atTheBeginning: true)
}
refresh.attributedTitle = NSAttributedString(string: cellar.tryLoadingMessage())
closeSearchController(sender: self)
}
@objc func searchSubreddit(notification: NSNotification) {
if let userInfo = notification.userInfo, let query = userInfo[SearchController.queryKey] as? String {
cellar = SearchLinkCellar(query: query, subreddit: nil, width: self.view.frame.size.width, fontSize: 18)
self.tableView.reloadData()
cellar.load(atTheBeginning: true)
}
refresh.attributedTitle = NSAttributedString(string: cellar.tryLoadingMessage())
closeSearchController(sender: self)
}
@objc func didUpdateSingleImageSize(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let obj = userInfo["obj"] as? MediaLinkContainer {
print(obj)
for i in 0..<cellar.containers.count {
let b = cellar.containers[i]
if b.link.id == obj.link.id {
print(i)
self.tableView.beginUpdates()
self.tableView.reloadRows(at: [IndexPath(row: i, section: 0)], with: .fade)
self.tableView.endUpdates()
break
}
}
}
}
}
@objc func didLoadImage(notification: NSNotification) {
if let userInfo = notification.userInfo, let indexPath = userInfo[MediaLinkContainerPrefetchAtIndexPathKey] as? IndexPath, let container = userInfo[MediaLinkContainerPrefetchContentKey] as? LinkContainable {
if 0..<self.cellar.containers.count ~= indexPath.row {
if container === self.cellar.containers[indexPath.row] {
self.tableView.beginUpdates()
self.tableView.reloadRows(at: [indexPath], with: .fade)
self.tableView.endUpdates()
}
}
}
}
@objc func didLoadLinkContainerCellar(notification: NSNotification) {
if let userInfo = notification.userInfo, let atTheBeginning = userInfo[LinkContainerCellar.isAtTheBeginningKey] as? Bool {
if atTheBeginning {
self.tableView.reloadData()
refresh.endRefreshing()
}
refresh.attributedTitle = NSAttributedString(string: cellar.tryLoadingMessage())
} else if let userInfo = notification.userInfo, let indices = userInfo[LinkContainerCellar.insertIndicesKey] as? [IndexPath], let cellar = userInfo[LinkContainerCellar.providerKey] as? LinkContainerCellar {
if self.cellar === cellar {
self.tableView.beginUpdates()
self.tableView.insertRows(at: indices, with: .none)
self.tableView.endUpdates()
refresh.attributedTitle = NSAttributedString(string: cellar.tryLoadingMessage())
}
}
}
@objc func didChangedDraggingThumbnail(notification: Notification) {
if let userInfo = notification.userInfo, let thumbnail = userInfo["thumbnail"] as? Thumbnail {
self.imageViews().forEach({$0.isHidden = false})
self.targetImageView(thumbnail: thumbnail)?.isHidden = true
}
// self.navigationController?.navigationBar.alpha = 0
// self.navigationController?.toolbar.alpha = 0
}
@objc func didUpdateLinkContainable(notification: Notification) {
if let userInfo = notification.userInfo, let contents = userInfo["contents"] as? LinkContainable {
if let index = self.cellar.containers.index(where: {contents.link.id == $0.link.id}) {
let indices = [IndexPath(row: index, section: 0)]
self.tableView.beginUpdates()
self.tableView.reloadRows(at: indices, with: .none)
self.tableView.endUpdates()
}
}
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
print("viewWillTransition")
self.cellar.layout(with: size.width, fontSize: 18)
self.tableView.reloadData()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// let con = CommentViewController(nibName: nil, bundle: nil)
// print(con.link)
// con.link = Link(id: "2zbpqj")
// self.navigationController?.pushViewController(con, animated: true)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "OpenComment" {
if let indexPath = self.tableView.indexPathForSelectedRow, let viewController = segue.destination as? CommentViewController {
let contents = cellar.containers[indexPath.row]
viewController.link = contents.link
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(FrontViewController.openSubreddit(notification:)), name: SearchControllerDidOpenSubredditName, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(FrontViewController.searchSubreddit(notification:)), name: SearchControllerDidSearchSubredditName, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(FrontViewController.openSubreddit(notification:)), name: SubredditSelectTabBarControllerDidOpenSubredditName, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(FrontViewController.didUpdateSingleImageSize(notification:)), name: MediaLinkContainerSingleImageSizeDidUpdate, object: nil)
if MediaLinkCellSingleImageSize {
NotificationCenter.default.addObserver(self, selector: #selector(FrontViewController.didLoadImage(notification:)), name: LinkContainerDidLoadImageName, object: nil)
}
NotificationCenter.default.addObserver(self, selector: #selector(FrontViewController.didLoadLinkContainerCellar(notification:)), name: LinkContainerCellarDidLoadName, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(FrontViewController.didTapActionNotification(notification:)), name: LinkCellDidTapActionNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(FrontViewController.didTapCommentNotification(notification:)), name: LinkCellDidTapCommentNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(FrontViewController.didTapNameNotification(notification:)), name: LinkCellDidTapNameNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(FrontViewController.didTapTitleNotification(notification:)), name: LinkCellDidTapTitleNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(FrontViewController.didTapThumbnailNotification(notification:)), name: LinkCellDidTapThumbnailNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(FrontViewController.didUpdateLinkContainable(notification:)), name: ThingContainableDidUpdate, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(FrontViewController.didChangeThumbnailPage(notification:)), name: ImageViewPageControllerDidChangePageName, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(FrontViewController.didChangedDraggingThumbnail(notification:)), name: ImageViewPageControllerDidStartDraggingThumbnailName, object: nil)
let backButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
self.navigationItem.backBarButtonItem = backButtonItem
tableView.separatorInset = UIEdgeInsets.zero
tableView.layoutMargins = UIEdgeInsets.zero
tableView.register(LinkCell.self, forCellReuseIdentifier: "LinkCell")
tableView.register(ThumbnailLinkCell.self, forCellReuseIdentifier: "ThumbnailLinkCell")
tableView.register(InvisibleCell.self, forCellReuseIdentifier: "InvisibleCell")
refresh.attributedTitle = NSAttributedString(string: "Loading...")
refresh.addTarget(self, action: #selector(FrontViewController.refreshDidChange(sender:)), for: .valueChanged)
tableView.addSubview(refresh)
// cellar = SubredditCellar(subreddit: "movies", width: self.view.frame.size.width, fontSize: 18)
cellar = SubredditCellar(subreddit: "test", width: self.view.frame.size.width, fontSize: 18)
self.tableView.reloadData()
cellar.load(atTheBeginning: true)
registerForPreviewing(with: self, sourceView: self.view)
NotificationCenter.default.addObserver(self, selector: #selector(FrontViewController.keyboardWillChangeFrame(notification:)), name: .UIKeyboardDidShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(FrontViewController.keyboardWillChangeFrame(notification:)), name: .UIKeyboardWillHide, object: nil)
}
// MARK: - UIViewControllerPreviewingDelegate
func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
if viewControllerToCommit is CommentViewController {
self.navigationController?.pushViewController(viewControllerToCommit, animated: false)
} else if let controller = viewControllerToCommit as? ImageViewPageController {
controller.setOffset()
controller.modalPresentationStyle = .custom
controller.transitioningDelegate = self
self.present(viewControllerToCommit, animated: false, completion: nil)
} else if let controller = viewControllerToCommit as? WebViewController {
let nav = UINavigationController(rootViewController: controller)
self.present(nav, animated: false, completion: nil)
} else {
self.present(viewControllerToCommit, animated: false, completion: nil)
}
}
func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
if let cell = getCellAt(location) as? LinkCell {
if let (thumbnail, sourceRect, _, _) = cell.thumbnailAt(location, peekView: self.view) {
previewingContext.sourceRect = sourceRect
let controller = createImageViewPageControllerWith(cellar.thumbnails, openThumbnail: thumbnail, isOpenedBy3DTouch: true)
controller.isNavigationBarHidden = true
return controller
} else if let (name, sourceRect) = cell.userAt(location, peekView: self.view) {
previewingContext.sourceRect = sourceRect
let nav = UserViewController.controller(name)
return nav
} else if let (sourceRect, contents) = cell.commentAt(location, peekView: self.view) {
previewingContext.sourceRect = sourceRect
let commentViewController = CommentViewController(nibName: nil, bundle: nil)
commentViewController.link = contents.link
return commentViewController
} else if let (url, sourceRect) = cell.urlAt(location, peekView: self.view) {
previewingContext.sourceRect = sourceRect
let controller = WebViewController(nibName: nil, bundle: nil)
controller.url = url
return controller
} else {
if let container = cell.container, let url = URL(string: container.link.url) {
let sourceRect = cell.convert(cell.bounds, to: self.view)
previewingContext.sourceRect = sourceRect
let controller = WebViewController(nibName: nil, bundle: nil)
controller.url = url
return controller
}
}
}
return nil
}
func getCellAt(_ location: CGPoint) -> UITableViewCell? {
if let view = self.view {
for cell in tableView.visibleCells {
if let parent = cell.superview {
let p = view.convert(location, to: parent)
let r = cell.frame
if r.contains(p) {
return cell
}
}
}
}
return nil
}
// MARK: - Table view delegate & data source
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cellar.containers[indexPath.row].downloadImages()
if tableView == self.tableView {
if indexPath.row == (cellar.containers.count - 1) {
cellar.load(atTheBeginning: false)
}
}
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return cellar.containers[indexPath.row].height
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellar.containers.count
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let container = cellar.containers[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: container.cellIdentifier)
switch (container, cell) {
case (_, let cell as InvisibleCell):
return cell
case (let container as MediaLinkContainer, _):
// aa
let cell = MediaLinkCell(numberOfThumbnails: container.thumbnails.count)
cell.container = container
return cell
case (let container, let cell as LinkCell):
cell.container = container
return cell
default:
return UITableViewCell(style: .default, reuseIdentifier: "Default")
}
}
// MARK: - UITextFieldDelegate
@objc func keyboardWillChangeFrame(notification: Notification) {
if let userInfo = notification.userInfo {
if let rect = userInfo[UIKeyboardFrameEndUserInfoKey] as? CGRect {
print(rect)
if let searchControllerViewBottomSpaceConstraint = self.searchControllerViewBottomSpaceConstraint {
searchControllerViewBottomSpaceConstraint.constant = 667 - rect.origin.y
}
}
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
}
func textFieldDidBeginEditing(_ textField: UITextField) {
searchController = SearchController(style: .plain)
if let navigationController = self.navigationController {
navigationController.addChildViewController(searchController)
navigationController.view.addSubview(searchController.view)
searchController.view.translatesAutoresizingMaskIntoConstraints = false
let views: [String: UIView] = [
"v1": searchController.view,
"v2": self.navigationController!.view
]
navigationController.view.addConstraints(
NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[v1]-0-|", options: NSLayoutFormatOptions(), metrics: [:], views: views)
)
navigationController.view.bringSubview(toFront: navigationController.navigationBar)
let constraint1 = NSLayoutConstraint(item: navigationController.view, attribute: .top, relatedBy: .equal, toItem: searchController.view, attribute: .top, multiplier: 1, constant: -64)
let constraint2 = NSLayoutConstraint(item: navigationController.view, attribute: .bottom, relatedBy: .equal, toItem: searchController.view, attribute: .bottom, multiplier: 1, constant: 100)
navigationController.view.addConstraint(constraint1)
navigationController.view.addConstraint(constraint2)
searchControllerViewBottomSpaceConstraint = constraint2
navigationController.isToolbarHidden = true
self.navigationItem.rightBarButtonItem = nil
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(FrontViewController.closeSearchController(sender:)))
searchController.view.alpha = 0
UIView.animate(withDuration: 0.3) {
self.searchController.view.alpha = 1
}
}
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
print(textField.text!)
if let t = textField.text {
let query = t + string
searchController.didChangeQuery(text: query)
}
return true
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// if let t = textField.text {
// }
return false
}
// MARK: - LinkContainerCellar
func didLoadContents(controller: LinkContainerCellar, indexPaths: [NSIndexPath]) {
DispatchQueue.main.async(execute: { () -> Void in
self.tableView.beginUpdates()
self.tableView.insertRows(at: indexPaths.map { $0 as IndexPath}, with: .none)
self.tableView.endUpdates()
})
}
func didFailLoadContents(controller: LinkContainerCellar, error: NSError) {
if error.code == 401 {
DispatchQueue.main.async(execute: { () -> Void in
// authentication expired?
let alert = UIAlertController(title: "Error", message: "Authentication failed. Do you want to try again?", preferredStyle: .alert)
let actionOK = UIAlertAction(title: "Try", style: .default, handler: { (_) -> Void in
// reload
})
alert.addAction(actionOK)
let actionCancel = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alert.addAction(actionCancel)
self.present(alert, animated: true, completion: nil)
})
}
}
}