Skip to content

Commit

Permalink
fix: correctly shift enter and enter send, more fade animations + cre…
Browse files Browse the repository at this point in the history
…ate timeline model
  • Loading branch information
Feichtmeier committed Jan 11, 2025
1 parent 502a5e5 commit 6325a09
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 150 deletions.
41 changes: 0 additions & 41 deletions lib/chat/chat_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,6 @@ class ChatModel extends SafeChangeNotifier {
Room? get selectedRoom => _selectedRoom;
Future<void> setSelectedRoom(Room? value) async {
_selectedRoom = value;
if (value == null) {
_roomSearchActive = false;
}
notifyListeners();
}

Expand Down Expand Up @@ -370,42 +367,4 @@ class ChatModel extends SafeChangeNotifier {
_setProcessingJoinOrLeave(false);
}
}

// TIMELINES

bool _updatingTimeline = false;
bool get updatingTimeline => _updatingTimeline;
void setUpdatingTimeline(bool value) {
if (value == _updatingTimeline) return;
_updatingTimeline = value;
notifyListeners();
}

Future<void> requestHistory(
Timeline timeline, {
int historyCount = Room.defaultHistoryCount,
StateFilter? filter,
bool notify = true,
}) async {
if (notify) {
setUpdatingTimeline(true);
}
if (timeline.isRequestingHistory) {
setUpdatingTimeline(false);
return;
}
await timeline.requestHistory(filter: filter, historyCount: historyCount);
if (notify) {
setUpdatingTimeline(false);
}
}

bool _roomSearchActive = false;
bool get roomSearchActive => _roomSearchActive;
void toggleRoomSearch({bool? value}) {
bool theValue = value ?? !_roomSearchActive;
if (theValue == _roomSearchActive) return;
_roomSearchActive = theValue;
notifyListeners();
}
}
8 changes: 4 additions & 4 deletions lib/chat/draft_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ class DraftModel extends SafeChangeNotifier {
}
}

final draft = getDraft(room.id);
if (draft?.isNotEmpty ?? false) {
final draft = '${getDraft(room.id)}';
removeDraft(room.id);
if (draft.isNotEmpty) {
try {
await room.sendTextEvent(
draft!.trim(),
draft.trim(),
inReplyTo: replyEvent,
editEventId: _editEvents[room.id]?.eventId,
);
Expand All @@ -96,7 +97,6 @@ class DraftModel extends SafeChangeNotifier {
_sending = false;
_replyEvent = null;
_editEvents[room.id] = null;
removeDraft(room.id);

notifyListeners();
}
Expand Down
43 changes: 43 additions & 0 deletions lib/chat/timeline_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import 'package:matrix/matrix.dart';
import 'package:safe_change_notifier/safe_change_notifier.dart';

class TimelineModel extends SafeChangeNotifier {
// TIMELINES

bool _updatingTimeline = false;
bool get updatingTimeline => _updatingTimeline;
void setUpdatingTimeline(bool value) {
if (value == _updatingTimeline) return;
_updatingTimeline = value;
notifyListeners();
}

Future<void> requestHistory(
Timeline timeline, {
int historyCount = 350,
StateFilter? filter,
bool notify = true,
}) async {
if (notify) {
setUpdatingTimeline(true);
}
if (timeline.isRequestingHistory) {
setUpdatingTimeline(false);
return;
}
await timeline.requestHistory(filter: filter, historyCount: historyCount);
await timeline.setReadMarker();
if (notify) {
setUpdatingTimeline(false);
}
}

bool _timelineSearchActive = false;
bool get timelineSearchActive => _timelineSearchActive;
void toggleTimelineSearch({bool? value}) {
bool theValue = value ?? !_timelineSearchActive;
if (theValue == _timelineSearchActive) return;
_timelineSearchActive = theValue;
notifyListeners();
}
}
4 changes: 2 additions & 2 deletions lib/chat/view/chat_room/chat_room_info_drawer_topic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:matrix/matrix.dart';
import 'package:watch_it/watch_it.dart';

import '../../../common/view/ui_constants.dart';
import '../../chat_model.dart';
import '../../timeline_model.dart';

class ChatRoomInfoDrawerTopic extends StatelessWidget with WatchItMixin {
const ChatRoomInfoDrawerTopic({
Expand All @@ -17,7 +17,7 @@ class ChatRoomInfoDrawerTopic extends StatelessWidget with WatchItMixin {
@override
Widget build(BuildContext context) {
final updatingTimeline =
watchPropertyValue((ChatModel m) => m.updatingTimeline);
watchPropertyValue((TimelineModel m) => m.updatingTimeline);

return SliverToBoxAdapter(
child: Center(
Expand Down
61 changes: 41 additions & 20 deletions lib/chat/view/chat_room/chat_room_master_tile_subtitle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ class ChatRoomMasterTileSubTitle extends StatelessWidget with WatchItMixin {
initialValue: room.lastEvent,
).data;

return typingUsers.isEmpty
? _LastEvent(
key: ObjectKey(lastEvent),
lastEvent: lastEvent ?? room.lastEvent,
fallbackText:
room.membership == Membership.invite ? room.name : null,
)
: Text(
typingUsers.length > 1
? context.l10n.numUsersTyping(typingUsers.length)
: context.l10n
.userIsTyping(typingUsers.first.displayName ?? ''),
style: context.textTheme.bodyMedium
?.copyWith(color: context.colorScheme.primary),
maxLines: 1,
);
if (typingUsers.isEmpty) {
return _LastEvent(
key: ValueKey('${lastEvent?.eventId}lastevent'),
lastEvent: room.lastEvent,
fallbackText: room.membership == Membership.invite ? room.name : null,
);
}

return Text(
typingUsers.length > 1
? context.l10n.numUsersTyping(typingUsers.length)
: context.l10n.userIsTyping(typingUsers.first.displayName ?? ''),
style: context.textTheme.bodyMedium
?.copyWith(color: context.colorScheme.primary),
maxLines: 1,
);
}
}

Expand All @@ -60,21 +60,42 @@ class _LastEvent extends StatefulWidget with WatchItStatefulWidgetMixin {
class _LastEventState extends State<_LastEvent> {
late final Future<String> _future;

static final Map<String, String> _cache = {};

@override
void initState() {
super.initState();
_future = widget.lastEvent
?.calcLocalizedBody(const MatrixDefaultLocalizations()) ??
Future.value(widget.lastEvent?.body ?? '');
_future = widget.lastEvent != null &&
_cache.containsKey(widget.lastEvent!.eventId)
? Future.value(_cache[widget.lastEvent!.eventId]!)
: widget.lastEvent
?.calcLocalizedBody(const MatrixDefaultLocalizations()) ??
Future.value(widget.lastEvent?.body ?? '');
}

@override
Widget build(BuildContext context) {
if (widget.lastEvent != null &&
_cache.containsKey(widget.lastEvent!.eventId)) {
return Text(
_cache[widget.lastEvent!.eventId]!,
maxLines: 1,
);
}

return FutureBuilder(
future: _future,
builder: (context, snapshot) {
if (snapshot.hasData && widget.lastEvent != null) {
_cache[widget.lastEvent!.eventId] = snapshot.data!;
return Text(
snapshot.data!,
maxLines: 1,
);
}

return Text(
snapshot.hasData ? snapshot.data! : (widget.fallbackText ?? ' '),
widget.fallbackText ?? ' ',
maxLines: 1,
);
},
Expand Down
37 changes: 22 additions & 15 deletions lib/chat/view/chat_room/chat_room_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import '../../../common/view/snackbars.dart';
import '../../../common/view/ui_constants.dart';
import '../../../l10n/l10n.dart';
import '../../chat_model.dart';
import '../../timeline_model.dart';
import 'chat_room_default_background.dart';
import 'chat_room_info_drawer.dart';
import 'chat_timeline_list.dart';
import 'chat_room_timeline_list.dart';
import 'input/chat_input.dart';
import 'titlebar/chat_room_title_bar.dart';

Expand Down Expand Up @@ -50,7 +51,8 @@ class _ChatRoomPageState extends State<ChatRoomPage> {
final archiveActive = watchPropertyValue((ChatModel m) => m.archiveActive);
final loadingArchive =
watchPropertyValue((ChatModel m) => m.loadingArchive);
final updating = watchPropertyValue((ChatModel m) => m.updatingTimeline);
final updating =
watchPropertyValue((TimelineModel m) => m.updatingTimeline);

registerStreamHandler(
select: (ChatModel m) => m.getLeftRoomStream(widget.room.id),
Expand Down Expand Up @@ -103,7 +105,7 @@ class _ChatRoomPageState extends State<ChatRoomPage> {

return Padding(
padding: const EdgeInsets.only(bottom: kMediumPadding),
child: ChatTimelineList(
child: ChatRoomTimelineList(
timeline: snapshot.data!,
room: widget.room,
listKey: _roomListKey,
Expand All @@ -112,22 +114,27 @@ class _ChatRoomPageState extends State<ChatRoomPage> {
},
),
),
if (updating)
Positioned(
top: 3 * kBigPadding,
child: FloatingActionButton.small(
backgroundColor: context.colorScheme.isLight
? Colors.white
: Colors.black.scale(lightness: 0.09),
onPressed: () {},
child: const SizedBox.square(
dimension: 20,
child: Progress(
strokeWidth: 2,
Positioned(
top: 3 * kBigPadding,
child: IgnorePointer(
child: AnimatedOpacity(
duration: const Duration(milliseconds: 200),
opacity: updating ? 1 : 0,
child: FloatingActionButton.small(
backgroundColor: context.colorScheme.isLight
? Colors.white
: Colors.black.scale(lightness: 0.09),
onPressed: () {},
child: const SizedBox.square(
dimension: 20,
child: Progress(
strokeWidth: 2,
),
),
),
),
),
),
],
);
}
Expand Down
Loading

0 comments on commit 6325a09

Please sign in to comment.