-
Notifications
You must be signed in to change notification settings - Fork 2
/
ApiBindingViewModel.kt
196 lines (183 loc) · 6.67 KB
/
ApiBindingViewModel.kt
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package kim.jeonghyeon.sample.viewmodel
import kim.jeonghyeon.client.flowSingle
import kim.jeonghyeon.client.toData
import kim.jeonghyeon.client.viewModelFlow
import kim.jeonghyeon.net.bindApi
import kim.jeonghyeon.sample.api.AnnotationAction
import kim.jeonghyeon.sample.api.SampleApi
import kim.jeonghyeon.sample.api.UserApi
import kim.jeonghyeon.sample.di.serviceLocator
import kim.jeonghyeon.util.log
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.merge
class ApiBindingViewModel(val api: SampleApi = serviceLocator.sampleApi, val userApi: UserApi = serviceLocator.userApi) : ModelViewModel() {
//todo [KSA-48] support localization on kotlin side
override val title: String = "Api Binding"
val result = viewModelFlow<String>()
override fun onInit() {
result.load(initStatus) {
api.getIncreasedNumber().toString()
}
}
fun onClickBind2Api() {
result.loadInIdle(status) {
bindApi {
api.getIncreasedNumber()
}.bindApi {
api.getIncreasedNumber()
}.execute().toString()
}
}
fun onClickBind3Api() {
result.loadInIdle(status) {
bindApi {
api.getIncreasedNumber()
}.bindApi {
api.getIncreasedNumber()
}.bindApi { _, _ ->
api.getIncreasedNumber()
}.execute().toString()
}
}
fun onClickBindResposneToParameter() {
result.loadInIdle(status) {
bindApi {
api.getIncreasedNumber()
}.bindApi {
api.getIncreasedNumber()
}.bindApi { first, second ->
api.minus(first.bindParameter(0), second.bindParameter(1))
}.execute().toString()
}
}
fun onClickBindResposneFieldToParameter() {
result.loadInIdle(status) {
bindApi {
api.getAnnotation("key1", AnnotationAction.QUERY, "header")
}.bindApi {
//bind field
api.repeat(it.bindParameter(0) { response ->
response::key
}, 2)
}.bindApi { first, _ ->
//bind field's field
api.repeat(first.bindParameter(0) { response ->
val bind = response::data.bind()
bind::second
}, 2)
}.execute().toString()
}
}
/**
* todo https://hyun.myjetbrains.com/youtrack/issue/KSA-138
* currently just return error. but it'll be possible to return previous success data when middle api occur error
* so, client decide to retry from middle or from first
*/
fun onClickHandleError() {
result.loadInIdle(status) {
bindApi {
api.getSuccess()
}.bindApi {
//bind field
api.getRandomError(2)
}.bindApi { _, _ ->
//bind field's field
api.getSuccess()
}.execute().toString()
}
}
fun onClickBindApiAuthRequired() {
result.loadInIdle(status) {
bindApi {
api.getIncreasedNumber()
}.bindApi {
userApi.getUser()
}.execute().toString()
}
}
}
// TODO reactive way.
//class ApiBindingViewModel2(val api: SampleApi = serviceLocator.sampleApi, val userApi: UserApi = serviceLocator.userApi) : ModelViewModel() {
//
// //todo [KSA-48] support localization on kotlin side
// override val title: String = "Api Binding"
//
// val clickBind2Api = viewModelFlow<Unit>()
// val clickBind3Api = viewModelFlow<Unit>()
// val clickBindResposneToParameter = viewModelFlow<Unit>()
// val clickBindResposneFieldToParameter = viewModelFlow<Unit>()
// val clickHandleError = viewModelFlow<Unit>()
// val clickBindApiAuthRequired = viewModelFlow<Unit>()
//
// val result by add {
// merge(
// initFlow.mapInIdle {
// api.getIncreasedNumber().toString()
// }.toData(scope, initStatus),
//
// merge(
// clickBind2Api.mapInIdle {
// bindApi {
// api.getIncreasedNumber()
// }.bindApi {
// api.getIncreasedNumber()
// }.execute().toString()
// },
// clickBind3Api.mapInIdle {
// bindApi {
// api.getIncreasedNumber()
// }.bindApi {
// api.getIncreasedNumber()
// }.bindApi { _, _ ->
// api.getIncreasedNumber()
// }.execute().toString()
// },
// clickBindResposneToParameter.mapInIdle {
// bindApi {
// api.getIncreasedNumber()
// }.bindApi {
// api.getIncreasedNumber()
//
// }.bindApi { first, second ->
// api.minus(first.bindParameter(0), second.bindParameter(1))
// }.execute().toString()
// },
// clickBindResposneFieldToParameter.mapInIdle {
// bindApi {
// api.getAnnotation("key1", AnnotationAction.QUERY, "header")
// }.bindApi {
// //bind field
// api.repeat(it.bindParameter(0) { response ->
// response::key
// }, 2)
// }.bindApi { first, _ ->
// //bind field's field
// api.repeat(first.bindParameter(0) { response ->
// val bind = response::data.bind()
// bind::second
// }, 2)
// }.execute().toString()
// },
// clickHandleError.mapInIdle {
// bindApi {
// api.getSuccess()
// }.bindApi {
// //bind field
// api.getRandomError(2)
// }.bindApi { _, _ ->
// //bind field's field
// api.getSuccess()
// }.execute().toString()
// },
// clickBindApiAuthRequired.mapInIdle {
// bindApi {
// api.getIncreasedNumber()
// }.bindApi {
// userApi.getUser()
// }.execute().toString()
// }
// )
// .toData(scope, status)
// )
// }
//}