From 40be9885c5f00e95a6b98eb33d179c9ccf34b6ee Mon Sep 17 00:00:00 2001 From: radixrat Date: Thu, 2 Feb 2023 13:53:57 -0800 Subject: [PATCH] add remote loading image setting --- damus/ContentView.swift | 2 +- damus/Views/ChatView.swift | 4 +-- damus/Views/ConfigView.swift | 38 ++++++++++++++++++++++-- damus/Views/DMChatView.swift | 2 +- damus/Views/EventView.swift | 2 +- damus/Views/ProfilePicView.swift | 18 ++++++++--- damus/Views/ProfilePictureSelector.swift | 2 +- 7 files changed, 56 insertions(+), 12 deletions(-) diff --git a/damus/ContentView.swift b/damus/ContentView.swift index 8681cb7e..470da9d0 100644 --- a/damus/ContentView.swift +++ b/damus/ContentView.swift @@ -252,7 +252,7 @@ struct ContentView: View { Button { isSideBarOpened.toggle() } label: { - ProfilePicView(pubkey: damus_state!.pubkey, size: 32, highlight: .none, profiles: damus_state!.profiles) + ProfilePicView(pubkey: damus_state!.pubkey, size: 32, highlight: .none, profiles: damus_state!.profiles, contacts: damus_state!.contacts) } } diff --git a/damus/Views/ChatView.swift b/damus/Views/ChatView.swift index 4654f350..43ed220d 100644 --- a/damus/Views/ChatView.swift +++ b/damus/Views/ChatView.swift @@ -75,7 +75,7 @@ struct ChatView: View { HStack { VStack { if is_active || just_started { - ProfilePicView(pubkey: event.pubkey, size: 32, highlight: is_active ? .main : .none, profiles: damus_state.profiles) + ProfilePicView(pubkey: event.pubkey, size: 32, highlight: is_active ? .main : .none, profiles: damus_state.profiles, contacts:damus_state.contacts) } Spacer() @@ -97,7 +97,7 @@ struct ChatView: View { if let ref_id = thread.replies.lookup(event.id) { if !is_reply_to_prev() { /* - ReplyQuoteView(keypair: damus_state.keypair, quoter: event, event_id: ref_id, profiles: damus_state.profiles, previews: damus_state.previews) + ReplyQuoteView(keypair: damus_state.keypair, quoter: event, event_id: ref_id, profiles: damus_state.profiles, previews: damus_state.previews, contacts: damus_state.contacts) .frame(maxHeight: expand_reply ? nil : 100) .environmentObject(thread) .onTapGesture { diff --git a/damus/Views/ConfigView.swift b/damus/Views/ConfigView.swift index 217469e4..0f69c8b4 100644 --- a/damus/Views/ConfigView.swift +++ b/damus/Views/ConfigView.swift @@ -19,8 +19,8 @@ struct ConfigView: View { @State var privkey_copied: Bool = false @State var pubkey_copied: Bool = false @State var delete_text: String = "" - @ObservedObject var settings: UserSettingsStore + @AppStorage("remote_image_policy") var remote_image_policy: String = "everyone" let generator = UIImpactFeedbackGenerator(style: .light) @@ -129,7 +129,30 @@ struct ConfigView: View { KingfisherManager.shared.cache.cleanExpiredDiskCache() } } - + + Section("Profile Image Loading Policy") { + Menu { + Button { + UserDefaults.standard.set("everyone", forKey:"remote_image_policy") + } label: { + Text("Everyone") + } + Button { + UserDefaults.standard.set("friends", forKey:"remote_image_policy") + } label: { + Text("Friends Only") + } + Button { + UserDefaults.standard.set("restricted", forKey:"remote_image_policy") + } label: { + Text("Restricted") + } + } label: { + Text("\(convertImageLoadPolicyTxt(policy:remote_image_policy))") + .frame(maxWidth: .infinity, alignment: .leading) + } + } + if state.is_privkey_user { Section(NSLocalizedString("Delete", comment: "Section title for deleting the user")) { Button(NSLocalizedString("Delete Account", comment: "Button to delete the user's account."), role: .destructive) { @@ -176,6 +199,17 @@ struct ConfigView: View { } } +func convertImageLoadPolicyTxt(policy: String) -> String { + switch policy { + case "restricted": + return "Restricted" + case "friends": + return "Friends Only" + default: + return "Everyone" + } +} + struct ConfigView_Previews: PreviewProvider { static var previews: some View { NavigationView { diff --git a/damus/Views/DMChatView.swift b/damus/Views/DMChatView.swift index e4858cd8..8a1f1ce1 100644 --- a/damus/Views/DMChatView.swift +++ b/damus/Views/DMChatView.swift @@ -42,7 +42,7 @@ struct DMChatView: View { let profile_page = ProfileView(damus_state: damus_state, profile: pmodel, followers: fmodel) return NavigationLink(destination: profile_page) { HStack { - ProfilePicView(pubkey: pubkey, size: 24, highlight: .none, profiles: damus_state.profiles) + ProfilePicView(pubkey: pubkey, size: 24, highlight: .none, profiles: damus_state.profiles, contacts:damus_state.contacts) ProfileName(pubkey: pubkey, profile: profile, damus: damus_state, show_friend_confirmed: true) } diff --git a/damus/Views/EventView.swift b/damus/Views/EventView.swift index ef4c61cc..a63e7974 100644 --- a/damus/Views/EventView.swift +++ b/damus/Views/EventView.swift @@ -104,7 +104,7 @@ struct EventView: View { let pv = ProfileView(damus_state: damus, profile: pmodel, followers: FollowersModel(damus_state: damus, target: pubkey)) NavigationLink(destination: pv) { - ProfilePicView(pubkey: pubkey, size: PFP_SIZE, highlight: .none, profiles: damus.profiles) + ProfilePicView(pubkey: pubkey, size: PFP_SIZE, highlight: .none, profiles: damus.profiles, contacts: damus.contacts) } Spacer() diff --git a/damus/Views/ProfilePicView.swift b/damus/Views/ProfilePicView.swift index 09742372..b4ac298b 100644 --- a/damus/Views/ProfilePicView.swift +++ b/damus/Views/ProfilePicView.swift @@ -98,19 +98,21 @@ struct ProfilePicView: View { let size: CGFloat let highlight: Highlight let profiles: Profiles + let contacts: Contacts @State var picture: String? - init (pubkey: String, size: CGFloat, highlight: Highlight, profiles: Profiles, picture: String? = nil) { + init (pubkey: String, size: CGFloat, highlight: Highlight, profiles: Profiles, contacts: Contacts, picture: String? = nil) { self.pubkey = pubkey self.profiles = profiles + self.contacts = contacts self.size = size self.highlight = highlight self._picture = State(initialValue: picture) } var body: some View { - InnerProfilePicView(url: get_profile_url(picture: picture, pubkey: pubkey, profiles: profiles), fallbackUrl: URL(string: robohash(pubkey)), pubkey: pubkey, size: size, highlight: highlight) + InnerProfilePicView(url: get_profile_url(picture: picture, pubkey: pubkey, profiles: profiles, contacts: contacts), fallbackUrl: URL(string: robohash(pubkey)), pubkey: pubkey, size: size, highlight: highlight) .onReceive(handle_notify(.profile_updated)) { notif in let updated = notif.object as! ProfileUpdate @@ -126,7 +128,14 @@ struct ProfilePicView: View { } func get_profile_url(picture: String?, pubkey: String, profiles: Profiles) -> URL { - let pic = picture ?? profiles.lookup(id: pubkey)?.picture ?? robohash(pubkey) + var pic: String + let remote_image_policy = UserDefaults.standard.string(forKey: "remote_image_policy") + if remote_image_policy == "restricted" || (remote_image_policy == "friends" && !contacts.is_friend(pubkey)) { + pic = robohash(pubkey) + } else { + pic = picture ?? profiles.lookup(id: pubkey)?.picture ?? robohash(pubkey) + } + if let url = URL(string: pic) { return url } @@ -150,7 +159,8 @@ struct ProfilePicView_Previews: PreviewProvider { pubkey: pubkey, size: 100, highlight: .none, - profiles: make_preview_profiles(pubkey)) + profiles: make_preview_profiles(pubkey), + contacts: Contacts()) } } diff --git a/damus/Views/ProfilePictureSelector.swift b/damus/Views/ProfilePictureSelector.swift index 6eeac672..4abba4a3 100644 --- a/damus/Views/ProfilePictureSelector.swift +++ b/damus/Views/ProfilePictureSelector.swift @@ -13,7 +13,7 @@ struct ProfilePictureSelector: View { var body: some View { let highlight: Highlight = .custom(Color.white, 2.0) ZStack { - ProfilePicView(pubkey: pubkey, size: 80.0, highlight: highlight, profiles: Profiles()) + ProfilePicView(pubkey: pubkey, size: 80.0, highlight: highlight, profiles: Profiles(), contacts: Contacts()) } } }