2023-07-06 17:18:41 +03:30
|
|
|
import 'dart:convert';
|
|
|
|
|
|
2023-07-15 18:00:44 +03:30
|
|
|
import 'package:hiddify/core/prefs/general_prefs.dart';
|
2023-07-06 17:18:41 +03:30
|
|
|
import 'package:hiddify/core/prefs/prefs_state.dart';
|
|
|
|
|
import 'package:hiddify/data/data_providers.dart';
|
|
|
|
|
import 'package:hiddify/domain/clash/clash.dart';
|
|
|
|
|
import 'package:hiddify/domain/connectivity/connectivity.dart';
|
|
|
|
|
import 'package:hiddify/utils/utils.dart';
|
|
|
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
|
|
|
|
|
|
part 'prefs_controller.g.dart';
|
|
|
|
|
|
|
|
|
|
@Riverpod(keepAlive: true)
|
|
|
|
|
class PrefsController extends _$PrefsController with AppLogger {
|
|
|
|
|
@override
|
|
|
|
|
PrefsState build() {
|
|
|
|
|
return PrefsState(
|
2023-07-15 18:00:44 +03:30
|
|
|
general: _getGeneralPrefs(),
|
2023-07-06 17:18:41 +03:30
|
|
|
clash: _getClashPrefs(),
|
|
|
|
|
network: _getNetworkPrefs(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SharedPreferences get _prefs => ref.read(sharedPreferencesProvider);
|
|
|
|
|
|
2023-07-15 18:00:44 +03:30
|
|
|
static const _generalKey = "general_prefs";
|
2023-07-06 17:18:41 +03:30
|
|
|
static const _overridesKey = "clash_overrides";
|
2023-07-15 18:00:44 +03:30
|
|
|
static const _networkKey = "network_prefs";
|
|
|
|
|
|
|
|
|
|
GeneralPrefs _getGeneralPrefs() {
|
|
|
|
|
final persisted = _prefs.getString(_generalKey);
|
|
|
|
|
if (persisted == null) return const GeneralPrefs();
|
|
|
|
|
return GeneralPrefs.fromJson(jsonDecode(persisted) as Map<String, dynamic>);
|
|
|
|
|
}
|
2023-07-06 17:18:41 +03:30
|
|
|
|
|
|
|
|
ClashConfig _getClashPrefs() {
|
|
|
|
|
final persisted = _prefs.getString(_overridesKey);
|
|
|
|
|
if (persisted == null) return ClashConfig.initial;
|
|
|
|
|
return ClashConfig.fromJson(jsonDecode(persisted) as Map<String, dynamic>);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NetworkPrefs _getNetworkPrefs() {
|
|
|
|
|
final persisted = _prefs.getString(_networkKey);
|
|
|
|
|
if (persisted == null) return const NetworkPrefs();
|
|
|
|
|
return NetworkPrefs.fromJson(jsonDecode(persisted) as Map<String, dynamic>);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-15 18:00:44 +03:30
|
|
|
Future<void> patchGeneralPrefs({bool? silentStart}) async {
|
|
|
|
|
final newPrefs = state.general.copyWith(
|
|
|
|
|
silentStart: silentStart ?? state.general.silentStart,
|
|
|
|
|
);
|
|
|
|
|
await _prefs.setString(_generalKey, jsonEncode(newPrefs.toJson()));
|
|
|
|
|
state = state.copyWith(general: newPrefs);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-06 17:18:41 +03:30
|
|
|
Future<void> patchClashOverrides(ClashConfigPatch overrides) async {
|
|
|
|
|
final newPrefs = state.clash.patch(overrides);
|
|
|
|
|
await _prefs.setString(_overridesKey, jsonEncode(newPrefs.toJson()));
|
|
|
|
|
state = state.copyWith(clash: newPrefs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> patchNetworkPrefs({
|
|
|
|
|
bool? systemProxy,
|
|
|
|
|
bool? bypassPrivateNetworks,
|
|
|
|
|
}) async {
|
|
|
|
|
final newPrefs = state.network.copyWith(
|
|
|
|
|
systemProxy: systemProxy ?? state.network.systemProxy,
|
|
|
|
|
bypassPrivateNetworks:
|
|
|
|
|
bypassPrivateNetworks ?? state.network.bypassPrivateNetworks,
|
|
|
|
|
);
|
|
|
|
|
await _prefs.setString(_networkKey, jsonEncode(newPrefs.toJson()));
|
|
|
|
|
state = state.copyWith(network: newPrefs);
|
|
|
|
|
}
|
|
|
|
|
}
|