Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b6045f56 authored by George Lin's avatar George Lin
Browse files

Suspend set selected clock function

1. Make the function suspend to void UI blocking.
2. Only set the clock after a short delay to avoid users scrolling
   frquently and too many heavy calls of setting clocks.

Test: Manually tested the clock carousel is smooth
Bug: 278850684
Change-Id: Ie3c1e6f274597f97d44cea59eb533aaf9c8cc949
parent 74708e17
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -357,7 +357,7 @@ open class ThemePickerInjector : WallpaperPicker2Injector(), CustomizationInject
        interactor: ClockPickerInteractor,
    ): ClockCarouselViewModel.Factory {
        return clockCarouselViewModelFactory
            ?: ClockCarouselViewModel.Factory(interactor).also {
            ?: ClockCarouselViewModel.Factory(interactor, Dispatchers.IO).also {
                clockCarouselViewModelFactory = it
            }
    }
+1 −1
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ interface ClockPickerRepository {

    val selectedClockSize: Flow<ClockSize>

    fun setSelectedClock(clockId: String)
    suspend fun setSelectedClock(clockId: String)

    /**
     * Set clock color to the settings.
+1 −1
Original line number Diff line number Diff line
@@ -110,7 +110,7 @@ class ClockPickerRepositoryImpl(
            }
            .mapNotNull { it }

    override fun setSelectedClock(clockId: String) {
    override suspend fun setSelectedClock(clockId: String) {
        registry.mutateSetting { oldSettings ->
            val newSettings = oldSettings.copy(clockId = clockId)
            newSettings.metadata = oldSettings.metadata
+1 −1
Original line number Diff line number Diff line
@@ -47,7 +47,7 @@ class ClockPickerInteractor(private val repository: ClockPickerRepository) {

    val selectedClockSize: Flow<ClockSize> = repository.selectedClockSize

    fun setSelectedClock(clockId: String) {
    suspend fun setSelectedClock(clockId: String) {
        repository.setSelectedClock(clockId)
    }

+17 −1
Original line number Diff line number Diff line
@@ -19,7 +19,9 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.android.customization.picker.clock.domain.interactor.ClockPickerInteractor
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
@@ -30,6 +32,7 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch

/**
 * Clock carousel view model that provides data for the carousel of clock previews. When there is
@@ -38,6 +41,7 @@ import kotlinx.coroutines.flow.stateIn
class ClockCarouselViewModel
constructor(
    private val interactor: ClockPickerInteractor,
    private val backgroundDispatcher: CoroutineDispatcher,
) : ViewModel() {
    @OptIn(ExperimentalCoroutinesApi::class)
    val allClockIds: StateFlow<List<String>> =
@@ -77,17 +81,25 @@ constructor(
            .map { allClockIds -> if (allClockIds.size == 1) allClockIds[0] else null }
            .mapNotNull { it }

    private var setSelectedClockJob: Job? = null
    fun setSelectedClock(clockId: String) {
        setSelectedClockJob?.cancel()
        setSelectedClockJob =
            viewModelScope.launch(backgroundDispatcher) {
                delay(SET_SELECTED_CLOCK_DELAY_MILLIS)
                interactor.setSelectedClock(clockId)
            }
    }

    class Factory(
        private val interactor: ClockPickerInteractor,
        private val backgroundDispatcher: CoroutineDispatcher,
    ) : ViewModelProvider.Factory {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            @Suppress("UNCHECKED_CAST")
            return ClockCarouselViewModel(
                interactor = interactor,
                backgroundDispatcher = backgroundDispatcher,
            )
                as T
        }
@@ -95,5 +107,9 @@ constructor(

    companion object {
        const val CLOCKS_EVENT_UPDATE_DELAY_MILLIS: Long = 100

        // In the case if the user scroll the clock carousel frequently, we make a delay for
        // setting the selected clock to avoid too many heavy calls.
        const val SET_SELECTED_CLOCK_DELAY_MILLIS: Long = 650
    }
}
Loading