Skip to content

Commit 4250e99

Browse files
Merge pull request #96 from writeas/add-collection-setting-menu
Add collection setting menu
2 parents ca6ab8c + 9e48f75 commit 4250e99

File tree

5 files changed

+124
-22
lines changed

5 files changed

+124
-22
lines changed

Shared/Models/WriteFreelyModel.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,14 @@ extension WriteFreelyModel {
178178
loggedInClient.getPost(byId: postId, completion: updateFromServerHandler)
179179
}
180180
}
181+
182+
func move(post: WFAPost, from oldCollection: WFACollection?, to newCollection: WFACollection?) {
183+
guard let loggedInClient = client,
184+
let postId = post.postId else { return }
185+
186+
post.collectionAlias = newCollection?.alias
187+
loggedInClient.movePost(postId: postId, to: newCollection?.alias, completion: movePostHandler)
188+
}
181189
}
182190

183191
private extension WriteFreelyModel {
@@ -383,6 +391,23 @@ private extension WriteFreelyModel {
383391
print(error)
384392
}
385393
}
394+
395+
func movePostHandler(result: Result<Bool, Error>) {
396+
do {
397+
let succeeded = try result.get()
398+
if succeeded {
399+
DispatchQueue.main.async {
400+
LocalStorageManager().saveContext()
401+
self.posts.loadCachedPosts()
402+
}
403+
}
404+
} catch {
405+
DispatchQueue.main.async {
406+
LocalStorageManager.persistentContainer.viewContext.rollback()
407+
}
408+
print(error)
409+
}
410+
}
386411
}
387412

388413
private extension WriteFreelyModel {

Shared/PostList/PostListView.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ struct PostListView: View {
104104
managedPost.title = ""
105105
managedPost.body = ""
106106
managedPost.status = PostStatus.local.rawValue
107+
managedPost.collectionAlias = nil
107108
switch model.preferences.font {
108109
case 1:
109110
managedPost.appearance = "sans"
@@ -116,11 +117,12 @@ struct PostListView: View {
116117
managedPost.language = languageCode
117118
managedPost.rtl = Locale.characterDirection(forLanguage: languageCode) == .rightToLeft
118119
}
119-
if let selectedCollectionAlias = selectedCollection?.alias {
120-
managedPost.collectionAlias = selectedCollectionAlias
121-
}
122120
DispatchQueue.main.async {
123-
model.selectedPost = managedPost
121+
self.selectedCollection = nil
122+
self.showAllPosts = false
123+
withAnimation {
124+
self.model.selectedPost = managedPost
125+
}
124126
}
125127
}
126128
}

WriteFreely-MultiPlatform.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,7 @@
11411141
repositoryURL = "[email protected]:writeas/writefreely-swift.git";
11421142
requirement = {
11431143
kind = upToNextMajorVersion;
1144-
minimumVersion = 0.2.3;
1144+
minimumVersion = 0.3.0;
11451145
};
11461146
};
11471147
/* End XCRemoteSwiftPackageReference section */

WriteFreely-MultiPlatform.xcodeproj/xcuserdata/angelo.xcuserdatad/xcschemes/xcschememanagement.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
<key>WriteFreely-MultiPlatform (iOS).xcscheme_^#shared#^_</key>
88
<dict>
99
<key>orderHint</key>
10-
<integer>0</integer>
10+
<integer>1</integer>
1111
</dict>
1212
<key>WriteFreely-MultiPlatform (macOS).xcscheme_^#shared#^_</key>
1313
<dict>
1414
<key>orderHint</key>
15-
<integer>1</integer>
15+
<integer>0</integer>
1616
</dict>
1717
</dict>
1818
</dict>

iOS/PostEditor/PostEditorView.swift

Lines changed: 90 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@ import SwiftUI
22

33
struct PostEditorView: View {
44
@EnvironmentObject var model: WriteFreelyModel
5+
@Environment(\.managedObjectContext) var moc
56
@Environment(\.horizontalSizeClass) var horizontalSizeClass
67
@Environment(\.presentationMode) var presentationMode
8+
79
@ObservedObject var post: WFAPost
810
@State private var updatingTitleFromServer: Bool = false
911
@State private var updatingBodyFromServer: Bool = false
1012

13+
@State private var selectedCollection: WFACollection?
14+
15+
@FetchRequest(
16+
entity: WFACollection.entity(),
17+
sortDescriptors: [NSSortDescriptor(keyPath: \WFACollection.title, ascending: true)]
18+
) var collections: FetchedResults<WFACollection>
19+
1120
var body: some View {
1221
VStack {
1322
if post.hasNewerRemoteCopy {
@@ -143,25 +152,80 @@ struct PostEditorView: View {
143152
ToolbarItem(placement: .principal) {
144153
PostEditorStatusToolbarView(post: post)
145154
}
146-
ToolbarItemGroup(placement: .navigationBarTrailing) {
147-
Button(action: {
148-
if model.account.isLoggedIn {
149-
publishPost()
155+
ToolbarItem(placement: .primaryAction) {
156+
Menu(content: {
157+
if post.status == PostStatus.local.rawValue {
158+
Menu(content: {
159+
Label("Publish to…", systemImage: "paperplane")
160+
Button(action: {
161+
if model.account.isLoggedIn {
162+
post.collectionAlias = nil
163+
publishPost()
164+
} else {
165+
self.model.isPresentingSettingsView = true
166+
}
167+
}, label: {
168+
Text(" \(model.account.server == "https://write.as" ? "Anonymous" : "Drafts")")
169+
})
170+
ForEach(collections) { collection in
171+
Button(action: {
172+
if model.account.isLoggedIn {
173+
post.collectionAlias = collection.alias
174+
publishPost()
175+
} else {
176+
self.model.isPresentingSettingsView = true
177+
}
178+
}, label: {
179+
Text(" \(collection.title)")
180+
})
181+
}
182+
}, label: {
183+
Label("Publish…", systemImage: "paperplane")
184+
})
150185
} else {
151-
self.model.isPresentingSettingsView = true
186+
Button(action: {
187+
if model.account.isLoggedIn {
188+
publishPost()
189+
} else {
190+
self.model.isPresentingSettingsView = true
191+
}
192+
}, label: {
193+
Label("Publish", systemImage: "paperplane")
194+
})
195+
.disabled(
196+
post.status ==
197+
PostStatus.published.rawValue ||
198+
!model.hasNetworkConnection ||
199+
post.body.count == 0
200+
)
201+
}
202+
Button(action: {
203+
sharePost()
204+
}, label: {
205+
Label("Share", systemImage: "square.and.arrow.up")
206+
})
207+
.disabled(post.postId == nil)
208+
// Button(action: {
209+
// print("Tapped 'Delete...' button")
210+
// }, label: {
211+
// Label("Delete…", systemImage: "trash")
212+
// })
213+
if model.account.isLoggedIn && post.status != PostStatus.local.rawValue {
214+
Section(header: Text("Move To Collection")) {
215+
Label("Move to:", systemImage: "arrowshape.zigzag.right")
216+
Picker(selection: $selectedCollection, label: Text("Move to…")) {
217+
Text(
218+
" \(model.account.server == "https://write.as" ? "Anonymous" : "Drafts")"
219+
).tag(nil as WFACollection?)
220+
ForEach(collections) { collection in
221+
Text(" \(collection.title)").tag(collection as WFACollection?)
222+
}
223+
}
224+
}
152225
}
153226
}, label: {
154-
Image(systemName: "paperplane")
155-
})
156-
.disabled(
157-
post.status == PostStatus.published.rawValue || !model.hasNetworkConnection || post.body.count == 0
158-
)
159-
Button(action: {
160-
sharePost()
161-
}, label: {
162-
Image(systemName: "square.and.arrow.up")
227+
Image(systemName: "ellipsis.circle")
163228
})
164-
.disabled(post.postId == nil)
165229
}
166230
}
167231
.onChange(of: post.hasNewerRemoteCopy, perform: { _ in
@@ -181,6 +245,17 @@ struct PostEditorView: View {
181245
}
182246
}
183247
})
248+
.onChange(of: selectedCollection, perform: { [selectedCollection] newCollection in
249+
if post.collectionAlias == newCollection?.alias {
250+
return
251+
} else {
252+
post.collectionAlias = newCollection?.alias
253+
model.move(post: post, from: selectedCollection, to: newCollection)
254+
}
255+
})
256+
.onAppear(perform: {
257+
self.selectedCollection = collections.first { $0.alias == post.collectionAlias }
258+
})
184259
.onDisappear(perform: {
185260
if post.title.count == 0
186261
&& post.body.count == 0

0 commit comments

Comments
 (0)