2023-05-25 13:30:15 -04:00
|
|
|
import 'package:contacts_plus_plus/auxiliary.dart';
|
2023-05-20 15:09:22 -04:00
|
|
|
import 'package:contacts_plus_plus/clients/audio_cache_client.dart';
|
2023-05-06 13:24:28 -04:00
|
|
|
import 'package:contacts_plus_plus/clients/messaging_client.dart';
|
|
|
|
import 'package:contacts_plus_plus/models/friend.dart';
|
2023-05-16 09:57:44 -04:00
|
|
|
import 'package:contacts_plus_plus/widgets/default_error_widget.dart';
|
2023-05-07 07:38:45 -04:00
|
|
|
import 'package:contacts_plus_plus/widgets/friends/friend_online_status_indicator.dart';
|
2023-05-21 11:27:29 -04:00
|
|
|
import 'package:contacts_plus_plus/widgets/messages/message_input_bar.dart';
|
2023-05-06 13:24:28 -04:00
|
|
|
import 'package:contacts_plus_plus/widgets/messages/messages_session_header.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
|
|
|
|
import 'message_bubble.dart';
|
|
|
|
|
|
|
|
class MessagesList extends StatefulWidget {
|
|
|
|
const MessagesList({required this.friend, super.key});
|
|
|
|
|
|
|
|
final Friend friend;
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<StatefulWidget> createState() => _MessagesListState();
|
|
|
|
}
|
|
|
|
|
2023-05-16 04:51:55 -04:00
|
|
|
class _MessagesListState extends State<MessagesList> with SingleTickerProviderStateMixin {
|
2023-05-06 13:24:28 -04:00
|
|
|
final ScrollController _sessionListScrollController = ScrollController();
|
2023-05-17 17:20:56 -04:00
|
|
|
|
|
|
|
bool _showSessionListScrollChevron = false;
|
2023-05-06 13:24:28 -04:00
|
|
|
|
|
|
|
double get _shevronOpacity => _showSessionListScrollChevron ? 1.0 : 0.0;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_sessionListScrollController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_sessionListScrollController.addListener(() {
|
|
|
|
if (_sessionListScrollController.position.maxScrollExtent > 0 && !_showSessionListScrollChevron) {
|
|
|
|
setState(() {
|
|
|
|
_showSessionListScrollChevron = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (_sessionListScrollController.position.atEdge && _sessionListScrollController.position.pixels > 0
|
|
|
|
&& _showSessionListScrollChevron) {
|
|
|
|
setState(() {
|
|
|
|
_showSessionListScrollChevron = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-18 07:52:34 -04:00
|
|
|
|
2023-05-06 13:24:28 -04:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-05-21 11:27:29 -04:00
|
|
|
final sessions = widget.friend.userStatus.activeSessions;
|
2023-05-06 13:24:28 -04:00
|
|
|
final appBarColor = Theme
|
|
|
|
.of(context)
|
|
|
|
.colorScheme
|
|
|
|
.surfaceVariant;
|
2023-05-16 09:57:44 -04:00
|
|
|
return Consumer<MessagingClient>(
|
2023-05-16 10:11:03 -04:00
|
|
|
builder: (context, mClient, _) {
|
|
|
|
final cache = mClient.getUserMessageCache(widget.friend.id);
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: Row(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
FriendOnlineStatusIndicator(userStatus: widget.friend.userStatus),
|
|
|
|
const SizedBox(width: 8,),
|
|
|
|
Text(widget.friend.username),
|
|
|
|
if (widget.friend.isHeadless) Padding(
|
|
|
|
padding: const EdgeInsets.only(left: 12),
|
|
|
|
child: Icon(Icons.dns, size: 18, color: Theme
|
|
|
|
.of(context)
|
|
|
|
.colorScheme
|
|
|
|
.onSecondaryContainer
|
2023-05-21 08:23:53 -04:00
|
|
|
.withAlpha(150),
|
|
|
|
),
|
2023-05-16 10:11:03 -04:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
scrolledUnderElevation: 0.0,
|
|
|
|
backgroundColor: appBarColor,
|
2023-05-06 13:24:28 -04:00
|
|
|
),
|
2023-05-16 10:11:03 -04:00
|
|
|
body: Column(
|
|
|
|
children: [
|
|
|
|
if (sessions.isNotEmpty) Container(
|
|
|
|
constraints: const BoxConstraints(maxHeight: 64),
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
color: appBarColor,
|
2023-05-25 13:30:15 -04:00
|
|
|
border: const Border(bottom: BorderSide(width: 1, color: Colors.black),)
|
2023-05-16 10:11:03 -04:00
|
|
|
),
|
|
|
|
child: Stack(
|
|
|
|
children: [
|
|
|
|
ListView.builder(
|
|
|
|
controller: _sessionListScrollController,
|
|
|
|
scrollDirection: Axis.horizontal,
|
|
|
|
itemCount: sessions.length,
|
|
|
|
itemBuilder: (context, index) => SessionTile(session: sessions[index]),
|
|
|
|
),
|
|
|
|
AnimatedOpacity(
|
|
|
|
opacity: _shevronOpacity,
|
|
|
|
curve: Curves.easeOut,
|
|
|
|
duration: const Duration(milliseconds: 200),
|
|
|
|
child: Align(
|
|
|
|
alignment: Alignment.centerRight,
|
|
|
|
child: Container(
|
|
|
|
padding: const EdgeInsets.only(left: 16, right: 4, top: 1, bottom: 1),
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
gradient: LinearGradient(
|
|
|
|
begin: Alignment.centerLeft,
|
|
|
|
end: Alignment.centerRight,
|
|
|
|
colors: [
|
|
|
|
appBarColor.withOpacity(0),
|
|
|
|
appBarColor,
|
|
|
|
appBarColor,
|
|
|
|
],
|
|
|
|
),
|
2023-05-16 09:57:44 -04:00
|
|
|
),
|
2023-05-16 10:11:03 -04:00
|
|
|
height: double.infinity,
|
|
|
|
child: const Icon(Icons.chevron_right),
|
2023-05-16 09:57:44 -04:00
|
|
|
),
|
|
|
|
),
|
2023-05-16 10:11:03 -04:00
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
2023-05-06 13:24:28 -04:00
|
|
|
),
|
2023-05-16 10:11:03 -04:00
|
|
|
Expanded(
|
2023-05-17 17:20:56 -04:00
|
|
|
child: Stack(
|
|
|
|
children: [
|
|
|
|
Builder(
|
|
|
|
builder: (context) {
|
|
|
|
if (cache == null) {
|
|
|
|
return const Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
LinearProgressIndicator()
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (cache.error != null) {
|
|
|
|
return DefaultErrorWidget(
|
|
|
|
message: cache.error.toString(),
|
|
|
|
onRetry: () {
|
|
|
|
setState(() {
|
|
|
|
mClient.deleteUserMessageCache(widget.friend.id);
|
|
|
|
});
|
|
|
|
mClient.loadUserMessageCache(widget.friend.id);
|
|
|
|
},
|
2023-05-16 10:11:03 -04:00
|
|
|
);
|
|
|
|
}
|
2023-05-17 17:20:56 -04:00
|
|
|
if (cache.messages.isEmpty) {
|
|
|
|
return Center(
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
const Icon(Icons.message_outlined),
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 24),
|
|
|
|
child: Text(
|
|
|
|
"There are no messages here\nWhy not say hello?",
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: Theme
|
|
|
|
.of(context)
|
|
|
|
.textTheme
|
|
|
|
.titleMedium,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2023-05-20 15:09:22 -04:00
|
|
|
return Provider(
|
|
|
|
create: (BuildContext context) => AudioCacheClient(),
|
|
|
|
child: ListView.builder(
|
|
|
|
reverse: true,
|
|
|
|
itemCount: cache.messages.length,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
final entry = cache.messages[index];
|
|
|
|
if (index == cache.messages.length - 1) {
|
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.only(top: 12),
|
|
|
|
child: MessageBubble(message: entry,),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return MessageBubble(message: entry,);
|
|
|
|
},
|
|
|
|
),
|
2023-05-17 17:20:56 -04:00
|
|
|
);
|
2023-05-16 10:11:03 -04:00
|
|
|
},
|
2023-05-17 17:20:56 -04:00
|
|
|
),
|
|
|
|
],
|
2023-05-16 10:11:03 -04:00
|
|
|
),
|
|
|
|
),
|
2023-05-21 11:27:29 -04:00
|
|
|
MessageInputBar(
|
|
|
|
recipient: widget.friend,
|
|
|
|
disabled: cache == null || cache.error != null,
|
|
|
|
onMessageSent: () {
|
|
|
|
setState(() {});
|
|
|
|
},
|
2023-05-16 09:57:44 -04:00
|
|
|
),
|
2023-05-16 10:11:03 -04:00
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2023-05-06 13:24:28 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|