appearance.dart 3.7 KB

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