From b280765631abc657a9e8a6df06db3499fae3e4cc Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Mon, 7 Feb 2022 14:26:14 -0800 Subject: [PATCH] Fallback to Default Profile Images when Image Previews are Disabled --- lib/cwtch/cwtchNotifier.dart | 4 +++- lib/models/contact.dart | 10 ++++++++++ lib/models/profile.dart | 12 ++++++++++++ lib/models/profilelist.dart | 5 +++-- lib/views/addeditprofileview.dart | 7 +++++-- lib/views/contactsview.dart | 4 +++- lib/views/messageview.dart | 4 +++- lib/widgets/contactrow.dart | 2 +- lib/widgets/messagerow.dart | 8 +++++++- lib/widgets/profilerow.dart | 2 +- 10 files changed, 48 insertions(+), 10 deletions(-) diff --git a/lib/cwtch/cwtchNotifier.dart b/lib/cwtch/cwtchNotifier.dart index cd0d564f..055b781d 100644 --- a/lib/cwtch/cwtchNotifier.dart +++ b/lib/cwtch/cwtchNotifier.dart @@ -55,7 +55,7 @@ class CwtchNotifier { } EnvironmentConfig.debugLog("NewPeer $data"); // if tag != v1-defaultPassword then it is either encrypted OR it is an unencrypted account created during pre-beta... - profileCN.add(data["Identity"], data["name"], data["picture"], data["ContactsJson"], data["ServerList"], data["Online"] == "true", data["tag"] != "v1-defaultPassword"); + profileCN.add(data["Identity"], data["name"], data["picture"], data["defaultPicture"], data["ContactsJson"], data["ServerList"], data["Online"] == "true", data["tag"] != "v1-defaultPassword"); break; case "ContactCreated": EnvironmentConfig.debugLog("ContactCreated $data"); @@ -67,6 +67,7 @@ class CwtchNotifier { nickname: data["nick"], status: data["status"], imagePath: data["picture"], + defaultImagePath: data["defaultPicture"], blocked: data["blocked"] == "true", accepted: data["accepted"] == "true", savePeerHistory: data["saveConversationHistory"] == null ? "DeleteHistoryConfirmed" : data["saveConversationHistory"], @@ -107,6 +108,7 @@ class CwtchNotifier { blocked: false, // we created accepted: true, // we created imagePath: data["picture"], + defaultImagePath: data["picture"], nickname: data["GroupName"], status: status, server: data["GroupServer"], diff --git a/lib/models/contact.dart b/lib/models/contact.dart index 7f840747..38cd31cd 100644 --- a/lib/models/contact.dart +++ b/lib/models/contact.dart @@ -14,6 +14,7 @@ class ContactInfoState extends ChangeNotifier { late bool _blocked; late String _status; late String _imagePath; + late String _defaultImagePath; late String _savePeerHistory; late int _unreadMessages = 0; late int _totalMessages = 0; @@ -37,6 +38,7 @@ class ContactInfoState extends ChangeNotifier { blocked = false, status = "", imagePath = "", + defaultImagePath = "", savePeerHistory = "DeleteHistoryConfirmed", numMessages = 0, numUnread = 0, @@ -49,6 +51,7 @@ class ContactInfoState extends ChangeNotifier { this._blocked = blocked; this._status = status; this._imagePath = imagePath; + this._defaultImagePath = defaultImagePath; this._totalMessages = numMessages; this._unreadMessages = numUnread; this._savePeerHistory = savePeerHistory; @@ -166,6 +169,13 @@ class ContactInfoState extends ChangeNotifier { notifyListeners(); } + String get defaultImagePath => this._defaultImagePath; + + set defaultImagePath(String newVal) { + this._defaultImagePath = newVal; + notifyListeners(); + } + DateTime get lastMessageTime => this._lastMessageTime; set lastMessageTime(DateTime newVal) { diff --git a/lib/models/profile.dart b/lib/models/profile.dart index 75b37f0b..070a25cc 100644 --- a/lib/models/profile.dart +++ b/lib/models/profile.dart @@ -14,6 +14,7 @@ class ProfileInfoState extends ChangeNotifier { final String onion; String _nickname = ""; String _imagePath = ""; + String _defaultImagePath = ""; int _unreadMessages = 0; bool _online = false; Map _downloads = Map(); @@ -27,14 +28,17 @@ class ProfileInfoState extends ChangeNotifier { required this.onion, nickname = "", imagePath = "", + defaultImagePath = "", unreadMessages = 0, contactsJson = "", serversJson = "", online = false, encrypted = true, + String, }) { this._nickname = nickname; this._imagePath = imagePath; + this._defaultImagePath = defaultImagePath; this._unreadMessages = unreadMessages; this._online = online; this._encrypted = encrypted; @@ -50,6 +54,7 @@ class ProfileInfoState extends ChangeNotifier { nickname: contact["name"], status: contact["status"], imagePath: contact["picture"], + defaultImagePath: contact["isGroup"] ? contact["picture"] : contact["defaultPicture"], accepted: contact["accepted"], blocked: contact["blocked"], savePeerHistory: contact["saveConversationHistory"], @@ -115,6 +120,12 @@ class ProfileInfoState extends ChangeNotifier { notifyListeners(); } + String get defaultImagePath => this._defaultImagePath; + set defaultImagePath(String newVal) { + this._defaultImagePath = newVal; + notifyListeners(); + } + int get unreadMessages => this._unreadMessages; set unreadMessages(int newVal) { this._unreadMessages = newVal; @@ -161,6 +172,7 @@ class ProfileInfoState extends ChangeNotifier { contact["identifier"], contact["onion"], nickname: contact["name"], + defaultImagePath: contact["defaultPicture"], status: contact["status"], imagePath: contact["picture"], accepted: contact["accepted"], diff --git a/lib/models/profilelist.dart b/lib/models/profilelist.dart index e3766e01..53a12384 100644 --- a/lib/models/profilelist.dart +++ b/lib/models/profilelist.dart @@ -6,10 +6,11 @@ class ProfileListState extends ChangeNotifier { List _profiles = []; int get num => _profiles.length; - void add(String onion, String name, String picture, String contactsJson, String serverJson, bool online, bool encrypted) { + void add(String onion, String name, String picture, String defaultPicture, String contactsJson, String serverJson, bool online, bool encrypted) { var idx = _profiles.indexWhere((element) => element.onion == onion); if (idx == -1) { - _profiles.add(ProfileInfoState(onion: onion, nickname: name, imagePath: picture, contactsJson: contactsJson, serversJson: serverJson, online: online, encrypted: encrypted)); + _profiles.add(ProfileInfoState( + onion: onion, nickname: name, imagePath: picture, defaultImagePath: defaultPicture, contactsJson: contactsJson, serversJson: serverJson, online: online, encrypted: encrypted)); } else { _profiles[idx].updateFrom(onion, name, picture, contactsJson, serverJson, online); } diff --git a/lib/views/addeditprofileview.dart b/lib/views/addeditprofileview.dart index 3a054f4f..ef398ffd 100644 --- a/lib/views/addeditprofileview.dart +++ b/lib/views/addeditprofileview.dart @@ -112,9 +112,12 @@ class _AddEditProfileViewState extends State { }, () {}); }, child: ProfileImage( - imagePath: Provider.of(context).imagePath, + imagePath: Provider.of(context).isExperimentEnabled(ImagePreviewsExperiment) + ? Provider.of(context).imagePath + : Provider.of(context).defaultImagePath, diameter: 120, - tooltip: Provider.of(context).isExperimentEnabled(ImagePreviewsExperiment) ? AppLocalizations.of(context)!.tooltipSelectACustomProfileImage : "", + tooltip: + Provider.of(context).isExperimentEnabled(ImagePreviewsExperiment) ? AppLocalizations.of(context)!.tooltipSelectACustomProfileImage : "", maskOut: false, border: theme.theme.portraitOnlineBorderColor, badgeTextColor: Colors.red, diff --git a/lib/views/contactsview.dart b/lib/views/contactsview.dart index 317a63b6..ad3a5c8f 100644 --- a/lib/views/contactsview.dart +++ b/lib/views/contactsview.dart @@ -103,7 +103,9 @@ class _ContactsViewState extends State { title: RepaintBoundary( child: Row(children: [ ProfileImage( - imagePath: Provider.of(context).imagePath, + imagePath: Provider.of(context).isExperimentEnabled(ImagePreviewsExperiment) + ? Provider.of(context).imagePath + : Provider.of(context).defaultImagePath, diameter: 42, border: Provider.of(context).current().portraitOnlineBorderColor, badgeTextColor: Colors.red, diff --git a/lib/views/messageview.dart b/lib/views/messageview.dart index d5d9a01c..5ef99e48 100644 --- a/lib/views/messageview.dart +++ b/lib/views/messageview.dart @@ -139,7 +139,9 @@ class _MessageViewState extends State { leading: Provider.of(context).uiColumns(appState.isLandscape(context)).length > 1 ? Container() : null, title: Row(children: [ ProfileImage( - imagePath: Provider.of(context).imagePath, + imagePath: Provider.of(context).isExperimentEnabled(ImagePreviewsExperiment) + ? Provider.of(context).imagePath + : Provider.of(context).defaultImagePath, diameter: 42, border: Provider.of(context).current().portraitOnlineBorderColor, badgeTextColor: Colors.red, diff --git a/lib/widgets/contactrow.dart b/lib/widgets/contactrow.dart index cdad4f77..6cbc6ef7 100644 --- a/lib/widgets/contactrow.dart +++ b/lib/widgets/contactrow.dart @@ -50,7 +50,7 @@ class _ContactRowState extends State { badgeColor: Provider.of(context).theme.portraitContactBadgeColor, badgeTextColor: Provider.of(context).theme.portraitContactBadgeTextColor, diameter: 64.0, - imagePath: contact.imagePath, + imagePath: Provider.of(context).isExperimentEnabled(ImagePreviewsExperiment) ? contact.imagePath : contact.defaultImagePath, maskOut: !contact.isOnline(), border: contact.isOnline() ? Provider.of(context).theme.portraitOnlineBorderColor diff --git a/lib/widgets/messagerow.dart b/lib/widgets/messagerow.dart index feffa1d2..0deb647b 100644 --- a/lib/widgets/messagerow.dart +++ b/lib/widgets/messagerow.dart @@ -153,6 +153,12 @@ class MessageRowState extends State with SingleTickerProviderStateMi } else { var contact = Provider.of(context); ContactInfoState? sender = Provider.of(context).contactList.findContact(Provider.of(context).senderHandle); + + String imagePath = Provider.of(context).senderImage!; + if (sender != null) { + imagePath = + Provider.of(context).isExperimentEnabled(ImagePreviewsExperiment) ? sender.imagePath : sender.defaultImagePath; + } Widget wdgPortrait = GestureDetector( onTap: !isGroup ? null @@ -164,7 +170,7 @@ class MessageRowState extends State with SingleTickerProviderStateMi child: ProfileImage( diameter: 48.0, // default to the contact image...otherwise use a derived sender image... - imagePath: sender?.imagePath ?? Provider.of(context).senderImage!, + imagePath: imagePath, border: contact.status == "Authenticated" ? Provider.of(context).theme.portraitOnlineBorderColor : Provider.of(context).theme.portraitOfflineBorderColor, badgeTextColor: Colors.red, badgeColor: Colors.red, diff --git a/lib/widgets/profilerow.dart b/lib/widgets/profilerow.dart index 6808009d..9c2a576b 100644 --- a/lib/widgets/profilerow.dart +++ b/lib/widgets/profilerow.dart @@ -38,7 +38,7 @@ class _ProfileRowState extends State { badgeColor: Provider.of(context).theme.portraitProfileBadgeColor, badgeTextColor: Provider.of(context).theme.portraitProfileBadgeTextColor, diameter: 64.0, - imagePath: profile.imagePath, + imagePath: Provider.of(context).isExperimentEnabled(ImagePreviewsExperiment) ? profile.imagePath : profile.defaultImagePath, border: profile.isOnline ? Provider.of(context).theme.portraitOnlineBorderColor : Provider.of(context).theme.portraitOfflineBorderColor)), Expanded( child: Column(