Skip to content

Commit

Permalink
+ Adds: a delay for the refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
AjeshRPai committed Feb 1, 2024
1 parent 1859174 commit 34b6d4a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.compose.runtime.mutableStateOf
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.fluxc.store.AccountStore
Expand All @@ -15,6 +16,8 @@ import org.wordpress.android.util.NetworkUtilsWrapper
import javax.inject.Inject
import javax.inject.Named

const val REFRESH_DELAY = 500L

class SiteMonitorTabViewModelSlice @Inject constructor(
@param:Named(BG_THREAD) private val bgDispatcher: CoroutineDispatcher,
private val networkUtilsWrapper: NetworkUtilsWrapper,
Expand Down Expand Up @@ -58,9 +61,15 @@ class SiteMonitorTabViewModelSlice @Inject constructor(
}

fun refreshData() {
_isRefreshing.value = true
loadView()
_isRefreshing.value = false
scope.launch {
_isRefreshing.value = true
// this delay is to prevent the refresh from being too fast
// so that the user can see the refresh animation
// also this would fix the unit tests
delay(REFRESH_DELAY)
loadView()
_isRefreshing.value = false
}
}

private fun checkForInternetConnectivityAndPostErrorIfNeeded(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class SiteMonitorTabViewModelSliceTest : BaseUnitTest() {

val site = mock<SiteModel>()

val refreshStates = mutableListOf<Boolean>()

@Before
fun setUp() = test {
viewModel = SiteMonitorTabViewModelSlice(
Expand Down Expand Up @@ -120,6 +122,19 @@ class SiteMonitorTabViewModelSliceTest : BaseUnitTest() {
assertThat(viewModel.uiState.value).isInstanceOf(SiteMonitorUiState.GenericError::class.java)
}

@Test
fun `given loaded state, when refresh is invoked, then uiState loaded is posted`() = test {
viewModel.start(SiteMonitorType.METRICS, SiteMonitorTabItem.Metrics.urlTemplate, site)
advanceUntilIdle()
viewModel.onUrlLoaded()
viewModel.refreshData()

assertThat(viewModel.isRefreshing.value).isTrue()
advanceUntilIdle()
assertThat(viewModel.uiState.value).isInstanceOf(SiteMonitorUiState.Prepared::class.java)
assertThat(viewModel.isRefreshing.value).isFalse()
}

companion object {
const val USER_NAME = "user_name"
const val ACCESS_TOKEN = "access_token"
Expand Down

0 comments on commit 34b6d4a

Please sign in to comment.