Skip to content

Commit

Permalink
Validation + error handling + Compact mode
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-venugopal committed Jan 12, 2025
1 parent 45a07d0 commit 2038325
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 14 deletions.
13 changes: 10 additions & 3 deletions Source/Core/TrackIO/TrackReader+Lyrics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ extension TrackReader {
}

// Online search
guard onlineSearchEnabled else {return}
guard onlineSearchEnabled, track.title != nil && track.artist != nil else {return}

Task.detached(priority: immediate ? .userInitiated : .utility) {

Expand Down Expand Up @@ -110,11 +110,18 @@ extension TrackReader {
preferences.metadataPreferences.lyrics.enableOnlineSearch.value
}

func searchForLyricsOnline(for track: Track, uiUpdateBlock: @escaping (TimedLyrics) -> Void) async {
func searchForLyricsOnline(for track: Track, uiUpdateBlock: @escaping (TimedLyrics?) -> Void) async {

Task.detached(priority: .userInitiated) {

guard let bestLyrics = await LyricsSearchService().searchLyrics(for: track) else {return}
guard let bestLyrics = await LyricsSearchService().searchLyrics(for: track) else {

await MainActor.run {
uiUpdateBlock(nil)
}

return
}

let timedLyrics = TimedLyrics(from: bestLyrics, trackDuration: track.duration)
track.metadata.externalTimedLyrics = timedLyrics
Expand Down
72 changes: 64 additions & 8 deletions Source/UI/CompactPlayer/Lyrics/CompactPlayerLyrics.xib
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="CompactPlayerLyricsViewController" customModule="Aural" customModuleProvider="target">
<connections>
<outlet property="btnChooseFile" destination="W9L-ph-9nS" id="VGc-iC-5s6"/>
<outlet property="btnSearchOnline" destination="P9L-2Y-obt" id="TnH-zu-CRJ"/>
<outlet property="imgLyrics" destination="vX7-eY-UTv" id="crU-hZ-pKm"/>
<outlet property="lblCaption" destination="VB1-cw-yTt" id="cuL-fD-oxW"/>
<outlet property="lblDragDrop" destination="D2X-ff-icB" id="riH-Rp-EM7"/>
<outlet property="lblSearching" destination="iT9-Yb-xDR" id="HnS-dd-4kD"/>
<outlet property="prototypeTextField" destination="Aj8-VZ-vO8" id="Nxx-l2-GPd"/>
<outlet property="searchSpinner" destination="81V-Qi-dYv" id="pyR-DW-m4D"/>
<outlet property="tabView" destination="oKq-7S-hY6" id="5Fx-zO-dam"/>
<outlet property="tableVertScroller" destination="bmY-q1-lSZ" id="MNu-5f-ND5"/>
<outlet property="tableView" destination="Uiu-25-iep" id="zqp-rX-gFD"/>
Expand Down Expand Up @@ -198,7 +204,7 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField focusRingType="none" horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="D2X-ff-icB" userLabel="Drop a Lyrics file (.lrc / .lrcx)" customClass="CenterTextLabel" customModule="Aural" customModuleProvider="target">
<rect key="frame" x="100" y="236" width="284" height="25"/>
<rect key="frame" x="8" y="206" width="284" height="25"/>
<constraints>
<constraint firstAttribute="height" constant="25" id="PN0-sk-ZjN"/>
<constraint firstAttribute="width" constant="280" id="UY0-eu-GjJ"/>
Expand All @@ -210,31 +216,37 @@
</textFieldCell>
</textField>
<button translatesAutoresizingMaskIntoConstraints="NO" id="W9L-ph-9nS">
<rect key="frame" x="137" y="199" width="210" height="32"/>
<buttonCell key="cell" type="square" title="Choose a Lyrics file" bezelStyle="shadowlessSquare" image="doc" catalog="system" imagePosition="right" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="d7X-uA-QiG">
<rect key="frame" x="45" y="169" width="210" height="32"/>
<buttonCell key="cell" type="square" title="Choose a Lyrics file" bezelStyle="shadowlessSquare" image="doc" catalog="system" imagePosition="right" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="d7X-uA-QiG" customClass="TextAndImageButtonCell" customModule="Aural" customModuleProvider="target">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<constraints>
<constraint firstAttribute="width" constant="210" id="YAh-V5-0pe"/>
<constraint firstAttribute="height" constant="32" id="xvX-48-3Pf"/>
</constraints>
<connections>
<action selector="loadLyricsButtonAction:" target="-2" id="WHe-NN-UE4"/>
</connections>
</button>
<button translatesAutoresizingMaskIntoConstraints="NO" id="P9L-2Y-obt" userLabel="Search for Lyrics online">
<rect key="frame" x="137" y="157" width="210" height="32"/>
<buttonCell key="cell" type="square" title="Search for Lyrics online" bezelStyle="shadowlessSquare" image="network" catalog="system" imagePosition="right" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="2KK-Pr-HbH">
<rect key="frame" x="45" y="127" width="210" height="32"/>
<buttonCell key="cell" type="square" title="Search for Lyrics online" bezelStyle="shadowlessSquare" image="network" catalog="system" imagePosition="right" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="2KK-Pr-HbH" customClass="TextAndImageButtonCell" customModule="Aural" customModuleProvider="target">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<constraints>
<constraint firstAttribute="height" constant="32" id="KfC-74-eCW"/>
<constraint firstAttribute="width" constant="210" id="SIZ-QX-8l3"/>
</constraints>
<connections>
<action selector="searchForLyricsOnlineButtonAction:" target="-2" id="0iA-yv-EPU"/>
</connections>
</button>
<box boxType="custom" cornerRadius="4" title="Box" translatesAutoresizingMaskIntoConstraints="NO" id="FTK-2U-EGC" userLabel="Drop Box">
<rect key="frame" x="0.0" y="61" width="483" height="288"/>
<box boxType="custom" borderType="none" borderWidth="0.0" title="Box" titlePosition="noTitle" translatesAutoresizingMaskIntoConstraints="NO" id="FTK-2U-EGC" userLabel="Drop Box">
<rect key="frame" x="0.0" y="0.0" width="300" height="349"/>
<view key="contentView" id="yPY-Js-QWZ">
<rect key="frame" x="1" y="1" width="481" height="286"/>
<rect key="frame" x="0.0" y="0.0" width="300" height="349"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
</view>
</box>
Expand All @@ -257,6 +269,49 @@
<view key="view" id="WJa-Hd-10b">
<rect key="frame" x="0.0" y="0.0" width="300" height="349"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<imageView horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="vX7-eY-UTv">
<rect key="frame" x="125" y="147.5" width="50.5" height="57"/>
<constraints>
<constraint firstAttribute="width" constant="50" id="V3r-bO-aaq"/>
<constraint firstAttribute="height" constant="50" id="xpW-mM-trE"/>
</constraints>
<imageCell key="cell" refusesFirstResponder="YES" alignment="left" imageScaling="proportionallyUpOrDown" image="music.mic" catalog="system" id="A8h-jB-XRc"/>
</imageView>
</subviews>
<constraints>
<constraint firstItem="vX7-eY-UTv" firstAttribute="centerX" secondItem="WJa-Hd-10b" secondAttribute="centerX" id="dSQ-ry-Sdd"/>
<constraint firstItem="vX7-eY-UTv" firstAttribute="centerY" secondItem="WJa-Hd-10b" secondAttribute="centerY" id="jKM-9D-cuy"/>
</constraints>
</view>
</tabViewItem>
<tabViewItem label="Item 3" identifier="" id="M7S-O3-P2T" userLabel="Searching">
<view key="view" id="zyC-kh-n2e">
<rect key="frame" x="0.0" y="0.0" width="300" height="349"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField focusRingType="none" horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="iT9-Yb-xDR" userLabel="Drop a Lyrics file (.lrc / .lrcx)" customClass="CenterTextLabel" customModule="Aural" customModuleProvider="target">
<rect key="frame" x="8" y="202" width="284" height="25"/>
<constraints>
<constraint firstAttribute="height" constant="25" id="Tuh-iz-CQn"/>
<constraint firstAttribute="width" constant="280" id="xsQ-5n-IDv"/>
</constraints>
<textFieldCell key="cell" lineBreakMode="clipping" alignment="center" title="Searching for Lyrics online ..." id="juq-qj-6pU">
<font key="font" size="15" name="Play-Regular"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<progressIndicator maxValue="100" indeterminate="YES" style="spinning" translatesAutoresizingMaskIntoConstraints="NO" id="81V-Qi-dYv">
<rect key="frame" x="134" y="159" width="32" height="32"/>
</progressIndicator>
</subviews>
<constraints>
<constraint firstItem="81V-Qi-dYv" firstAttribute="centerY" secondItem="zyC-kh-n2e" secondAttribute="centerY" id="LwS-Qi-RxR"/>
<constraint firstItem="81V-Qi-dYv" firstAttribute="top" secondItem="iT9-Yb-xDR" secondAttribute="bottom" constant="11" id="OhY-Zp-jVz"/>
<constraint firstItem="iT9-Yb-xDR" firstAttribute="centerX" secondItem="zyC-kh-n2e" secondAttribute="centerX" id="YzB-Ia-v46"/>
<constraint firstItem="81V-Qi-dYv" firstAttribute="centerX" secondItem="iT9-Yb-xDR" secondAttribute="centerX" id="dfs-vz-gDJ"/>
</constraints>
</view>
</tabViewItem>
</tabViewItems>
Expand Down Expand Up @@ -284,6 +339,7 @@
</objects>
<resources>
<image name="doc" catalog="system" width="14" height="16"/>
<image name="music.mic" catalog="system" width="16" height="16"/>
<image name="network" catalog="system" width="15" height="15"/>
</resources>
</document>
4 changes: 2 additions & 2 deletions Source/UI/Lyrics/Lyrics.xib
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,10 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<imageView horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="NvJ-F7-AdE">
<rect key="frame" x="202" y="91.5" width="80.5" height="107"/>
<rect key="frame" x="202" y="101.5" width="80.5" height="87"/>
<constraints>
<constraint firstAttribute="width" constant="80" id="mnB-b6-s0M"/>
<constraint firstAttribute="height" constant="100" id="xKI-26-0aj"/>
<constraint firstAttribute="height" constant="80" id="xKI-26-0aj"/>
</constraints>
<imageCell key="cell" refusesFirstResponder="YES" alignment="left" imageScaling="proportionallyUpOrDown" image="music.mic" catalog="system" id="2D1-Q6-bLR"/>
</imageView>
Expand Down
21 changes: 20 additions & 1 deletion Source/UI/Lyrics/LyricsViewController+Timed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,23 @@ extension LyricsViewController {

guard let track else {return}

guard track.title != nil || track.artist != nil else {

NSAlert.showError(withTitle: "Online lyrics search not possible", andText: "The playing track does not have artist/title metadata.")
return
}

tabView.selectTabViewItem(at: 4)
searchSpinner.startAnimation(nil)

let uiUpdateBlock = {(timedLyrics: TimedLyrics) in
let uiUpdateBlock = {(timedLyrics: TimedLyrics?) in

guard let timedLyrics else {

self.tabView.selectTabViewItem(at: 2)
self.searchSpinner.stopAnimation(nil)
return
}

if playbackInfoDelegate.playingTrack == track {

Expand Down Expand Up @@ -192,6 +205,10 @@ extension LyricsViewController: NSTableViewDelegate {

@objc var lineBreakMode: NSLineBreakMode {.byTruncatingTail}

var paragraphStyle: NSMutableParagraphStyle {
lineBreakMode == .byTruncatingTail ? .byTruncatingTail : .byWordWrapping
}

func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
Self.rowHeight
}
Expand Down Expand Up @@ -255,6 +272,8 @@ extension LyricsViewController: NSTableViewDelegate {
mutStr = mutStr + segment.postSegmentContent.attributed(font: systemFontScheme.lyricsHighlightFont, color: systemColorScheme.primarySelectedTextColor)
}

mutStr.addAttribute(.paragraphStyle, value: self.paragraphStyle, range: NSMakeRange(0, mutStr.length))

cell.attributedText = mutStr
}

Expand Down

0 comments on commit 2038325

Please sign in to comment.