appearance.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import 'dart:async';
  2. import 'package:app_flowy/user/application/user_settings_service.dart';
  3. import 'package:equatable/equatable.dart';
  4. import 'package:flowy_infra/theme.dart';
  5. import 'package:flowy_sdk/log.dart';
  6. import 'package:flowy_sdk/protobuf/flowy-user/user_setting.pb.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:easy_localization/easy_localization.dart';
  9. /// [AppearanceSetting] is used to modify the appear setting of AppFlowy application. Including the [Locale], [AppTheme], etc.
  10. class AppearanceSetting extends ChangeNotifier with EquatableMixin {
  11. final AppearanceSettingsPB _setting;
  12. AppTheme _theme;
  13. Locale _locale;
  14. AppearanceSetting(AppearanceSettingsPB setting)
  15. : _setting = setting,
  16. _theme = AppTheme.fromName(name: setting.theme),
  17. _locale = Locale(
  18. setting.locale.languageCode,
  19. setting.locale.countryCode,
  20. );
  21. /// Returns the current [AppTheme]
  22. AppTheme get theme => _theme;
  23. /// Returns the current [Locale]
  24. Locale get locale => _locale;
  25. /// Updates the current theme and notify the listeners the theme was changed.
  26. /// Do nothing if the passed in themeType equal to the current theme type.
  27. ///
  28. void setTheme(ThemeType themeType) {
  29. if (_theme.ty == themeType) {
  30. return;
  31. }
  32. _theme = AppTheme.fromType(themeType);
  33. _setting.theme = themeTypeToString(themeType);
  34. _saveAppearSetting();
  35. notifyListeners();
  36. }
  37. /// Updates the current locale and notify the listeners the locale was changed
  38. /// Fallback to [en] locale If the newLocale is not supported.
  39. ///
  40. void setLocale(BuildContext context, Locale newLocale) {
  41. if (!context.supportedLocales.contains(newLocale)) {
  42. Log.warn("Unsupported locale: $newLocale, Fallback to locale: en");
  43. newLocale = const Locale('en');
  44. }
  45. context.setLocale(newLocale);
  46. if (_locale != newLocale) {
  47. _locale = newLocale;
  48. _setting.locale.languageCode = _locale.languageCode;
  49. _setting.locale.countryCode = _locale.countryCode ?? "";
  50. _saveAppearSetting();
  51. notifyListeners();
  52. }
  53. }
  54. /// Saves key/value setting to disk.
  55. /// Removes the key if the passed in value is null
  56. void setKeyValue(String key, String? value) {
  57. if (key.isEmpty) {
  58. Log.warn("The key should not be empty");
  59. return;
  60. }
  61. if (value == null) {
  62. _setting.settingKeyValue.remove(key);
  63. }
  64. if (_setting.settingKeyValue[key] != value) {
  65. if (value == null) {
  66. _setting.settingKeyValue.remove(key);
  67. } else {
  68. _setting.settingKeyValue[key] = value;
  69. }
  70. }
  71. _saveAppearSetting();
  72. notifyListeners();
  73. }
  74. String? getValue(String key) {
  75. if (key.isEmpty) {
  76. Log.warn("The key should not be empty");
  77. return null;
  78. }
  79. return _setting.settingKeyValue[key];
  80. }
  81. /// Called when the application launch.
  82. /// Uses the device locale when open the application for the first time
  83. void readLocaleWhenAppLaunch(BuildContext context) {
  84. if (_setting.resetToDefault) {
  85. _setting.resetToDefault = false;
  86. _saveAppearSetting();
  87. setLocale(context, context.deviceLocale);
  88. return;
  89. }
  90. setLocale(context, _locale);
  91. }
  92. Future<void> _saveAppearSetting() async {
  93. SettingsFFIService().setAppearanceSetting(_setting).then((result) {
  94. result.fold(
  95. (l) => null,
  96. (error) => Log.error(error),
  97. );
  98. });
  99. }
  100. @override
  101. List<Object> get props {
  102. return [_setting.hashCode];
  103. }
  104. }