Skip to content

Commit 5c1da34

Browse files
fix: pagination bugs
1 parent 02291fc commit 5c1da34

File tree

8 files changed

+24
-32
lines changed

8 files changed

+24
-32
lines changed

core/src/main/java/org/openedx/core/data/api/CourseApi.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import org.openedx.core.data.model.DownloadCoursePreview
1414
import org.openedx.core.data.model.EnrollmentStatus
1515
import org.openedx.core.data.model.HandoutsModel
1616
import org.openedx.core.data.model.ResetCourseDates
17-
import org.openedx.core.data.model.ShiftDueDatesBody
1817
import retrofit2.http.Body
1918
import retrofit2.http.GET
2019
import retrofit2.http.Header
@@ -118,8 +117,6 @@ interface CourseApi {
118117
@Query("page") page: Int
119118
): CourseDatesResponse
120119

121-
@POST("/api/course_experience/v1/reset_multiple_course_deadlines/")
122-
suspend fun shiftDueDate(
123-
@Body shiftDueDatesBody: ShiftDueDatesBody
124-
)
120+
@POST("/api/course_experience/v1/reset_all_relative_course_deadlines/")
121+
suspend fun shiftDueDate()
125122
}

core/src/main/java/org/openedx/core/data/model/ShiftDueDatesBody.kt

Lines changed: 0 additions & 7 deletions
This file was deleted.

core/src/main/java/org/openedx/core/presentation/dates/DatesUI.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ private fun CourseDateItem(
312312
.fillMaxWidth()
313313
.padding(top = 4.dp),
314314
text = dateBlock.courseName,
315+
maxLines = 1,
315316
style = MaterialTheme.appTypography.labelMedium,
316317
)
317318
}

dates/src/main/java/org/openedx/dates/data/repository/DatesRepository.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.openedx.dates.data.repository
22

33
import org.openedx.core.data.api.CourseApi
4-
import org.openedx.core.data.model.ShiftDueDatesBody
54
import org.openedx.core.data.storage.CorePreferences
65
import org.openedx.core.domain.model.CourseDate
76
import org.openedx.core.domain.model.CourseDatesResponse
@@ -16,6 +15,9 @@ class DatesRepository(
1615
suspend fun getUserDates(page: Int): CourseDatesResponse {
1716
val username = preferencesManager.user?.username ?: ""
1817
val response = api.getUserDates(username, page)
18+
if (page == 1) {
19+
dao.clearCachedData()
20+
}
1921
dao.insertCourseDateEntities(response.results.map { CourseDateEntity.createFrom(it) })
2022
return response.mapToDomain()
2123
}
@@ -24,6 +26,5 @@ class DatesRepository(
2426
return dao.getCourseDateEntities().mapNotNull { it.mapToDomain() }
2527
}
2628

27-
suspend fun shiftDueDate(courseIds: List<String>) =
28-
api.shiftDueDate(ShiftDueDatesBody(courseIds))
29+
suspend fun shiftDueDate() = api.shiftDueDate()
2930
}

dates/src/main/java/org/openedx/dates/domain/interactor/DatesInteractor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ class DatesInteractor(
1010

1111
suspend fun getUserDatesFromCache() = repository.getUserDatesFromCache()
1212

13-
suspend fun shiftDueDate(courseIds: List<String>) = repository.shiftDueDate(courseIds)
13+
suspend fun shiftDueDate() = repository.shiftDueDate()
1414
}

dates/src/main/java/org/openedx/dates/presentation/dates/DatesFragment.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,10 @@ private fun DatesScreen(
250250
layoutInfo.visibleItemsInfo.lastOrNull()?.index ?: 0
251251
val totalItemsCount = layoutInfo.totalItemsCount
252252
if (totalItemsCount > 0 &&
253-
lastVisibleItemIndex >= (totalItemsCount * LOAD_MORE_THRESHOLD).toInt()
253+
lastVisibleItemIndex >= (totalItemsCount * LOAD_MORE_THRESHOLD).toInt() &&
254+
!uiState.isLoading &&
255+
!uiState.isRefreshing &&
256+
uiState.canLoadMore
254257
) {
255258
onAction(DatesViewActions.LoadMore)
256259
}

dates/src/main/java/org/openedx/dates/presentation/dates/DatesViewModel.kt

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.openedx.dates.presentation.dates
22

33
import androidx.fragment.app.FragmentManager
44
import androidx.lifecycle.viewModelScope
5+
import kotlinx.coroutines.Job
56
import kotlinx.coroutines.flow.MutableSharedFlow
67
import kotlinx.coroutines.flow.MutableStateFlow
78
import kotlinx.coroutines.flow.SharedFlow
@@ -54,13 +55,14 @@ class DatesViewModel(
5455
var useRelativeDates = corePreferences.isRelativeDatesEnabled
5556

5657
private var page = 1
58+
private var fetchDataJob: Job? = null
5759

5860
init {
5961
fetchDates(false)
6062
}
6163

6264
private fun fetchDates(refresh: Boolean) {
63-
viewModelScope.launch {
65+
fetchDataJob = viewModelScope.launch {
6466
try {
6567
updateLoadingState(refresh)
6668
val response = getUserDates(refresh)
@@ -70,6 +72,7 @@ class DatesViewModel(
7072
updateUIWithCachedResponse()
7173
}
7274
} catch (e: Exception) {
75+
page = -1
7376
handleFetchException(e)
7477
} finally {
7578
clearLoadingState()
@@ -86,19 +89,17 @@ class DatesViewModel(
8689
}
8790
}
8891

89-
private suspend fun getUserDates(refresh: Boolean) = if (refresh) {
90-
page = 1
91-
datesInteractor.getUserDates(page)
92-
} else {
93-
if (networkConnection.isOnline() || page > 1) {
92+
private suspend fun getUserDates(refresh: Boolean): CourseDatesResponse? {
93+
if (refresh) page = 1
94+
return if (networkConnection.isOnline() || page > 1) {
9495
datesInteractor.getUserDates(page)
9596
} else {
9697
null
9798
}
9899
}
99100

100101
private fun updateUIWithResponse(response: CourseDatesResponse, refresh: Boolean) {
101-
if (response.next.isNotNull() && page != response.count) {
102+
if (response.next.isNotNull()) {
102103
_uiState.update { state -> state.copy(canLoadMore = true) }
103104
page++
104105
} else {
@@ -158,12 +159,7 @@ class DatesViewModel(
158159
isShiftDueDatesPressed = true,
159160
)
160161
}
161-
val pastDueDates = _uiState.value.dates[DatesSection.PAST_DUE] ?: emptyList()
162-
val courseIds = pastDueDates
163-
.filter { it.relative }
164-
.map { it.courseId }
165-
.distinct()
166-
datesInteractor.shiftDueDate(courseIds)
162+
datesInteractor.shiftDueDate()
167163
refreshData()
168164
} catch (e: Exception) {
169165
handleFetchException(e)
@@ -184,6 +180,7 @@ class DatesViewModel(
184180
}
185181

186182
fun refreshData() {
183+
fetchDataJob?.cancel()
187184
fetchDates(true)
188185
}
189186

dates/src/test/java/org/openedx/dates/DatesViewModelTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ class DatesViewModelTest {
208208
viewModel.shiftDueDate()
209209
advanceUntilIdle()
210210

211-
coVerify { datesInteractor.shiftDueDate(listOf("course-123")) }
211+
coVerify { datesInteractor.shiftDueDate() }
212212
// isShiftDueDatesPressed should be reset to false after processing.
213213
assertFalse(viewModel.uiState.value.isShiftDueDatesPressed)
214214
}
@@ -229,7 +229,7 @@ class DatesViewModelTest {
229229
results = listOf(courseDate)
230230
)
231231
coEvery { datesInteractor.getUserDates(1) } returns courseDatesResponse
232-
coEvery { datesInteractor.shiftDueDate(any()) } throws Exception()
232+
coEvery { datesInteractor.shiftDueDate() } throws Exception()
233233

234234
val viewModel = DatesViewModel(
235235
datesRouter,

0 commit comments

Comments
 (0)