Metadata screen

This commit is contained in:
Thomas
2022-12-23 16:12:50 +01:00
parent 549cbb9bce
commit dff450282b
9 changed files with 150 additions and 12 deletions

View File

@@ -7,7 +7,7 @@
import Foundation
class ProfileModel: ObservableObject {
class ProfileModel: ObservableObject, Equatable {
@Published var events: [NostrEvent] = []
@Published var contacts: NostrEvent? = nil
@Published var following: Int = 0
@@ -31,6 +31,14 @@ class ProfileModel: ObservableObject {
self.damus = damus
}
static func == (lhs: ProfileModel, rhs: ProfileModel) -> Bool {
return lhs.pubkey == rhs.pubkey
}
func hash(into hasher: inout Hasher) {
hasher.combine(pubkey)
}
func unsubscribe() {
print("unsubscribing from profile \(pubkey) with sub_id \(sub_id)")
damus.pool.unsubscribe(sub_id: sub_id)

View File

@@ -8,12 +8,13 @@
import Foundation
struct Profile: Decodable {
struct Profile: Decodable, Equatable {
let name: String?
let display_name: String?
let about: String?
let picture: String?
let website: String?
let nip05: String?
let lud06: String?
let lud16: String?
@@ -34,6 +35,3 @@ struct NostrSubscription {
let sub_id: String
let filter: NostrFilter
}

View File

@@ -13,8 +13,12 @@ struct NostrMetadata: Codable {
let name: String?
let about: String?
let website: String?
let nip05: String?
let picture: String?
let lud06: String?
let lud16: String?
}
func create_account_to_metadata(_ model: CreateAccountModel) -> NostrMetadata {
return NostrMetadata(display_name: model.real_name, name: model.nick_name, about: model.about, website: nil)
return NostrMetadata(display_name: model.real_name, name: model.nick_name, about: model.about, website: nil, nip05: nil, picture: nil, lud06: nil, lud16: nil)
}

View File

@@ -73,11 +73,19 @@ struct ConfigView: View {
CopyButton(is_pk: false)
}
Toggle("Show", isOn: $show_privkey)
}
}
Section("Account settings") {
NavigationLink {
MetadataView(damus_state: state, profileModel: ProfileModel(pubkey: state.pubkey, damus: state))
} label: {
Text("Profile metadata")
}
}
Section("Reset") {
Button("Logout") {
confirm_logout = true
@@ -129,7 +137,7 @@ struct ConfigView: View {
guard let privkey = state.keypair.privkey else {
return
}
let info = RelayInfo.rw
guard (try? state.pool.add_relay(url, info: info)) != nil else {
@@ -154,6 +162,8 @@ struct ConfigView: View {
struct ConfigView_Previews: PreviewProvider {
static var previews: some View {
ConfigView(state: test_damus_state())
NavigationView {
ConfigView(state: test_damus_state())
}
}
}

View File

@@ -0,0 +1,113 @@
//
// MetadataView.swift
// damus
//
// Created by Thomas Tastet on 23/12/2022.
//
import SwiftUI
struct MetadataView: View {
let damus_state: DamusState
@State var name: String = ""
@State var about: String = ""
@State var picture: String = ""
@State var nip05: String = ""
@State var nickname: String = ""
@State var lud06: String = ""
@State var lud16: String = ""
@State private var showAlert = false
@State private var isFocused = false
@StateObject var profileModel: ProfileModel
func save() {
let metadata = NostrMetadata(
display_name: name,
name: nickname,
about: about,
website: nil,
nip05: nip05.isEmpty ? nil : nip05,
picture: picture.isEmpty ? nil : picture,
lud06: lud06.isEmpty ? nil : lud06,
lud16: lud16.isEmpty ? nil : lud16
);
let m_metadata_ev = make_metadata_event(keypair: damus_state.keypair, metadata: metadata)
if let metadata_ev = m_metadata_ev {
damus_state.pool.send(.event(metadata_ev))
}
}
var body: some View {
VStack(alignment: .leading) {
Form {
Section("Your Nostr Profile") {
TextField("Your username", text: $name)
.textInputAutocapitalization(.never)
TextField("Your @", text: $nickname)
.textInputAutocapitalization(.never)
TextField("Profile Picture Url", text: $picture)
.autocorrectionDisabled(true)
.textInputAutocapitalization(.never)
TextField("NIP 05 (@)", text: $nip05)
.autocorrectionDisabled(true)
.textInputAutocapitalization(.never)
}
Section("Description") {
ZStack(alignment: .topLeading) {
TextEditor(text: $about)
.textInputAutocapitalization(.sentences)
if about.isEmpty {
Text("Type your description here...")
.offset(x: 0, y: 7)
.foregroundColor(Color(uiColor: .placeholderText))
}
}
}
Section("Advanced") {
TextField("Lud06", text: $lud06)
.autocorrectionDisabled(true)
.textInputAutocapitalization(.never)
TextField("Lud16", text: $lud16)
.autocorrectionDisabled(true)
.textInputAutocapitalization(.never)
}
Button("Save") {
save()
showAlert = true
}.alert(isPresented: $showAlert) {
Alert(title: Text("Saved"), message: Text("Your metadata has been saved."), dismissButton: .default(Text("OK")))
}
}
}
.onAppear() {
profileModel.subscribe()
let data = damus_state.profiles.lookup(id: profileModel.pubkey)
name = data?.display_name ?? name
nickname = data?.name ?? name
about = data?.about ?? about
picture = data?.picture ?? picture
nip05 = data?.nip05 ?? nip05
lud06 = data?.lud06 ?? lud06
lud16 = data?.lud16 ?? lud16
}
.onDisappear {
profileModel.unsubscribe()
}
}
}
struct MetadataView_Previews: PreviewProvider {
static var previews: some View {
let ds = test_damus_state()
let profile_model = ProfileModel(pubkey: ds.pubkey, damus: ds)
MetadataView(damus_state: ds, profileModel: profile_model)
}
}

View File

@@ -79,6 +79,7 @@ struct PostView: View {
.padding(.top, 8)
.padding(.leading, 10)
.foregroundColor(Color(uiColor: .placeholderText))
.allowsHitTesting(false)
}
}
}

View File

@@ -101,7 +101,7 @@ struct ProfilePicView: View {
func make_preview_profiles(_ pubkey: String) -> Profiles {
let profiles = Profiles()
let picture = "http://cdn.jb55.com/img/red-me.jpg"
let profile = Profile(name: "jb55", display_name: "William Casarin", about: "It's me", picture: picture, website: "https://jb55.com", lud06: nil, lud16: nil)
let profile = Profile(name: "jb55", display_name: "William Casarin", about: "It's me", picture: picture, website: "https://jb55.com", nip05: "jb55@damus.io", lud06: nil, lud16: nil)
let ts_profile = TimestampedProfile(profile: profile, timestamp: 0)
profiles.add(id: pubkey, profile: ts_profile)
return profiles

View File

@@ -211,7 +211,7 @@ func test_damus_state() -> DamusState {
let pubkey = "3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681"
let damus = DamusState(pool: RelayPool(), keypair: Keypair(pubkey: pubkey, privkey: "privkey"), likes: EventCounter(our_pubkey: pubkey), boosts: EventCounter(our_pubkey: pubkey), contacts: Contacts(), tips: TipCounter(our_pubkey: pubkey), profiles: Profiles(), dms: DirectMessagesModel())
let prof = Profile(name: "damus", display_name: "Damus", about: "iOS app!", picture: "https://damus.io/img/logo.png", website: "https://damus.io", lud06: nil, lud16: "jb55@sendsats.lol")
let prof = Profile(name: "damus", display_name: "Damus", about: "iOS app!", picture: "https://damus.io/img/logo.png", website: "https://damus.io", nip05: "jb@damus.io", lud06: nil, lud16: "jb55@sendsats.lol")
let tsprof = TimestampedProfile(profile: prof, timestamp: 0)
damus.profiles.add(id: pubkey, profile: tsprof)
return damus