This repository has been archived by the owner on May 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 190
/
OEXRouter+Swift.swift
711 lines (613 loc) · 40.6 KB
/
OEXRouter+Swift.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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
//
// OEXRouter+Swift.swift
// edX
//
// Created by Akiva Leffert on 5/7/15.
// Copyright (c) 2015 edX. All rights reserved.
//
import Foundation
import WebKit
// The router is an indirection point for navigation throw our app.
// New router logic should live here so it can be written in Swift.
// We should gradually migrate the existing router class here and then
// get rid of the objc version
public enum CourseHTMLBlockSubkind {
case Base
case Problem
case OpenAssesment
case DragAndDrop
case WordCloud
case LTIConsumer
}
enum CourseBlockDisplayType {
case Unknown
case Outline
case Unit
case Video
case HTML(CourseHTMLBlockSubkind)
case Discussion(DiscussionModel)
var isUnknown : Bool {
switch self {
case .Unknown: return true
default: return false
}
}
}
extension CourseBlock {
var displayType : CourseBlockDisplayType {
switch self.type {
case .Unknown(_), .HTML: return multiDevice ? .HTML(.Base) : .Unknown
case .Problem: return multiDevice ? .HTML(.Problem) : .Unknown
case .OpenAssesment: return multiDevice ? .HTML(.OpenAssesment) : .Unknown
case .DragAndDrop: return multiDevice ? .HTML(.DragAndDrop) : .Unknown
case .WordCloud: return multiDevice ? .HTML(.WordCloud) : .Unknown
case .LTIConsumer: return multiDevice ? .HTML(.LTIConsumer) : .Unknown
case .Course: return .Outline
case .Chapter: return .Outline
case .Section: return .Outline
case .Unit: return .Unit
case let .Video(summary): return (summary.isSupportedVideo) ? .Video : .Unknown
case let .Discussion(discussionModel): return .Discussion(discussionModel)
}
}
}
extension OEXRouter {
func showCoursewareForCourseWithID(courseID : String, fromController controller : UIViewController) {
showContainerForBlockWithID(blockID: nil, type: CourseBlockDisplayType.Outline, parentID: nil, courseID : courseID, fromController: controller)
}
func unitControllerForCourseID(courseID : String, blockID : CourseBlockID?, initialChildID : CourseBlockID?, forMode mode: CourseOutlineMode? = .full) -> CourseContentPageViewController {
let contentPageController = CourseContentPageViewController(environment: environment, courseID: courseID, rootID: blockID, initialChildID: initialChildID, forMode: mode ?? .full)
return contentPageController
}
func navigateToComponentScreen(from controller: UIViewController, courseID: CourseBlockID, componentID: CourseBlockID, completion: ((UIViewController) -> Void)? = nil) {
if environment.config.isNewComponentNavigationEnabled {
navigateToComponentScreenNew(from: controller, courseID: courseID, componentID: componentID, completion: completion)
} else {
navigateToComponentScreenOld(from: controller, courseID: courseID, componentID: componentID, completion: completion)
}
}
func navigateToComponentScreenNew(from controller: UIViewController, courseID: CourseBlockID, componentID: CourseBlockID, completion: ((UIViewController) -> Void)? = nil) {
var parentViewController: UIViewController?
if environment.config.isNewDashboardEnabled {
if let dashboardController = controller.navigationController?.viewControllers.first as? NewCourseDashboardViewController {
dashboardController.switchTab(with: .courseDashboard)
parentViewController = dashboardController
}
} else {
if let dashboardController = controller.navigationController?.viewControllers.first(where: { $0 is CourseDashboardViewController }) as? CourseDashboardViewController {
dashboardController.switchTab(with: .courseDashboard)
parentViewController = dashboardController
}
}
let contentController = NewCourseContentController(environment: environment, blockID: componentID, resumeCourseBlockID: componentID, courseID: courseID)
parentViewController?.navigationController?.pushViewController(contentController, animated: true, completion: completion)
}
func navigateToComponentScreenOld(from controller: UIViewController, courseID: CourseBlockID, componentID: CourseBlockID, completion: ((UIViewController) -> Void)? = nil) {
let courseQuerier = environment.dataManager.courseDataManager.querierForCourseWithID(courseID: courseID, environment: environment)
guard let childBlock = courseQuerier.blockWithID(id: componentID).firstSuccess().value,
let unitBlock = courseQuerier.parentOfBlockWith(id: childBlock.blockID, type: .Unit).firstSuccess().value,
let sectionBlock = courseQuerier.parentOfBlockWith(id: childBlock.blockID, type: .Section).firstSuccess().value,
let chapterBlock = courseQuerier.parentOfBlockWith(id: childBlock.blockID, type: .Chapter).firstSuccess().value else {
Logger.logError("ANALYTICS", "Unable to load block: \(componentID)")
return
}
var outlineViewController: UIViewController
if controller is CourseOutlineViewController {
outlineViewController = controller
} else {
if environment.config.isNewDashboardEnabled {
guard let dashboardController = controller.navigationController?.viewControllers.first as? NewCourseDashboardViewController else { return }
dashboardController.switchTab(with: .courseDashboard)
guard let outlineController = dashboardController.currentVisibileController else { return }
outlineViewController = outlineController
} else {
guard let dashboardController = controller.navigationController?.viewControllers.first(where: { $0 is CourseDashboardViewController}) as? CourseDashboardViewController else { return }
dashboardController.switchTab(with: .courseDashboard)
guard let outlineController = dashboardController.currentVisibleController as? CourseOutlineViewController else { return }
outlineViewController = outlineController
}
}
showContainerForBlockWithID(blockID: sectionBlock.blockID, type: sectionBlock.displayType, parentID: chapterBlock.blockID, courseID: courseID, fromController: outlineViewController) { [weak self] visibleController in
self?.showContainerForBlockWithID(blockID: childBlock.blockID, type: childBlock.displayType, parentID: unitBlock.blockID, courseID: courseID, fromController: visibleController, completion: completion)
}
}
func showCourseUnknownBlock(blockID: CourseBlockID?, courseID: CourseBlockID, fromController controller: UIViewController) {
let unsupportedController = CourseUnknownBlockViewController(blockID: blockID, courseID : courseID, environment : environment)
controller.navigationController?.pushViewController(unsupportedController, animated: true)
}
func showContainerForBlockWithID(blockID: CourseBlockID?, type: CourseBlockDisplayType, parentID: CourseBlockID?, courseID: CourseBlockID, fromController controller: UIViewController, forMode mode: CourseOutlineMode? = .full, completion: ((UIViewController) -> Void)? = nil) {
if environment.config.isNewComponentNavigationEnabled {
let contentController = NewCourseContentController(environment: environment, blockID: blockID, parentID: parentID, courseID: courseID, courseOutlineMode: mode)
controller.navigationController?.pushViewController(contentController, animated: true, completion: completion)
} else {
showContainerForBlockWithIDOld(blockID: blockID, type: type, parentID: parentID, courseID: courseID, fromController: controller, forMode: mode, completion: completion)
}
}
func showContainerForBlockWithIDOld(blockID: CourseBlockID?, type: CourseBlockDisplayType, parentID: CourseBlockID?, courseID: CourseBlockID, fromController controller: UIViewController, forMode mode: CourseOutlineMode? = .full, completion: ((UIViewController) -> Void)? = nil) {
switch type {
case .Outline:
fallthrough
case .Unit:
let outlineController = controllerForBlockWithID(blockID: blockID, type: type, courseID: courseID, forMode: mode)
controller.navigationController?.pushViewController(outlineController, animated: true, completion: completion)
case .HTML:
fallthrough
case .Video:
fallthrough
case .Unknown:
let pageController = unitControllerForCourseID(courseID: courseID, blockID: parentID, initialChildID: blockID, forMode: mode)
if let delegate = controller as? CourseContentPageViewControllerDelegate {
pageController.navigationDelegate = delegate
}
controller.navigationController?.pushViewController(pageController, animated: true, completion: completion)
case .Discussion:
let pageController = unitControllerForCourseID(courseID: courseID, blockID: parentID, initialChildID: blockID)
if let delegate = controller as? CourseContentPageViewControllerDelegate {
pageController.navigationDelegate = delegate
}
controller.navigationController?.pushViewController(pageController, animated: true, completion: completion)
}
}
func showCelebratoryModal(fromController controller: UIViewController, courseID: String) -> CelebratoryModalViewController {
let celebratoryModalView = CelebratoryModalViewController(courseID: courseID, environment: environment)
celebratoryModalView.modalPresentationStyle = .overCurrentContext
celebratoryModalView.modalTransitionStyle = .crossDissolve
controller.present(celebratoryModalView, animated: false, completion: nil)
return celebratoryModalView
}
private func controllerForBlockWithID(blockID: CourseBlockID?, type: CourseBlockDisplayType, courseID: String, forMode mode: CourseOutlineMode? = .full, gated: Bool? = false, shouldCelebrationAppear: Bool = false) -> UIViewController {
if gated ?? false {
return CourseUnknownBlockViewController(blockID: blockID, courseID : courseID, environment : environment)
}
switch type {
case .Outline:
let outlineController = CourseOutlineViewController(environment: self.environment, courseID: courseID, rootID: blockID, forMode: mode)
return outlineController
case .Unit:
return unitControllerForCourseID(courseID: courseID, blockID: blockID, initialChildID: nil, forMode: mode)
case .HTML(let subkind):
let controller = HTMLBlockViewController(blockID: blockID, courseID: courseID, environment: environment, subkind: subkind)
return controller
case .Video:
let controller = VideoBlockViewController(environment: environment, blockID: blockID, courseID: courseID, shouldCelebrationAppear: shouldCelebrationAppear)
return controller
case .Unknown:
let controller = CourseUnknownBlockViewController(blockID: blockID, courseID : courseID, environment : environment)
return controller
case let .Discussion(discussionModel):
let controller = DiscussionBlockViewController(blockID: blockID, courseID: courseID, topicID: discussionModel.topicID, environment: environment)
return controller
}
}
func controllerForBlock(block : CourseBlock, courseID : String, shouldCelebrationAppear: Bool = false) -> UIViewController {
return controllerForBlockWithID(blockID: block.blockID, type: block.displayType, courseID: courseID, gated: block.isGated, shouldCelebrationAppear: shouldCelebrationAppear)
}
@objc(showMyCoursesAnimated:pushingCourseWithID:) func showMyCourses(animated: Bool = true, pushingCourseWithID courseID: String? = nil) {
let controller = EnrolledTabBarViewController(environment: environment)
let learnController = controller.children.flatMap { $0.children }.compactMap { $0 as? LearnContainerViewController } .first
showContentStack(withRootController: controller, animated: animated)
if let courseID = courseID, let learnController = learnController {
showCourseWithID(courseID: courseID, fromController: learnController, animated: false, newEnrollment: true)
}
}
@objc func showEnrolledTabBarView() {
let controller = EnrolledTabBarViewController(environment: environment)
showContentStack(withRootController: controller, animated: false)
}
func showCourseDates(controller:UIViewController, courseID: String) {
let courseDates = CourseDatesViewController(environment: environment, courseID: courseID)
controller.navigationController?.pushViewController(courseDates, animated: true)
}
func showCourseVideos(controller:UIViewController, courseID: String) {
showContainerForBlockWithID(blockID: nil, type: CourseBlockDisplayType.Outline, parentID: nil, courseID : courseID, fromController: controller, forMode: .video)
}
func showDatesTabController(controller: UIViewController) {
if environment.config.isNewDashboardEnabled {
if let dashboardController = controller.findParentViewController(type: NewCourseContentController.self)?.navigationController?.viewControllers.first as? NewCourseDashboardViewController {
popToRoot(controller: dashboardController) {
dashboardController.switchTab(with: .courseDates)
}
} else if let dashboardController = UIApplication.shared.topMostController() as? NewCourseDashboardViewController {
dashboardController.switchTab(with: .courseDates)
}
} else {
if let dashboardController = controller.findParentViewController(type: CourseDashboardViewController.self) {
dashboardController.switchTab(with: .courseDates)
} else if let dashboardController = controller.navigationController?.viewControllers.first(where: { $0 is CourseDashboardViewController}) as? CourseDashboardViewController {
controller.navigationController?.popToViewController(dashboardController, animated: false)
dashboardController.switchTab(with: .courseDates)
}
}
}
// MARK: Deep Linking
//Method can be use to navigate on particular tab of course dashboard with deep link type
func showCourse(with deeplink: DeepLink, courseID: String, from controller: UIViewController, completion: (() -> Void)? = nil) {
if environment.config.isNewDashboardEnabled {
showCourseNew(with: deeplink, courseID: courseID, from: controller, completion: completion)
} else {
showCourseOld(with: deeplink, courseID: courseID, from: controller, completion: completion)
}
}
private func showCourseOld(with deeplink: DeepLink, courseID: String, from controller: UIViewController, completion: (() -> Void)? = nil) {
let courseDashboardController = controller.navigationController?.viewControllers.first(where: { $0.isKind(of: CourseDashboardViewController.self) })
if let dashboardController = courseDashboardController as? CourseDashboardViewController, dashboardController.courseID == deeplink.courseId {
controller.navigationController?.setToolbarHidden(true, animated: false)
controller.navigationController?.popToViewController(dashboardController, animated: true)
dashboardController.switchTab(with: deeplink.type, componentID: deeplink.componentID)
completion?()
} else if let enrolledTabBarController = controller.find(viewController: EnrolledTabBarViewController.self) {
if let courseDashboardController = courseDashboardController {
courseDashboardController.navigationController?.popToRootViewController(animated: true) { [weak self] in
let switchedViewController = enrolledTabBarController.switchTab(with: deeplink.type)
self?.showCourseWithID(courseID: courseID, fromController: switchedViewController, animated: true) { controller in
guard let dashboardController = controller as? CourseDashboardViewController else { return }
dashboardController.switchTab(with: deeplink.type, componentID: deeplink.componentID)
completion?()
}
}
} else {
let switchedViewController = enrolledTabBarController.switchTab(with: deeplink.type)
if let switchedViewController = switchedViewController as? LearnContainerViewController {
switchedViewController.navigationController?.popToRootViewController(animated: true) {
switchedViewController.switchTo(component: .courses)
}
}
showCourseWithID(courseID: courseID, fromController: switchedViewController, animated: true) { controller in
guard let dashboardController = controller as? CourseDashboardViewController else { return }
dashboardController.switchTab(with: deeplink.type, componentID: deeplink.componentID)
completion?()
}
}
}
}
private func showCourseNew(with deeplink: DeepLink, courseID: String, from controller: UIViewController, completion: (() -> Void)? = nil) {
if let enrolledTabBarController = controller.find(viewController: EnrolledTabBarViewController.self) {
let switchedViewController = enrolledTabBarController.switchTab(with: deeplink.type)
if let switchedViewController = switchedViewController as? LearnContainerViewController {
switchedViewController.navigationController?.popToRootViewController(animated: true) {
switchedViewController.switchTo(component: .courses)
}
}
showCourseWithID(courseID: courseID, fromController: switchedViewController, animated: true) { controller in
var dashboardController: NewCourseDashboardViewController?
if let controller = controller as? ForwardingNavigationController {
dashboardController = controller.viewControllers.first(where: { $0 is NewCourseDashboardViewController }) as? NewCourseDashboardViewController
} else {
dashboardController = controller as? NewCourseDashboardViewController
}
dashboardController?.switchTab(with: deeplink.type, deeplink: deeplink)
completion?()
}
}
}
func showProgram(with type: DeepLinkType, url: URL? = nil, from controller: UIViewController) {
let tabbarController = controller.find(viewController: EnrolledTabBarViewController.self)
if let learnController = tabbarController?.switchTab(with: type) as? LearnContainerViewController {
popToRoot(controller: learnController)
if let programsViewController = learnController.switchTo(component: .programs) as? ProgramsViewController,
let url = url {
showProgramDetails(with: url, from: programsViewController)
}
}
}
func showAnnouncment(from controller : UIViewController, courseID : String) {
let announcementViewController = CourseAnnouncementsViewController(environment: environment, courseID: courseID)
controller.navigationController?.pushViewController(announcementViewController, animated: true)
}
private func popToRoot(controller: UIViewController, completion: (() -> Void)? = nil) {
controller.navigationController?.setToolbarHidden(true, animated: false)
controller.navigationController?.popToRootViewController(animated: true, completion: completion)
}
func showDiscoveryController(from controller: UIViewController, type: DeepLinkType, isUserLoggedIn: Bool, pathID: String?) {
let bottomBar = BottomBarView(environment: environment)
var discoveryController = discoveryViewController(bottomBar: bottomBar, searchQuery: nil)
if isUserLoggedIn {
// Pop out all views and switches enrolledCourses tab on the bases of link type
if let enrolledTabBarView = controller.find(viewController: EnrolledTabBarViewController.self) {
popToRoot(controller: enrolledTabBarView)
discoveryController = enrolledTabBarView.switchTab(with: type)
}
else {
//Create new stack of views and switch tab
let enrolledTabController = EnrolledTabBarViewController(environment: environment)
showContentStack(withRootController: enrolledTabController, animated: false)
discoveryController = enrolledTabController.switchTab(with: type)
}
}
else {
if let controllers = controller.navigationController?.viewControllers, let discoveryView = controllers.first as? DiscoveryViewController {
popToRoot(controller: controller)
discoveryController = discoveryView
}
else if let discoveryController = discoveryController {
showControllerFromStartupScreen(controller: discoveryController)
}
}
// If the pathID is given the detail view will open
if let pathID = pathID, let discoveryController = discoveryController {
showDiscoveryDetail(from: discoveryController, type: type, pathID: pathID, bottomBar: bottomBar)
}
}
func showDiscoveryDetail(from controller: UIViewController, type: DeepLinkType, pathID: String, bottomBar: UIView?) {
if type == .discoveryCourseDetail {
showCourseDetails(from: controller, with: pathID, bottomBar: bottomBar)
}
else if type == .discoveryProgramDetail {
showProgramDetail(from: controller, with: pathID, bottomBar: bottomBar)
}
}
func showDiscussionResponsesFromViewController(controller: UIViewController, courseID : String, thread : DiscussionThread, isDiscussionBlackedOut: Bool) {
let storyboard = UIStoryboard(name: "DiscussionResponses", bundle: nil)
let responsesViewController = storyboard.instantiateInitialViewController() as! DiscussionResponsesViewController
responsesViewController.environment = environment
responsesViewController.courseID = courseID
responsesViewController.thread = thread
responsesViewController.isDiscussionBlackedOut = isDiscussionBlackedOut
controller.navigationController?.pushViewController(responsesViewController, animated: true)
}
func showDiscussionResponses(from controller: UIViewController, courseID: String, threadID: String, isDiscussionBlackedOut: Bool, completion: (()->Void)?) {
let storyboard = UIStoryboard(name: "DiscussionResponses", bundle: nil)
let responsesViewController = storyboard.instantiateInitialViewController() as! DiscussionResponsesViewController
responsesViewController.environment = environment
responsesViewController.courseID = courseID
responsesViewController.threadID = threadID
responsesViewController.isDiscussionBlackedOut = isDiscussionBlackedOut
controller.navigationController?.delegate = self
if let completion = completion {
controller.navigationController?.pushViewController(viewController: responsesViewController, completion: completion)
} else {
controller.navigationController?.pushViewController(responsesViewController, animated: true)
}
}
func showDiscussionComments(from controller: UIViewController, courseID: String, commentID: String, threadID: String) {
let discussionCommentController = DiscussionCommentsViewController(environment: environment, courseID: courseID, commentID: commentID, threadID: threadID)
if let delegate = controller as? DiscussionCommentsViewControllerDelegate {
discussionCommentController.delegate = delegate
}
controller.navigationController?.pushViewController(discussionCommentController, animated: true)
}
func showDiscussionCommentsFromViewController(controller: UIViewController, courseID : String, response : DiscussionComment, closed : Bool, thread: DiscussionThread, isDiscussionBlackedOut: Bool) {
let commentsVC = DiscussionCommentsViewController(environment: environment, courseID : courseID, responseItem: response, closed: closed, thread: thread, isDiscussionBlackedOut: isDiscussionBlackedOut)
if let delegate = controller as? DiscussionCommentsViewControllerDelegate {
commentsVC.delegate = delegate
}
controller.navigationController?.pushViewController(commentsVC, animated: true)
}
func showDiscussionNewCommentFromController(controller: UIViewController, courseID : String, thread:DiscussionThread, context: DiscussionNewCommentViewController.Context) {
let newCommentViewController = DiscussionNewCommentViewController(environment: environment, courseID : courseID, thread:thread, context: context)
if let delegate = controller as? DiscussionNewCommentViewControllerDelegate {
newCommentViewController.delegate = delegate
}
controller.present(ForwardingNavigationController(rootViewController: newCommentViewController), animated: true, completion: nil)
}
func showPostsFromController(controller : UIViewController, courseID : String, topic: DiscussionTopic) {
let postsController = PostsViewController(environment: environment, courseID: courseID, topic: topic)
controller.navigationController?.pushViewController(postsController, animated: true)
}
func showDiscussionPosts(from controller: UIViewController, courseID: String, topicID: String) {
let postsController = PostsViewController(environment: environment, courseID: courseID, topicID: topicID)
controller.navigationController?.pushViewController(postsController, animated: true)
}
func showAllPostsFromController(controller : UIViewController, courseID : String, followedOnly following : Bool) {
let postsController = PostsViewController(environment: environment, courseID: courseID, following : following)
controller.navigationController?.pushViewController(postsController, animated: true)
}
func showPostsFromController(controller : UIViewController, courseID : String, queryString : String) {
let postsController = PostsViewController(environment: environment, courseID: courseID, queryString : queryString)
controller.navigationController?.pushViewController(postsController, animated: true)
}
func showDiscussionTopicsFromController(controller: UIViewController, courseID : String) {
let topicsController = DiscussionTopicsViewController(environment: environment, courseID: courseID)
controller.navigationController?.pushViewController(topicsController, animated: true)
}
func showDiscussionNewPostFromController(controller: UIViewController, courseID : String, selectedTopic : DiscussionTopic?) {
let newPostController = DiscussionNewPostViewController(environment: environment, courseID: courseID, selectedTopic: selectedTopic)
if let delegate = controller as? DiscussionNewPostViewControllerDelegate {
newPostController.delegate = delegate
}
controller.present(ForwardingNavigationController(rootViewController: newPostController), animated: true, completion: nil)
}
func showHandoutsFromController(controller : UIViewController, courseID : String) {
let handoutsViewController = CourseHandoutsViewController(environment: environment, courseID: courseID)
controller.navigationController?.pushViewController(handoutsViewController, animated: true)
}
func showProfile(controller: UIViewController? = nil, completion: ((_ success: Bool) -> ())? = nil) {
let profileViewController = ProfileOptionsViewController(environment: environment)
let navigationController = ForwardingNavigationController(rootViewController: profileViewController)
controller?.navigationController?.present(navigationController, animated: true) {
completion?(true)
}
}
func showValuePropDetailView(from controller: UIViewController? = nil, screen: CourseUpgradeScreen, course: OEXCourse, blockID: CourseBlockID? = nil, completion: (() -> Void)? = nil) {
let upgradeDetailController = ValuePropDetailViewController(screen: screen, course: course, blockID: blockID, environment: environment)
controller?.present(ForwardingNavigationController(rootViewController: upgradeDetailController), animated: true, completion: completion)
}
func showBrowserViewController(from controller: UIViewController, title: String?, url: URL, completion: (() -> Void)? = nil, modal: Bool = true) {
let browserViewController = BrowserViewController(title: title, url: url, environment: environment)
if let controller = controller as? BrowserViewControllerDelegate {
browserViewController.delegate = controller
}
if modal {
let navController = ForwardingNavigationController(rootViewController: browserViewController)
navController.modalPresentationStyle = .fullScreen
controller.present(navController, animated: true, completion: completion)
}
else {
controller.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
controller.navigationController?.pushViewController(browserViewController, animated: true)
}
}
func showBannerViewController(from controller: UIViewController, url: URL, title: String?, delegate: BannerViewControllerDelegate? = nil, modal: Bool = true, showNavbar: Bool = false) {
let bannerController = BannerViewController(url: url, title: title, environment: environment, showNavbar: showNavbar)
bannerController.delegate = delegate
if modal {
let navController = ForwardingNavigationController(rootViewController: bannerController)
navController.modalPresentationStyle = .fullScreen
controller.present(navController, animated: true, completion: nil)
}
else {
controller.navigationController?.pushViewController(bannerController, animated: true)
}
}
func showProfileForUsername(controller: UIViewController? = nil, username : String, editable: Bool = true, modal: Bool = false) {
OEXAnalytics.shared().trackProfileViewed(username: username)
let editable = self.environment.session.currentUser?.username == username
let profileController = UserProfileViewController(environment: environment, username: username, editable: editable)
if modal {
controller?.present(ForwardingNavigationController(rootViewController: profileController), animated: true, completion: nil)
}
else {
if let controller = controller {
controller.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
controller.navigationController?.pushViewController(profileController, animated: true)
} else {
showContentStack(withRootController: profileController, animated: true)
}
}
}
func showProfileEditorFromController(controller : UIViewController) {
guard let profile = environment.dataManager.userProfileManager.feedForCurrentUser().output.value else {
return
}
let editController = UserProfileEditViewController(profile: profile, environment: environment)
controller.navigationController?.pushViewController(editController, animated: true)
}
func showDownloadVideoQuality(from controller: UIViewController, delegate: VideoDownloadQualityDelegate?, modal: Bool = false) {
let videoQualityController = VideoDownloadQualityViewController.init(environment: environment, delegate: delegate)
if modal {
controller.present(ForwardingNavigationController(rootViewController: videoQualityController), animated: true, completion: nil)
} else {
controller.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
controller.navigationController?.pushViewController(videoQualityController, animated: true)
}
}
func showCertificate(url: NSURL, title: String?, fromController controller: UIViewController) {
let c = CertificateViewController(environment: environment)
c.title = title
controller.navigationController?.pushViewController(c, animated: true)
c.loadRequest(request: URLRequest(url: url as URL) as NSURLRequest)
}
func showCourseWithID(courseID: String, fromController: UIViewController, animated: Bool = true, newEnrollment: Bool = false, completion: ((UIViewController) -> Void)? = nil) {
if environment.config.isNewDashboardEnabled {
let courseDashboardViewController = NewCourseDashboardViewController(environment: environment, courseID: courseID, newEnrollment: newEnrollment)
let controller = ForwardingNavigationController(rootViewController: courseDashboardViewController)
controller.navigationController?.setNavigationBarHidden(true, animated: false)
controller.modalPresentationStyle = .fullScreen
fromController.navigationController?.presentViewControler(controller, animated: animated, completion: completion)
} else {
let controller = CourseDashboardViewController(environment: environment, courseID: courseID)
controller.hidesBottomBarWhenPushed = true
fromController.navigationController?.pushViewController(controller, animated: animated, completion: completion)
}
}
func showCourseCatalog(fromController: UIViewController? = nil, bottomBar: UIView? = nil, searchQuery: String? = nil) {
guard let controller = discoveryViewController(bottomBar: bottomBar, searchQuery: searchQuery, fromStartupScreen: true) else { return }
if let fromController = fromController {
fromController.tabBarController?.selectedIndex = EnrolledTabBarViewController.courseCatalogIndex
} else {
showControllerFromStartupScreen(controller: controller)
}
}
func discoveryViewController(bottomBar: UIView? = nil, searchQuery: String? = nil, fromStartupScreen: Bool = false) -> UIViewController? {
guard environment.config.discovery.isEnabled else { return nil }
return environment.config.discovery.type == .webview ? OEXFindCoursesViewController(environment: environment, showBottomBar: true, bottomBar: bottomBar, searchQuery: searchQuery, fromStartupScreen: fromStartupScreen) : CourseCatalogViewController(environment: environment)
}
func showProgramDetail(from controller: UIViewController, with pathId: String, bottomBar: UIView?) {
let programDetailViewController = ProgramsDiscoveryViewController(with: environment, pathId: pathId, bottomBar: bottomBar?.copy() as? UIView)
pushViewController(controller: programDetailViewController, fromController: controller)
}
private func showControllerFromStartupScreen(controller: UIViewController) {
let backButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: nil, action: nil)
backButton.oex_setAction({
controller.dismiss(animated: true, completion: nil)
})
controller.navigationItem.leftBarButtonItem = backButton
let navController = ForwardingNavigationController(rootViewController: controller)
present(navController, from:nil, completion: nil)
}
func showCourseCatalogDetail(courseID: String, fromController: UIViewController) {
let detailController = CourseCatalogDetailViewController(environment: environment, courseID: courseID)
fromController.navigationController?.pushViewController(detailController, animated: true)
}
func showAppReviewIfNeeded(fromController: UIViewController) {
if RatingViewController.canShowAppReview(environment: environment){
let reviewController = RatingViewController(environment: environment)
reviewController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
reviewController.providesPresentationContextTransitionStyle = true
reviewController.definesPresentationContext = true
if let controller = fromController as? RatingViewControllerDelegate {
reviewController.delegate = controller
}
fromController.present(reviewController, animated: false, completion: nil)
}
}
func showWhatsNew(fromController controller : UIViewController) {
let whatsNewController = WhatsNewViewController(environment: environment)
let navController = ForwardingNavigationController(rootViewController: whatsNewController)
navController.setNavigationBarHidden(true, animated: false)
controller.present(navController, animated: true, completion: nil)
}
// MARK: - LOGIN / LOGOUT
@objc func showSplash() {
removeCurrentContentController()
let splashController: UIViewController
if !environment.config.isRegistrationEnabled {
splashController = loginViewController()
}
else if environment.config.newLogistrationFlowEnabled {
splashController = StartupViewController(environment: environment)
} else {
splashController = OEXLoginSplashViewController(environment: environment)
}
makeContentControllerCurrent(splashController)
}
func pushViewController(controller: UIViewController, fromController: UIViewController) {
fromController.navigationController?.pushViewController(controller, animated: true)
}
@objc public func logout() {
invalidateToken()
environment.session.closeAndClear()
environment.session.removeAllWebData()
showLoggedOutScreen()
}
func invalidateToken() {
if let refreshToken = environment.session.token?.refreshToken, let clientID = environment.config.oauthClientID() {
let networkRequest = LogoutApi.invalidateToken(refreshToken: refreshToken, clientID: clientID)
environment.networkManager.taskForRequest(networkRequest) { _ in }
}
environment.networkManager.tokenStatus = .prelogin
}
// MARK: - Debug
func showDebugPane() {
let debugMenu = DebugMenuViewController(environment: environment)
showContentStack(withRootController: debugMenu, animated: true)
}
public func showProgramDetails(with url: URL, from controller: UIViewController) {
let programDetailsController = ProgramsViewController(environment: environment, programsURL: url, viewType: .detail)
programDetailsController.hidesBottomBarWhenPushed = true
controller.navigationController?.pushViewController(programDetailsController, animated: true)
controller.navigationController?.setNavigationBarHidden(false, animated: true)
}
@objc public func showCourseDetails(from controller: UIViewController, with coursePathID: String, bottomBar: UIView?) {
let courseInfoViewController = OEXCourseInfoViewController(environment: environment, pathID: coursePathID, bottomBar: bottomBar?.copy() as? UIView)
controller.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
controller.navigationController?.pushViewController(courseInfoViewController, animated: true)
}
}
extension OEXRouter: UINavigationControllerDelegate {
public func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
viewController.navigationController?.completionHandler()
}
}
extension UIViewController {
func find<T: UIViewController>(viewController: T.Type) -> T? {
var currentViewController = self
while let parentViewController = currentViewController.parent {
if let result = parentViewController as? T {
return result
}
currentViewController = parentViewController
}
return nil
}
}