Use ObservedObject rather than unwrapping model.selectedPost

This commit is contained in:
Angelo Stavrow 2021-01-27 10:18:26 -05:00
parent 89261831bc
commit f948a16ad0
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
2 changed files with 88 additions and 89 deletions

View File

@ -112,19 +112,21 @@ struct PostListView: View {
) )
.toolbar { .toolbar {
ToolbarItemGroup(placement: .primaryAction) { ToolbarItemGroup(placement: .primaryAction) {
ActivePostToolbarView() if model.selectedPost != nil {
.alert(isPresented: $model.isPresentingNetworkErrorAlert, content: { ActivePostToolbarView(activePost: model.selectedPost!)
Alert( .alert(isPresented: $model.isPresentingNetworkErrorAlert, content: {
title: Text("Connection Error"), Alert(
message: Text(""" title: Text("Connection Error"),
message: Text("""
There is no internet connection at the moment. \ There is no internet connection at the moment. \
Please reconnect or try again later. Please reconnect or try again later.
"""), """),
dismissButton: .default(Text("OK"), action: { dismissButton: .default(Text("OK"), action: {
model.isPresentingNetworkErrorAlert = false model.isPresentingNetworkErrorAlert = false
}) })
) )
}) })
}
} }
} }
.navigationTitle( .navigationTitle(

View File

@ -2,6 +2,7 @@ import SwiftUI
struct ActivePostToolbarView: View { struct ActivePostToolbarView: View {
@EnvironmentObject var model: WriteFreelyModel @EnvironmentObject var model: WriteFreelyModel
@ObservedObject var activePost: WFAPost
@State private var isPresentingSharingServicePicker: Bool = false @State private var isPresentingSharingServicePicker: Bool = false
@State private var selectedCollection: WFACollection? @State private var selectedCollection: WFACollection?
@ -11,100 +12,96 @@ struct ActivePostToolbarView: View {
) var collections: FetchedResults<WFACollection> ) var collections: FetchedResults<WFACollection>
var body: some View { var body: some View {
if let activePost = model.selectedPost { HStack {
HStack { if model.account.isLoggedIn &&
if model.account.isLoggedIn && activePost.status != PostStatus.local.rawValue &&
activePost.status != PostStatus.local.rawValue && !(activePost.wasDeletedFromServer || activePost.hasNewerRemoteCopy) {
!(activePost.wasDeletedFromServer || activePost.hasNewerRemoteCopy) { Section(header: Text("Move To:")) {
Section(header: Text("Move To:")) { Picker(selection: $selectedCollection, label: Text("Move To…"), content: {
Picker(selection: $selectedCollection, label: Text("Move To…"), content: { Text("\(model.account.server == "https://write.as" ? "Anonymous" : "Drafts")")
Text("\(model.account.server == "https://write.as" ? "Anonymous" : "Drafts")") .tag(nil as WFACollection?)
.tag(nil as WFACollection?)
Divider()
ForEach(collections) { collection in
Text("\(collection.title)").tag(collection as WFACollection?)
}
})
}
}
PostEditorStatusToolbarView(post: activePost)
.frame(minWidth: 50, alignment: .center)
.layoutPriority(1)
.padding(.horizontal)
if activePost.status == PostStatus.local.rawValue {
Menu(content: {
Label("Publish To:", systemImage: "paperplane")
Divider() Divider()
ForEach(collections) { collection in
Text("\(collection.title)").tag(collection as WFACollection?)
}
})
}
}
PostEditorStatusToolbarView(post: activePost)
.frame(minWidth: 50, alignment: .center)
.layoutPriority(1)
.padding(.horizontal)
if activePost.status == PostStatus.local.rawValue {
Menu(content: {
Label("Publish To:", systemImage: "paperplane")
Divider()
Button(action: {
if model.account.isLoggedIn {
withAnimation {
activePost.collectionAlias = nil
publishPost(activePost)
}
} else {
openSettingsWindow()
}
}, label: {
Text("\(model.account.server == "https://write.as" ? "Anonymous" : "Drafts")")
})
ForEach(collections) { collection in
Button(action: { Button(action: {
if model.account.isLoggedIn { if model.account.isLoggedIn {
withAnimation { withAnimation {
activePost.collectionAlias = nil activePost.collectionAlias = collection.alias
publishPost(activePost) publishPost(activePost)
} }
} else { } else {
openSettingsWindow() openSettingsWindow()
} }
}, label: { }, label: {
Text("\(model.account.server == "https://write.as" ? "Anonymous" : "Drafts")") Text("\(collection.title)")
}) })
ForEach(collections) { collection in
Button(action: {
if model.account.isLoggedIn {
withAnimation {
activePost.collectionAlias = collection.alias
publishPost(activePost)
}
} else {
openSettingsWindow()
}
}, label: {
Text("\(collection.title)")
})
}
}, label: {
Label("Publish…", systemImage: "paperplane")
})
.disabled(activePost.body.isEmpty)
.help("Publish the post to the web.\(model.account.isLoggedIn ? "" : " You must be logged in to do this.")") // swiftlint:disable:this line_length
} else {
HStack(spacing: 4) {
Button(
action: {
self.isPresentingSharingServicePicker = true
},
label: { Image(systemName: "square.and.arrow.up") }
)
.disabled(activePost.status == PostStatus.local.rawValue)
.help("Copy the post's URL to your Mac's pasteboard.")
.popover(isPresented: $isPresentingSharingServicePicker) {
PostEditorSharingPicker(
isPresented: $isPresentingSharingServicePicker,
sharingItems: createPostUrl()
)
.frame(width: .zero, height: .zero)
}
Button(action: { publishPost(activePost) }, label: { Image(systemName: "paperplane") })
.disabled(activePost.body.isEmpty || activePost.status == PostStatus.published.rawValue)
.help("Publish the post to the web.\(model.account.isLoggedIn ? "" : " You must be logged in to do this.")") // swiftlint:disable:this line_length
} }
}, label: {
Label("Publish…", systemImage: "paperplane")
})
.disabled(model.selectedPost!.body.isEmpty)
.help("Publish the post to the web.\(model.account.isLoggedIn ? "" : " You must be logged in to do this.")") // swiftlint:disable:this line_length
} else {
HStack(spacing: 4) {
Button(
action: {
self.isPresentingSharingServicePicker = true
},
label: { Image(systemName: "square.and.arrow.up") }
)
.disabled(activePost.status == PostStatus.local.rawValue)
.help("Copy the post's URL to your Mac's pasteboard.")
.popover(isPresented: $isPresentingSharingServicePicker) {
PostEditorSharingPicker(
isPresented: $isPresentingSharingServicePicker,
sharingItems: createPostUrl()
)
.frame(width: .zero, height: .zero)
}
Button(action: { publishPost(activePost) }, label: { Image(systemName: "paperplane") })
.disabled(activePost.body.isEmpty || activePost.status == PostStatus.published.rawValue)
.help("Publish the post to the web.\(model.account.isLoggedIn ? "" : " You must be logged in to do this.")") // swiftlint:disable:this line_length
} }
} }
.onAppear(perform: {
self.selectedCollection = collections.first { $0.alias == activePost.collectionAlias }
})
.onChange(of: selectedCollection, perform: { [selectedCollection] newCollection in
if activePost.collectionAlias == newCollection?.alias {
return
} else {
withAnimation {
activePost.collectionAlias = newCollection?.alias
model.move(post: activePost, from: selectedCollection, to: newCollection)
}
}
})
} else {
EmptyView()
} }
.onAppear(perform: {
self.selectedCollection = collections.first { $0.alias == activePost.collectionAlias }
})
.onChange(of: selectedCollection, perform: { [selectedCollection] newCollection in
if activePost.collectionAlias == newCollection?.alias {
return
} else {
withAnimation {
activePost.collectionAlias = newCollection?.alias
model.move(post: activePost, from: selectedCollection, to: newCollection)
}
}
})
} }
private func createPostUrl() -> [Any] { private func createPostUrl() -> [Any] {