Skip to content

Instantly share code, notes, and snippets.

@pallavtrivedi03
Created December 25, 2021 08:33
Show Gist options
  • Save pallavtrivedi03/e0363f4a6472c8e4ac40454588df25c9 to your computer and use it in GitHub Desktop.
Save pallavtrivedi03/e0363f4a6472c8e4ac40454588df25c9 to your computer and use it in GitHub Desktop.
import Foundation
import Combine
class SignUpViewModel: ObservableObject {
@Published var userProfile: UserProfileModel?
private var cancellables = Set<AnyCancellable>()
func getOnboardingData() {
let publishers = Publishers.Zip(
getPublisher(for: SignUpCouponsModel.self, url: URLList.coupons),
getPublisher(for: SignUpProfileModel.self, url: URLList.profile)
)
publishers.map { (couponsModel, profileModel) in
UserProfileModel(profileData: profileModel, rewardedCoupons: [couponsModel])
}
.sink { (completion) in
if case let .failure(error) = completion {
print("Error -> \(error.localizedDescription)")
}
} receiveValue: { [weak self] profileModel in
self?.userProfile = profileModel
}
.store(in: &cancellables)
}
func getPublisher<T: Decodable>(for type: T.Type, url: String) -> Future<T, Error> {
NetworkManager.shared.getData(url: url, type: T.self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment