2022-03-20 17:17:06 +08:00
|
|
|
import 'dart:async';
|
|
|
|
|
2022-03-01 10:31:39 -05:00
|
|
|
import 'package:app_flowy/user/application/user_settings_service.dart';
|
2022-01-28 18:34:21 +08:00
|
|
|
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-11-10 14:22:18 +08:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
2022-01-28 18:34:21 +08:00
|
|
|
|
2022-11-10 14:22:18 +08:00
|
|
|
part 'appearance.freezed.dart';
|
|
|
|
|
|
|
|
/// [AppearanceSettingsCubit] is used to modify the appear setting of AppFlowy application. Includes the [Locale] and [AppTheme].
|
|
|
|
class AppearanceSettingsCubit extends Cubit<AppearanceSettingsState> {
|
2022-09-28 11:44:44 +08:00
|
|
|
final AppearanceSettingsPB _setting;
|
2022-01-28 21:10:13 +05:30
|
|
|
|
2022-11-10 14:22:18 +08:00
|
|
|
AppearanceSettingsCubit(AppearanceSettingsPB setting)
|
2022-09-28 11:44:44 +08:00
|
|
|
: _setting = setting,
|
2022-11-16 14:40:30 +08:00
|
|
|
super(AppearanceSettingsState.initial(
|
|
|
|
setting.theme,
|
|
|
|
setting.font,
|
|
|
|
setting.monospaceFont,
|
|
|
|
setting.locale,
|
|
|
|
));
|
2022-01-28 18:34:21 +08:00
|
|
|
|
2022-09-28 11:44:44 +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.
|
2022-11-10 14:22:18 +08:00
|
|
|
void setTheme(Brightness brightness) {
|
|
|
|
if (state.theme.brightness == brightness) {
|
2022-09-28 11:44:44 +08:00
|
|
|
return;
|
|
|
|
}
|
2022-01-28 18:34:21 +08:00
|
|
|
|
2022-11-10 14:22:18 +08:00
|
|
|
_setting.theme = themeTypeToString(brightness);
|
|
|
|
_saveAppearanceSettings();
|
2022-01-28 18:34:21 +08:00
|
|
|
|
2022-11-16 14:40:30 +08:00
|
|
|
emit(state.copyWith(
|
|
|
|
theme: AppTheme.fromName(
|
|
|
|
themeName: _setting.theme,
|
|
|
|
font: state.theme.font,
|
|
|
|
monospaceFont: state.theme.monospaceFont,
|
|
|
|
),
|
|
|
|
));
|
2022-01-28 18:34:21 +08:00
|
|
|
}
|
|
|
|
|
2022-09-28 11:44:44 +08:00
|
|
|
/// Updates the current locale and notify the listeners the locale was changed
|
|
|
|
/// Fallback to [en] locale If the newLocale is not supported.
|
2022-02-04 15:45:06 -05:00
|
|
|
void setLocale(BuildContext context, Locale newLocale) {
|
2022-09-18 13:55:47 -04:00
|
|
|
if (!context.supportedLocales.contains(newLocale)) {
|
2022-09-28 11:44:44 +08:00
|
|
|
Log.warn("Unsupported locale: $newLocale, Fallback to locale: en");
|
2022-09-18 13:55:47 -04:00
|
|
|
newLocale = const Locale('en');
|
|
|
|
}
|
|
|
|
|
|
|
|
context.setLocale(newLocale);
|
2022-02-05 21:50:49 +08:00
|
|
|
|
2022-11-10 14:22:18 +08:00
|
|
|
if (state.locale != newLocale) {
|
|
|
|
_setting.locale.languageCode = newLocale.languageCode;
|
|
|
|
_setting.locale.countryCode = newLocale.countryCode ?? "";
|
|
|
|
_saveAppearanceSettings();
|
2022-09-28 11:44:44 +08:00
|
|
|
|
2022-11-10 14:22:18 +08:00
|
|
|
emit(state.copyWith(locale: newLocale));
|
2022-01-28 18:48:09 +08:00
|
|
|
}
|
2022-01-28 18:34:21 +08:00
|
|
|
}
|
2022-02-01 09:12:59 +08:00
|
|
|
|
2022-09-28 11:44:44 +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
|
|
|
|
2022-09-28 11:44:44 +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
|
|
|
}
|
2022-11-10 14:22:18 +08:00
|
|
|
_saveAppearanceSettings();
|
2022-10-25 17:30:57 +08:00
|
|
|
}
|
2022-09-28 11:44:44 +08:00
|
|
|
|
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-09-28 11:44:44 +08:00
|
|
|
}
|
2022-10-25 17:30:57 +08:00
|
|
|
return _setting.settingKeyValue[key];
|
2022-09-28 11:44:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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;
|
2022-11-10 14:22:18 +08:00
|
|
|
_saveAppearanceSettings();
|
2022-02-04 15:45:06 -05:00
|
|
|
setLocale(context, context.deviceLocale);
|
2022-09-18 13:55:47 -04:00
|
|
|
return;
|
2022-02-01 12:15:11 +08:00
|
|
|
}
|
2022-09-18 13:55:47 -04:00
|
|
|
|
2022-11-10 14:22:18 +08:00
|
|
|
setLocale(context, state.locale);
|
2022-02-01 09:12:59 +08:00
|
|
|
}
|
2022-09-28 11:44:44 +08:00
|
|
|
|
2022-11-10 14:22:18 +08:00
|
|
|
Future<void> _saveAppearanceSettings() async {
|
2022-10-25 17:30:57 +08:00
|
|
|
SettingsFFIService().setAppearanceSetting(_setting).then((result) {
|
|
|
|
result.fold(
|
|
|
|
(l) => null,
|
|
|
|
(error) => Log.error(error),
|
|
|
|
);
|
|
|
|
});
|
2022-09-28 11:44:44 +08:00
|
|
|
}
|
2022-11-10 14:22:18 +08:00
|
|
|
}
|
2022-09-28 11:44:44 +08:00
|
|
|
|
2022-11-10 14:22:18 +08:00
|
|
|
@freezed
|
|
|
|
class AppearanceSettingsState with _$AppearanceSettingsState {
|
|
|
|
const factory AppearanceSettingsState({
|
|
|
|
required AppTheme theme,
|
|
|
|
required Locale locale,
|
|
|
|
}) = _AppearanceSettingsState;
|
|
|
|
|
|
|
|
factory AppearanceSettingsState.initial(
|
|
|
|
String themeName,
|
2022-11-16 14:40:30 +08:00
|
|
|
String font,
|
|
|
|
String monospaceFont,
|
2022-11-10 14:22:18 +08:00
|
|
|
LocaleSettingsPB locale,
|
|
|
|
) =>
|
|
|
|
AppearanceSettingsState(
|
2022-11-16 14:40:30 +08:00
|
|
|
theme: AppTheme.fromName(
|
|
|
|
themeName: themeName,
|
|
|
|
font: font,
|
|
|
|
monospaceFont: monospaceFont,
|
|
|
|
),
|
2022-11-10 14:22:18 +08:00
|
|
|
locale: Locale(locale.languageCode, locale.countryCode),
|
|
|
|
);
|
2022-01-28 18:34:21 +08:00
|
|
|
}
|