Line data Source code
1 : import 'package:cwtch/models/appstate.dart';
2 : import 'package:cwtch/models/contact.dart';
3 : import 'package:cwtch/models/message.dart';
4 : import 'package:cwtch/models/messagecache.dart';
5 : import 'package:cwtch/models/profile.dart';
6 : import 'package:cwtch/widgets/messageloadingbubble.dart';
7 : import 'package:flutter/material.dart';
8 : import 'package:provider/provider.dart';
9 : import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
10 : import 'package:flutter_gen/gen_l10n/app_localizations.dart';
11 : import '../main.dart';
12 : import '../settings.dart';
13 :
14 : class MessageList extends StatefulWidget {
15 : ItemPositionsListener scrollListener;
16 0 : MessageList(this.scrollListener);
17 :
18 0 : @override
19 0 : _MessageListState createState() => _MessageListState();
20 : }
21 :
22 : class _MessageListState extends State<MessageList> {
23 0 : @override
24 : Widget build(BuildContext outerContext) {
25 : // On Android we can have unsynced messages at the front of the index from when the UI was asleep, if there are some, kick off sync of those first
26 0 : if (Provider.of<ContactInfoState>(context).messageCache.indexUnsynced != 0) {
27 0 : var conversationId = Provider.of<AppState>(outerContext, listen: false).selectedConversation!;
28 0 : MessageCache? cache = Provider.of<ProfileInfoState>(outerContext, listen: false).contactList.getContact(conversationId)?.messageCache;
29 0 : ByIndex(0).loadUnsynced(Provider.of<FlwtchState>(context, listen: false).cwtch, Provider.of<AppState>(outerContext, listen: false).selectedProfile!, conversationId, cache!);
30 : }
31 0 : if (Provider.of<ContactInfoState>(outerContext, listen: false).everFetched == false) {
32 0 : messageHandler(outerContext, Provider.of<ProfileInfoState>(outerContext, listen: false).onion, Provider.of<ContactInfoState>(outerContext, listen: false).identifier,
33 0 : ByIndex(Provider.of<ContactInfoState>(outerContext, listen: false).uiLoadedMessages));
34 0 : Provider.of<ContactInfoState>(outerContext, listen: false).everFetched = true;
35 : }
36 0 : var initi = Provider.of<AppState>(outerContext, listen: false).initialScrollIndex;
37 0 : bool isP2P = !Provider.of<ContactInfoState>(context).isGroup;
38 0 : bool isGroupAndSyncing = Provider.of<ContactInfoState>(context).isGroup == true && Provider.of<ContactInfoState>(context).status == "Authenticated";
39 :
40 0 : bool preserveHistoryByDefault = Provider.of<Settings>(context, listen: false).preserveHistoryByDefault;
41 0 : bool showEphemeralWarning = (isP2P && (!preserveHistoryByDefault && Provider.of<ContactInfoState>(context).savePeerHistory != "SaveHistory"));
42 0 : bool showOfflineWarning = Provider.of<ContactInfoState>(context).isOnline() == false;
43 : bool showSyncing = isGroupAndSyncing;
44 : bool showMessageWarning = showEphemeralWarning || showOfflineWarning || showSyncing;
45 : // We used to only load historical messages when the conversation is with a p2p contact OR the conversation is a server and *not* syncing.
46 : // With the message cache in place this is no longer necessary
47 : bool loadMessages = true;
48 :
49 : // if it's been more than 2 minutes since we last clicked the button let users click the button again
50 : // OR if users have never clicked the button AND they appear offline, then they can click the button
51 : // NOTE: all these listeners are false...this is not ideal, but if they were true we would end up rebuilding the message view every tick (which would kill performance)
52 : // any significant changes in state e.g. peer offline or button clicks will trigger a rebuild anyway
53 0 : bool canReconnect = DateTime.now().difference(Provider.of<ContactInfoState>(context, listen: false).lastRetryTime).abs() > Duration(seconds: 30) ||
54 0 : (Provider.of<ProfileInfoState>(context, listen: false).appearOffline &&
55 0 : (Provider.of<ContactInfoState>(context, listen: false).lastRetryTime == Provider.of<ContactInfoState>(context, listen: false).loaded));
56 :
57 0 : var reconnectButton = Padding(
58 0 : padding: EdgeInsets.all(2),
59 : child: canReconnect
60 0 : ? Tooltip(
61 0 : message: AppLocalizations.of(context)!.retryConnectionTooltip,
62 0 : child: ElevatedButton(
63 0 : style: ButtonStyle(padding: MaterialStateProperty.all(EdgeInsets.all(20))),
64 0 : child: Text(AppLocalizations.of(context)!.retryConnection),
65 0 : onPressed: () {
66 0 : if (Provider.of<ContactInfoState>(context, listen: false).isGroup) {
67 0 : Provider.of<FlwtchState>(context, listen: false)
68 0 : .cwtch
69 0 : .AttemptReconnectionServer(Provider.of<ProfileInfoState>(context, listen: false).onion, Provider.of<ContactInfoState>(context, listen: false).server!);
70 : } else {
71 0 : Provider.of<FlwtchState>(context, listen: false)
72 0 : .cwtch
73 0 : .AttemptReconnection(Provider.of<ProfileInfoState>(context, listen: false).onion, Provider.of<ContactInfoState>(context, listen: false).onion);
74 : }
75 0 : Provider.of<ContactInfoState>(context, listen: false).lastRetryTime = DateTime.now();
76 0 : Provider.of<ContactInfoState>(context, listen: false).contactEvents.add(ContactEvent("Actively Retried Connection"));
77 0 : setState(() {
78 : // force update of this view...otherwise the button won't be removed fast enough...
79 : });
80 : },
81 : ))
82 0 : : CircularProgressIndicator(color: Provider.of<Settings>(context).theme.hilightElementColor));
83 :
84 0 : return RepaintBoundary(
85 0 : child: Container(
86 0 : color: Provider.of<Settings>(context).theme.backgroundMainColor,
87 0 : child: Column(children: [
88 0 : Visibility(
89 : visible: showMessageWarning,
90 0 : child: Container(
91 0 : padding: EdgeInsets.all(5.0),
92 0 : color: Provider.of<Settings>(context).theme.backgroundHilightElementColor,
93 0 : child: DefaultTextStyle(
94 0 : style: TextStyle(color: Provider.of<Settings>(context).theme.hilightElementColor),
95 : child: showSyncing
96 0 : ? Text(AppLocalizations.of(context)!.serverNotSynced, textAlign: TextAlign.center)
97 : : showOfflineWarning
98 0 : ? Column(crossAxisAlignment: CrossAxisAlignment.center, children: [
99 0 : Text(
100 0 : Provider.of<ContactInfoState>(context).isGroup
101 0 : ? AppLocalizations.of(context)!.serverConnectivityDisconnected
102 0 : : AppLocalizations.of(context)!.peerOfflineMessage,
103 : textAlign: TextAlign.center),
104 : reconnectButton
105 : ])
106 : // Only show the ephemeral status for peer conversations, not for groups...
107 : : (showEphemeralWarning
108 0 : ? Text(AppLocalizations.of(context)!.chatHistoryDefault, textAlign: TextAlign.center)
109 : :
110 : // We are not allowed to put null here, so put an empty text widget
111 0 : Text("")),
112 : ))),
113 0 : Expanded(
114 0 : child: Container(
115 : // Only show broken heart is the contact is offline...
116 0 : decoration: BoxDecoration(
117 0 : image: Provider.of<ContactInfoState>(outerContext).isOnline()
118 0 : ? (Provider.of<Settings>(context).themeImages && Provider.of<Settings>(context).theme.chatImage != null)
119 0 : ? DecorationImage(
120 : repeat: ImageRepeat.repeat,
121 0 : image: Provider.of<Settings>(context, listen: false).theme.loadImage(Provider.of<Settings>(context, listen: false).theme.chatImage, context: context),
122 0 : colorFilter: ColorFilter.mode(Provider.of<Settings>(context).theme.chatImageColor, BlendMode.srcIn))
123 : : null
124 0 : : DecorationImage(
125 : fit: BoxFit.scaleDown,
126 : alignment: Alignment.center,
127 0 : image: AssetImage("assets/core/negative_heart_512px.png"),
128 0 : colorFilter: ColorFilter.mode(Provider.of<Settings>(context).theme.hilightElementColor.withOpacity(0.15), BlendMode.srcIn))),
129 : // Don't load messages for syncing server...
130 0 : child: Padding(
131 0 : padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 20.0),
132 : child: loadMessages
133 0 : ? ScrollablePositionedList.builder(
134 0 : itemPositionsListener: widget.scrollListener,
135 0 : itemScrollController: Provider.of<ContactInfoState>(outerContext).messageScrollController,
136 0 : initialScrollIndex: initi > 4 ? initi - 4 : 0,
137 0 : itemCount: Provider.of<ContactInfoState>(outerContext).uiLoadedMessages + 1,
138 : reverse: true, // NOTE: There seems to be a bug in flutter that corrects the mouse wheel scroll, but not the drag direction...
139 : shrinkWrap: true,
140 0 : itemBuilder: (itemBuilderContext, index) {
141 0 : var profileOnion = Provider.of<ProfileInfoState>(itemBuilderContext, listen: false).onion;
142 0 : var contactHandle = Provider.of<ContactInfoState>(itemBuilderContext, listen: false).identifier;
143 : var messageIndex = index;
144 0 : var loadedMessages = Provider.of<ContactInfoState>(itemBuilderContext, listen: false).uiLoadedMessages;
145 0 : var endOfMessages = Provider.of<ContactInfoState>(itemBuilderContext, listen: false).totalMessages + 1;
146 0 : if (index == endOfMessages - 1) {
147 0 : return ListTile(
148 0 : title: Text(AppLocalizations.of(itemBuilderContext)!.messageHistoryEndOfHistory, textAlign: TextAlign.center),
149 : dense: true,
150 : );
151 0 : } else if (index >= loadedMessages) {
152 0 : return ElevatedButton(
153 0 : child: Text(AppLocalizations.of(itemBuilderContext)!.messageHistoryLoadOlderMessages),
154 0 : key: Key("loadAdditionalMessages"),
155 0 : onPressed: () {
156 0 : messageHandler(itemBuilderContext, profileOnion, contactHandle, ByIndex(messageIndex));
157 : },
158 : );
159 : }
160 0 : return FutureBuilder(
161 0 : future: messageHandler(itemBuilderContext, profileOnion, contactHandle, ByIndex(messageIndex)),
162 0 : builder: (fbcontext, snapshot) {
163 0 : if (snapshot.hasData) {
164 0 : var message = snapshot.data as Message;
165 : // here we create an index key for the contact and assign it to the row. Indexes are unique so we can
166 : // reliably use this without running into duplicate keys...it isn't ideal as it means keys need to be re-built
167 : // when new messages are added...however it is better than the alternative of not having widget keys at all.
168 0 : var key = Provider.of<ContactInfoState>(itemBuilderContext, listen: false).getMessageKey(contactHandle, messageIndex);
169 0 : return message.getWidget(fbcontext, key, messageIndex);
170 : } else {
171 0 : return MessageLoadingBubble();
172 : }
173 : },
174 : );
175 : },
176 : )
177 : : null)))
178 : ])));
179 : }
180 : }
|