Skip to content

Commit

Permalink
Merge pull request #96 from writeas/add-collection-setting-menu
Browse files Browse the repository at this point in the history
Add collection setting menu
  • Loading branch information
AngeloStavrow authored Oct 12, 2020
2 parents ca6ab8c + 9e48f75 commit 4250e99
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 22 deletions.
25 changes: 25 additions & 0 deletions Shared/Models/WriteFreelyModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ extension WriteFreelyModel {
loggedInClient.getPost(byId: postId, completion: updateFromServerHandler)
}
}

func move(post: WFAPost, from oldCollection: WFACollection?, to newCollection: WFACollection?) {
guard let loggedInClient = client,
let postId = post.postId else { return }

post.collectionAlias = newCollection?.alias
loggedInClient.movePost(postId: postId, to: newCollection?.alias, completion: movePostHandler)
}
}

private extension WriteFreelyModel {
Expand Down Expand Up @@ -383,6 +391,23 @@ private extension WriteFreelyModel {
print(error)
}
}

func movePostHandler(result: Result<Bool, Error>) {
do {
let succeeded = try result.get()
if succeeded {
DispatchQueue.main.async {
LocalStorageManager().saveContext()
self.posts.loadCachedPosts()
}
}
} catch {
DispatchQueue.main.async {
LocalStorageManager.persistentContainer.viewContext.rollback()
}
print(error)
}
}
}

private extension WriteFreelyModel {
Expand Down
10 changes: 6 additions & 4 deletions Shared/PostList/PostListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ struct PostListView: View {
managedPost.title = ""
managedPost.body = ""
managedPost.status = PostStatus.local.rawValue
managedPost.collectionAlias = nil
switch model.preferences.font {
case 1:
managedPost.appearance = "sans"
Expand All @@ -116,11 +117,12 @@ struct PostListView: View {
managedPost.language = languageCode
managedPost.rtl = Locale.characterDirection(forLanguage: languageCode) == .rightToLeft
}
if let selectedCollectionAlias = selectedCollection?.alias {
managedPost.collectionAlias = selectedCollectionAlias
}
DispatchQueue.main.async {
model.selectedPost = managedPost
self.selectedCollection = nil
self.showAllPosts = false
withAnimation {
self.model.selectedPost = managedPost
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion WriteFreely-MultiPlatform.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,7 @@
repositoryURL = "[email protected]:writeas/writefreely-swift.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 0.2.3;
minimumVersion = 0.3.0;
};
};
/* End XCRemoteSwiftPackageReference section */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
<key>WriteFreely-MultiPlatform (iOS).xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
<integer>1</integer>
</dict>
<key>WriteFreely-MultiPlatform (macOS).xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
<integer>0</integer>
</dict>
</dict>
</dict>
Expand Down
105 changes: 90 additions & 15 deletions iOS/PostEditor/PostEditorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@ import SwiftUI

struct PostEditorView: View {
@EnvironmentObject var model: WriteFreelyModel
@Environment(\.managedObjectContext) var moc
@Environment(\.horizontalSizeClass) var horizontalSizeClass
@Environment(\.presentationMode) var presentationMode

@ObservedObject var post: WFAPost
@State private var updatingTitleFromServer: Bool = false
@State private var updatingBodyFromServer: Bool = false

@State private var selectedCollection: WFACollection?

@FetchRequest(
entity: WFACollection.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \WFACollection.title, ascending: true)]
) var collections: FetchedResults<WFACollection>

var body: some View {
VStack {
if post.hasNewerRemoteCopy {
Expand Down Expand Up @@ -143,25 +152,80 @@ struct PostEditorView: View {
ToolbarItem(placement: .principal) {
PostEditorStatusToolbarView(post: post)
}
ToolbarItemGroup(placement: .navigationBarTrailing) {
Button(action: {
if model.account.isLoggedIn {
publishPost()
ToolbarItem(placement: .primaryAction) {
Menu(content: {
if post.status == PostStatus.local.rawValue {
Menu(content: {
Label("Publish to…", systemImage: "paperplane")
Button(action: {
if model.account.isLoggedIn {
post.collectionAlias = nil
publishPost()
} else {
self.model.isPresentingSettingsView = true
}
}, label: {
Text(" \(model.account.server == "https://write.as" ? "Anonymous" : "Drafts")")
})
ForEach(collections) { collection in
Button(action: {
if model.account.isLoggedIn {
post.collectionAlias = collection.alias
publishPost()
} else {
self.model.isPresentingSettingsView = true
}
}, label: {
Text(" \(collection.title)")
})
}
}, label: {
Label("Publish…", systemImage: "paperplane")
})
} else {
self.model.isPresentingSettingsView = true
Button(action: {
if model.account.isLoggedIn {
publishPost()
} else {
self.model.isPresentingSettingsView = true
}
}, label: {
Label("Publish", systemImage: "paperplane")
})
.disabled(
post.status ==
PostStatus.published.rawValue ||
!model.hasNetworkConnection ||
post.body.count == 0
)
}
Button(action: {
sharePost()
}, label: {
Label("Share", systemImage: "square.and.arrow.up")
})
.disabled(post.postId == nil)
// Button(action: {
// print("Tapped 'Delete...' button")
// }, label: {
// Label("Delete…", systemImage: "trash")
// })
if model.account.isLoggedIn && post.status != PostStatus.local.rawValue {
Section(header: Text("Move To Collection")) {
Label("Move to:", systemImage: "arrowshape.zigzag.right")
Picker(selection: $selectedCollection, label: Text("Move to…")) {
Text(
" \(model.account.server == "https://write.as" ? "Anonymous" : "Drafts")"
).tag(nil as WFACollection?)
ForEach(collections) { collection in
Text(" \(collection.title)").tag(collection as WFACollection?)
}
}
}
}
}, label: {
Image(systemName: "paperplane")
})
.disabled(
post.status == PostStatus.published.rawValue || !model.hasNetworkConnection || post.body.count == 0
)
Button(action: {
sharePost()
}, label: {
Image(systemName: "square.and.arrow.up")
Image(systemName: "ellipsis.circle")
})
.disabled(post.postId == nil)
}
}
.onChange(of: post.hasNewerRemoteCopy, perform: { _ in
Expand All @@ -181,6 +245,17 @@ struct PostEditorView: View {
}
}
})
.onChange(of: selectedCollection, perform: { [selectedCollection] newCollection in
if post.collectionAlias == newCollection?.alias {
return
} else {
post.collectionAlias = newCollection?.alias
model.move(post: post, from: selectedCollection, to: newCollection)
}
})
.onAppear(perform: {
self.selectedCollection = collections.first { $0.alias == post.collectionAlias }
})
.onDisappear(perform: {
if post.title.count == 0
&& post.body.count == 0
Expand Down

0 comments on commit 4250e99

Please sign in to comment.