From 54913270403a64a7eef9379390200bbd74582353 Mon Sep 17 00:00:00 2001 From: Airyz <36567925+Airyzz@users.noreply.github.com> Date: Sat, 22 Jun 2024 20:30:21 +0930 Subject: [PATCH] cleaning up --- commet/lib/ui/atoms/room_list.dart | 280 ---------------------------- commet/lib/ui/atoms/space_list.dart | 6 +- 2 files changed, 5 insertions(+), 281 deletions(-) delete mode 100644 commet/lib/ui/atoms/room_list.dart diff --git a/commet/lib/ui/atoms/room_list.dart b/commet/lib/ui/atoms/room_list.dart deleted file mode 100644 index a8140a4d..00000000 --- a/commet/lib/ui/atoms/room_list.dart +++ /dev/null @@ -1,280 +0,0 @@ -import 'dart:async'; - -import 'package:commet/client/components/direct_messages/direct_message_component.dart'; -import 'package:commet/ui/atoms/dot_indicator.dart'; -import 'package:commet/ui/atoms/notification_badge.dart'; -import 'package:flutter/widgets.dart'; -import 'package:intl/intl.dart'; -import 'package:tiamat/atoms/text_button.dart'; -import 'package:tiamat/tiamat.dart' as tiamat; -import 'package:flutter/material.dart' as m; -import '../../client/client.dart'; - -class RoomList extends StatefulWidget { - const RoomList(this.rooms, - {super.key, - this.onInsertStream, - this.onUpdateStream, - this.onRoomSelected, - this.onRemoveStream, - this.onRoomReordered, - this.expandable = false, - this.showHeader = false, - this.expanderText, - this.onChildUpdatedStream, - this.onRoomSelectionChanged}); - final bool expandable; - final bool showHeader; - final List rooms; - final Stream? onUpdateStream; - final Stream? onChildUpdatedStream; - final Stream? onInsertStream; - final Stream? onRemoveStream; - final String? expanderText; - final void Function(int)? onRoomSelected; - final void Function(int oldIndex, int newIndex)? onRoomReordered; - final Stream? onRoomSelectionChanged; - @override - State createState() => _RoomListState(); -} - -class _RoomListState extends State - with SingleTickerProviderStateMixin { - final GlobalKey _listKey = GlobalKey(); - int _count = 0; - StreamSubscription? onInsertListener; - StreamSubscription? onRemoveListener; - StreamSubscription? onUpdateListener; - StreamSubscription? onChildUpdatedListener; - StreamSubscription? onRoomSelectionChangedListener; - AnimationController? controller; - bool expanded = false; - bool editMode = false; - int _selectedIndex = -1; - - String get labelRoomsList => Intl.message("Rooms", - desc: "Header label for the list of rooms", name: "labelRoomsList"); - - @override - void initState() { - onUpdateListener = widget.onUpdateStream?.listen((event) { - setState(() {}); - }); - - onInsertListener = widget.onInsertStream?.listen((index) { - _listKey.currentState?.insertItem(index); - _count++; - }); - - onRemoveListener = widget.onRemoveStream?.listen(onRoomRemoved); - - onChildUpdatedListener = widget.onChildUpdatedStream?.listen((event) { - _listKey.currentState?.setState(() {}); - }); - - onRoomSelectionChangedListener = - widget.onRoomSelectionChanged?.listen((room) { - if (_selectedIndex != -1 && widget.rooms[_selectedIndex] == room) return; - - setState(() { - _selectedIndex = widget.rooms.indexOf(room); - }); - }); - - _count = widget.rooms.length; - controller = AnimationController( - vsync: this, duration: const Duration(milliseconds: 100)); - - super.initState(); - } - - @override - void dispose() { - onInsertListener?.cancel(); - onUpdateListener?.cancel(); - onRoomSelectionChangedListener?.cancel(); - onChildUpdatedListener?.cancel(); - super.dispose(); - } - - @override - Widget build(BuildContext context) { - return SingleChildScrollView( - child: Column( - children: [ - if (widget.showHeader) header(), - if (widget.expandable) - tiamat.TextButton( - widget.expanderText!, - onTap: toggleExpansion, - icon: m.Icons.expand_circle_down, - ), - if (!widget.expandable) listRooms(), - if (widget.expandable) - SizeTransition( - sizeFactor: controller!, - axisAlignment: -1.0, - child: Padding( - padding: const EdgeInsets.fromLTRB(8, 0, 0, 0), - child: listRooms(), - ), - ), - ], - ), - ); - } - - Widget header() { - return Padding( - padding: const EdgeInsets.all(2.0), - child: SizedBox( - height: 40, - child: Row( - mainAxisAlignment: MainAxisAlignment.start, - crossAxisAlignment: CrossAxisAlignment.center, - children: [ - tiamat.Text.label(labelRoomsList), - ], - ), - ), - ); - } - - void toggleEditMode() { - setState(() { - editMode = !editMode; - }); - } - - Widget listRooms() { - if (editMode) { - return m.ReorderableListView.builder( - itemBuilder: (context, index) { - return TextButton( - icon: m.Icons.tag, - widget.rooms[index].displayName, - key: widget.rooms[index].key, - ); - }, - itemCount: widget.rooms.length, - onReorder: (oldIndex, newIndex) { - widget.onRoomReordered?.call(oldIndex, newIndex); - }, - shrinkWrap: true, - ); - } - - return AnimatedList( - key: _listKey, - initialItemCount: _count, - scrollDirection: Axis.vertical, - physics: const NeverScrollableScrollPhysics(), - shrinkWrap: true, - padding: const EdgeInsets.all(0), - itemBuilder: (context, i, animation) { - var room = widget.rooms[i]; - bool isDm = room.client - .getComponent() - ?.isRoomDirectMessage(room) ?? - false; - return buildRoomButton( - animation, - room, - context, - iconColor: getIconColor(context, i), - textColor: getTextColor(context, i), - highlighted: _selectedIndex == i, - isDm: isDm, - onTap: () { - widget.onRoomSelected?.call(i); - setState(() { - _selectedIndex = i; - }); - }, - ); - }, - ); - } - - SizeTransition buildRoomButton( - Animation animation, - Room room, - BuildContext context, { - Color? iconColor, - Color? textColor, - bool highlighted = false, - bool isDm = false, - void Function()? onTap, - }) { - return SizeTransition( - sizeFactor: animation, - child: Padding( - padding: const EdgeInsets.fromLTRB(0, 2, 0, 0), - child: SizedBox( - height: 37, - child: TextButton(room.displayName, - highlighted: highlighted, - icon: isDm ? m.Icons.alternate_email_rounded : m.Icons.tag, - iconColor: iconColor, - textColor: textColor, - footer: room.displayHighlightedNotificationCount > 0 - ? NotificationBadge(room.displayHighlightedNotificationCount) - : room.displayNotificationCount > 0 - ? const Padding( - padding: EdgeInsets.all(2.0), - child: DotIndicator(), - ) - : null, - onTap: onTap), - ), - ), - ); - } - - void toggleExpansion() { - setState(() { - expanded = !expanded; - if (expanded) controller?.forward(from: controller!.value); - if (!expanded) controller?.reverse(from: controller!.value); - }); - } - - Color getTextColor(BuildContext context, int index) { - if (index == _selectedIndex) - return m.Theme.of(context).colorScheme.onSurface; - - var room = widget.rooms[index]; - - if (room.notificationCount > 0 || room.highlightedNotificationCount > 0) - return m.Theme.of(context).colorScheme.onSurface; - - return m.Theme.of(context).colorScheme.secondary; - } - - Color getIconColor(BuildContext context, int index) { - var room = widget.rooms[index]; - - if (room.notificationCount > 0 || room.highlightedNotificationCount > 0) - return m.Theme.of(context).colorScheme.onSurface; - - return m.Theme.of(context).colorScheme.secondary; - } - - void onRoomRemoved(int index) { - if (mounted) { - var room = widget.rooms[index]; - var iconColor = getIconColor(context, index); - var color = getTextColor(context, index); - - if (index == _selectedIndex) { - _selectedIndex = -1; - } - - _listKey.currentState?.removeItem( - index, - (context, animation) => buildRoomButton(animation, room, context, - iconColor: iconColor, textColor: color)); - _count--; - } - } -} diff --git a/commet/lib/ui/atoms/space_list.dart b/commet/lib/ui/atoms/space_list.dart index a8676cf8..4a04d157 100644 --- a/commet/lib/ui/atoms/space_list.dart +++ b/commet/lib/ui/atoms/space_list.dart @@ -5,6 +5,7 @@ import 'package:commet/ui/atoms/room_text_button.dart'; import 'package:commet/utils/event_bus.dart'; import 'package:flutter/material.dart'; import 'package:implicitly_animated_list/implicitly_animated_list.dart'; +import 'package:intl/intl.dart'; import 'package:tiamat/tiamat.dart' as tiamat; class SpaceList extends StatefulWidget { @@ -34,6 +35,9 @@ class _SpaceListState extends State { Room? selectedRoom; bool topLevel = false; + String get labelRoomsList => Intl.message("Rooms", + desc: "Header label for the list of rooms", name: "labelRoomsList"); + @override void initState() { subSpaces = widget.space.subspaces; @@ -118,7 +122,7 @@ class _SpaceListState extends State { Padding( padding: const EdgeInsets.all(8.0), child: tiamat.Text( - "Rooms", + labelRoomsList, color: Theme.of(context).colorScheme.secondary, ), ),