Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix jumping to begining of day on group message #358

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 38 additions & 48 deletions bruig/flutterui/bruig/lib/components/chat/messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,25 +163,57 @@ class _MessagesState extends State<Messages> {
return const Empty();
}

int calculateTotalMessageCount() {
int count = 0;
for (var dayGCMsgs in chat.dayGCMsgs) {
count += dayGCMsgs.msgs.length + 1; // +1 for the day change message
}
return count;
}

@override
Widget build(BuildContext context) {
var theme = Theme.of(context);
var textColor = theme.dividerColor;
var backgroundColor = theme.backgroundColor;
var backgroundColor = theme.highlightColor;
return Scaffold(
floatingActionButton: _getFAB(textColor, backgroundColor),
body: SelectionArea(
child: ScrollablePositionedList.builder(
itemCount: chat.isGC ? chat.dayGCMsgs.length : chat.msgs.length,
itemCount: chat.isGC ? calculateTotalMessageCount() : chat.msgs.length,
physics: const ClampingScrollPhysics(),
itemBuilder: chat.isGC
? (context, index) {
return DayGroupedMessages(chat, chat.dayGCMsgs[index], nick,
client, _scrollToBottom);
int count = 0;
for (var dayGCMsgs in chat.dayGCMsgs) {
if (index == count) {
// This is a day change message
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
DateChange(
child: Text(
dayGCMsgs.date,
style: TextStyle(color: textColor),
),
),
],
);
}
count++; // for the day change message
if (index < count + dayGCMsgs.msgs.length) {
var msg = dayGCMsgs.msgs[index - count];
return Container(
color: backgroundColor,
child: Event(chat, msg, nick, client, _scrollToBottom),
);
}
count += dayGCMsgs.msgs.length;
}
return const SizedBox.shrink();
}
: (context, index) {
return Event(
chat, chat.msgs[index], nick, client, _scrollToBottom);
return Event(chat, chat.msgs[index], nick, client, _scrollToBottom);
},
itemScrollController: widget.itemScrollController,
itemPositionsListener: widget.itemPositionsListener,
Expand All @@ -190,45 +222,3 @@ class _MessagesState extends State<Messages> {
);
}
}

class DayGroupedMessages extends StatelessWidget {
final DayGCMessages dayGCMsgs;
final ChatModel chat;
final String nick;
final ClientModel client;
final Function() scrollToBottom;
const DayGroupedMessages(
this.chat, this.dayGCMsgs, this.nick, this.client, this.scrollToBottom,
{Key? key})
: super(key: key);

showSubMenu(bool isGC, String id) => client.showSubMenu(isGC, id);

@override
Widget build(BuildContext context) {
var theme = Theme.of(context);
var textColor = theme.dividerColor;
var bgColor = theme.highlightColor;
return Column(children: [
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
DateChange(
child: Text(dayGCMsgs.date, style: TextStyle(color: textColor)))
]),
Container(
margin: const EdgeInsets.only(left: 8, right: 8),
padding: const EdgeInsets.only(bottom: 5, left: 5, right: 5, top: 5),
decoration: BoxDecoration(
color: bgColor, borderRadius: BorderRadius.circular(10)),
child: Column(children: [
ListView.builder(
shrinkWrap: true,
itemCount: dayGCMsgs.msgs.length,
itemBuilder: (context, index) {
return Event(
chat, dayGCMsgs.msgs[index], nick, client, scrollToBottom);
},
)
]))
]);
}
}
Loading