damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 79f422397704d73140f7d375e0bcb715709a0156
parent db763d07fea0d2d75f0a4d1abc2f6c6a57762a3e
Author: William Casarin <jb55@jb55.com>
Date:   Fri, 23 Dec 2022 13:13:30 -0800

Profile Editor

Changelog-Added: Added profile edit view
Changelog-Changed: Don't fetch followers right away

Diffstat:
Mdamus/Views/EditMetadataView.swift | 55++++++++++++++++++++++++++-----------------------------
Mdamus/Views/FollowButtonView.swift | 18++++--------------
Mdamus/Views/FollowingView.swift | 8+++++++-
Mdamus/Views/ProfilePicView.swift | 42+++++++++++++++++++++++++++++++-----------
Mdamus/Views/ProfileView.swift | 60++++++++++++++++++++++++++++++++++++++++++++++--------------
5 files changed, 114 insertions(+), 69 deletions(-)

diff --git a/damus/Views/EditMetadataView.swift b/damus/Views/EditMetadataView.swift @@ -58,21 +58,22 @@ struct EditMetadataView: View { @State var picture: String @State var nip05: String @State var name: String - @State var lud06: String - @State var lud16: String - @State private var showAlert = false + @State var ln: String + @State var website: String + + @Environment(\.dismiss) var dismiss init (damus_state: DamusState) { self.damus_state = damus_state let data = damus_state.profiles.lookup(id: damus_state.pubkey) - name = data?.name ?? "" - display_name = data?.display_name ?? "" - about = data?.about ?? "" - picture = data?.picture ?? "" - nip05 = data?.nip05 ?? "" - lud06 = data?.lud06 ?? "" - lud16 = data?.lud16 ?? "" + _name = State(initialValue: data?.name ?? "") + _display_name = State(initialValue: data?.display_name ?? "") + _about = State(initialValue: data?.about ?? "") + _website = State(initialValue: data?.website ?? "") + _picture = State(initialValue: data?.picture ?? "") + _nip05 = State(initialValue: data?.nip05 ?? "") + _ln = State(initialValue: data?.lud16 ?? data?.lud06 ?? "") } func save() { @@ -80,11 +81,11 @@ struct EditMetadataView: View { display_name: display_name, name: name, about: about, - website: nil, + website: website, nip05: nip05.isEmpty ? nil : nip05, picture: picture.isEmpty ? nil : picture, - lud06: lud06.isEmpty ? nil : lud06, - lud16: lud16.isEmpty ? nil : lud16 + lud06: ln.contains("@") ? ln : nil, + lud16: ln.contains("@") ? nil : ln ); let m_metadata_ev = make_metadata_event(keypair: damus_state.keypair, metadata: metadata) @@ -98,10 +99,9 @@ struct EditMetadataView: View { VStack(alignment: .leading) { HStack { Spacer() - ProfilePicView(pubkey: damus_state.pubkey, size: PPM_SIZE, highlight: .none, profiles: damus_state.profiles, picture: picture) + InnerProfilePicView(url: URL(string: picture), pubkey: damus_state.pubkey, size: PPM_SIZE, highlight: .none) Spacer() } - .padding([.top], 30) Form { Section("Your Name") { TextField("Satoshi Nakamoto", text: $display_name) @@ -120,7 +120,12 @@ struct EditMetadataView: View { TextField("https://example.com/pic.jpg", text: $picture) .autocorrectionDisabled(true) .textInputAutocapitalization(.never) - + } + + Section("Website") { + TextField("https://jb55.com", text: $website) + .autocorrectionDisabled(true) + .textInputAutocapitalization(.never) } Section("About Me") { @@ -135,18 +140,11 @@ struct EditMetadataView: View { } } - Section(content: { - TextField("Lightning Address", text: $lud16) + Section("Bitcoin Lightning Tips") { + TextField("Lightning Address or LNURL", text: $ln) .autocorrectionDisabled(true) .textInputAutocapitalization(.never) - TextField("LNURL", text: $lud06) - .autocorrectionDisabled(true) - .textInputAutocapitalization(.never) - }, header: { - Text("Bitcoin Lightning Tips") - }, footer: { - Text("Only one needs to be set") - }) + } Section(content: { TextField("example.com", text: $nip05) @@ -160,12 +158,11 @@ struct EditMetadataView: View { Button("Save") { save() - showAlert = true - }.alert(isPresented: $showAlert) { - Alert(title: Text("Saved"), message: Text("Your metadata has been saved."), dismissButton: .default(Text("OK"))) + dismiss() } } } + .navigationTitle("Edit Profile") } } diff --git a/damus/Views/FollowButtonView.swift b/damus/Views/FollowButtonView.swift @@ -13,14 +13,9 @@ struct FollowButtonView: View { let target: FollowTarget @State var follow_state: FollowState - let perform: (() -> Void)? var body: some View { Button { - if perform != nil { - perform!() - } - follow_state = perform_follow_btn_action(follow_state, target: target) } label: { Text(follow_btn_txt(follow_state)) @@ -71,19 +66,16 @@ struct FollowButtonPreviews: View { var body: some View { VStack { Text("Unfollows") - FollowButtonView(target: target, follow_state: .unfollows, perform: nil) + FollowButtonView(target: target, follow_state: .unfollows) Text("Following") - FollowButtonView(target: target, follow_state: .following, perform: nil) + FollowButtonView(target: target, follow_state: .following) Text("Follows") - FollowButtonView(target: target, follow_state: .follows, perform: nil) + FollowButtonView(target: target, follow_state: .follows) Text("Unfollowing") - FollowButtonView(target: target, follow_state: .unfollowing, perform: nil) - - Text("Edit") - FollowButtonView(target: target, follow_state: .edit, perform: nil) + FollowButtonView(target: target, follow_state: .unfollowing) } } } @@ -106,8 +98,6 @@ func perform_follow_btn_action(_ fs: FollowState, target: FollowTarget) -> Follo case .unfollows: notify(.follow, target) return .unfollowing - case .edit: - return .edit } } diff --git a/damus/Views/FollowingView.swift b/damus/Views/FollowingView.swift @@ -32,7 +32,7 @@ struct FollowUserView: View { } .buttonStyle(PlainButtonStyle()) - FollowButtonView(target: target, follow_state: damus_state.contacts.follow_state(target.pubkey), perform: nil) + FollowButtonView(target: target, follow_state: damus_state.contacts.follow_state(target.pubkey)) } } } @@ -53,6 +53,12 @@ struct FollowersView: View { } } .navigationBarTitle("\(Profile.displayName(profile: profile, pubkey: whos))'s Followers") + .onAppear { + followers.subscribe() + } + .onDisappear { + followers.unsubscribe() + } } } diff --git a/damus/Views/ProfilePicView.swift b/damus/Views/ProfilePicView.swift @@ -32,17 +32,13 @@ func pfp_line_width(_ h: Highlight) -> CGFloat { } } -struct ProfilePicView: View { - +struct InnerProfilePicView: View { @Environment(\.redactionReasons) private var reasons + let url: URL? let pubkey: String let size: CGFloat let highlight: Highlight - let profiles: Profiles - let isPlaceholder: Bool = false - - @State var picture: String? = nil var PlaceholderColor: Color { return id_to_color(pubkey) @@ -56,11 +52,8 @@ struct ProfilePicView: View { .padding(2) } - var MainContent: some View { + var body: some View { Group { - let pic = picture ?? profiles.lookup(id: pubkey)?.picture ?? robohash(pubkey) - let url = URL(string: pic) - if reasons.isEmpty { KFAnimatedImage(url) .configure { view in @@ -82,8 +75,27 @@ struct ProfilePicView: View { .overlay(Circle().stroke(highlight_color(highlight), lineWidth: pfp_line_width(highlight))) } +} + +struct ProfilePicView: View { + + let pubkey: String + let size: CGFloat + let highlight: Highlight + let profiles: Profiles + + @State var picture: String? + + init (pubkey: String, size: CGFloat, highlight: Highlight, profiles: Profiles, picture: String? = nil) { + self.pubkey = pubkey + self.profiles = profiles + self.size = size + self.highlight = highlight + self._picture = State(initialValue: picture) + } + var body: some View { - MainContent + InnerProfilePicView(url: get_profile_url(picture: picture, pubkey: pubkey, profiles: profiles), pubkey: pubkey, size: size, highlight: highlight) .onReceive(handle_notify(.profile_updated)) { notif in let updated = notif.object as! ProfileUpdate @@ -98,6 +110,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) + if let url = URL(string: pic) { + return url + } + return URL(string: robohash(pubkey))! +} + func make_preview_profiles(_ pubkey: String) -> Profiles { let profiles = Profiles() let picture = "http://cdn.jb55.com/img/red-me.jpg" diff --git a/damus/Views/ProfileView.swift b/damus/Views/ProfileView.swift @@ -17,7 +17,6 @@ enum FollowState { case following case unfollowing case unfollows - case edit } func follow_btn_txt(_ fs: FollowState) -> String { @@ -30,8 +29,6 @@ func follow_btn_txt(_ fs: FollowState) -> String { return "Unfollowing..." case .unfollows: return "Follow" - case .edit: - return "Edit" } } @@ -45,8 +42,6 @@ func follow_btn_enabled_state(_ fs: FollowState) -> Bool { return false case .unfollows: return true - case .edit: - return true } } @@ -82,6 +77,40 @@ struct ProfileNameView: View { } } +struct EditButton: View { + let damus_state: DamusState + + @Environment(\.colorScheme) var colorScheme + + var body: some View { + NavigationLink(destination: EditMetadataView(damus_state: damus_state)) { + Text("Edit") + .padding(.horizontal, 25) + .padding(.vertical, 10) + .font(.caption.weight(.bold)) + .foregroundColor(fillColor()) + .background(emptyColor()) + .cornerRadius(20) + .overlay { + RoundedRectangle(cornerRadius: 16) + .stroke(borderColor(), lineWidth: 1) + } + } + } + + func fillColor() -> Color { + colorScheme == .light ? .black : .white + } + + func emptyColor() -> Color { + colorScheme == .light ? .white : .black + } + + func borderColor() -> Color { + colorScheme == .light ? .black.opacity(0.1) : .white.opacity(0.2) + } +} + struct ProfileView: View { let damus_state: DamusState @@ -134,14 +163,17 @@ struct ProfileView: View { DMButton - FollowButtonView( - target: profile.get_follow_target(), - follow_state: profile.pubkey == damus_state.pubkey ? .edit : damus_state.contacts.follow_state(profile.pubkey), - perform: profile.pubkey == damus_state.pubkey ? { showingEditProfile.toggle() } : nil - ) + if profile.pubkey != damus_state.pubkey { + FollowButtonView( + target: profile.get_follow_target(), + follow_state: damus_state.contacts.follow_state(profile.pubkey) + ) + } else { + NavigationLink(destination: EditMetadataView(damus_state: damus_state)) { + EditButton(damus_state: damus_state) + } + } - }.sheet(isPresented: $showingEditProfile) { - EditMetadataView(damus_state: damus_state) } ProfileNameView(pubkey: profile.pubkey, profile: data, contacts: damus_state.contacts) @@ -201,11 +233,11 @@ struct ProfileView: View { } .onAppear() { profile.subscribe() - followers.subscribe() + //followers.subscribe() } .onDisappear { profile.unsubscribe() - followers.unsubscribe() + //followers.unsubscribe() // our profilemodel needs a bit more help } }