@@ -16,7 +16,7 @@ pub struct PlaybackStatus {
1616}
1717
1818enum AudioCommand {
19- Load ( String , Option < i64 > , Sender < Result < TrackInfo , String > > ) , // Add track_id
19+ Load ( String , Option < i64 > , Sender < Result < TrackInfo , String > > ) , // Add track_id
2020 LoadAndPlay ( String , Option < i64 > , Sender < Result < TrackInfo , String > > ) ,
2121 Play ( Sender < Result < ( ) , String > > ) ,
2222 Pause ( Sender < Result < ( ) , String > > ) ,
@@ -176,53 +176,57 @@ fn audio_thread(rx: Receiver<AudioCommand>, app: AppHandle) {
176176
177177 // Check play count threshold (75%)
178178 if !play_count_state. threshold_reached
179- && progress. duration_ms > 0
180- && play_count_state. track_id . is_some ( ) {
179+ && progress. duration_ms > 0
180+ && play_count_state. track_id . is_some ( )
181+ {
181182 let ratio = progress. position_ms as f64 / progress. duration_ms as f64 ;
182183
183184 if ratio >= 0.75
184- && let Some ( track_id) = play_count_state. track_id {
185- // Spawn async task to avoid blocking audio thread
186- let app_handle = app. clone ( ) ;
187- std:: thread:: spawn ( move || {
188- use crate :: db:: Database ;
189- use crate :: db:: library;
190-
191- let db = app_handle. state :: < Database > ( ) ;
192- if let Ok ( conn) = db. conn ( ) {
193- let _ = library:: update_play_count ( & conn, track_id) ;
194- debug ! ( track_id, "Play count updated" ) ;
195- }
196- } ) ;
197- play_count_state. threshold_reached = true ;
198- }
185+ && let Some ( track_id) = play_count_state. track_id
186+ {
187+ // Spawn async task to avoid blocking audio thread
188+ let app_handle = app. clone ( ) ;
189+ std:: thread:: spawn ( move || {
190+ use crate :: db:: Database ;
191+ use crate :: db:: library;
192+
193+ let db = app_handle. state :: < Database > ( ) ;
194+ if let Ok ( conn) = db. conn ( ) {
195+ let _ = library:: update_play_count ( & conn, track_id) ;
196+ debug ! ( track_id, "Play count updated" ) ;
197+ }
198+ } ) ;
199+ play_count_state. threshold_reached = true ;
200+ }
199201 }
200202
201203 // Check scrobble threshold (90% default, configurable)
202204 if !scrobble_state. threshold_reached
203- && progress. duration_ms > 0
204- && scrobble_state. track_id . is_some ( ) {
205+ && progress. duration_ms > 0
206+ && scrobble_state. track_id . is_some ( )
207+ {
205208 let ratio = progress. position_ms as f64 / progress. duration_ms as f64 ;
206209
207210 if ratio >= scrobble_state. threshold_percent
208- && let Some ( track_id) = scrobble_state. track_id {
209- // Spawn async task to avoid blocking audio thread
210- let app_handle = app . clone ( ) ;
211- std :: thread :: spawn ( move || {
212- use crate :: commands :: lastfm ;
213- use crate :: db :: Database ;
214-
215- let db = app_handle . state :: < Database > ( ) ;
216- if let Ok ( conn ) = db . conn ( ) {
217- // Queue scrobble from audio thread
218- match lastfm :: scrobble_from_audio_thread ( & app_handle , & conn , track_id ) {
219- Ok ( _ ) => debug ! ( track_id , "Scrobble queued" ) ,
220- Err ( e ) => error ! ( track_id, error = %e , "Failed to queue scrobble ") ,
221- }
211+ && let Some ( track_id) = scrobble_state. track_id
212+ {
213+ // Spawn async task to avoid blocking audio thread
214+ let app_handle = app . clone ( ) ;
215+ std :: thread :: spawn ( move || {
216+ use crate :: commands :: lastfm ;
217+ use crate :: db :: Database ;
218+
219+ let db = app_handle . state :: < Database > ( ) ;
220+ if let Ok ( conn ) = db . conn ( ) {
221+ // Queue scrobble from audio thread
222+ match lastfm :: scrobble_from_audio_thread ( & app_handle , & conn , track_id ) {
223+ Ok ( _ ) => debug ! ( track_id, "Scrobble queued ") ,
224+ Err ( e ) => error ! ( track_id , error = %e , "Failed to queue scrobble" ) ,
222225 }
223- } ) ;
224- scrobble_state. threshold_reached = true ;
225- }
226+ }
227+ } ) ;
228+ scrobble_state. threshold_reached = true ;
229+ }
226230 }
227231 }
228232
@@ -235,15 +239,23 @@ fn audio_thread(rx: Receiver<AudioCommand>, app: AppHandle) {
235239
236240#[ tracing:: instrument( skip( state) ) ]
237241#[ tauri:: command]
238- pub fn audio_load ( path : String , track_id : Option < i64 > , state : State < AudioState > ) -> Result < TrackInfo , String > {
242+ pub fn audio_load (
243+ path : String ,
244+ track_id : Option < i64 > ,
245+ state : State < AudioState > ,
246+ ) -> Result < TrackInfo , String > {
239247 let ( tx, rx) = mpsc:: channel ( ) ;
240248 state. send_command ( AudioCommand :: Load ( path, track_id, tx) ) ;
241249 rx. recv ( ) . map_err ( |_| "Channel closed" . to_string ( ) ) ?
242250}
243251
244252#[ tracing:: instrument( skip( state) ) ]
245253#[ tauri:: command]
246- pub fn audio_load_and_play ( path : String , track_id : Option < i64 > , state : State < AudioState > ) -> Result < TrackInfo , String > {
254+ pub fn audio_load_and_play (
255+ path : String ,
256+ track_id : Option < i64 > ,
257+ state : State < AudioState > ,
258+ ) -> Result < TrackInfo , String > {
247259 let ( tx, rx) = mpsc:: channel ( ) ;
248260 state. send_command ( AudioCommand :: LoadAndPlay ( path, track_id, tx) ) ;
249261 rx. recv ( ) . map_err ( |_| "Channel closed" . to_string ( ) ) ?
0 commit comments