Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit b0c6384

Browse files
committed
Enables schedule day navigation before user interaction
Change-Id: I634d706f8b46183c6f3eb6e98086ed0bacfde0d4
1 parent da3a514 commit b0c6384

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

mobile/src/main/java/com/google/samples/apps/iosched/ui/schedule/ScheduleViewModel.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,11 @@ class ScheduleViewModel @Inject constructor(
213213
} else {
214214
val index = result.data?.firstUnfinishedSessionIndex ?: return@combineTransform
215215
if (index != -1) {
216+
// User hasn't interacted yet and conference is happening
216217
emit(ScheduleScrollEvent(index))
218+
} else {
219+
// User hasn't interacted but conference not in progress, scroll to first event
220+
emit(ScheduleScrollEvent(currentEventIndex))
217221
}
218222
}
219223
}.shareIn(viewModelScope, WhileViewSubscribed, replay = 0) // Don't replay on rotation

mobile/src/test/java/com/google/samples/apps/iosched/ui/schedule/ScheduleViewModelTest.kt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ import kotlinx.coroutines.flow.Flow
6464
import kotlinx.coroutines.flow.first
6565
import kotlinx.coroutines.flow.firstOrNull
6666
import kotlinx.coroutines.flow.flow
67+
import kotlinx.coroutines.flow.toList
68+
import kotlinx.coroutines.launch
6769
import kotlinx.coroutines.test.TestCoroutineDispatcher
6870
import org.hamcrest.MatcherAssert.assertThat
6971
import org.hamcrest.core.Is.`is`
@@ -359,6 +361,69 @@ class ScheduleViewModelTest {
359361
)
360362
}
361363

364+
@Test
365+
fun scrollToEvent_beforeconference() {
366+
scrollToEvent_beforeConference(dayIndex = 0, targetPosition = 0)
367+
}
368+
369+
@Test
370+
fun scrollToEvent_beforeConference_clickOnSecondDay() {
371+
scrollToEvent_beforeConference(dayIndex = 1, targetPosition = 2)
372+
}
373+
374+
private fun scrollToEvent_beforeConference(dayIndex: Int, targetPosition: Int) =
375+
coroutineRule.runBlockingTest {
376+
val viewModel = createScheduleViewModel()
377+
378+
// Start observing
379+
viewModel.scheduleUiData.first()
380+
381+
// Trigger to generate indexer
382+
viewModel.scrollToStartOfDay(TestData.TestConferenceDays[0])
383+
384+
val result = mutableListOf<ScheduleScrollEvent>()
385+
val job = launch {
386+
viewModel.scrollToEvent.toList(result)
387+
}
388+
389+
// Trigger to generate a result in scrollToEvent
390+
viewModel.scrollToStartOfDay(TestData.TestConferenceDays[dayIndex])
391+
392+
assertTrue(result.size == 1)
393+
assertEquals(
394+
result[0],
395+
ScheduleScrollEvent(targetPosition = targetPosition, smoothScroll = false)
396+
)
397+
398+
job.cancel()
399+
}
400+
401+
@Test
402+
fun scrollToEvent_beforeConference_userHasInteracted() = coroutineRule.runBlockingTest {
403+
val viewModel = createScheduleViewModel()
404+
405+
viewModel.userHasInteracted = true
406+
407+
// Start observing
408+
viewModel.scheduleUiData.first()
409+
410+
// Trigger to generate indexer
411+
viewModel.scrollToStartOfDay(TestData.TestConferenceDays[0])
412+
413+
val result = mutableListOf<ScheduleScrollEvent>()
414+
val job = launch {
415+
viewModel.scrollToEvent.toList(result)
416+
}
417+
418+
// Trigger to generate a result in scrollToEvent
419+
viewModel.scrollToStartOfDay(TestData.TestConferenceDays[1])
420+
421+
assertTrue(result.size == 1)
422+
assertEquals(result[0], ScheduleScrollEvent(targetPosition = 2, smoothScroll = false))
423+
424+
job.cancel()
425+
}
426+
362427
private fun createScheduleViewModel(
363428
loadScheduleSessionsUseCase: LoadScheduleUserSessionsUseCase =
364429
createTestLoadUserSessionsByDayUseCase(),

0 commit comments

Comments
 (0)