commit c7288505246f2ea697432dc7e0e2e8ffb58b3521
parent bc638f79f6bc4d17d86062dbbf1f44a106bd5a41
Author: William Casarin <jb55@jb55.com>
Date: Mon, 13 Feb 2023 09:26:31 -0800
Refactor drafts
Diffstat:
5 files changed, 23 insertions(+), 24 deletions(-)
diff --git a/damus/ContentView.swift b/damus/ContentView.swift
@@ -620,7 +620,7 @@ struct ContentView: View {
settings: UserSettingsStore(),
relay_filters: relay_filters,
relay_metadata: metadatas,
- drafts_model: home.drafts_model
+ drafts: Drafts()
)
home.damus_state = self.damus_state!
diff --git a/damus/Models/DamusState.swift b/damus/Models/DamusState.swift
@@ -23,7 +23,7 @@ struct DamusState {
let settings: UserSettingsStore
let relay_filters: RelayFilters
let relay_metadata: RelayMetadatas
- let drafts_model: DraftsModel
+ let drafts: Drafts
var pubkey: String {
return keypair.pubkey
@@ -35,6 +35,6 @@ struct DamusState {
static var empty: DamusState {
- return DamusState.init(pool: RelayPool(), keypair: Keypair(pubkey: "", privkey: ""), likes: EventCounter(our_pubkey: ""), boosts: EventCounter(our_pubkey: ""), contacts: Contacts(our_pubkey: ""), tips: TipCounter(our_pubkey: ""), profiles: Profiles(), dms: DirectMessagesModel(our_pubkey: ""), previews: PreviewCache(), zaps: Zaps(our_pubkey: ""), lnurls: LNUrls(), settings: UserSettingsStore(), relay_filters: RelayFilters(our_pubkey: ""), relay_metadata: RelayMetadatas(), drafts_model: DraftsModel())
+ return DamusState.init(pool: RelayPool(), keypair: Keypair(pubkey: "", privkey: ""), likes: EventCounter(our_pubkey: ""), boosts: EventCounter(our_pubkey: ""), contacts: Contacts(our_pubkey: ""), tips: TipCounter(our_pubkey: ""), profiles: Profiles(), dms: DirectMessagesModel(our_pubkey: ""), previews: PreviewCache(), zaps: Zaps(our_pubkey: ""), lnurls: LNUrls(), settings: UserSettingsStore(), relay_filters: RelayFilters(our_pubkey: ""), relay_metadata: RelayMetadatas(), drafts: Drafts())
}
}
diff --git a/damus/Models/DraftsModel.swift b/damus/Models/DraftsModel.swift
@@ -7,7 +7,7 @@
import Foundation
-class DraftsModel: ObservableObject {
+class Drafts: ObservableObject {
@Published var post: String = ""
- @Published var replies = Dictionary<NostrEvent, String>()
+ @Published var replies: [NostrEvent: String] = [:]
}
diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift
@@ -52,18 +52,15 @@ class HomeModel: ObservableObject {
@Published var events: [NostrEvent] = []
@Published var loading: Bool = false
@Published var signal: SignalModel = SignalModel()
- @Published var drafts_model: DraftsModel
init() {
self.damus_state = DamusState.empty
self.dms = DirectMessagesModel(our_pubkey: damus_state.pubkey)
- self.drafts_model = DraftsModel()
}
init(damus_state: DamusState) {
self.damus_state = damus_state
self.dms = DirectMessagesModel(our_pubkey: damus_state.pubkey)
- self.drafts_model = DraftsModel()
}
var pool: RelayPool {
diff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift
@@ -49,10 +49,10 @@ struct PostView: View {
NotificationCenter.default.post(name: .post, object: NostrPostResult.post(new_post))
- if replying_to == nil {
- damus_state.drafts_model.post = ""
+ if let replying_to {
+ damus_state.drafts.replies.removeValue(forKey: replying_to)
} else {
- damus_state.drafts_model.replies.removeValue(forKey: replying_to!)
+ damus_state.drafts.post = ""
}
dismiss()
@@ -89,10 +89,10 @@ struct PostView: View {
.focused($focus)
.textInputAutocapitalization(.sentences)
.onChange(of: post) { _ in
- if replying_to == nil {
- damus_state.drafts_model.post = post
+ if let replying_to {
+ damus_state.drafts.replies[replying_to] = post
} else {
- damus_state.drafts_model.replies[replying_to!] = post
+ damus_state.drafts.post = post
}
}
@@ -114,13 +114,15 @@ struct PostView: View {
}
}
.onAppear() {
- if replying_to == nil {
- post = damus_state.drafts_model.post
- } else {
- if damus_state.drafts_model.replies[replying_to!] == nil {
- damus_state.drafts_model.replies[replying_to!] = ""
+ if let replying_to {
+ if damus_state.drafts.replies[replying_to] == nil {
+ damus_state.drafts.replies[replying_to] = ""
+ }
+ if let p = damus_state.drafts.replies[replying_to] {
+ post = p
}
- post = damus_state.drafts_model.replies[replying_to!]!
+ } else {
+ post = damus_state.drafts.post
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
@@ -128,10 +130,10 @@ struct PostView: View {
}
}
.onDisappear {
- if replying_to == nil && damus_state.drafts_model.post.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
- damus_state.drafts_model.post = ""
- } else if replying_to != nil && damus_state.drafts_model.replies[replying_to!]?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty == true {
- damus_state.drafts_model.replies.removeValue(forKey: replying_to!)
+ if let replying_to, let reply = damus_state.drafts.replies[replying_to], reply.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
+ damus_state.drafts.replies.removeValue(forKey: replying_to)
+ } else if replying_to == nil && damus_state.drafts.post.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
+ damus_state.drafts.post = ""
}
}
.padding()