Skip to content

Commit

Permalink
Decrement appUpdateLaunches instead of incrementing
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianDevel committed Feb 7, 2024
1 parent 70c0380 commit 7bd677a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ abstract class BaseInAppUpdateManager(activity: FragmentActivity) : DefaultLifec

with(viewModel) {
shouldCheckUpdate(::checkUpdateIsAvailable)
incrementAppUpdateLaunches()
decrementAppUpdateLaunches()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ class StoresSettingsRepository(private val context: Context) {

private const val DEFAULT_IS_USER_WANTING_UPDATES = true
private const val DEFAULT_HAS_APP_UPDATE_DOWNLOADED = false
private const val DEFAULT_APP_UPDATE_LAUNCHES = 0
const val DEFAULT_APP_UPDATE_LAUNCHES = 20
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import android.app.Application
import android.content.Context
import androidx.datastore.preferences.core.Preferences
import androidx.lifecycle.*
import com.infomaniak.lib.stores.StoresSettingsRepository.Companion.APP_UPDATE_LAUNCHES
import com.infomaniak.lib.stores.StoresSettingsRepository.Companion.DEFAULT_APP_UPDATE_LAUNCHES
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

Expand All @@ -43,14 +45,17 @@ class StoresViewModel(application: Application) : AndroidViewModel(application)
fun resetUpdateSettings() = viewModelScope.launch(ioCoroutineContext) { storesSettingsRepository.resetUpdateSettings() }

//region BaseInAppUpdateManager
fun incrementAppUpdateLaunches() = viewModelScope.launch(ioCoroutineContext) {
val appUpdateLaunches = storesSettingsRepository.getValue(StoresSettingsRepository.APP_UPDATE_LAUNCHES)
set(StoresSettingsRepository.APP_UPDATE_LAUNCHES, appUpdateLaunches + 1)
fun decrementAppUpdateLaunches() = viewModelScope.launch(ioCoroutineContext) {
val appUpdateLaunches = storesSettingsRepository.getValue(APP_UPDATE_LAUNCHES)
set(APP_UPDATE_LAUNCHES, appUpdateLaunches - 1)
}

fun shouldCheckUpdate(checkUpdateCallback: () -> Unit) = viewModelScope.launch(ioCoroutineContext) {
with(storesSettingsRepository.getAll()) {
if (appUpdateLaunches != 0 && (isUserWantingUpdates || appUpdateLaunches % 10 == 0)) checkUpdateCallback()
if (isUserWantingUpdates || appUpdateLaunches <= 0) {
checkUpdateCallback()
storesSettingsRepository.setValue(APP_UPDATE_LAUNCHES, DEFAULT_APP_UPDATE_LAUNCHES)
}
}
}
//endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ class InAppUpdateManager(
}

override fun installDownloadedUpdate() {
viewModel.set(StoresSettingsRepository.HAS_APP_UPDATE_DOWNLOADED, false)
with(viewModel) {
set(StoresSettingsRepository.HAS_APP_UPDATE_DOWNLOADED, false)
set(StoresSettingsRepository.APP_UPDATE_LAUNCHES, StoresSettingsRepository.DEFAULT_APP_UPDATE_LAUNCHES)
}
onInstallStart()
appUpdateManager.completeUpdate()
.addOnSuccessListener { onInstallSuccess?.invoke() }
Expand Down

0 comments on commit 7bd677a

Please sign in to comment.