-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add PostAuthorPicker #24621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add PostAuthorPicker #24621
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 0 additions & 78 deletions
78
WordPress/Classes/ViewRelated/Post/PostAuthorSelectorViewController.swift
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
WordPress/Classes/ViewRelated/Post/Views/PostAuthorPicker.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import SwiftUI | ||
| import WordPressData | ||
| import WordPressUI | ||
| import WordPressShared | ||
|
|
||
| struct PostAuthorPicker: View { | ||
| @StateObject private var viewModel: PostAuthorPickerViewModel | ||
| @State private var searchText = "" | ||
| @Environment(\.dismiss) private var dismiss | ||
|
|
||
| init(post: AbstractPost, onSelection: @escaping () -> Void) { | ||
| _viewModel = StateObject(wrappedValue: PostAuthorPickerViewModel(post: post, onSelection: onSelection)) | ||
| } | ||
|
|
||
| var body: some View { | ||
| List { | ||
| ForEach(filteredAuthors) { author in | ||
| Button { | ||
| viewModel.selectAuthor(author) | ||
| dismiss() | ||
| } label: { | ||
| AuthorRow(author: author, isSelected: viewModel.isSelected(author)) | ||
| } | ||
| .buttonStyle(.plain) | ||
| } | ||
| } | ||
| .environment(\.defaultMinListRowHeight, 54) | ||
| .listStyle(.plain) | ||
| .searchable(text: $searchText) | ||
| .navigationTitle(Strings.title) | ||
| .navigationBarTitleDisplayMode(.inline) | ||
| } | ||
|
|
||
| private var filteredAuthors: [PostAuthorPickerViewModel.AuthorItem] { | ||
| if searchText.isEmpty { | ||
| return viewModel.authors | ||
| } | ||
| return viewModel.authors.search(searchText) { author in | ||
| // Combine display name and username for search | ||
| var searchableText = author.displayName | ||
| if let username = author.username { | ||
| searchableText += " @\(username)" | ||
| } | ||
| return searchableText | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private struct AuthorRow: View { | ||
| let author: PostAuthorPickerViewModel.AuthorItem | ||
| let isSelected: Bool | ||
|
|
||
| var body: some View { | ||
| HStack(spacing: 12) { | ||
| AvatarView(style: .single(author.avatarURL), diameter: 36) | ||
|
|
||
| VStack(alignment: .leading) { | ||
| Text(author.displayName) | ||
| .font(.callout.weight(.medium)) | ||
|
|
||
| if let username = author.username { | ||
| Text("@\(username)") | ||
| .font(.footnote) | ||
| .foregroundColor(.secondary) | ||
| } | ||
| } | ||
| .lineLimit(1) | ||
|
|
||
| Spacer() | ||
|
|
||
| if isSelected { | ||
| Image(systemName: "checkmark") | ||
| .foregroundColor(.accentColor) | ||
| .fontWeight(.medium) | ||
| } | ||
| } | ||
| .contentShape(Rectangle()) | ||
| } | ||
| } | ||
|
|
||
| private enum Strings { | ||
| static let title = NSLocalizedString( | ||
| "postAuthorPicker.title", | ||
| value: "Author", | ||
| comment: "Title for the post author selection screen" | ||
| ) | ||
| } | ||
57 changes: 57 additions & 0 deletions
57
WordPress/Classes/ViewRelated/Post/Views/PostAuthorPickerViewModel.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import Foundation | ||
| import WordPressData | ||
| import Combine | ||
|
|
||
| @MainActor | ||
| final class PostAuthorPickerViewModel: ObservableObject { | ||
| struct AuthorItem: Identifiable { | ||
| let id: NSNumber | ||
| let displayName: String | ||
| let username: String? | ||
| let avatarURL: URL? | ||
|
|
||
| init(from blogAuthor: BlogAuthor) { | ||
| self.id = blogAuthor.userID | ||
| self.displayName = blogAuthor.displayName ?? "" | ||
| self.username = blogAuthor.username | ||
| self.avatarURL = blogAuthor.avatarURL.flatMap { URL(string: $0) } | ||
| } | ||
| } | ||
|
|
||
| @Published private(set) var authors: [AuthorItem] = [] | ||
|
|
||
| private let post: AbstractPost | ||
| private let onSelection: () -> Void | ||
| private let currentAuthorID: NSNumber? | ||
|
|
||
| init(post: AbstractPost, onSelection: @escaping () -> Void) { | ||
| self.post = post | ||
| self.onSelection = onSelection | ||
| self.currentAuthorID = post.authorID | ||
|
|
||
| loadAuthors() | ||
| } | ||
|
|
||
| func selectAuthor(_ author: AuthorItem) { | ||
| guard !post.isFault, post.managedObjectContext != nil else { return } | ||
|
|
||
| post.authorID = author.id | ||
| post.author = author.displayName | ||
| post.authorAvatarURL = author.avatarURL?.absoluteString | ||
|
|
||
| onSelection() | ||
| } | ||
|
|
||
| func isSelected(_ author: AuthorItem) -> Bool { | ||
| author.id == currentAuthorID | ||
| } | ||
|
|
||
| private func loadAuthors() { | ||
| authors = (post.blog.authors ?? []) | ||
| .filter { !$0.deletedFromBlog } | ||
| .map(AuthorItem.init) | ||
| .sorted { | ||
| $0.displayName.localizedCaseInsensitiveCompare($1.displayName) == .orderedAscending | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I presume this is generated by AI? I feel bad about annoying you with the same comments. Any chance we can tell AI not to generate code like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should rethink its usage. It's a standard officially documented pattern and there are only a few scenarios where it can lead to issues. It's too convenient not to use because it's the only good way to inject observable state into a view and have it retain it (state/stateObject). In this case, it's not issue because it's a new screen every time and a new view identify, to it rests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are multiple precedents in the codebase where
StateObject(wrappedValue:)is used. I'd continue using it where it's OK – if it's part of the new screen it's fine.