-
Notifications
You must be signed in to change notification settings - Fork 28
feat: account switcher #75
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // AccountsListResponse.swift | ||
| // Kaset | ||
| // | ||
| // Created for account switcher feature. | ||
|
|
||
| import Foundation | ||
|
|
||
| /// Response containing the list of available user accounts. | ||
| /// | ||
| /// This struct represents the parsed response from the YouTube Music accounts list API, | ||
| /// containing all accounts (primary and brand) associated with the authenticated user. | ||
| /// | ||
| /// ## Usage | ||
| /// ```swift | ||
| /// let response = AccountsListResponse( | ||
| /// googleEmail: "user@gmail.com", | ||
| /// accounts: [primaryAccount, brandAccount1, brandAccount2] | ||
| /// ) | ||
| /// | ||
| /// if let selected = response.selectedAccount { | ||
| /// DiagnosticsLogger.log("Current account: \(selected.name)") | ||
| /// } | ||
| /// | ||
| /// if response.hasMultipleAccounts { | ||
| /// // Show account switcher UI | ||
| /// } | ||
| /// ``` | ||
| public struct AccountsListResponse: Sendable { | ||
| // MARK: - Properties | ||
|
|
||
| /// The Google email address associated with the primary account. | ||
| /// Extracted from the response header. | ||
| public let googleEmail: String? | ||
|
|
||
| /// All available accounts (primary and brand accounts). | ||
| public let accounts: [UserAccount] | ||
|
|
||
| // MARK: - Computed Properties | ||
|
|
||
| /// The currently selected/active account. | ||
| /// Returns the first account where `isSelected` is true, or nil if none selected. | ||
| public var selectedAccount: UserAccount? { | ||
| self.accounts.first { $0.isSelected } | ||
| } | ||
|
|
||
| /// Whether multiple accounts are available for switching. | ||
| /// Returns `true` if more than one account exists. | ||
| public var hasMultipleAccounts: Bool { | ||
| self.accounts.count > 1 | ||
| } | ||
|
|
||
| // MARK: - Initialization | ||
|
|
||
| /// Creates a new AccountsListResponse. | ||
| /// | ||
| /// - Parameters: | ||
| /// - googleEmail: The Google email from the response header. | ||
| /// - accounts: Array of parsed UserAccount objects. | ||
| public init(googleEmail: String?, accounts: [UserAccount]) { | ||
| self.googleEmail = googleEmail | ||
| self.accounts = accounts | ||
| } | ||
| } |
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,113 @@ | ||
| // UserAccount.swift | ||
| // Kaset | ||
| // | ||
| // Created for account switcher feature. | ||
|
|
||
| import Foundation | ||
|
|
||
| /// Represents a YouTube Music user account (primary or brand account). | ||
| /// | ||
| /// YouTube Music allows users to have multiple accounts: | ||
| /// - **Primary account**: The main Google account (no brandId) | ||
| /// - **Brand accounts**: Managed channel accounts associated with the primary account | ||
| /// | ||
| /// ## API Response Mapping | ||
| /// - `name`: From `accountName.runs[0].text` | ||
| /// - `handle`: From `channelHandle.runs[0].text` (optional) | ||
| /// - `brandId`: From `serviceEndpoint.selectActiveIdentityEndpoint.supportedTokens[].pageIdToken.pageId` | ||
| /// - `thumbnailURL`: From `accountPhoto.thumbnails[0].url` | ||
| /// - `isSelected`: From `isSelected` | ||
| public struct UserAccount: Identifiable, Equatable, Sendable, Hashable { | ||
| // MARK: - Properties | ||
|
|
||
| /// Unique identifier for the account. | ||
| /// Uses `brandId` for brand accounts, "primary" for the main account. | ||
| public let id: String | ||
|
|
||
| /// Display name of the account. | ||
| public let name: String | ||
|
|
||
| /// Channel handle (e.g., "@username"), if available. | ||
| public let handle: String? | ||
|
|
||
| /// Brand account identifier, nil for primary accounts. | ||
| public let brandId: String? | ||
|
|
||
| /// URL for the account's profile photo thumbnail. | ||
| public let thumbnailURL: URL? | ||
|
|
||
| /// Whether this account is currently selected/active. | ||
| public let isSelected: Bool | ||
|
|
||
| // MARK: - Computed Properties | ||
|
|
||
| /// Returns `true` if this is the primary Google account (not a brand account). | ||
| public var isPrimary: Bool { | ||
| self.brandId == nil | ||
| } | ||
|
|
||
| /// Human-readable label for the account type. | ||
| /// Returns "Personal" for primary accounts, "Brand" for brand accounts. | ||
| public var typeLabel: String { | ||
| self.isPrimary ? "Personal" : "Brand" | ||
| } | ||
|
|
||
| // MARK: - Initialization | ||
|
|
||
| /// Creates a new UserAccount instance. | ||
| /// | ||
| /// - Parameters: | ||
| /// - id: Unique identifier for the account. | ||
| /// - name: Display name of the account. | ||
| /// - handle: Optional channel handle. | ||
| /// - brandId: Brand account identifier (nil for primary). | ||
| /// - thumbnailURL: URL for the profile photo. | ||
| /// - isSelected: Whether the account is currently active. | ||
| public init( | ||
| id: String, | ||
| name: String, | ||
| handle: String?, | ||
| brandId: String?, | ||
| thumbnailURL: URL?, | ||
| isSelected: Bool | ||
| ) { | ||
| self.id = id | ||
| self.name = name | ||
| self.handle = handle | ||
| self.brandId = brandId | ||
| self.thumbnailURL = thumbnailURL | ||
| self.isSelected = isSelected | ||
| } | ||
|
|
||
| // MARK: - Factory Methods | ||
|
|
||
| /// Creates a UserAccount from API response fields. | ||
| /// | ||
| /// This factory method automatically determines the account ID based on whether | ||
| /// a brandId is provided. | ||
| /// | ||
| /// - Parameters: | ||
| /// - name: Display name from `accountName.runs[0].text`. | ||
| /// - handle: Optional handle from `channelHandle.runs[0].text`. | ||
| /// - brandId: Brand ID from `pageIdToken.pageId`, nil for primary account. | ||
| /// - thumbnailURL: Thumbnail URL from `accountPhoto.thumbnails[0].url`. | ||
| /// - isSelected: Selection state from `isSelected`. | ||
| /// - Returns: A configured UserAccount instance. | ||
| public static func from( | ||
| name: String, | ||
| handle: String?, | ||
| brandId: String?, | ||
| thumbnailURL: URL?, | ||
| isSelected: Bool | ||
| ) -> UserAccount { | ||
| let accountId = brandId ?? "primary" | ||
| return UserAccount( | ||
| id: accountId, | ||
| name: name, | ||
| handle: handle, | ||
| brandId: brandId, | ||
| thumbnailURL: thumbnailURL, | ||
| isSelected: isSelected | ||
| ) | ||
| } | ||
| } |
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.
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.
Appending brandId as raw UTF-8 bytes to the hash data could cause collisions if endpoint/body combinations produce JSON that ends with bytes matching a brandId prefix. Consider using a separator or structured format (e.g., JSON with explicit brandId field) to ensure unambiguous cache key generation.