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';
|
2021-10-31 12:52:58 +01:00
|
|
|
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';
|
2021-10-31 12:52:58 +01:00
|
|
|
import '../util/async_store.dart';
|
2021-12-04 18:03:54 +01:00
|
|
|
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:49:24 +01:00
|
|
|
|
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()
|
2021-10-31 12:52:58 +01:00
|
|
|
@LocaleConverter()
|
2021-12-04 18:03:54 +01:00
|
|
|
class ConfigStore extends _ConfigStore with _$ConfigStore, DisposableStore {
|
2021-10-31 12:52:58 +01:00
|
|
|
static const _prefsKey = 'v1:ConfigStore';
|
|
|
|
late final SharedPreferences _sharedPrefs;
|
2021-02-24 21:54:15 +01:00
|
|
|
|
2021-10-31 12:52:58 +01:00
|
|
|
@visibleForTesting
|
|
|
|
ConfigStore();
|
|
|
|
|
|
|
|
factory ConfigStore.load(SharedPreferences sharedPrefs) {
|
|
|
|
final store = _$ConfigStoreFromJson(
|
|
|
|
jsonDecode(sharedPrefs.getString(_prefsKey) ?? '{}')
|
|
|
|
as Map<String, dynamic>,
|
|
|
|
).._sharedPrefs = sharedPrefs;
|
|
|
|
|
2021-12-04 18:03:54 +01:00
|
|
|
store.addReaction(autorun((_) => store.save()));
|
2021-10-31 12:52:58 +01:00
|
|
|
|
|
|
|
return store;
|
2020-08-30 22:43:16 +02:00
|
|
|
}
|
|
|
|
|
2021-10-31 12:52:58 +01:00
|
|
|
Future<void> save() async {
|
|
|
|
final serialized = jsonEncode(_$ConfigStoreToJson(this));
|
|
|
|
|
|
|
|
await _sharedPrefs.setString(_prefsKey, serialized);
|
2020-08-30 22:43:16 +02:00
|
|
|
}
|
2021-10-31 12:52:58 +01: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
|
|
|
|
2021-10-31 12:52:58 +01:00
|
|
|
@observable
|
2021-04-16 20:15:48 +02:00
|
|
|
@JsonKey(defaultValue: true)
|
2021-10-31 12:52:58 +01:00
|
|
|
bool showAvatars = true;
|
2021-04-16 20:15:48 +02:00
|
|
|
|
2021-10-31 12:52:58 +01:00
|
|
|
@observable
|
2021-04-16 20:15:48 +02:00
|
|
|
@JsonKey(defaultValue: true)
|
2021-10-31 12:52:58 +01:00
|
|
|
bool showScores = true;
|
2021-04-16 20:15:48 +02:00
|
|
|
|
2021-04-17 12:22:15 +02:00
|
|
|
// default is set in fromJson
|
2021-10-31 12:52:58 +01:00
|
|
|
@observable
|
2021-04-16 21:43:02 +02:00
|
|
|
@JsonKey(fromJson: _sortTypeFromJson)
|
2021-10-31 12:52:58 +01:00
|
|
|
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
|
2021-10-31 12:52:58 +01:00
|
|
|
@observable
|
2021-04-16 21:43:02 +02:00
|
|
|
@JsonKey(fromJson: _postListingTypeFromJson)
|
2021-10-31 12:52:58 +01:00
|
|
|
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]
|
2021-10-31 12:52:58 +01:00
|
|
|
@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
|
|
|
|
2021-10-31 12:52:58 +01: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;
|
|
|
|
}();
|
2021-10-31 12:52:58 +01:00
|
|
|
|
|
|
|
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]
|
2021-10-31 12:52:58 +01:00
|
|
|
@action
|
2021-04-16 21:19:59 +02:00
|
|
|
Future<void> importLemmyUserSettings(Jwt token) async {
|
2021-10-31 12:52:58 +01:00
|
|
|
final site = await lemmyImportState.runLemmy(
|
|
|
|
token.payload.iss,
|
|
|
|
GetSite(auth: token.raw),
|
2021-04-09 00:11:44 +02:00
|
|
|
);
|
2020-08-30 22:43:16 +02:00
|
|
|
|
2021-10-31 12:52:58 +01: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;
|