Refactor drafts
This commit is contained in:
@@ -620,7 +620,7 @@ struct ContentView: View {
|
|||||||
settings: UserSettingsStore(),
|
settings: UserSettingsStore(),
|
||||||
relay_filters: relay_filters,
|
relay_filters: relay_filters,
|
||||||
relay_metadata: metadatas,
|
relay_metadata: metadatas,
|
||||||
drafts_model: home.drafts_model
|
drafts: Drafts()
|
||||||
)
|
)
|
||||||
home.damus_state = self.damus_state!
|
home.damus_state = self.damus_state!
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ struct DamusState {
|
|||||||
let settings: UserSettingsStore
|
let settings: UserSettingsStore
|
||||||
let relay_filters: RelayFilters
|
let relay_filters: RelayFilters
|
||||||
let relay_metadata: RelayMetadatas
|
let relay_metadata: RelayMetadatas
|
||||||
let drafts_model: DraftsModel
|
let drafts: Drafts
|
||||||
|
|
||||||
var pubkey: String {
|
var pubkey: String {
|
||||||
return keypair.pubkey
|
return keypair.pubkey
|
||||||
@@ -35,6 +35,6 @@ struct DamusState {
|
|||||||
|
|
||||||
|
|
||||||
static var empty: 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
|
import Foundation
|
||||||
|
|
||||||
class DraftsModel: ObservableObject {
|
class Drafts: ObservableObject {
|
||||||
@Published var post: String = ""
|
@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 events: [NostrEvent] = []
|
||||||
@Published var loading: Bool = false
|
@Published var loading: Bool = false
|
||||||
@Published var signal: SignalModel = SignalModel()
|
@Published var signal: SignalModel = SignalModel()
|
||||||
@Published var drafts_model: DraftsModel
|
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
self.damus_state = DamusState.empty
|
self.damus_state = DamusState.empty
|
||||||
self.dms = DirectMessagesModel(our_pubkey: damus_state.pubkey)
|
self.dms = DirectMessagesModel(our_pubkey: damus_state.pubkey)
|
||||||
self.drafts_model = DraftsModel()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
init(damus_state: DamusState) {
|
init(damus_state: DamusState) {
|
||||||
self.damus_state = damus_state
|
self.damus_state = damus_state
|
||||||
self.dms = DirectMessagesModel(our_pubkey: damus_state.pubkey)
|
self.dms = DirectMessagesModel(our_pubkey: damus_state.pubkey)
|
||||||
self.drafts_model = DraftsModel()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var pool: RelayPool {
|
var pool: RelayPool {
|
||||||
|
|||||||
@@ -49,10 +49,10 @@ struct PostView: View {
|
|||||||
|
|
||||||
NotificationCenter.default.post(name: .post, object: NostrPostResult.post(new_post))
|
NotificationCenter.default.post(name: .post, object: NostrPostResult.post(new_post))
|
||||||
|
|
||||||
if replying_to == nil {
|
if let replying_to {
|
||||||
damus_state.drafts_model.post = ""
|
damus_state.drafts.replies.removeValue(forKey: replying_to)
|
||||||
} else {
|
} else {
|
||||||
damus_state.drafts_model.replies.removeValue(forKey: replying_to!)
|
damus_state.drafts.post = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
dismiss()
|
dismiss()
|
||||||
@@ -89,10 +89,10 @@ struct PostView: View {
|
|||||||
.focused($focus)
|
.focused($focus)
|
||||||
.textInputAutocapitalization(.sentences)
|
.textInputAutocapitalization(.sentences)
|
||||||
.onChange(of: post) { _ in
|
.onChange(of: post) { _ in
|
||||||
if replying_to == nil {
|
if let replying_to {
|
||||||
damus_state.drafts_model.post = post
|
damus_state.drafts.replies[replying_to] = post
|
||||||
} else {
|
} else {
|
||||||
damus_state.drafts_model.replies[replying_to!] = post
|
damus_state.drafts.post = post
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,13 +114,15 @@ struct PostView: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onAppear() {
|
.onAppear() {
|
||||||
if replying_to == nil {
|
if let replying_to {
|
||||||
post = damus_state.drafts_model.post
|
if damus_state.drafts.replies[replying_to] == nil {
|
||||||
} else {
|
damus_state.drafts.replies[replying_to] = ""
|
||||||
if damus_state.drafts_model.replies[replying_to!] == nil {
|
|
||||||
damus_state.drafts_model.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) {
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
||||||
@@ -128,10 +130,10 @@ struct PostView: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onDisappear {
|
.onDisappear {
|
||||||
if replying_to == nil && damus_state.drafts_model.post.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
|
if let replying_to, let reply = damus_state.drafts.replies[replying_to], reply.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
|
||||||
damus_state.drafts_model.post = ""
|
damus_state.drafts.replies.removeValue(forKey: replying_to)
|
||||||
} else if replying_to != nil && damus_state.drafts_model.replies[replying_to!]?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty == true {
|
} else if replying_to == nil && damus_state.drafts.post.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
|
||||||
damus_state.drafts_model.replies.removeValue(forKey: replying_to!)
|
damus_state.drafts.post = ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.padding()
|
.padding()
|
||||||
|
|||||||
Reference in New Issue
Block a user