TF-3977 Separate settings keys for easier in conflict
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:tmail_ui_user/features/manage_account/presentation/model/preferences/preferences_root.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/domain/model/preferences/preferences_config.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/domain/model/preferences/preferences_setting.dart';
|
||||
|
||||
abstract class ManageAccountDataSource {
|
||||
Future<void> persistLanguage(Locale localeCurrent);
|
||||
|
||||
Future<void> updateLocalSettings(PreferencesRoot preferencesRoot);
|
||||
Future<PreferencesSetting> toggleLocalSettingsState(PreferencesConfig preferencesConfig);
|
||||
|
||||
Future<PreferencesRoot> getLocalSettings();
|
||||
}
|
||||
Future<PreferencesSetting> getLocalSettings();
|
||||
}
|
||||
|
||||
+20
-4
@@ -3,7 +3,10 @@ import 'dart:ui';
|
||||
import 'package:tmail_ui_user/features/manage_account/data/datasource/manage_account_datasource.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/data/local/language_cache_manager.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/data/local/preferences_setting_manager.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/presentation/model/preferences/preferences_root.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/domain/model/preferences/preferences_config.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/domain/model/preferences/preferences_setting.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/domain/model/preferences/spam_report_config.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/domain/model/preferences/thread_detail_config.dart';
|
||||
import 'package:tmail_ui_user/main/exceptions/exception_thrower.dart';
|
||||
|
||||
class ManageAccountDataSourceImpl extends ManageAccountDataSource {
|
||||
@@ -26,14 +29,27 @@ class ManageAccountDataSourceImpl extends ManageAccountDataSource {
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateLocalSettings(PreferencesRoot preferencesRoot) {
|
||||
Future<PreferencesSetting> toggleLocalSettingsState(PreferencesConfig preferencesConfig) {
|
||||
return Future.sync(() async {
|
||||
return await _preferencesSettingManager.savePreferences(preferencesRoot);
|
||||
if (preferencesConfig is ThreadDetailConfig) {
|
||||
await _preferencesSettingManager.updateThread(
|
||||
preferencesConfig.isEnabled,
|
||||
);
|
||||
} else if (preferencesConfig is SpamReportConfig) {
|
||||
await _preferencesSettingManager.updateSpamReport(
|
||||
isEnabled: preferencesConfig.isEnabled,
|
||||
);
|
||||
} else {
|
||||
await _preferencesSettingManager.savePreferences(
|
||||
preferencesConfig,
|
||||
);
|
||||
}
|
||||
return await _preferencesSettingManager.loadPreferences();
|
||||
}).catchError(_exceptionThrower.throwException);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<PreferencesRoot> getLocalSettings() {
|
||||
Future<PreferencesSetting> getLocalSettings() {
|
||||
return Future.sync(() async {
|
||||
return await _preferencesSettingManager.loadPreferences();
|
||||
}).catchError(_exceptionThrower.throwException);
|
||||
|
||||
@@ -1,55 +1,113 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/presentation/model/preferences/preferences_root.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/presentation/model/preferences/spam_report_config.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/domain/model/preferences/default_preferences_config.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/domain/model/preferences/empty_preferences_config.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/domain/model/preferences/preferences_config.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/domain/model/preferences/preferences_setting.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/domain/model/preferences/spam_report_config.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/domain/model/preferences/thread_detail_config.dart';
|
||||
|
||||
class PreferencesSettingManager {
|
||||
static const String _preferencesSettingKey = 'PREFERENCES_SETTING';
|
||||
static const String _preferencesSettingThreadKey =
|
||||
'${_preferencesSettingKey}_THREAD';
|
||||
static const String _preferencesSettingSpamReportKey =
|
||||
'${_preferencesSettingKey}_SPAM_REPORT';
|
||||
|
||||
const PreferencesSettingManager(this._sharedPreferences);
|
||||
|
||||
final SharedPreferences _sharedPreferences;
|
||||
|
||||
Future<PreferencesRoot> loadPreferences() async {
|
||||
Future<PreferencesSetting> loadPreferences() async {
|
||||
await _sharedPreferences.reload();
|
||||
|
||||
final jsonString = _sharedPreferences.getString(_preferencesSettingKey);
|
||||
final keys = _sharedPreferences.getKeys();
|
||||
final preferencesKeys =
|
||||
keys.where((key) => key.startsWith(_preferencesSettingKey)).toList();
|
||||
|
||||
if (jsonString != null) {
|
||||
return PreferencesRoot.fromJson(jsonDecode(jsonString));
|
||||
final listConfigs = preferencesKeys.map((key) {
|
||||
final jsonString = _sharedPreferences.getString(key);
|
||||
if (jsonString != null) {
|
||||
final jsonDecoded = jsonDecode(jsonString);
|
||||
|
||||
switch (key) {
|
||||
case _preferencesSettingThreadKey:
|
||||
return ThreadDetailConfig.fromJson(jsonDecoded);
|
||||
case _preferencesSettingSpamReportKey:
|
||||
return SpamReportConfig.fromJson(jsonDecoded);
|
||||
default:
|
||||
return DefaultPreferencesConfig.fromJson(jsonDecoded);
|
||||
}
|
||||
}
|
||||
return EmptyPreferencesConfig();
|
||||
}).toList();
|
||||
|
||||
if (listConfigs.isEmpty) {
|
||||
return PreferencesSetting.initial();
|
||||
}
|
||||
|
||||
return PreferencesRoot.initial();
|
||||
return PreferencesSetting(listConfigs);
|
||||
}
|
||||
|
||||
Future<void> savePreferences(PreferencesRoot root) async {
|
||||
await _sharedPreferences.setString(
|
||||
_preferencesSettingKey,
|
||||
jsonEncode(root.toJson()),
|
||||
);
|
||||
Future<void> savePreferences(PreferencesConfig config) async {
|
||||
if (config is ThreadDetailConfig) {
|
||||
await _sharedPreferences.setString(
|
||||
_preferencesSettingThreadKey,
|
||||
jsonEncode(config.toJson()),
|
||||
);
|
||||
} else if (config is SpamReportConfig) {
|
||||
await _sharedPreferences.setString(
|
||||
_preferencesSettingSpamReportKey,
|
||||
jsonEncode(config.toJson()),
|
||||
);
|
||||
} else {
|
||||
await _sharedPreferences.setString(
|
||||
_preferencesSettingKey,
|
||||
jsonEncode(config.toJson()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> updateThread(bool enabled) async {
|
||||
final current = await loadPreferences();
|
||||
final updated = current.updateThreadDetail(enabled);
|
||||
await savePreferences(updated);
|
||||
Future<void> updateThread(bool isEnabled) async {
|
||||
final currentConfig = await getThreadConfig();
|
||||
final updatedConfig = currentConfig.copyWith(isEnabled: isEnabled);
|
||||
await savePreferences(updatedConfig);
|
||||
}
|
||||
|
||||
Future<void> updateSpamReport({
|
||||
bool? isEnabled,
|
||||
int? lastTimeDismissedMilliseconds,
|
||||
}) async {
|
||||
final current = await loadPreferences();
|
||||
final updated = current.updateSpamReport(
|
||||
final currentConfig = await getSpamReportConfig();
|
||||
final updatedConfig = currentConfig.copyWith(
|
||||
isEnabled: isEnabled,
|
||||
lastTimeDismissedMilliseconds: lastTimeDismissedMilliseconds,
|
||||
);
|
||||
await savePreferences(updated);
|
||||
await savePreferences(updatedConfig);
|
||||
}
|
||||
|
||||
Future<SpamReportConfig> getSpamReportConfig() async {
|
||||
final preferences = await loadPreferences();
|
||||
return preferences.setting.spamReport;
|
||||
await _sharedPreferences.reload();
|
||||
|
||||
final jsonString = _sharedPreferences.getString(
|
||||
_preferencesSettingSpamReportKey,
|
||||
);
|
||||
|
||||
return jsonString == null
|
||||
? SpamReportConfig.initial()
|
||||
: SpamReportConfig.fromJson(jsonDecode(jsonString));
|
||||
}
|
||||
|
||||
Future<ThreadDetailConfig> getThreadConfig() async {
|
||||
await _sharedPreferences.reload();
|
||||
|
||||
final jsonString = _sharedPreferences.getString(
|
||||
_preferencesSettingThreadKey,
|
||||
);
|
||||
|
||||
return jsonString == null
|
||||
? ThreadDetailConfig.initial()
|
||||
: ThreadDetailConfig.fromJson(jsonDecode(jsonString));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:tmail_ui_user/features/manage_account/data/datasource/manage_account_datasource.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/domain/model/preferences/preferences_config.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/domain/model/preferences/preferences_setting.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/domain/repository/manage_account_repository.dart';
|
||||
import 'package:tmail_ui_user/features/manage_account/presentation/model/preferences/preferences_root.dart';
|
||||
|
||||
class ManageAccountRepositoryImpl extends ManageAccountRepository {
|
||||
|
||||
@@ -16,12 +17,12 @@ class ManageAccountRepositoryImpl extends ManageAccountRepository {
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateLocalSettings(PreferencesRoot preferencesRoot) {
|
||||
return dataSource.updateLocalSettings(preferencesRoot);
|
||||
Future<PreferencesSetting> toggleLocalSettingsState(PreferencesConfig preferencesConfig) {
|
||||
return dataSource.toggleLocalSettingsState(preferencesConfig);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<PreferencesRoot> getLocalSettings() {
|
||||
Future<PreferencesSetting> getLocalSettings() {
|
||||
return dataSource.getLocalSettings();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user