From 7648916f6a673e517307ac5d6e897aeeed0a94e1 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Tue, 29 Jun 2021 11:49:29 -0700 Subject: [PATCH] Don't attempt to load messages on group syncing (Fixes: #56) Also should help with #32 --- .../main/kotlin/im/cwtch/flwtch/FlwtchWorker.kt | 8 +++++--- .../main/kotlin/im/cwtch/flwtch/MainActivity.kt | 6 +++--- lib/main.dart | 8 ++++---- lib/widgets/messagelist.dart | 16 ++++++++++++---- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/android/app/src/main/kotlin/im/cwtch/flwtch/FlwtchWorker.kt b/android/app/src/main/kotlin/im/cwtch/flwtch/FlwtchWorker.kt index 08d60cc8..7cb33d71 100644 --- a/android/app/src/main/kotlin/im/cwtch/flwtch/FlwtchWorker.kt +++ b/android/app/src/main/kotlin/im/cwtch/flwtch/FlwtchWorker.kt @@ -60,10 +60,11 @@ class FlwtchWorker(context: Context, parameters: WorkerParameters) : val evt = MainActivity.AppbusEvent(Cwtch.getAppBusEvent()) if (evt.EventType == "NewMessageFromPeer" || evt.EventType == "NewMessageFromGroup") { val data = JSONObject(evt.Data) + val handle = if (evt.EventType == "NewMessageFromPeer") data.getString("RemotePeer") else data.getString("GroupID"); if (data["RemotePeer"] != data["ProfileOnion"]) { val channelId = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - createMessageNotificationChannel(data.getString("RemotePeer"), data.getString("RemotePeer")) + createMessageNotificationChannel(handle, handle) } else { // If earlier version channel ID is not used // https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context) @@ -74,11 +75,12 @@ class FlwtchWorker(context: Context, parameters: WorkerParameters) : val key = loader.getLookupKeyForAsset("assets/" + data.getString("Picture"))//"assets/profiles/001-centaur.png") val fh = applicationContext.assets.open(key) + val clickIntent = Intent(applicationContext, MainActivity::class.java).also { intent -> intent.action = Intent.ACTION_RUN intent.putExtra("EventType", "NotificationClicked") intent.putExtra("ProfileOnion", data.getString("ProfileOnion")) - intent.putExtra("RemotePeer", if (evt.EventType == "NewMessageFromPeer") data.getString("RemotePeer") else data.getString("GroupID")) + intent.putExtra("Handle", handle) } val newNotification = NotificationCompat.Builder(applicationContext, channelId) @@ -89,7 +91,7 @@ class FlwtchWorker(context: Context, parameters: WorkerParameters) : .setContentIntent(PendingIntent.getActivity(applicationContext, 1, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT)) .setAutoCancel(true) .build() - notificationManager.notify(getNotificationID(data.getString("ProfileOnion"), data.getString("RemotePeer")), newNotification) + notificationManager.notify(getNotificationID(data.getString("ProfileOnion"), handle), newNotification) } } diff --git a/android/app/src/main/kotlin/im/cwtch/flwtch/MainActivity.kt b/android/app/src/main/kotlin/im/cwtch/flwtch/MainActivity.kt index 9f2ff6e6..4025131f 100644 --- a/android/app/src/main/kotlin/im/cwtch/flwtch/MainActivity.kt +++ b/android/app/src/main/kotlin/im/cwtch/flwtch/MainActivity.kt @@ -52,13 +52,13 @@ class MainActivity: FlutterActivity() { if (notificationClickChannel == null || intent.extras == null) return if (intent.extras!!.getString("EventType") == "NotificationClicked") { - if (!intent.extras!!.containsKey("ProfileOnion") || !intent.extras!!.containsKey("RemotePeer")) { + if (!intent.extras!!.containsKey("ProfileOnion") || !intent.extras!!.containsKey("Handle")) { Log.i("onNewIntent", "got notification clicked intent with no onions") return } val profile = intent.extras!!.getString("ProfileOnion") - val handle = intent.extras!!.getString("RemotePeer") - val mappo = mapOf("ProfileOnion" to profile, "RemotePeer" to handle) + val handle = intent.extras!!.getString("Handle") + val mappo = mapOf("ProfileOnion" to profile, "Handle" to handle) val j = JSONObject(mappo) notificationClickChannel!!.invokeMethod("NotificationClicked", j.toString()) } else if (intent.extras!!.getString("EventType") == "ShutdownClicked") { diff --git a/lib/main.dart b/lib/main.dart index 9bf7c380..f74ddbdc 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -167,8 +167,8 @@ class FlwtchState extends State { Future _externalNotificationClicked(MethodCall call) async { var args = jsonDecode(call.arguments); var profile = profs.getProfile(args["ProfileOnion"])!; - var contact = profile.contactList.getContact(args["RemotePeer"])!; - contact.unreadMessages = 0; + var convo = profile.contactList.getContact(args["Handle"])!; + convo.unreadMessages = 0; // single pane mode pushes; double pane mode reads AppState.selectedProfile/Conversation var isLandscape = Provider.of(navKey.currentContext!, listen: false).isLandscape(navKey.currentContext!); @@ -183,7 +183,7 @@ class FlwtchState extends State { return MultiProvider( providers: [ ChangeNotifierProvider.value(value: profile), - ChangeNotifierProvider.value(value: contact), + ChangeNotifierProvider.value(value: convo), ], builder: (context, child) => MessageView(), ); @@ -192,7 +192,7 @@ class FlwtchState extends State { ); } else { //dual pane Provider.of(navKey.currentContext!, listen: false).selectedProfile = args["ProfileOnion"]; - Provider.of(navKey.currentContext!, listen: false).selectedConversation = args["RemotePeer"]; + Provider.of(navKey.currentContext!, listen: false).selectedConversation = args["Handle"]; } } diff --git a/lib/widgets/messagelist.dart b/lib/widgets/messagelist.dart index a6b2e265..67158294 100644 --- a/lib/widgets/messagelist.dart +++ b/lib/widgets/messagelist.dart @@ -16,10 +16,18 @@ class _MessageListState extends State { @override Widget build(BuildContext outerContext) { - bool showEphemeralWarning = (Provider.of(context).isGroup == false && Provider.of(context).savePeerHistory != "SaveHistory"); + + bool isP2P = !Provider.of(context).isGroup; + bool isGroupAndSyncing = Provider.of(context).isGroup == true && Provider.of(context).status == "Authenticated"; + bool isGroupAndSynced = Provider.of(context).isGroup && Provider.of(context).status == "Synced"; + bool isGroupAndNotAuthenticated= Provider.of(context).isGroup && Provider.of(context).status != "Authenticated"; + + bool showEphemeralWarning = (isP2P && Provider.of(context).savePeerHistory != "SaveHistory"); bool showOfflineWarning = Provider.of(context).isOnline() == false; bool showMessageWarning = showEphemeralWarning || showOfflineWarning; - bool showSyncing = Provider.of(context).isGroup == true && Provider.of(context).status != "Synced"; + bool showSyncing = isGroupAndSyncing; + // Only load historical messages when the conversation is with a p2p contact OR the conversation is a server and *not* syncing. + bool loadMessages = isP2P || (isGroupAndSynced || isGroupAndNotAuthenticated); return RepaintBoundary( child: Container( @@ -56,7 +64,7 @@ class _MessageListState extends State { image: AssetImage("assets/core/negative_heart_512px.png"), colorFilter: ColorFilter.mode(Provider.of(context).theme.hilightElementTextColor(), BlendMode.srcIn))), // Don't load messages for syncing server... - child: ListView.builder( + child: loadMessages ? ListView.builder( controller: ctrlr1, itemCount: Provider.of(outerContext).totalMessages, reverse: true, // NOTE: There seems to be a bug in flutter that corrects the mouse wheel scroll, but not the drag direction... @@ -78,7 +86,7 @@ class _MessageListState extends State { return RepaintBoundary(child: MessageRow(key: Provider.of(bcontext).getMessageKey(idx))); }); }, - )))) + ) : null ))) ]))); } }