Skip to content

Instantly share code, notes, and snippets.

@manuelvicnt
Created November 14, 2019 12:53
Show Gist options
  • Save manuelvicnt/0f687f1f1692f95b1b7b809674f68319 to your computer and use it in GitHub Desktop.
Save manuelvicnt/0f687f1f1692f95b1b7b809674f68319 to your computer and use it in GitHub Desktop.
/* Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
class DefaultSessionAndUserEventRepository(
private val userEventDataSource: UserEventDataSource,
private val sessionRepository: SessionRepository
) : SessionAndUserEventRepository {
override fun getObservableUserEvent(
userId: String?,
eventId: SessionId
): Flow<Result<LoadUserSessionUseCaseResult>> {
// Handles null userId
// Observes the user events and merges them with session data
return userEventDataSource.getObservableUserEvent(userId, eventId).map { userEventResult ->
// lambda of the map operator that can call suspend functions
val event = sessionRepository.getSession(eventId)
// Merges session with user data and emits the result
val userSession = UserSession(
event,
userEventResult.userEvent ?: createDefaultUserEvent(event)
)
Result.Success(LoadUserSessionUseCaseResult(userSession))
}
}
}
@AfterBongHit
Copy link

ViewModel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment