Skip to content
This repository has been archived by the owner on Feb 12, 2020. It is now read-only.

Commit

Permalink
Add cache
Browse files Browse the repository at this point in the history
  • Loading branch information
jamfly committed Oct 2, 2019
1 parent c11844a commit e09e9d7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
13 changes: 8 additions & 5 deletions TAT/CurriculumViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class CurriculumViewController: UIViewController {

private lazy var activityIndicator: UIActivityIndicatorView = {
let activityIndicator = UIActivityIndicatorView(style: .whiteLarge)
activityIndicator.color = .blue
activityIndicator.color = .systemPink
return activityIndicator
}()

Expand Down Expand Up @@ -82,8 +82,10 @@ final class CurriculumViewController: UIViewController {
}

private func bindViewModel() {
let viewDidLoadTrigger = Observable.just(())
let searchTrigger = Observable.merge(viewDidLoadTrigger, leftBarItem.rx.tap.asObservable())
let input = CurriculumViewModel.Input(targetStudentId: Observable.just("104440026"),
searchTrigger: leftBarItem.rx.tap.asObservable())
searchTrigger: searchTrigger)
let output = viewModel.transform(input: input)

output.state
Expand All @@ -99,9 +101,10 @@ final class CurriculumViewController: UIViewController {

output.semesters
.subscribe(onNext: { [weak self] (semesters) in
print(semesters)
let semsterString = semesters.map { "\($0.year) 學年 第\($0.semester)學期" }
self?.updateTitleView(by: semsterString)
}, onError: { (error) in
print(error)
})
.disposed(by: rx.disposeBag)

Expand All @@ -119,12 +122,12 @@ final class CurriculumViewController: UIViewController {
}

private func setUpLayouts() {
setUpActivityIndicator()
setUpCollectionView()
setUpActivityIndicator()
}

private func setUpActivityIndicator() {
view.addSubview(activityIndicator)
collectionView.addSubview(activityIndicator)
activityIndicator.snp.makeConstraints { (make) in
make.center.equalToSuperview()
}
Expand Down
10 changes: 4 additions & 6 deletions TAT/ViewModels/CurriculumViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,16 @@ extension CurriculumViewModel {
func transform(input: Input) -> Output {
let semesterInput = SemesterViewModel.Input(targetStudentId: input.targetStudentId)

let semesters = semesterViewModel.transform(input: semesterInput).semesters
let semesterOutput = semesterViewModel.transform(input: semesterInput)

let courseInput = CourseViewModel.Input(year: Observable.just("108"),
semester: Observable.just("1"),
targetStudentId: input.targetStudentId,
searchTrigger: input.searchTrigger)
let courseOutput = courseViewModel.transform(input: courseInput)
let state = courseOutput.state
let courses = courseOutput.courses
return Output(state: state,
semesters: semesters,
courses: courses)
return Output(state: courseOutput.state,
semesters: semesterOutput.semesters,
courses: courseOutput.courses)
}

}
30 changes: 27 additions & 3 deletions TAT/ViewModels/SemesterViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,36 @@ final class SemesterViewModel: NSObject, ViewModelType {
extension SemesterViewModel {

func transform(input: SemesterViewModel.Input) -> SemesterViewModel.Output {
let semesters = input.targetStudentId
.filter { $0 != "" }
.flatMap { [unowned self] (targetStudentId) -> Observable<[Semester]> in
self.generateSemesters(from: targetStudentId)
}
.share()

let semesters = input.targetStudentId.flatMap { [unowned self] (targetStudentId) -> Observable<[Semester]> in
return self.curriculumsUseCase.semesters(targetStudentId: targetStudentId)
}
semesters
.subscribe(onNext: { (semesters) in
if UserDefaults.standard.object(forKey: "semesters") == nil {
guard let semesters = try? JSONEncoder().encode(semesters) else { return }
UserDefaults.standard.set(semesters, forKey: "semesters")
}
}, onError: { (error) in
print(error)
})
.disposed(by: rx.disposeBag)

return Output(semesters: semesters)
}

private func generateSemesters(from targetStudentId: String) -> Observable<[Semester]> {
guard let cachedTargetStudentId = UserDefaults.standard.string(forKey: "targetStudentId"),
cachedTargetStudentId == targetStudentId else {
UserDefaults.standard.set(targetStudentId, forKey: "targetStudentId")
return curriculumsUseCase.semesters(targetStudentId: targetStudentId)
}
guard let cachedData = UserDefaults.standard.object(forKey: "semesters") as? Data,
let cachedSemesters = try? JSONDecoder().decode([Semester].self, from: cachedData)
else { fatalError("cannot cast to semesters") }
return Observable.just(cachedSemesters)
}
}

0 comments on commit e09e9d7

Please sign in to comment.