Skip to content

Commit

Permalink
Fixes details screen showing incorrect data during transition animations
Browse files Browse the repository at this point in the history
Bug: 77988949

Change-Id: Idf302d99d6ccf27a275378839841ff9f7ac802a4
  • Loading branch information
JoseAlcerreca committed Apr 16, 2018
1 parent 06e39cf commit 9541971
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import org.threeten.bp.Duration
import org.threeten.bp.ZoneId
import org.threeten.bp.ZonedDateTime
import timber.log.Timber
import java.util.*
import java.util.UUID
import javax.inject.Inject

private const val TEN_SECONDS = 10_000L
Expand Down Expand Up @@ -338,7 +338,6 @@ class SessionDetailViewModel @Inject constructor(
override fun onCleared() {
// Clear subscriptions that might be leaked or that will not be used in the future.
loadUserSessionUseCase.onCleared()
loadRelatedSessionUseCase.onCleared()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ open class DefaultSessionAndUserEventRepository @Inject constructor(
) : SessionAndUserEventRepository {

private val sessionsByDayResult = MediatorLiveData<Result<LoadUserSessionsByDayUseCaseResult>>()
private val sessionResult = MediatorLiveData<Result<LoadUserSessionUseCaseResult>>()

// Keep a reference to the observable for a single event (from the details screen) so we can
// stop observing when done.
Expand Down Expand Up @@ -109,6 +108,7 @@ open class DefaultSessionAndUserEventRepository @Inject constructor(
userId: String?,
eventId: SessionId
): LiveData<Result<LoadUserSessionUseCaseResult>> {
val sessionResult = MediatorLiveData<Result<LoadUserSessionUseCaseResult>>()

// If there is no logged-in user, return the session with a null UserEvent
if (userId == null) {
Expand Down Expand Up @@ -253,12 +253,8 @@ open class DefaultSessionAndUserEventRepository @Inject constructor(
}

override fun clearSingleEventSubscriptions() {
// The details screen is closing so reset subscriptions and old values
sessionResult.value = null
// The UserEvent data source can stop observing user data
userEventDataSource.clearSingleEventSubscriptions()
// Also free its observable in the repository.
observableUserEvent?.let { sessionResult.removeSource(it) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.samples.apps.iosched.shared.domain.sessions

import android.arch.lifecycle.LiveData
import com.google.samples.apps.iosched.shared.data.userevent.DefaultSessionAndUserEventRepository
import com.google.samples.apps.iosched.shared.data.userevent.UserEventMessage
import com.google.samples.apps.iosched.shared.domain.MediatorUseCase
Expand All @@ -29,13 +30,19 @@ open class LoadUserSessionUseCase @Inject constructor(
private val userEventRepository: DefaultSessionAndUserEventRepository
) : MediatorUseCase<Pair<String?, SessionId>, LoadUserSessionUseCaseResult>() {

private var userSession: LiveData<Result<LoadUserSessionUseCaseResult>>? = null

override fun execute(parameters: Pair<String?, SessionId>) {
val (userId, eventId) = parameters
val userSession = userEventRepository.getObservableUserEvent(userId, eventId)

result.removeSource(userSession)
result.value = null
result.addSource(userSession) {
// Remove old data sources
clearSources()

// Fetch an observable of the data
val newUserSession = userEventRepository.getObservableUserEvent(userId, eventId)

// Post new values to the result object.
result.addSource(newUserSession) {
DefaultScheduler.execute {
when (it) {
is Result.Success -> {
Expand All @@ -51,11 +58,19 @@ open class LoadUserSessionUseCase @Inject constructor(
}
}
}
// Save a reference to the observable for later cleaning of sources.
userSession = newUserSession
}

fun onCleared() {
// This use case is no longer going to be used so remove subscriptions
userEventRepository.clearSingleEventSubscriptions()
clearSources()
}

private fun clearSources() {
userSession?.let {
result.removeSource(it)
}
result.value = null
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import com.google.samples.apps.iosched.shared.domain.internal.DefaultScheduler
import com.google.samples.apps.iosched.shared.model.SessionId
import com.google.samples.apps.iosched.shared.model.UserSession
import com.google.samples.apps.iosched.shared.result.Result
import timber.log.Timber
import javax.inject.Inject

/**
Expand Down

0 comments on commit 9541971

Please sign in to comment.