125 lines
3.4 KiB
Dart
Raw Normal View History

2022-03-20 17:17:06 +08:00
import 'dart:async';
import 'package:app_flowy/user/application/user_settings_service.dart';
2022-01-28 18:34:21 +08:00
import 'package:equatable/equatable.dart';
import 'package:flowy_infra/theme.dart';
2022-02-19 13:52:52 +08:00
import 'package:flowy_sdk/log.dart';
2022-07-04 15:07:11 +08:00
import 'package:flowy_sdk/protobuf/flowy-user/user_setting.pb.dart';
2022-01-28 18:34:21 +08:00
import 'package:flutter/material.dart';
2022-01-29 09:24:04 +08:00
import 'package:easy_localization/easy_localization.dart';
2022-01-28 18:34:21 +08:00
/// [AppearanceSetting] is used to modify the appear setting of AppFlowy application. Including the [Locale], [AppTheme], etc.
class AppearanceSetting extends ChangeNotifier with EquatableMixin {
final AppearanceSettingsPB _setting;
2022-01-28 18:34:21 +08:00
AppTheme _theme;
Locale _locale;
AppearanceSetting(AppearanceSettingsPB setting)
: _setting = setting,
_theme = AppTheme.fromName(name: setting.theme),
_locale = Locale(
setting.locale.languageCode,
setting.locale.countryCode,
);
2022-01-28 18:34:21 +08:00
/// Returns the current [AppTheme]
2022-01-28 18:34:21 +08:00
AppTheme get theme => _theme;
/// Returns the current [Locale]
Locale get locale => _locale;
2022-01-28 18:34:21 +08:00
/// Updates the current theme and notify the listeners the theme was changed.
/// Do nothing if the passed in themeType equal to the current theme type.
///
void setTheme(ThemeType themeType) {
if (_theme.ty == themeType) {
return;
}
2022-01-28 18:34:21 +08:00
_theme = AppTheme.fromType(themeType);
_setting.theme = themeTypeToString(themeType);
_saveAppearSetting();
2022-01-28 18:34:21 +08:00
notifyListeners();
2022-01-28 18:34:21 +08:00
}
/// Updates the current locale and notify the listeners the locale was changed
/// Fallback to [en] locale If the newLocale is not supported.
///
void setLocale(BuildContext context, Locale newLocale) {
if (!context.supportedLocales.contains(newLocale)) {
Log.warn("Unsupported locale: $newLocale, Fallback to locale: en");
newLocale = const Locale('en');
}
context.setLocale(newLocale);
if (_locale != newLocale) {
_locale = newLocale;
_setting.locale.languageCode = _locale.languageCode;
_setting.locale.countryCode = _locale.countryCode ?? "";
_saveAppearSetting();
2022-01-28 18:48:09 +08:00
notifyListeners();
}
2022-01-28 18:34:21 +08:00
}
/// Saves key/value setting to disk.
/// Removes the key if the passed in value is null
void setKeyValue(String key, String? value) {
if (key.isEmpty) {
Log.warn("The key should not be empty");
return;
}
2022-02-01 12:15:11 +08:00
if (value == null) {
_setting.settingKeyValue.remove(key);
}
if (_setting.settingKeyValue[key] != value) {
if (value == null) {
_setting.settingKeyValue.remove(key);
} else {
_setting.settingKeyValue[key] = value;
}
2022-10-25 17:30:57 +08:00
}
_saveAppearSetting();
notifyListeners();
}
2022-10-25 17:30:57 +08:00
String? getValue(String key) {
if (key.isEmpty) {
Log.warn("The key should not be empty");
return null;
}
2022-10-25 17:30:57 +08:00
return _setting.settingKeyValue[key];
}
/// Called when the application launch.
/// Uses the device locale when open the application for the first time
void readLocaleWhenAppLaunch(BuildContext context) {
if (_setting.resetToDefault) {
_setting.resetToDefault = false;
_saveAppearSetting();
setLocale(context, context.deviceLocale);
return;
2022-02-01 12:15:11 +08:00
}
setLocale(context, _locale);
}
Future<void> _saveAppearSetting() async {
2022-10-25 17:30:57 +08:00
SettingsFFIService().setAppearanceSetting(_setting).then((result) {
result.fold(
(l) => null,
(error) => Log.error(error),
);
});
}
@override
List<Object> get props {
return [_setting.hashCode];
}
2022-01-28 18:34:21 +08:00
}