Skip to content

Commit a34a70f

Browse files
committed
Remove force-unwrapping of optionals in generating shared URL string
1 parent 0a68aad commit a34a70f

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

iOS/PostEditor/PostEditorView.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,20 @@ struct PostEditorView: View {
190190
}
191191

192192
private func sharePost() {
193-
guard let urlString = model.selectedPost?.slug != nil ?
194-
"\(model.account.server)/\((model.selectedPost?.collectionAlias)!)/\((model.selectedPost?.slug)!)" :
195-
"\(model.account.server)/\((model.selectedPost?.postId)!)" else { return }
193+
// If the post doesn't have a post ID, it isn't published, and therefore can't be shared, so return early.
194+
guard let postId = post.postId else { return }
195+
196+
var urlString: String
197+
198+
if let postSlug = post.slug,
199+
let postCollectionAlias = post.collectionAlias {
200+
// This post is in a collection, so share the URL as server/collectionAlias/postSlug.
201+
urlString = "\(model.account.server)/\((postCollectionAlias))/\((postSlug))"
202+
} else {
203+
// This is a draft post, so share the URL as server/postID
204+
urlString = "\(model.account.server)/\((postId))"
205+
}
206+
196207
guard let data = URL(string: urlString) else { return }
197208

198209
let activityView = UIActivityViewController(activityItems: [data], applicationActivities: nil)

0 commit comments

Comments
 (0)