Refactor status toolbar out of PostEditorView and fix macOS build error

This commit is contained in:
Angelo Stavrow 2020-09-01 15:00:20 -04:00
parent afa026dc57
commit 82056733cb
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
4 changed files with 127 additions and 37 deletions

View File

@ -0,0 +1,118 @@
import SwiftUI
struct PostEditorStatusToolbarView: View {
#if os(iOS)
@Environment(\.horizontalSizeClass) var horizontalSizeClass
#endif
@EnvironmentObject var model: WriteFreelyModel
@ObservedObject var post: Post
var body: some View {
if post.hasNewerRemoteCopy {
#if os(iOS)
if horizontalSizeClass == .compact {
VStack {
PostStatusBadgeView(post: post)
HStack {
Text("⚠️ Newer copy on server. Replace local copy?")
.font(.caption)
.foregroundColor(.secondary)
Button(action: {
model.updateFromServer(post: post)
}, label: {
Image(systemName: "square.and.arrow.down")
})
}
.padding(.bottom)
}
.padding(.top)
} else {
HStack {
PostStatusBadgeView(post: post)
.padding(.trailing)
Text("⚠️ Newer copy on server. Replace local copy?")
.font(.callout)
.foregroundColor(.secondary)
Button(action: {
model.updateFromServer(post: post)
}, label: {
Image(systemName: "square.and.arrow.down")
})
}
}
#else
HStack {
PostStatusBadgeView(post: post)
.padding(.trailing)
Text("⚠️ Newer copy on server. Replace local copy?")
.font(.callout)
.foregroundColor(.secondary)
Button(action: {
model.updateFromServer(post: post)
}, label: {
Image(systemName: "square.and.arrow.down")
})
}
#endif
} else {
PostStatusBadgeView(post: post)
}
}
}
struct ToolbarView_LocalPreviews: PreviewProvider {
static var previews: some View {
let model = WriteFreelyModel()
let post = testPost
return PostEditorStatusToolbarView(post: post)
.environmentObject(model)
}
}
struct ToolbarView_RemotePreviews: PreviewProvider {
static var previews: some View {
let model = WriteFreelyModel()
let newerRemotePost = Post(
title: testPost.wfPost.title ?? "",
body: testPost.wfPost.body,
createdDate: testPost.wfPost.createdDate ?? Date(),
status: testPost.status,
collection: testPost.collection
)
newerRemotePost.hasNewerRemoteCopy = true
return PostEditorStatusToolbarView(post: newerRemotePost)
.environmentObject(model)
}
}
#if os(iOS)
struct ToolbarView_CompactLocalPreviews: PreviewProvider {
static var previews: some View {
let model = WriteFreelyModel()
let post = testPost
return PostEditorStatusToolbarView(post: post)
.environmentObject(model)
.environment(\.horizontalSizeClass, .compact)
}
}
#endif
#if os(iOS)
struct ToolbarView_CompactRemotePreviews: PreviewProvider {
static var previews: some View {
let model = WriteFreelyModel()
let newerRemotePost = Post(
title: testPost.wfPost.title ?? "",
body: testPost.wfPost.body,
createdDate: testPost.wfPost.createdDate ?? Date(),
status: testPost.status,
collection: testPost.collection
)
newerRemotePost.hasNewerRemoteCopy = true
return PostEditorStatusToolbarView(post: newerRemotePost)
.environmentObject(model)
.environment(\.horizontalSizeClass, .compact)
}
}
#endif

View File

@ -1,7 +1,6 @@
import SwiftUI
struct PostEditorView: View {
@Environment(\.horizontalSizeClass) var horizontalSizeClass
@EnvironmentObject var model: WriteFreelyModel
@ObservedObject var post: Post
@ -30,40 +29,7 @@ struct PostEditorView: View {
.padding()
.toolbar {
ToolbarItem(placement: .status) {
if post.hasNewerRemoteCopy {
if horizontalSizeClass == .compact {
VStack {
PostStatusBadgeView(post: post)
HStack {
Text("⚠️ Newer copy on server. Replace local copy?")
.font(.caption)
.foregroundColor(.secondary)
Button(action: {
model.updateFromServer(post: post)
}, label: {
Image(systemName: "square.and.arrow.down")
})
}
.padding(.bottom)
}
.padding(.top)
} else {
HStack {
PostStatusBadgeView(post: post)
.padding(.trailing)
Text("⚠️ Newer copy on server. Replace local copy?")
.font(.callout)
.foregroundColor(.secondary)
Button(action: {
model.updateFromServer(post: post)
}, label: {
Image(systemName: "square.and.arrow.down")
})
}
}
} else {
PostStatusBadgeView(post: post)
}
PostEditorStatusToolbarView(post: post)
}
ToolbarItem(placement: .primaryAction) {
Button(action: {

View File

@ -34,6 +34,8 @@
1756AE7A24CB65DF00FD7257 /* PostListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1756AE7924CB65DF00FD7257 /* PostListView.swift */; };
1756AE7B24CB65DF00FD7257 /* PostListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1756AE7924CB65DF00FD7257 /* PostListView.swift */; };
1756AE8124CB844500FD7257 /* View+Keyboard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1756AE8024CB844500FD7257 /* View+Keyboard.swift */; };
1756DBB324FECDBB00207AB8 /* PostEditorStatusToolbarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1756DBB224FECDBB00207AB8 /* PostEditorStatusToolbarView.swift */; };
1756DBB424FECDBB00207AB8 /* PostEditorStatusToolbarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1756DBB224FECDBB00207AB8 /* PostEditorStatusToolbarView.swift */; };
1762DCB324EB086C0019C4EB /* CollectionListModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1762DCB224EB086C0019C4EB /* CollectionListModel.swift */; };
1762DCB424EB086C0019C4EB /* CollectionListModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1762DCB224EB086C0019C4EB /* CollectionListModel.swift */; };
1765F62A24E18EA200C9EBF0 /* SidebarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1765F62924E18EA200C9EBF0 /* SidebarView.swift */; };
@ -90,6 +92,7 @@
1756AE7624CB2EDD00FD7257 /* PostEditorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostEditorView.swift; sourceTree = "<group>"; };
1756AE7924CB65DF00FD7257 /* PostListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostListView.swift; sourceTree = "<group>"; };
1756AE8024CB844500FD7257 /* View+Keyboard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+Keyboard.swift"; sourceTree = "<group>"; };
1756DBB224FECDBB00207AB8 /* PostEditorStatusToolbarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostEditorStatusToolbarView.swift; sourceTree = "<group>"; };
1762DCB224EB086C0019C4EB /* CollectionListModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionListModel.swift; sourceTree = "<group>"; };
1765F62924E18EA200C9EBF0 /* SidebarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SidebarView.swift; sourceTree = "<group>"; };
17A5388724DDA31F00DEFF9A /* MacAccountView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacAccountView.swift; sourceTree = "<group>"; };
@ -166,6 +169,7 @@
isa = PBXGroup;
children = (
1756AE7624CB2EDD00FD7257 /* PostEditorView.swift */,
1756DBB224FECDBB00207AB8 /* PostEditorStatusToolbarView.swift */,
);
path = PostEditor;
sourceTree = "<group>";
@ -546,6 +550,7 @@
17120DAC24E1B99F002B9F6C /* AccountLoginView.swift in Sources */,
17120DA924E1B2F5002B9F6C /* AccountLogoutView.swift in Sources */,
171BFDFA24D4AF8300888236 /* CollectionListView.swift in Sources */,
1756DBB324FECDBB00207AB8 /* PostEditorStatusToolbarView.swift in Sources */,
17120DB224E1E19C002B9F6C /* SettingsHeaderView.swift in Sources */,
1756AE7724CB2EDD00FD7257 /* PostEditorView.swift in Sources */,
17DF32D524C8CA3400BCE2E3 /* PostStatusBadgeView.swift in Sources */,
@ -587,6 +592,7 @@
1762DCB424EB086C0019C4EB /* CollectionListModel.swift in Sources */,
17A5389324DDED0000DEFF9A /* PreferencesView.swift in Sources */,
1756AE6F24CB255B00FD7257 /* PostStore.swift in Sources */,
1756DBB424FECDBB00207AB8 /* PostEditorStatusToolbarView.swift in Sources */,
1756AE6C24CB1E4B00FD7257 /* Post.swift in Sources */,
17A5388F24DDEC7400DEFF9A /* AccountView.swift in Sources */,
1756AE7524CB26FA00FD7257 /* PostCellView.swift in Sources */,

View File

@ -7,12 +7,12 @@
<key>WriteFreely-MultiPlatform (iOS).xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
<integer>0</integer>
</dict>
<key>WriteFreely-MultiPlatform (macOS).xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
<integer>1</integer>
</dict>
</dict>
</dict>