improve applyPrefs
This commit is contained in:
+9
-1
@@ -25,6 +25,7 @@ import 'package:flutter/services.dart';
|
||||
import 'package:flutter_mozilla_components/flutter_mozilla_components.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
import 'package:weblibre/core/logger.dart';
|
||||
import 'package:weblibre/features/geckoview/features/preferences/data/models/preference_setting.dart';
|
||||
import 'package:weblibre/features/geckoview/features/tabs/utils/setting_groups_serializer.dart';
|
||||
|
||||
@@ -75,7 +76,14 @@ class StartupPreferenceEnforcementService
|
||||
);
|
||||
|
||||
if (prefsToEnforce.isNotEmpty) {
|
||||
await GeckoPrefService().applyPrefs(prefsToEnforce);
|
||||
try {
|
||||
await GeckoPrefService().applyPrefs(prefsToEnforce);
|
||||
} catch (e) {
|
||||
logger.w(
|
||||
'Failed to enforce startup preferences, will retry on next launch',
|
||||
error: e,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+44
-43
@@ -94,80 +94,81 @@ class GeckoPrefApiImpl : GeckoPrefApi, BrowserPrefObserverDelegate {
|
||||
prefs: Map<String, Any>,
|
||||
callback: (Result<Map<String, GeckoPref>>) -> Unit
|
||||
) {
|
||||
var fault: Boolean = false;
|
||||
if (prefs.isEmpty()) {
|
||||
callback(Result.success(emptyMap()))
|
||||
return
|
||||
}
|
||||
|
||||
val entries = prefs.entries.toList()
|
||||
|
||||
fun applyNext(index: Int) {
|
||||
if (index >= entries.size) {
|
||||
getPrefs(prefs.keys.toList(), callback = callback)
|
||||
return
|
||||
}
|
||||
|
||||
val pref = entries[index]
|
||||
|
||||
fun onSuccess() = applyNext(index + 1)
|
||||
fun onError(e: Exception) = callback(Result.failure(e))
|
||||
|
||||
for (pref in prefs) {
|
||||
when (pref.value) {
|
||||
is String -> components.core.engine.setBrowserPref(
|
||||
pref.key,
|
||||
pref.value as String,
|
||||
Branch.USER,
|
||||
onSuccess = {},
|
||||
onError = {
|
||||
callback(Result.failure(Exception("${it.message} ${it.cause}")))
|
||||
fault = true
|
||||
}
|
||||
onSuccess = { onSuccess() },
|
||||
onError = { onError(Exception("${it.message} ${it.cause}")) }
|
||||
)
|
||||
|
||||
is Boolean -> components.core.engine.setBrowserPref(
|
||||
pref.key,
|
||||
pref.value as Boolean,
|
||||
Branch.USER,
|
||||
onSuccess = {},
|
||||
onError = {
|
||||
callback(Result.failure(Exception("${it.message} ${it.cause}")))
|
||||
fault = true
|
||||
}
|
||||
onSuccess = { onSuccess() },
|
||||
onError = { onError(Exception("${it.message} ${it.cause}")) }
|
||||
)
|
||||
|
||||
is Long -> components.core.engine.setBrowserPref(
|
||||
pref.key,
|
||||
(pref.value as Long).toInt(),
|
||||
Branch.USER,
|
||||
onSuccess = {},
|
||||
onError = {
|
||||
callback(Result.failure(Exception("${it.message} ${it.cause}")))
|
||||
fault = true
|
||||
}
|
||||
onSuccess = { onSuccess() },
|
||||
onError = { onError(Exception("${it.message} ${it.cause}")) }
|
||||
)
|
||||
|
||||
else -> {
|
||||
callback(Result.failure(Exception("Unsupported value type: ${pref.value::class.simpleName}")))
|
||||
fault = true
|
||||
onError(Exception("Unsupported value type: ${pref.value::class.simpleName}"))
|
||||
}
|
||||
}
|
||||
|
||||
if (fault) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
getPrefs(
|
||||
prefs.keys.toList(),
|
||||
callback = callback
|
||||
)
|
||||
applyNext(0)
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalAndroidComponentsApi::class)
|
||||
override fun resetPrefs(preferenceNames: List<String>, callback: (Result<Unit>) -> Unit) {
|
||||
var fault: Boolean = false;
|
||||
|
||||
for (pref in preferenceNames) {
|
||||
components.core.engine.clearBrowserUserPref(
|
||||
pref = pref,
|
||||
onSuccess = { },
|
||||
onError = {
|
||||
callback(Result.failure(Exception("${it.message} ${it.cause}")))
|
||||
fault = true
|
||||
}
|
||||
)
|
||||
|
||||
if (fault) {
|
||||
return;
|
||||
}
|
||||
if (preferenceNames.isEmpty()) {
|
||||
callback(Result.success(Unit))
|
||||
return
|
||||
}
|
||||
|
||||
callback(Result.success(Unit))
|
||||
fun resetNext(index: Int) {
|
||||
if (index >= preferenceNames.size) {
|
||||
callback(Result.success(Unit))
|
||||
return
|
||||
}
|
||||
|
||||
components.core.engine.clearBrowserUserPref(
|
||||
pref = preferenceNames[index],
|
||||
onSuccess = { resetNext(index + 1) },
|
||||
onError = {
|
||||
callback(Result.failure(Exception("${it.message} ${it.cause}")))
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
resetNext(0)
|
||||
}
|
||||
|
||||
override fun startObserveChanges() {
|
||||
|
||||
Reference in New Issue
Block a user