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 191
/
EnrolledCoursesViewController.swift
233 lines (194 loc) · 8.71 KB
/
EnrolledCoursesViewController.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
//
// EnrolledCoursesViewController.swift
// edX
//
// Created by Akiva Leffert on 12/21/15.
// Copyright © 2015 edX. All rights reserved.
//
import Foundation
var isActionTakenOnUpgradeSnackBar: Bool = false
fileprivate var isFirstLoad: Bool = true
class EnrolledCoursesViewController : OfflineSupportViewController, CoursesTableViewControllerDelegate, PullRefreshControllerDelegate, LoadStateViewReloadSupport {
typealias Environment = OEXAnalyticsProvider & OEXConfigProvider & DataManagerProvider & NetworkManagerProvider & ReachabilityProvider & OEXRouterProvider
private let environment : Environment
private let tableController : CoursesTableViewController
private let loadController = LoadStateViewController()
private let refreshController = PullRefreshController()
private let insetsController = ContentInsetsController()
fileprivate let enrollmentFeed: Feed<[UserCourseEnrollment]?>
private let userPreferencesFeed: Feed<UserPreference?>
init(environment: Environment) {
self.tableController = CoursesTableViewController(environment: environment, context: .EnrollmentList)
self.enrollmentFeed = environment.dataManager.enrollmentManager.feed
self.userPreferencesFeed = environment.dataManager.userPreferenceManager.feed
self.environment = environment
super.init(env: environment)
self.navigationItem.title = Strings.myCourses
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.accessibilityIdentifier = "enrolled-courses-screen"
addChildViewController(tableController)
tableController.didMove(toParentViewController: self)
self.loadController.setupInController(controller: self, contentView: tableController.view)
self.view.addSubview(tableController.view)
tableController.view.snp_makeConstraints {make in
make.edges.equalTo(self.view)
}
tableController.delegate = self
self.view.backgroundColor = OEXStyles.shared().standardBackgroundColor()
refreshController.setupInScrollView(scrollView: self.tableController.tableView)
refreshController.delegate = self
insetsController.setupInController(owner: self, scrollView: tableController.tableView)
insetsController.addSource(source: self.refreshController)
// We visually separate each course card so we also need a little padding
// at the bottom to match
insetsController.addSource(
source: ConstantInsetsSource(insets: UIEdgeInsets(top: 0, left: 0, bottom: StandardVerticalMargin, right: 0), affectsScrollIndicators: false)
)
self.enrollmentFeed.refresh()
self.userPreferencesFeed.refresh()
setupListener()
setupFooter()
setupObservers()
}
override func viewWillAppear(_ animated: Bool) {
environment.analytics.trackScreen(withName: OEXAnalyticsScreenMyCourses)
showVersionUpgradeSnackBarIfNecessary()
super.viewWillAppear(animated)
hideSnackBarForFullScreenError()
showWhatsNewIfNeeded()
}
override func reloadViewData() {
refreshIfNecessary()
}
private func setupListener() {
enrollmentFeed.output.listen(self) {[weak self] result in
if !(self?.enrollmentFeed.output.active ?? false) {
self?.refreshController.endRefreshing()
}
switch result {
case let Result.success(enrollments):
if let enrollments = enrollments {
self?.tableController.courses = enrollments.flatMap { $0.course }
self?.tableController.tableView.reloadData()
self?.loadController.state = .Loaded
if enrollments.count <= 0 {
self?.enrollmentsEmptyState()
}
}
else {
self?.loadController.state = .Initial
}
case let Result.failure(error):
//App is showing occasionally error on app launch, so skipping first error on app launch
//TODO: Find exact root cause of error and remove this patch
if isFirstLoad {
isFirstLoad = false
self?.enrollmentFeed.refresh()
return
}
self?.loadController.state = LoadState.failed(error: error)
if error.errorIsThisType(NSError.oex_outdatedVersionError()) {
self?.hideSnackBar()
}
}
}
}
private func setupFooter() {
if environment.config.courseEnrollmentConfig.isCourseDiscoveryEnabled() {
let footer = EnrolledCoursesFooterView()
footer.findCoursesAction = {[weak self] in
self?.environment.router?.showCourseCatalog(bottomBar: nil)
}
footer.sizeToFit()
self.tableController.tableView.tableFooterView = footer
}
else {
tableController.tableView.tableFooterView = UIView()
}
}
private func enrollmentsEmptyState() {
if !environment.config.courseEnrollmentConfig.isCourseDiscoveryEnabled() {
let error = NSError.oex_error(with: .unknown, message: Strings.EnrollmentList.noEnrollment)
loadController.state = LoadState.failed(error: error, icon: Icon.UnknownError)
}
}
private func setupObservers() {
let config = environment.config
NotificationCenter.default.oex_addObserver(observer: self, name: NSNotification.Name.OEXExternalRegistrationWithExistingAccount.rawValue) { (notification, observer, _) -> Void in
let platform = config.platformName()
let service = notification.object as? String ?? ""
let message = Strings.externalRegistrationBecameLogin(platformName: platform, service: service)
observer.showOverlay(withMessage: message)
}
NotificationCenter.default.oex_addObserver(observer: self, name: AppNewVersionAvailableNotification) { (notification, observer, _) -> Void in
observer.showVersionUpgradeSnackBarIfNecessary()
}
}
func refreshIfNecessary() {
if environment.reachability.isReachable() && !enrollmentFeed.output.active {
enrollmentFeed.refresh()
if loadController.state.isError {
loadController.state = .Initial
}
}
}
private func showVersionUpgradeSnackBarIfNecessary() {
if let _ = VersionUpgradeInfoController.sharedController.latestVersion {
var infoString = Strings.VersionUpgrade.newVersionAvailable
if let _ = VersionUpgradeInfoController.sharedController.lastSupportedDateString {
infoString = Strings.VersionUpgrade.deprecatedMessage
}
if !isActionTakenOnUpgradeSnackBar {
showVersionUpgradeSnackBar(string: infoString)
}
}
else {
hideSnackBar()
}
}
private func hideSnackBarForFullScreenError() {
if tableController.courses.count <= 0 {
hideSnackBar()
}
}
func coursesTableChoseCourse(course: OEXCourse) {
if let course_id = course.course_id {
self.environment.router?.showCourseWithID(courseID: course_id, fromController: self, animated: true)
}
else {
preconditionFailure("course without a course id")
}
}
private func showWhatsNewIfNeeded() {
if WhatsNewViewController.canShowWhatsNew(environment: environment as? RouterEnvironment) {
environment.router?.showWhatsNew(fromController: self)
}
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableController.tableView.autolayoutFooter()
}
//MARK:- PullRefreshControllerDelegate method
func refreshControllerActivated(controller: PullRefreshController) {
self.enrollmentFeed.refresh()
self.userPreferencesFeed.refresh()
}
//MARK:- LoadStateViewReloadSupport method
func loadStateViewReload() {
refreshIfNecessary()
}
}
// For testing use only
extension EnrolledCoursesViewController {
var t_loaded: OEXStream<()> {
return self.enrollmentFeed.output.map {_ in
return ()
}
}
}