From e09e9d72b0425472db4ddaf17bb5bacd86521eaa Mon Sep 17 00:00:00 2001 From: jamfly Date: Thu, 3 Oct 2019 01:45:20 +0800 Subject: [PATCH] Add cache --- TAT/CurriculumViewController.swift | 13 ++++++---- TAT/ViewModels/CurriculumViewModel.swift | 10 ++++---- TAT/ViewModels/SemesterViewModel.swift | 30 +++++++++++++++++++++--- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/TAT/CurriculumViewController.swift b/TAT/CurriculumViewController.swift index cb4b4cb..e9b1d8b 100644 --- a/TAT/CurriculumViewController.swift +++ b/TAT/CurriculumViewController.swift @@ -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 }() @@ -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 @@ -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) @@ -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() } diff --git a/TAT/ViewModels/CurriculumViewModel.swift b/TAT/ViewModels/CurriculumViewModel.swift index ef4449e..a1337fc 100644 --- a/TAT/ViewModels/CurriculumViewModel.swift +++ b/TAT/ViewModels/CurriculumViewModel.swift @@ -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) } } diff --git a/TAT/ViewModels/SemesterViewModel.swift b/TAT/ViewModels/SemesterViewModel.swift index e8a139d..6ae70cf 100644 --- a/TAT/ViewModels/SemesterViewModel.swift +++ b/TAT/ViewModels/SemesterViewModel.swift @@ -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) + } }