Skip to content

Refresh sync settings screen automatically #5773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,11 @@ interface SyncFeature {

@Toggle.DefaultValue(true)
fun seamlessAccountSwitching(): Toggle

@InternalAlwaysEnabled
@Toggle.DefaultValue(false)
fun exchangeKeysToSyncWithAnotherDevice(): Toggle

@Toggle.DefaultValue(true)
fun automaticallyUpdateSyncSettings(): Toggle
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ interface SyncFeatureToggle {
fun allowCreateAccount(): Boolean

fun allowCreateAccountOnNewerVersion(): Boolean

fun automaticallyUpdateSyncSettings(): Boolean
}

@ContributesBinding(
Expand Down Expand Up @@ -111,6 +113,10 @@ class SyncRemoteFeatureToggle @Inject constructor(
return isToggleEnabledOnNewerVersion(syncFeature.level3AllowCreateAccount())
}

override fun automaticallyUpdateSyncSettings(): Boolean {
return syncFeature.automaticallyUpdateSyncSettings().isEnabled()
}

private fun isToggleEnabledOnNewerVersion(toggle: Toggle): Boolean {
val rawStoredState = toggle.getRawStoredState()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.annotation.StringRes
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.duckduckgo.anvil.annotations.ContributesViewModel
import com.duckduckgo.common.utils.ConflatedJob
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.di.scopes.ActivityScope
import com.duckduckgo.sync.api.SyncState.OFF
Expand Down Expand Up @@ -56,13 +57,15 @@ import java.io.File
import javax.inject.Inject
import kotlinx.coroutines.channels.BufferOverflow.DROP_OLDEST
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

Expand All @@ -78,6 +81,9 @@ class SyncActivityViewModel @Inject constructor(
private val syncPixels: SyncPixels,
) : ViewModel() {

private var syncStateObserverJob = ConflatedJob()
private var backgroundRefreshJob = ConflatedJob()

private val command = Channel<Command>(1, DROP_OLDEST)
private val viewState = MutableStateFlow(ViewState())
fun commands(): Flow<Command> = command.receiveAsFlow().onStart {
Expand All @@ -90,21 +96,34 @@ class SyncActivityViewModel @Inject constructor(
}.flowOn(dispatchers.io())

private fun observeState() {
syncStateMonitor.syncState().onEach { syncState ->
val state = if (syncState == OFF) {
signedOutState()
} else {
signedInState()
}
viewState.value = state
}.onStart {
initViewStateThisDeviceState()
fetchRemoteDevices()
syncEngine.triggerSync(FEATURE_READ)
}.flowOn(dispatchers.io())
syncStateObserverJob += syncStateMonitor.syncState()
.onEach { syncState ->
val state = if (syncState == OFF) {
signedOutState()
} else {
signedInState()
}
viewState.value = state
}.onStart {
initViewStateThisDeviceState()
fetchRemoteDevices()
syncEngine.triggerSync(FEATURE_READ)
schedulePeriodicRefresh()
}.flowOn(dispatchers.io())
.launchIn(viewModelScope)
}

private fun schedulePeriodicRefresh() {
backgroundRefreshJob += viewModelScope.launch(dispatchers.io()) {
while (isActive && syncFeatureToggle.automaticallyUpdateSyncSettings()) {
delay(SETTINGS_REFRESH_RATE_MS)
if (syncAccountRepository.isSignedIn()) {
fetchRemoteDevices(showLoadingState = false)
}
}
}
}

private suspend fun checkIfDeviceSupported() {
val isSupported = withContext(dispatchers.io()) {
syncAccountRepository.isSyncSupported()
Expand Down Expand Up @@ -215,8 +234,11 @@ class SyncActivityViewModel @Inject constructor(
}
}

private suspend fun fetchRemoteDevices() {
viewState.value = viewState.value.showDeviceListItemLoading()
private suspend fun fetchRemoteDevices(showLoadingState: Boolean = true) {
if (showLoadingState) {
viewState.value = viewState.value.showDeviceListItemLoading()
}

val result = withContext(dispatchers.io()) {
syncAccountRepository.getConnectedDevices()
}
Expand Down Expand Up @@ -412,5 +434,6 @@ class SyncActivityViewModel @Inject constructor(
companion object {
private const val SOURCE_SYNC_DISABLED = "not_activated"
private const val SOURCE_SYNC_ENABLED = "activated"
private const val SETTINGS_REFRESH_RATE_MS = 5_000L
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,13 @@ class FakeSyncFeatureToggle : SyncFeatureToggle {
var allowSetupFlowsOnNewerVersion: Boolean = true
var allowCreateAccount: Boolean = true
var allowCreateAccountOnNewerVersion: Boolean = true
var automaticallyUpdateSyncSettings: Boolean = true
override fun showSync() = showSync
override fun allowDataSyncing() = allowDataSyncing
override fun allowDataSyncingOnNewerVersion() = allowDataSyncingOnNewerVersion
override fun allowSetupFlows() = allowSetupFlows
override fun allowSetupFlowsOnNewerVersion() = allowSetupFlowsOnNewerVersion
override fun allowCreateAccount() = allowCreateAccount
override fun allowCreateAccountOnNewerVersion() = allowCreateAccountOnNewerVersion
override fun automaticallyUpdateSyncSettings() = automaticallyUpdateSyncSettings
}
Loading