Created
July 17, 2018 19:40
-
-
Save aabanaag/cffb1dfe2562d971858d755a3692e03b to your computer and use it in GitHub Desktop.
Attendee Photo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func bindAttendeePhoto() { | |
let attendeePhoto = viewModel.photo | |
.flatMap { image -> Observable<UIImage> in | |
self.printBadge() | |
return Observable.just(image!) | |
} | |
.observeOn(ConcurrentDispatchQueueScheduler(qos: .background)) | |
.flatMapLatest { image -> Observable<(UIImage, Bool)> in | |
let eventId = self.event.id | |
let fileSize = image.getFilesize() | |
let isUploaded = self.viewModel.uploadAttendeePhoto(eventId: eventId!, fileSize: fileSize) | |
return Observable.combineLatest(Observable.just(image), isUploaded) | |
} | |
.share() | |
attendeePhoto | |
.map { $0 } | |
.observeOn(MainScheduler.instance) | |
.subscribe(onNext: { [weak self] image, _ in | |
if !(self?.viewModel.isPhotoSaved.value)! { | |
self?.saveImage(image: image) | |
} | |
}, onError: { err in | |
NotificationCenter.default.post(name: .attendeePhotoFailed, object: nil, userInfo: [ | |
"error": err, | |
"attendeeId": self.viewModel.attendeeId.value | |
]) | |
}) | |
.disposed(by: bag) | |
attendeePhoto | |
.map { $1 } | |
.bind(to: viewModel.isUploaded) | |
.disposed(by: bag) | |
viewModel.isUploaded | |
.asDriver(onErrorJustReturn: false) | |
.drive(onNext: { [weak self] isUploaded in | |
self?.toggleAttendeePhoto(hasUploaded: isUploaded) | |
}) | |
.disposed(by: bag) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment