This file contains 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
import UIKit | |
import Combine | |
class CombineController: UIViewController { | |
@IBOutlet weak var tableView: UITableView! | |
@IBOutlet weak var emptyView: UIView! | |
@IBOutlet weak var activityIndicator: UIActivityIndicatorView! | |
lazy var viewModel: CombineViewModel = { |
This file contains 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
import Foundation | |
import Alamofire | |
import Combine | |
class CombineViewModel: ObservableObject { | |
var apiManager: APIManager? | |
@Published var employees: [Employee] = [] //1 | |
init(manager: APIManager = APIManager()) { |
This file contains 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
import RxSwift | |
import RxCocoa | |
class RxSwiftController: UIViewController { | |
@IBOutlet weak var tableView: UITableView! | |
@IBOutlet weak var emptyView: UIView! | |
@IBOutlet weak var activityIndicator: UIActivityIndicatorView! | |
let disposeBag = DisposeBag() |
This file contains 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
import Foundation | |
import Alamofire | |
import RxSwift | |
import RxCocoa | |
class RxSwiftViewModel { | |
private let disposeBag = DisposeBag() | |
private let _employees = BehaviorRelay<[Employee]>(value: []) | |
private let _error = BehaviorRelay<Bool>(value: false) |
This file contains 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
/********* Binding to array in viewDidLoad */ | |
viewModel.employees.bind { (_) in | |
self.showTableView() | |
} | |
/********************************/ |
This file contains 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
func setupEventBusSubscriber() { | |
_ = EventBus.onMainThread(self, name: "fetchEmployees") { result in | |
if let event = result!.object as? EmployeesEvent { | |
if event.employees != nil { | |
self.showTableView() | |
} else if let message = event.errorMessage { | |
self.showAlert(title: "Error", message: message) | |
} | |
} | |
} |
This file contains 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
func callEvent() { | |
//Post Event (Publish Event) | |
EventBus.post("fetchEmployees", sender: EmployeesEvent(error: error, errorMessage: errorMessage, employees: employees)) | |
} |
This file contains 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
import Foundation | |
class EmployeesEvent: NSObject { | |
var error: Bool | |
var errorMessage: String? | |
var employees: [Employee]? | |
init(error: Bool, errorMessage: String? = nil, employees: [Employee]? = nil) { | |
self.error = error | |
self.errorMessage = errorMessage |
This file contains 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
import Foundation | |
open class EventBus { | |
struct Static { | |
static let instance = EventBus() | |
static let queue = DispatchQueue(label: "EventBus", attributes: []) | |
} |
This file contains 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
import Foundation | |
import Alamofire | |
protocol ObservableViewModelProtocol { | |
func fetchEmployees() | |
func setError(_ message: String) | |
var employees: Observable<[Employee]> { get set } //1 | |
var errorMessage: Observable<String?> { get set } | |
var error: Observable<Bool> { get set } | |
} |
NewerOlder