Skip to content

Commit 69a9b5e

Browse files
Merge pull request #94 from writeas/post-status-changes-on-update-from-server
Only update post status if we're not updating from the server
2 parents 609a330 + 3e54310 commit 69a9b5e

File tree

2 files changed

+55
-14
lines changed

2 files changed

+55
-14
lines changed

iOS/PostEditor/PostEditorView.swift

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ struct PostEditorView: View {
55
@Environment(\.horizontalSizeClass) var horizontalSizeClass
66
@Environment(\.presentationMode) var presentationMode
77
@ObservedObject var post: WFAPost
8+
@State private var updatingTitleFromServer: Bool = false
9+
@State private var updatingBodyFromServer: Bool = false
810

911
var body: some View {
1012
VStack {
@@ -47,9 +49,12 @@ struct PostEditorView: View {
4749
TextField("Title (optional)", text: $post.title)
4850
.font(.custom("OpenSans-Regular", size: 26, relativeTo: Font.TextStyle.largeTitle))
4951
.onChange(of: post.title) { _ in
50-
if post.status == PostStatus.published.rawValue {
52+
if post.status == PostStatus.published.rawValue && !updatingTitleFromServer {
5153
post.status = PostStatus.edited.rawValue
5254
}
55+
if updatingTitleFromServer {
56+
updatingTitleFromServer = false
57+
}
5358
}
5459
ZStack(alignment: .topLeading) {
5560
if post.body.count == 0 {
@@ -62,18 +67,24 @@ struct PostEditorView: View {
6267
TextEditor(text: $post.body)
6368
.font(.custom("OpenSans-Regular", size: 17, relativeTo: Font.TextStyle.body))
6469
.onChange(of: post.body) { _ in
65-
if post.status == PostStatus.published.rawValue {
70+
if post.status == PostStatus.published.rawValue && !updatingBodyFromServer {
6671
post.status = PostStatus.edited.rawValue
6772
}
73+
if updatingBodyFromServer {
74+
updatingBodyFromServer = false
75+
}
6876
}
6977
}
7078
case "wrap", "mono", "code":
7179
TextField("Title (optional)", text: $post.title)
7280
.font(.custom("Hack", size: 26, relativeTo: Font.TextStyle.largeTitle))
7381
.onChange(of: post.title) { _ in
74-
if post.status == PostStatus.published.rawValue {
82+
if post.status == PostStatus.published.rawValue && !updatingTitleFromServer {
7583
post.status = PostStatus.edited.rawValue
7684
}
85+
if updatingTitleFromServer {
86+
updatingTitleFromServer = false
87+
}
7788
}
7889
ZStack(alignment: .topLeading) {
7990
if post.body.count == 0 {
@@ -86,18 +97,24 @@ struct PostEditorView: View {
8697
TextEditor(text: $post.body)
8798
.font(.custom("Hack", size: 17, relativeTo: Font.TextStyle.body))
8899
.onChange(of: post.body) { _ in
89-
if post.status == PostStatus.published.rawValue {
100+
if post.status == PostStatus.published.rawValue && !updatingBodyFromServer {
90101
post.status = PostStatus.edited.rawValue
91102
}
103+
if updatingBodyFromServer {
104+
updatingBodyFromServer = false
105+
}
92106
}
93107
}
94108
default:
95109
TextField("Title (optional)", text: $post.title)
96110
.font(.custom("Lora", size: 26, relativeTo: Font.TextStyle.largeTitle))
97111
.onChange(of: post.title) { _ in
98-
if post.status == PostStatus.published.rawValue {
112+
if post.status == PostStatus.published.rawValue && !updatingTitleFromServer {
99113
post.status = PostStatus.edited.rawValue
100114
}
115+
if updatingTitleFromServer {
116+
updatingTitleFromServer = false
117+
}
101118
}
102119
ZStack(alignment: .topLeading) {
103120
if post.body.count == 0 {
@@ -110,9 +127,12 @@ struct PostEditorView: View {
110127
TextEditor(text: $post.body)
111128
.font(.custom("Lora", size: 17, relativeTo: Font.TextStyle.body))
112129
.onChange(of: post.body) { _ in
113-
if post.status == PostStatus.published.rawValue {
130+
if post.status == PostStatus.published.rawValue && !updatingBodyFromServer {
114131
post.status = PostStatus.edited.rawValue
115132
}
133+
if updatingBodyFromServer {
134+
updatingBodyFromServer = false
135+
}
116136
}
117137
}
118138
}
@@ -145,8 +165,9 @@ struct PostEditorView: View {
145165
}
146166
}
147167
.onChange(of: post.hasNewerRemoteCopy, perform: { _ in
148-
if post.status == PostStatus.edited.rawValue && !post.hasNewerRemoteCopy {
149-
post.status = PostStatus.published.rawValue
168+
if !post.hasNewerRemoteCopy {
169+
updatingTitleFromServer = true
170+
updatingBodyFromServer = true
150171
}
151172
})
152173
.onChange(of: post.status, perform: { _ in

macOS/PostEditor/PostEditorView.swift

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ struct PostEditorView: View {
55

66
@ObservedObject var post: WFAPost
77
@State private var isHovering: Bool = false
8+
@State private var updatingTitleFromServer: Bool = false
9+
@State private var updatingBodyFromServer: Bool = false
810

911
var body: some View {
1012
VStack {
@@ -15,9 +17,12 @@ struct PostEditorView: View {
1517
.padding(.bottom)
1618
.font(.custom("OpenSans-Regular", size: 26, relativeTo: Font.TextStyle.largeTitle))
1719
.onChange(of: post.title) { _ in
18-
if post.status == PostStatus.published.rawValue {
20+
if post.status == PostStatus.published.rawValue && !updatingTitleFromServer {
1921
post.status = PostStatus.edited.rawValue
2022
}
23+
if updatingTitleFromServer {
24+
updatingTitleFromServer = false
25+
}
2126
}
2227
ZStack(alignment: .topLeading) {
2328
if post.body.count == 0 {
@@ -31,9 +36,12 @@ struct PostEditorView: View {
3136
.font(.custom("OpenSans-Regular", size: 17, relativeTo: Font.TextStyle.body))
3237
.opacity(post.body.count == 0 && !isHovering ? 0.0 : 1.0)
3338
.onChange(of: post.body) { _ in
34-
if post.status == PostStatus.published.rawValue {
39+
if post.status == PostStatus.published.rawValue && !updatingBodyFromServer {
3540
post.status = PostStatus.edited.rawValue
3641
}
42+
if updatingBodyFromServer {
43+
updatingBodyFromServer = false
44+
}
3745
}
3846
.onHover(perform: { hovering in
3947
self.isHovering = hovering
@@ -46,9 +54,12 @@ struct PostEditorView: View {
4654
.padding(.bottom)
4755
.font(.custom("Hack", size: 26, relativeTo: Font.TextStyle.largeTitle))
4856
.onChange(of: post.title) { _ in
49-
if post.status == PostStatus.published.rawValue {
57+
if post.status == PostStatus.published.rawValue && !updatingTitleFromServer {
5058
post.status = PostStatus.edited.rawValue
5159
}
60+
if updatingTitleFromServer {
61+
updatingTitleFromServer = false
62+
}
5263
}
5364
ZStack(alignment: .topLeading) {
5465
if post.body.count == 0 {
@@ -62,9 +73,12 @@ struct PostEditorView: View {
6273
.font(.custom("Hack", size: 17, relativeTo: Font.TextStyle.body))
6374
.opacity(post.body.count == 0 && !isHovering ? 0.0 : 1.0)
6475
.onChange(of: post.body) { _ in
65-
if post.status == PostStatus.published.rawValue {
76+
if post.status == PostStatus.published.rawValue && !updatingBodyFromServer {
6677
post.status = PostStatus.edited.rawValue
6778
}
79+
if updatingBodyFromServer {
80+
updatingBodyFromServer = false
81+
}
6882
}
6983
.onHover(perform: { hovering in
7084
self.isHovering = hovering
@@ -77,9 +91,12 @@ struct PostEditorView: View {
7791
.padding(.bottom)
7892
.font(.custom("Lora", size: 26, relativeTo: Font.TextStyle.largeTitle))
7993
.onChange(of: post.title) { _ in
80-
if post.status == PostStatus.published.rawValue {
94+
if post.status == PostStatus.published.rawValue && !updatingTitleFromServer {
8195
post.status = PostStatus.edited.rawValue
8296
}
97+
if updatingTitleFromServer {
98+
updatingTitleFromServer = false
99+
}
83100
}
84101
ZStack(alignment: .topLeading) {
85102
if post.body.count == 0 {
@@ -93,9 +110,12 @@ struct PostEditorView: View {
93110
.font(.custom("Lora", size: 17, relativeTo: Font.TextStyle.body))
94111
.opacity(post.body.count == 0 && !isHovering ? 0.0 : 1.0)
95112
.onChange(of: post.body) { _ in
96-
if post.status == PostStatus.published.rawValue {
113+
if post.status == PostStatus.published.rawValue && !updatingBodyFromServer {
97114
post.status = PostStatus.edited.rawValue
98115
}
116+
if updatingBodyFromServer {
117+
updatingBodyFromServer = false
118+
}
99119
}
100120
.onHover(perform: { hovering in
101121
self.isHovering = hovering

0 commit comments

Comments
 (0)