Skip to content

Instantly share code, notes, and snippets.

@kodekarim
Created February 10, 2019 03:39
Show Gist options
  • Select an option

  • Save kodekarim/33532ff5a978f58ed006e9f41b3aa315 to your computer and use it in GitHub Desktop.

Select an option

Save kodekarim/33532ff5a978f58ed006e9f41b3aa315 to your computer and use it in GitHub Desktop.
Trying to assign the dict in bindables in CaseDetailsVM.swift not working but assigning array works in SmallCaseListVM.swift
import Foundation
class CaseDetailsVM {
enum CaseDeatilsViewCellType {
case normal(cellViewModel: CaseDetailsCellVM)
case error(message: String)
case empty
}
var onShowError: ((_ alert: SingleButtonAlert) -> Void)?
let showLoadingHud: Bindable = Bindable(false)
let caseDetailsCardCell = Bindable(CaseDeatilsViewCellType.self)
let appServerClient: AppServerClient
init(appServerClient: AppServerClient = AppServerClient()) {
self.appServerClient = appServerClient
}
func getSmallCaseList(id:String) {
showLoadingHud.value = true
appServerClient.getSmallCaseDetails(scid: id, completion: { [weak self] result in
self?.showLoadingHud.value = false
switch result {
case .success(let data):
guard data.success == true else {
self?.caseDetailsCardCell.value = [.empty]
return
}
self?.caseDetailsCardCell.value = data.compactMap { .normal(cellViewModel: $0 as CaseCardCellVM)}
case .failure(let error):
self?.caseDetailsCardCell.value = [.error(message: error?.getErrorMessage() ?? "Loading failed, check network connection")]
}
})
}
}
fileprivate extension AppServerClient.GetSmallCaseFailureReason {
func getErrorMessage() -> String? {
switch self {
case .unAuthorized:
return "UnAuthorized"
case .notFound:
return "Could not complete request, please try again."
}
}
}
class SmallCaseListVM {
enum SmallCaseViewCellType {
case normal(cellViewModel: CaseCardCellVM)
case error(message: String)
case empty
}
var onShowError: ((_ alert: SingleButtonAlert) -> Void)?
let showLoadingHud: Bindable = Bindable(false)
let caseCardCells = Bindable([SmallCaseViewCellType]())
let appServerClient: AppServerClient
init(appServerClient: AppServerClient = AppServerClient()) {
self.appServerClient = appServerClient
}
func getListImages() {
appServerClient.getListData(completion: { [weak self] result in
self?.showLoadingHud.value = false
switch result {
case .success(let data):
guard data.count > 0 else {
self?.caseCardCells.value = [.empty]
return
}
self?.caseCardCells.value = data.compactMap { .normal(cellViewModel: $0 as CaseCardCellVM)}
case .failure(let error):
self?.caseCardCells.value = [.error(message: error?.getErrorMessage() ?? "Loading failed, check network connection")]
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment