lemmur-app-android/lib/stores/config_store.dart

126 lines
3.4 KiB
Dart
Raw Normal View History

2021-02-24 21:54:15 +01:00
import 'dart:convert';
2020-08-30 22:43:16 +02:00
import 'package:flutter/material.dart';
2021-02-24 21:54:15 +01:00
import 'package:json_annotation/json_annotation.dart';
2021-04-16 20:15:48 +02:00
import 'package:lemmy_api_client/v3.dart';
import 'package:mobx/mobx.dart';
2020-08-30 22:43:16 +02:00
import 'package:shared_preferences/shared_preferences.dart';
2021-02-27 17:02:55 +01:00
import '../l10n/l10n.dart';
import '../util/async_store.dart';
import '../util/mobx_provider.dart';
2021-02-27 17:02:55 +01:00
2021-02-24 21:54:15 +01:00
part 'config_store.g.dart';
2020-10-26 00:39:57 +01:00
/// Store managing user-level configuration such as theme or language
2021-02-24 21:54:15 +01:00
@JsonSerializable()
@LocaleConverter()
class ConfigStore extends _ConfigStore with _$ConfigStore, DisposableStore {
static const _prefsKey = 'v1:ConfigStore';
late final SharedPreferences _sharedPrefs;
2021-02-24 21:54:15 +01:00
@visibleForTesting
ConfigStore();
factory ConfigStore.load(SharedPreferences sharedPrefs) {
final store = _$ConfigStoreFromJson(
jsonDecode(sharedPrefs.getString(_prefsKey) ?? '{}')
as Map<String, dynamic>,
).._sharedPrefs = sharedPrefs;
store.addReaction(autorun((_) => store.save()));
return store;
2020-08-30 22:43:16 +02:00
}
Future<void> save() async {
final serialized = jsonEncode(_$ConfigStoreToJson(this));
await _sharedPrefs.setString(_prefsKey, serialized);
2020-08-30 22:43:16 +02:00
}
}
abstract class _ConfigStore with Store {
@observable
@JsonKey(defaultValue: ThemeMode.system)
ThemeMode theme = ThemeMode.system;
@observable
@JsonKey(defaultValue: false)
bool amoledDarkMode = false;
// default value is set in the `LocaleConverter.fromJson`
@observable
Locale locale = const Locale('en');
2021-02-27 14:39:58 +01:00
@observable
2021-04-16 20:15:48 +02:00
@JsonKey(defaultValue: true)
bool showAvatars = true;
2021-04-16 20:15:48 +02:00
@observable
2021-04-16 20:15:48 +02:00
@JsonKey(defaultValue: true)
bool showScores = true;
2021-04-16 20:15:48 +02:00
2021-04-17 12:22:15 +02:00
// default is set in fromJson
@observable
2021-04-16 21:43:02 +02:00
@JsonKey(fromJson: _sortTypeFromJson)
SortType defaultSortType = SortType.hot;
2021-04-16 21:43:02 +02:00
2021-04-17 12:22:15 +02:00
// default is set in fromJson
@observable
2021-04-16 21:43:02 +02:00
@JsonKey(fromJson: _postListingTypeFromJson)
PostListingType defaultListingType = PostListingType.all;
final lemmyImportState = AsyncStore<FullSiteView>();
2021-04-16 21:43:02 +02:00
2021-04-16 21:19:59 +02:00
/// Copies over settings from lemmy to [ConfigStore]
@action
2021-04-16 21:19:59 +02:00
void copyLemmyUserSettings(LocalUserSettings localUserSettings) {
2021-04-16 20:15:48 +02:00
// themes from lemmy-ui that are dark mode
2021-04-23 19:26:30 +02:00
const darkModeLemmyUiThemes = {
'solar',
'cyborg',
'darkly',
'vaporwave-dark',
'i386',
};
2021-04-16 20:15:48 +02:00
showAvatars = localUserSettings.showAvatars;
theme = () {
2021-04-28 14:39:31 +02:00
if (localUserSettings.theme == 'browser') return ThemeMode.system;
if (darkModeLemmyUiThemes.contains(localUserSettings.theme)) {
return ThemeMode.dark;
}
return ThemeMode.light;
}();
if (L10n.supportedLocales.contains(Locale(localUserSettings.lang))) {
locale = Locale(localUserSettings.lang);
}
showScores = localUserSettings.showScores;
defaultSortType = localUserSettings.defaultSortType;
defaultListingType = localUserSettings.defaultListingType;
2021-04-16 20:15:48 +02:00
}
2021-04-16 21:19:59 +02:00
/// Fetches [LocalUserSettings] and imports them with [.copyLemmyUserSettings]
@action
2021-04-16 21:19:59 +02:00
Future<void> importLemmyUserSettings(Jwt token) async {
final site = await lemmyImportState.runLemmy(
token.payload.iss,
GetSite(auth: token.raw),
);
2020-08-30 22:43:16 +02:00
if (site != null) {
copyLemmyUserSettings(site.myUser!.localUserView.localUser);
}
2020-08-30 22:43:16 +02:00
}
}
2021-04-16 21:43:02 +02:00
SortType _sortTypeFromJson(String? json) =>
json != null ? SortType.fromJson(json) : SortType.hot;
PostListingType _postListingTypeFromJson(String? json) =>
json != null ? PostListingType.fromJson(json) : PostListingType.all;