-
Notifications
You must be signed in to change notification settings - Fork 5.1k
SwiftUI thread rows: copy thread metadata #8622
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
Open
saphid
wants to merge
6
commits into
pingdotgg:t3code/rebuild-mobile-app-swift
Choose a base branch
from
saphid:swiftui/pr-140-thread-copy-actions
base: t3code/rebuild-mobile-app-swift
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+508
−4
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
83752c3
fix(swift-ios): split thread copy actions
saphid 05a8d68
fix(swift-ios): keep thread copy actions individual
saphid c547c85
fix(swift-ios): omit placeholder project copy action
saphid 41cc809
fix(swift-ios): normalize thread environment identity
saphid e90618a
fix(swift-ios): correct environment fallback fixture
saphid 2cc5a7e
fix(swift-ios): match electron copy submenu
saphid 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
144 changes: 144 additions & 0 deletions
144
apps/swift-ios/Features/Workspace/ThreadCopyActions.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,144 @@ | ||
| import Foundation | ||
| import UIKit | ||
|
|
||
| enum ThreadCopyActionKind: Equatable, Sendable { | ||
| case path | ||
| case branch | ||
| case threadID | ||
| case project | ||
| case environment | ||
| case url | ||
|
|
||
| var title: String { | ||
| switch self { | ||
| case .path: "Path" | ||
| case .branch: "Branch" | ||
| case .threadID: "Thread ID" | ||
| case .project: "Project" | ||
| case .environment: "Environment" | ||
| case .url: "URL" | ||
| } | ||
| } | ||
|
|
||
| var systemImage: String { | ||
| switch self { | ||
| case .path: "folder" | ||
| case .branch: "arrow.triangle.branch" | ||
| case .threadID: "number" | ||
| case .project: "folder.badge.gearshape" | ||
| case .environment: "desktopcomputer" | ||
| case .url: "link" | ||
| } | ||
| } | ||
|
|
||
| var copyAnnouncement: String { "\(title) copied" } | ||
| } | ||
|
|
||
| struct ThreadCopyContext: Equatable, Sendable { | ||
| let projectName: String? | ||
| let projectWorkspaceRoot: String? | ||
| let environmentName: String? | ||
| let environmentID: String? | ||
| } | ||
|
|
||
| struct ThreadCopyAction: Equatable, Sendable { | ||
| let kind: ThreadCopyActionKind | ||
| let value: String? | ||
|
|
||
| var isAvailable: Bool { value != nil } | ||
|
|
||
| var announcement: String { | ||
| isAvailable ? kind.copyAnnouncement : "\(kind.title) unavailable" | ||
| } | ||
| } | ||
|
|
||
| enum ThreadCopyModel { | ||
| /// The long-press row menu mirrors the Electron thread menu. | ||
| static func menuActions( | ||
| for thread: FeatureThread, | ||
| context: ThreadCopyContext | ||
| ) -> [ThreadCopyAction] { | ||
| actions(for: thread, context: context).filter { action in | ||
| switch action.kind { | ||
| case .path, .branch, .threadID: | ||
| true | ||
| case .project, .environment, .url: | ||
| false | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static func actions( | ||
| for thread: FeatureThread, | ||
| context: ThreadCopyContext | ||
| ) -> [ThreadCopyAction] { | ||
| var actions: [ThreadCopyAction] = [] | ||
|
|
||
| let path = nonBlank(thread.worktreePath) ?? nonBlank(context.projectWorkspaceRoot) | ||
| actions.append(ThreadCopyAction(kind: .path, value: path)) | ||
| if let branch = nonBlank(thread.branch) { | ||
| actions.append(ThreadCopyAction(kind: .branch, value: branch)) | ||
| } | ||
| if let threadID = nonBlank(thread.wireID) ?? nonBlank(thread.id) { | ||
| actions.append(ThreadCopyAction(kind: .threadID, value: threadID)) | ||
| } | ||
| if let project = nonBlank(context.projectName) { | ||
| actions.append(ThreadCopyAction(kind: .project, value: project)) | ||
| } | ||
| if let environment = nonBlank(context.environmentName) { | ||
| actions.append(ThreadCopyAction(kind: .environment, value: environment)) | ||
| } | ||
|
|
||
| let environmentID = nonBlank(thread.environmentID) ?? nonBlank(context.environmentID) | ||
| if let url = threadURL(environmentID: environmentID, threadID: nonBlank(thread.wireID)) { | ||
| actions.append(ThreadCopyAction(kind: .url, value: url)) | ||
| } | ||
|
|
||
| return actions | ||
| } | ||
|
|
||
| private static func threadURL(environmentID: String?, threadID: String?) -> String? { | ||
| guard let environmentID, | ||
| let threadID, | ||
| let encodedEnvironmentID = pathSegment(environmentID), | ||
| let encodedThreadID = pathSegment(threadID) else { | ||
| return nil | ||
| } | ||
|
|
||
| var components = URLComponents() | ||
| components.scheme = "https" | ||
| components.host = "app.t3.codes" | ||
| components.percentEncodedPath = "/\(encodedEnvironmentID)/\(encodedThreadID)" | ||
| return components.url?.absoluteString | ||
| } | ||
|
|
||
| private static func pathSegment(_ value: String) -> String? { | ||
| let allowed = CharacterSet( | ||
| charactersIn: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~" | ||
| ) | ||
| return value.addingPercentEncoding(withAllowedCharacters: allowed) | ||
| } | ||
|
|
||
| /// Availability ignores surrounding whitespace, while the copied value remains byte-for-byte | ||
| /// identical to the value received from the server. | ||
| private static func nonBlank(_ value: String?) -> String? { | ||
| guard let value, | ||
| !value.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { | ||
| return nil | ||
| } | ||
| return value | ||
| } | ||
| } | ||
|
|
||
| @MainActor | ||
| enum ThreadCopyClipboard { | ||
| static func copy(_ action: ThreadCopyAction) { | ||
| if let value = action.value { | ||
| UIPasteboard.general.string = value | ||
| } | ||
| UIAccessibility.post( | ||
| notification: .announcement, | ||
| argument: action.announcement | ||
| ) | ||
| } | ||
| } | ||
|
macroscopeapp[bot] marked this conversation as resolved.
|
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.