Refactor drafts
This commit is contained in:
@@ -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!
|
||||
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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] = [:]
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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] = ""
|
||||
}
|
||||
post = damus_state.drafts_model.replies[replying_to!]!
|
||||
if let p = damus_state.drafts.replies[replying_to] {
|
||||
post = p
|
||||
}
|
||||
} 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()
|
||||
|
||||
Reference in New Issue
Block a user