-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathConfettiRepository.kt
More file actions
295 lines (259 loc) · 9.92 KB
/
ConfettiRepository.kt
File metadata and controls
295 lines (259 loc) · 9.92 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package dev.johnoreilly.confetti
import com.apollographql.apollo.ApolloCall
import com.apollographql.apollo.api.ApolloResponse
import com.apollographql.apollo.api.Mutation
import com.apollographql.apollo.api.Operation
import com.apollographql.apollo.exception.DefaultApolloException
import com.apollographql.cache.normalized.FetchPolicy
import com.apollographql.cache.normalized.apolloStore
import com.apollographql.cache.normalized.optimisticUpdates
import com.apollographql.cache.normalized.watch
import com.benasher44.uuid.uuid4
import dev.johnoreilly.confetti.type.buildBookmarks
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onEach
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
private fun <D : Operation.Data> ApolloCall<D>.tokenProvider(tokenProvider: TokenProvider?) = apply {
if (tokenProvider != null) {
addExecutionContext(TokenProviderContext(tokenProvider))
}
}
class ConfettiRepository : KoinComponent {
private val appSettings: AppSettings by inject()
private val apolloClientCache: ApolloClientCache by inject()
private val conferenceListeners: MutableList<suspend (String, String?) -> Unit> = mutableListOf()
fun addConferenceListener(onConferenceSet: suspend (String, String?) -> Unit = { _, _ -> }) {
conferenceListeners.add(onConferenceSet)
}
suspend fun updateConfenceListeners(conference: String, conferenceThemeColor: String?) {
conferenceListeners.forEach {
it(conference, conferenceThemeColor)
}
}
private suspend fun <D : Mutation.Data> modifyBookmarks(
conference: String,
uid: String?,
tokenProvider: TokenProvider?,
mutation: Mutation<D>,
data: (sessionIds: List<String>, id: String) -> D
): Boolean {
val client = apolloClientCache.getClient(conference, uid)
val optimisticData = try {
client.apolloStore.readOperation(GetBookmarksQuery()).data?.bookmarks?.let { bookmarks ->
data(bookmarks.sessionIds, bookmarks.id)
}
} catch (e: Exception) {
null
}
val response = client.mutation(mutation)
.tokenProvider(tokenProvider)
.apply {
if (optimisticData != null) {
optimisticUpdates(optimisticData)
}
}
.execute()
return response.data != null
}
suspend fun addBookmark(
conference: String,
uid: String?,
tokenProvider: TokenProvider?,
sessionId: String
): Boolean {
return modifyBookmarks(
conference,
uid,
tokenProvider,
AddBookmarkMutation(sessionId)
) { sessionIds, id ->
AddBookmarkMutation.Data {
addBookmark = buildBookmarks {
this.id = id
this.sessionIds = sessionIds + sessionId
}
}
}
}
suspend fun removeBookmark(
conference: String,
uid: String?,
tokenProvider: TokenProvider?,
sessionId: String
): Boolean {
return modifyBookmarks(
conference,
uid,
tokenProvider,
RemoveBookmarkMutation(sessionId)
) { sessionIds, id ->
RemoveBookmarkMutation.Data {
removeBookmark = buildBookmarks {
this.id = id
this.sessionIds = sessionIds - sessionId
}
}
}
}
/**
* ignores all errors unless the Flow terminates without an emission in which case it will emit an error
*/
private fun <D : Operation.Data> Flow<ApolloResponse<D>>.itemsOrError(operation: Operation<D>): Flow<ApolloResponse<D>> {
var hasValidResponse = false
return this.onEach {
if (it.data != null) {
hasValidResponse = true
}
}.filter {
it.data != null
}.onCompletion {
if (!hasValidResponse) {
emit(
ApolloResponse.Builder(
operation,
uuid4(),
).exception(DefaultApolloException("The flow terminated without a valid item")).build()
)
}
}
}
fun bookmarks(
conference: String,
uid: String?,
tokenProvider: TokenProvider?,
fetchPolicy: FetchPolicy
): Flow<ApolloResponse<GetBookmarksQuery.Data>> {
return apolloClientCache.getClient(conference, uid).query(GetBookmarksQuery())
.tokenProvider(tokenProvider)
.fetchPolicy(fetchPolicy)
.toFlow()
.itemsOrError(GetBookmarksQuery())
}
fun bookmarkedSessionsQuery(
conference: String,
uid: String?,
tokenProvider: TokenProvider?,
fetchPolicy: FetchPolicy
): ApolloCall<GetBookmarkedSessionsQuery.Data> {
println("JFOR, bookmarkedSessionsQuery, uid = $uid")
return apolloClientCache.getClient(conference, uid).query(GetBookmarkedSessionsQuery())
.tokenProvider(tokenProvider)
.fetchPolicy(fetchPolicy)
}
fun watchBookmarks(
conference: String,
uid: String?,
tokenProvider: TokenProvider?,
initialData: GetBookmarksQuery.Data?,
): Flow<ApolloResponse<GetBookmarksQuery.Data>> = flow {
val values = apolloClientCache.getClient(conference, uid).query(GetBookmarksQuery())
.tokenProvider(tokenProvider)
.watch(initialData)
emitAll(values)
}
suspend fun conferences(fetchPolicy: FetchPolicy): ApolloResponse<GetConferencesQuery.Data> {
return conferencesQuery(fetchPolicy)
.execute()
}
suspend fun conferenceVenue(conference: String, fetchPolicy: FetchPolicy): ApolloResponse<GetVenueQuery.Data> {
return conferenceVenueQuery(conference, fetchPolicy)
.execute()
}
fun conferencesQuery(fetchPolicy: FetchPolicy): ApolloCall<GetConferencesQuery.Data> =
apolloClientCache.getClient("all")
.query(GetConferencesQuery())
.fetchPolicy(fetchPolicy)
private fun conferenceVenueQuery(conference: String, fetchPolicy: FetchPolicy): ApolloCall<GetVenueQuery.Data> =
apolloClientCache.getClient(conference)
.query(GetVenueQuery())
.fetchPolicy(fetchPolicy)
suspend fun getConference(): String {
return appSettings.getConference()
}
suspend fun getConferenceThemeColor(): String {
return appSettings.getConferenceThemeColor()
}
/**
* This is OK to use in AppViewModel and from background refresh jobs but use with caution
* elsewhere as the conference might also come from a deep link in which case this value
* would conflict
*/
fun getConferenceFlow(): Flow<String> {
return appSettings.getConferenceFlow()
}
suspend fun setConference(conference: String, conferenceThemeColor: String?) {
println(conference)
appSettings.setConference(conference)
conferenceThemeColor?.let {
appSettings.setConferenceThemeColor(conferenceThemeColor)
}
updateConfenceListeners(conference, conferenceThemeColor)
}
suspend fun refresh(conference: String, fetchPolicy: FetchPolicy) {
try {
// TODO: We fetch the first page only, assuming there are <100 conferences. Pagination should be implemented instead.
apolloClientCache.getClient(conference)
.query(GetConferenceDataQuery())
.fetchPolicy(fetchPolicy)
.execute()
} catch (e: Exception) {
// this can be valid scenario of say offline and we get data from cache initially
// but can't connect to network.
// TODO should we surface this somewhere?
e.printStackTrace()
}
}
fun sessionDetailsQuery(
conference: String,
sessionId: String,
fetchPolicy: FetchPolicy
): ApolloCall<GetSessionQuery.Data> =
apolloClientCache.getClient(conference)
.query(GetSessionQuery(sessionId))
.fetchPolicy(fetchPolicy)
fun sessionDetails(
conference: String,
sessionId: String,
fetchPolicy: FetchPolicy
): Flow<ApolloResponse<GetSessionQuery.Data>> =
sessionDetailsQuery(conference, sessionId, fetchPolicy)
.toFlow()
suspend fun conferenceData(
conference: String,
fetchPolicy: FetchPolicy
): ApolloResponse<GetConferenceDataQuery.Data> =
apolloClientCache.getClient(conference).query(GetConferenceDataQuery())
.fetchPolicy(fetchPolicy).execute()
fun sessionsQuery(
conference: String,
fetchPolicy: FetchPolicy
): ApolloCall<GetSessionsQuery.Data> =
apolloClientCache.getClient(conference).query(GetSessionsQuery())
.fetchPolicy(fetchPolicy)
suspend fun sessions(
conference: String,
uid: String?,
tokenProvider: TokenProvider?,
fetchPolicy: FetchPolicy
): ApolloResponse<GetSessionsQuery.Data> =
apolloClientCache.getClient(conference, uid).query(GetSessionsQuery())
.tokenProvider(tokenProvider)
.fetchPolicy(fetchPolicy)
.execute()
fun conferenceHomeData(
conference: String,
fetchPolicy: FetchPolicy
): ApolloCall<GetConferenceDataQuery.Data> {
return apolloClientCache.getClient(conference)
.query(GetConferenceDataQuery())
.fetchPolicy(fetchPolicy)
}
fun speakerQuery(conference: String, id: String): ApolloCall<GetSpeakerQuery.Data> {
return apolloClientCache.getClient(conference).query(GetSpeakerQuery(id))
}
}