refactor: Use AppSettings enum based configuration everywhere and fix load from json on web

This commit is contained in:
Christian Kußowski 2025-10-19 19:36:47 +02:00
commit 4c357f6249
No known key found for this signature in database
GPG key ID: E067ECD60F1A0652
46 changed files with 357 additions and 402 deletions

View file

@ -13,10 +13,8 @@ import 'package:go_router/go_router.dart';
import 'package:image_picker/image_picker.dart';
import 'package:matrix/matrix.dart';
import 'package:scroll_to_index/scroll_to_index.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:universal_html/html.dart' as html;
import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/config/setting_keys.dart';
import 'package:fluffychat/config/themes.dart';
import 'package:fluffychat/l10n/l10n.dart';
@ -224,7 +222,7 @@ class ChatController extends State<ChatPageWithRoom>
}
void _loadDraft() async {
final prefs = await SharedPreferences.getInstance();
final prefs = Matrix.of(context).store;
final draft = prefs.getString('draft_$roomId');
if (draft != null && draft.isNotEmpty) {
sendController.text = draft;
@ -274,7 +272,7 @@ class ChatController extends State<ChatPageWithRoom>
KeyEventResult _customEnterKeyHandling(FocusNode node, KeyEvent evt) {
if (!HardwareKeyboard.instance.isShiftPressed &&
evt.logicalKey.keyLabel == 'Enter' &&
(AppConfig.sendOnEnter ?? !PlatformInfos.isMobile)) {
AppSettings.sendOnEnter.value) {
if (evt is KeyDownEvent) {
send();
}
@ -325,7 +323,7 @@ class ChatController extends State<ChatPageWithRoom>
WidgetsBinding.instance.addPostFrameCallback(_shareItems);
super.initState();
_displayChatDetailsColumn = ValueNotifier(
AppSettings.displayChatDetailsColumn.getItem(Matrix.of(context).store),
AppSettings.displayChatDetailsColumn.value,
);
sendingClient = Matrix.of(context).client;
@ -365,7 +363,9 @@ class ChatController extends State<ChatPageWithRoom>
var readMarkerEventIndex = readMarkerEventId.isEmpty
? -1
: timeline!.events
.filterByVisibleInGui(exceptionEventId: readMarkerEventId)
.filterByVisibleInGui(
exceptionEventId: readMarkerEventId,
)
.indexWhere((e) => e.eventId == readMarkerEventId);
// Read marker is existing but not found in first events. Try a single
@ -373,7 +373,9 @@ class ChatController extends State<ChatPageWithRoom>
if (readMarkerEventId.isNotEmpty && readMarkerEventIndex == -1) {
await timeline?.requestHistory(historyCount: _loadHistoryCount);
readMarkerEventIndex = timeline!.events
.filterByVisibleInGui(exceptionEventId: readMarkerEventId)
.filterByVisibleInGui(
exceptionEventId: readMarkerEventId,
)
.indexWhere((e) => e.eventId == readMarkerEventId);
}
@ -492,7 +494,7 @@ class ChatController extends State<ChatPageWithRoom>
_setReadMarkerFuture = timeline
.setReadMarker(
eventId: eventId,
public: AppConfig.sendPublicReadReceipts,
public: AppSettings.sendPublicReadReceipts.value,
)
.then((_) {
_setReadMarkerFuture = null;
@ -542,7 +544,7 @@ class ChatController extends State<ChatPageWithRoom>
Future<void> send() async {
if (sendController.text.trim().isEmpty) return;
_storeInputTimeoutTimer?.cancel();
final prefs = await SharedPreferences.getInstance();
final prefs = Matrix.of(context).store;
prefs.remove('draft_$roomId');
var parseCommands = true;
@ -960,7 +962,9 @@ class ChatController extends State<ChatPageWithRoom>
final eventIndex = foundEvent == null
? -1
: timeline!.events
.filterByVisibleInGui(exceptionEventId: eventId)
.filterByVisibleInGui(
exceptionEventId: eventId,
)
.indexOf(foundEvent);
if (eventIndex == -1) {
@ -1203,7 +1207,7 @@ class ChatController extends State<ChatPageWithRoom>
_storeInputTimeoutTimer?.cancel();
_storeInputTimeoutTimer = Timer(_storeInputTimeout, () async {
final prefs = await SharedPreferences.getInstance();
final prefs = Matrix.of(context).store;
await prefs.setString('draft_$roomId', text);
});
if (text.endsWith(' ') && Matrix.of(context).hasComplexBundles) {
@ -1220,7 +1224,7 @@ class ChatController extends State<ChatPageWithRoom>
}
}
}
if (AppConfig.sendTypingNotifications) {
if (AppSettings.sendTypingNotifications.value) {
typingCoolDown?.cancel();
typingCoolDown = Timer(const Duration(seconds: 2), () {
typingCoolDown = null;
@ -1307,7 +1311,6 @@ class ChatController extends State<ChatPageWithRoom>
void toggleDisplayChatDetailsColumn() async {
await AppSettings.displayChatDetailsColumn.setItem(
Matrix.of(context).store,
!_displayChatDetailsColumn.value,
);
_displayChatDetailsColumn.value = !_displayChatDetailsColumn.value;