-
Notifications
You must be signed in to change notification settings - Fork 0
/
DetailsViewModel.swift
68 lines (54 loc) · 1.57 KB
/
DetailsViewModel.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// DetailsViewModel.swift
// Test1
//
// Created by Primeholding on 13.09.18.
// Copyright © 2018 Primeholding. All rights reserved.
//
import Foundation
import RxSwift
import RxCocoa
// MARK: - Protocols
protocol DetailsViewModelInput: class {
func fetch()
}
protocol DetailsViewModelOutput {
var details: Driver<String> { get }
var isLoading: Driver<Bool> { get }
}
protocol DetailsViewModelType {
var input: DetailsViewModelInput { get }
var output: DetailsViewModelOutput { get }
}
// MARK: - View Model Implementaion
class DetailsViewModel: DetailsViewModelInput, DetailsViewModelOutput, DetailsViewModelType {
// MARK: Outputs
let details: Driver<String>
var isLoading: Driver<Bool> {
return self.loadingViewModel.isLoading.asDriver(onErrorJustReturn: false)
}
// MARK: Internaly used
private let loadingViewModel: LoadingViewModel
// MARK: Initialization
init(repo: DetailsRepository) {
let dataRequest = fetchSubject
.flatMapLatest { repo.fetch() }
.share(replay: 1)
details = dataRequest
.whenSuccess()
.startWith("")
.asDriver(onErrorJustReturn: "")
loadingViewModel = LoadingViewModel([
dataRequest.isLoading()
])
}
// MARK: Inputs
private let fetchSubject = PublishRelay<Void>()
func fetch() {
fetchSubject.accept(())
}
// MARK: InputOutput
var input: DetailsViewModelInput { return self }
var output: DetailsViewModelOutput { return self }
}
// MARK: - Custom Operators