Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 38 additions & 32 deletions StepicAdaptiveCourse/AdaptiveStepsPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ enum AdaptiveStepsViewState {
case connectionError
case normal
case congratulation
case coursePassed
}

protocol AdaptiveStepsView: class {
Expand Down Expand Up @@ -203,16 +204,18 @@ class AdaptiveStepsPresenter {
self.lessonsAPI?.retrieve(ids: lessonsIds, existing: [], refreshMode: .update, success: { (newLessonsImmutable) -> Void in
self.recommendedLessons = newLessonsImmutable

let lesson = self.recommendedLessons.first
guard let lesson = self.recommendedLessons.first else {
self.view?.state = .coursePassed
return
}

self.recommendedLessons.remove(at: 0)

if let lesson = lesson {
print("recommendations: using lesson = \(lesson.id)")
self.currentLesson = lesson
self.getStep(for: lesson, success: { step in
success(step)
})
}
print("recommendations: using lesson = \(lesson.id)")
self.currentLesson = lesson
self.getStep(for: lesson, success: { step in
success(step)
})

}, error: { (error) -> Void in
print("recommendations: failed downloading lessons data in Next")
Expand All @@ -221,33 +224,36 @@ class AdaptiveStepsPresenter {
})
} else {
print("recommendations: recommendations loaded (count = \(self.recommendedLessons.count)), using loaded lesson...")
let lesson = self.recommendedLessons.first

guard let lesson = self.recommendedLessons.first else {
view?.state = .coursePassed
return
}

self.recommendedLessons.remove(at: 0)

if let lesson = lesson {
print("recommendations: preloaded lesson = \(lesson.id)")
self.currentLesson = lesson
self.getStep(for: lesson, success: { step in
success(step)

// Load next batch
if self.recommendedLessons.count < self.nextRecommendationsBatch {
print("recommendations: recommendations loaded, loading next \(self.recommendationsBatchSize) lessons...")
self.loadRecommendations(for: course, count: self.recommendationsBatchSize, success: { recommendedLessons in
print("recommendations: loaded lessons: \(recommendedLessons.map{$0.id})")
var existingLessons = self.recommendedLessons.map { $0.id }
// Add current lesson cause we should ignore it while merging
existingLessons.append(lesson.id)
recommendedLessons.forEach { lesson in
if !existingLessons.contains(lesson.id) {
self.recommendedLessons.append(lesson)
}
print("recommendations: preloaded lesson = \(lesson.id)")
self.currentLesson = lesson
self.getStep(for: lesson, success: { step in
success(step)

// Load next batch
if self.recommendedLessons.count < self.nextRecommendationsBatch {
print("recommendations: recommendations loaded, loading next \(self.recommendationsBatchSize) lessons...")
self.loadRecommendations(for: course, count: self.recommendationsBatchSize, success: { recommendedLessons in
print("recommendations: loaded lessons: \(recommendedLessons.map{$0.id})")
var existingLessons = self.recommendedLessons.map { $0.id }
// Add current lesson cause we should ignore it while merging
existingLessons.append(lesson.id)
recommendedLessons.forEach { lesson in
if !existingLessons.contains(lesson.id) {
self.recommendedLessons.append(lesson)
}
})
}
})
}
}
})
}

})
}
}

Expand Down
8 changes: 7 additions & 1 deletion StepicAdaptiveCourse/AdaptiveStepsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class AdaptiveStepsViewController: UIViewController, AdaptiveStepsView {
case .normal:
self.placeholderView.isHidden = true
self.kolodaView.isHidden = false
case .connectionError:
case .connectionError, .coursePassed:
self.placeholderView.isHidden = false
self.kolodaView.isHidden = true
default:
Expand Down Expand Up @@ -204,6 +204,8 @@ extension AdaptiveStepsViewController: PlaceholderViewDataSource {
switch state {
case .connectionError:
return Images.placeholders.connectionError
case .coursePassed:
return Images.placeholders.coursePassed
default:
return nil
}
Expand All @@ -222,6 +224,8 @@ extension AdaptiveStepsViewController: PlaceholderViewDataSource {
switch state {
case .connectionError:
return nil
case .coursePassed:
return NSLocalizedString("NoRecommendations", comment: "")
default:
return nil
}
Expand All @@ -237,6 +241,8 @@ extension AdaptiveStepsViewController: PlaceholderViewDataSource {
switch state {
case .connectionError:
return NSLocalizedString("ConnectionErrorText", comment: "")
case .coursePassed:
return NSLocalizedString("CoursePassed", comment: "")
default:
return nil
}
Expand Down