-
Notifications
You must be signed in to change notification settings - Fork 2
Add recording picker, improve recording selection logic #35
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
mbernson
wants to merge
3
commits into
main
Choose a base branch
from
choose-recording
base: main
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.
Open
Changes from all commits
Commits
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,87 @@ | ||
| // | ||
| // RecordingChooser.swift | ||
| // HackerTube | ||
| // | ||
| // Created by Mathijs Bernson on 13/01/2026. | ||
| // | ||
|
|
||
| import AVFoundation | ||
| import CCCApi | ||
| import Foundation | ||
|
|
||
| struct RecordingChooser { | ||
| let preferredLanguages: [Locale] | ||
|
|
||
| init(preferredLanguages: [Locale] = { | ||
| if #available(iOS 26, tvOS 26, macOS 26, *) { | ||
| return Locale.preferredLocales | ||
| } else { | ||
| return Locale.preferredLanguages.map(Locale.init(identifier:)) | ||
| } | ||
| }()) { | ||
| self.preferredLanguages = preferredLanguages | ||
| } | ||
|
|
||
| /// Checks whether the app on the current OS can play back the given recording. | ||
| func canPlay(recording: Recording) -> Bool { | ||
| let mimeType = recording.mimeType | ||
|
|
||
| // Ignore any files that are attachments (talk slides) etc. | ||
| if mimeType.starts(with: "application") { | ||
| return false | ||
| } | ||
|
|
||
| // Subtitle file, this cannot be played back | ||
| if mimeType.starts(with: "text") { | ||
| return false | ||
| } | ||
|
|
||
| return AVURLAsset.isPlayableExtendedMIMEType(mimeType) | ||
| } | ||
|
|
||
| /// Selects the best recording to be played, based on the parameters and user preferences. | ||
| func choosePreferredRecording( | ||
| from recordings: [Recording], | ||
| prefersHighQuality: Bool, | ||
| prefersAudio: Bool | ||
| ) -> Recording? { | ||
| return recordings | ||
| // Remove everything that's not playable | ||
| .filter(canPlay) | ||
| // Put the audio versions first, if desired | ||
| .sorted(by: { lhs, rhs in | ||
| if prefersAudio { | ||
| return lhs.isAudio && !rhs.isAudio | ||
| } else { | ||
| return !lhs.isAudio && rhs.isAudio | ||
| } | ||
| }) | ||
| // Sort by language preference | ||
| .sorted { lhs, rhs in | ||
| let l = languageScore(for: lhs) | ||
| let r = languageScore(for: rhs) | ||
| if l.0 != r.0 { return l.0 > r.0 } | ||
| return l.1 < r.1 | ||
| } | ||
| // Put the HD versions first, if desired | ||
| .sorted(by: { lhs, rhs in | ||
| if prefersHighQuality { | ||
| return lhs.isHighQuality && !rhs.isHighQuality | ||
| } else { | ||
| return !lhs.isHighQuality && rhs.isHighQuality | ||
| } | ||
| }) | ||
| .first | ||
| } | ||
|
|
||
| private func languageScore(for recording: Recording) -> (Int, Int) { | ||
| let preferredCodes = preferredLanguages.compactMap { $0.language.languageCode } | ||
| // Normalize ISO 639-2 codes (e.g. "eng") to BCP-47 (e.g. "en") via Locale | ||
| let recordingCodes = recording.language | ||
| .split(separator: "-") | ||
| .compactMap { Locale(identifier: String($0)).language.languageCode } | ||
| let matchCount = recordingCodes.filter { preferredCodes.contains($0) }.count | ||
| let firstMatchIndex = preferredCodes.firstIndex { recordingCodes.contains($0) } ?? Int.max | ||
| return (matchCount, firstMatchIndex) | ||
| } | ||
| } |
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.
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.
Once we have a Settings screen in the app, it should be possible to: