diff --git a/lib/cwtch/cwtch.dart b/lib/cwtch/cwtch.dart index cf42d0e0..d04fd13b 100644 --- a/lib/cwtch/cwtch.dart +++ b/lib/cwtch/cwtch.dart @@ -35,6 +35,10 @@ abstract class Cwtch { // ignore: non_constant_identifier_names Future GetMessage(String profile, int handle, int index); + + // ignore: non_constant_identifier_names + Future GetMessageByID(String profile, int handle, int index); + // ignore: non_constant_identifier_names Future GetMessageByContentHash(String profile, int handle, String contentHash); diff --git a/lib/cwtch/ffi.dart b/lib/cwtch/ffi.dart index 78be3e70..c59928e5 100644 --- a/lib/cwtch/ffi.dart +++ b/lib/cwtch/ffi.dart @@ -694,4 +694,17 @@ class CwtchFfi implements Cwtch { final Free = free.asFunction(); Free(ptr); } + + @override + Future GetMessageByID(String profile, int handle, int index) async { + var getMessageC = library.lookup>("c_GetMessageByID"); + // ignore: non_constant_identifier_names + final GetMessage = getMessageC.asFunction(); + final utf8profile = profile.toNativeUtf8(); + Pointer jsonMessageBytes = GetMessage(utf8profile, utf8profile.length, handle, index); + String jsonMessage = jsonMessageBytes.toDartString(); + _UnsafeFreePointerAnyUseOfThisFunctionMustBeDoubleApproved(jsonMessageBytes); + malloc.free(utf8profile); + return jsonMessage; + } } diff --git a/lib/cwtch/gomobile.dart b/lib/cwtch/gomobile.dart index f8d62f3a..fa39a0be 100644 --- a/lib/cwtch/gomobile.dart +++ b/lib/cwtch/gomobile.dart @@ -88,10 +88,14 @@ class CwtchGomobile implements Cwtch { // ignore: non_constant_identifier_names Future GetMessage(String profile, int handle, int index) { - print("gomobile.dart GetMessage " + index.toString()); return cwtchPlatform.invokeMethod("GetMessage", {"profile": profile, "contact": handle, "index": index}); } + // ignore: non_constant_identifier_names + Future GetMessageByID(String profile, int handle, int index) { + return cwtchPlatform.invokeMethod("GetMessageByID", {"profile": profile, "contact": handle, "index": index}); + } + @override // ignore: non_constant_identifier_names void SendProfileEvent(String onion, String jsonEvent) { diff --git a/lib/models/message.dart b/lib/models/message.dart index 708cd8af..d6bac00f 100644 --- a/lib/models/message.dart +++ b/lib/models/message.dart @@ -1,4 +1,5 @@ import 'dart:convert'; +import 'package:cwtch/config.dart'; import 'package:flutter/widgets.dart'; import 'package:provider/provider.dart'; @@ -31,9 +32,16 @@ abstract class Message { Widget getPreviewWidget(BuildContext context); } -Future messageHandler(BuildContext context, String profileOnion, int conversationIdentifier, int index) { +Future messageHandler(BuildContext context, String profileOnion, int conversationIdentifier, int index, {bool byID = false}) { try { - var rawMessageEnvelopeFuture = Provider.of(context, listen: false).cwtch.GetMessage(profileOnion, conversationIdentifier, index); + Future rawMessageEnvelopeFuture; + + if (byID) { + rawMessageEnvelopeFuture = Provider.of(context, listen: false).cwtch.GetMessageByID(profileOnion, conversationIdentifier, index); + } else { + rawMessageEnvelopeFuture = Provider.of(context, listen: false).cwtch.GetMessage(profileOnion, conversationIdentifier, index); + } + return rawMessageEnvelopeFuture.then((dynamic rawMessageEnvelope) { var metadata = MessageMetadata(profileOnion, conversationIdentifier, index, -1, DateTime.now(), "", "", null, 0, false, true); try { @@ -50,7 +58,7 @@ Future messageHandler(BuildContext context, String profileOnion, int co if (messageWrapper['Message'] == null || messageWrapper['Message'] == '' || messageWrapper['Message'] == '{}') { return Future.delayed(Duration(seconds: 2), () { print("Tail recursive call to messageHandler called. This should be a rare event. If you see multiples of this log over a short period of time please log it as a bug."); - return messageHandler(context, profileOnion, conversationIdentifier, index).then((value) => value); + return messageHandler(context, profileOnion, conversationIdentifier, index, byID: byID).then((value) => value); }); } @@ -84,7 +92,7 @@ Future messageHandler(BuildContext context, String profileOnion, int co return MalformedMessage(metadata); } } catch (e) { - print("an error! " + e.toString()); + EnvironmentConfig.debugLog("an error! " + e.toString()); return MalformedMessage(metadata); } }); diff --git a/lib/views/messageview.dart b/lib/views/messageview.dart index c2170a20..8c34116b 100644 --- a/lib/views/messageview.dart +++ b/lib/views/messageview.dart @@ -168,7 +168,7 @@ class _MessageViewState extends State { if (Provider.of(context, listen: false).selectedConversation != null && Provider.of(context, listen: false).selectedIndex != null) { Provider.of(context, listen: false) .cwtch - .GetMessage(Provider.of(context, listen: false).selectedProfile!, Provider.of(context, listen: false).selectedConversation!, + .GetMessageByID(Provider.of(context, listen: false).selectedProfile!, Provider.of(context, listen: false).selectedConversation!, Provider.of(context, listen: false).selectedIndex!) .then((data) { try { @@ -268,7 +268,8 @@ class _MessageViewState extends State { var children; if (Provider.of(context).selectedConversation != null && Provider.of(context).selectedIndex != null) { var quoted = FutureBuilder( - future: messageHandler(context, Provider.of(context).selectedProfile!, Provider.of(context).selectedConversation!, Provider.of(context).selectedIndex!), + future: + messageHandler(context, Provider.of(context).selectedProfile!, Provider.of(context).selectedConversation!, Provider.of(context).selectedIndex!, byID: true), builder: (context, snapshot) { if (snapshot.hasData) { var message = snapshot.data! as Message; diff --git a/lib/widgets/messagerow.dart b/lib/widgets/messagerow.dart index 6c718a26..feaeae15 100644 --- a/lib/widgets/messagerow.dart +++ b/lib/widgets/messagerow.dart @@ -77,7 +77,7 @@ class MessageRowState extends State with SingleTickerProviderStateMi child: IconButton( tooltip: AppLocalizations.of(context)!.tooltipReplyToThisMessage, onPressed: () { - Provider.of(context, listen: false).selectedIndex = Provider.of(context, listen: false).messageIndex; + Provider.of(context, listen: false).selectedIndex = Provider.of(context, listen: false).messageID; }, icon: Icon(Icons.reply, color: Provider.of(context).theme.dropShadowColor()))); Widget wdgSpacer = Flexible(child: SizedBox(width: 60, height: 10)); @@ -185,7 +185,7 @@ class MessageRowState extends State with SingleTickerProviderStateMi }, onPanEnd: (details) { _runAnimation(details.velocity.pixelsPerSecond, size); - Provider.of(context, listen: false).selectedIndex = Provider.of(context, listen: false).messageIndex; + Provider.of(context, listen: false).selectedIndex = Provider.of(context, listen: false).messageID; }, child: Padding( padding: EdgeInsets.all(2), @@ -198,8 +198,8 @@ class MessageRowState extends State with SingleTickerProviderStateMi children: widgetRow, ))))); var mark = Provider.of(context).newMarker; - if (mark > 0 && mark == Provider.of(context).totalMessages - Provider.of(context).messageIndex) { - return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [Align(alignment: Alignment.center, child: _bubbleNew()), mr]); + if (mark > 0 && mark == Provider.of(context).messageIndex + 1) { + return Column(crossAxisAlignment: fromMe ? CrossAxisAlignment.end : CrossAxisAlignment.start, children: [Align(alignment: Alignment.center, child: _bubbleNew()), mr]); } else { return mr; }