Skip to content

Commit

Permalink
Added lyrics cache to LyricsManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Naapperas committed Apr 15, 2023
1 parent ff89434 commit 322bdcc
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions src/lyrics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::sync::Arc;
use log::trace;
use std::{cell::RefCell, collections::HashMap, sync::Arc};

use crate::{
lyrics_fetcher::LyricsFetcher,
Expand All @@ -10,26 +11,52 @@ use crate::{
pub struct LyricsManager {
queue: Arc<Queue>,
fetcher: LyricsFetcher,
// TODO: add a cache
cache: RefCell<HashMap<String, String>>,
}

impl LyricsManager {
pub fn new(queue: Arc<Queue>, fetcher: LyricsFetcher) -> Self {
LyricsManager { queue, fetcher }
LyricsManager {
queue,
fetcher,
cache: RefCell::new(HashMap::new()),
}
}

/// Saves the given lyrics to the user's filesystem.
///
/// Returns an optional message indicating the outcome of this operation.
pub fn save_lyrics(&self, lyrics: String) -> Option<String> {
Some("".to_string())
Some(lyrics)
}

/// Fetches and returns the lyrics of the given track
pub fn get_lyrics(&self, track: Track) -> String {
// TODO: implement caching
// TODO: see if this panics later on
let track_id = track.id.as_ref().unwrap();

{
// insert new scope so that we can perform both borrows from the RefCell
// the immutable borrow present in this scope is dropped,
// so it is safe to do another borrow after

let cache = self.cache.borrow();

if cache.contains_key(track_id) {
trace!("Retrieving cached lyrics for {}", track.title);
return cache.get(track_id).unwrap().to_owned();
}
}

// if we reach this point it means that the cache does not contain this entry yet, update it
let mut cache = self.cache.borrow_mut();

// make network request to fetch track's lyrics
let lyrics = self.fetcher.fetch(&track);

cache.insert(track_id.to_owned(), lyrics.clone());

self.fetcher.fetch(&track)
lyrics
}

/// Fetches and returns the lyrics of the currently playing track
Expand All @@ -40,13 +67,13 @@ impl LyricsManager {
}
}

/// Returns the track being played currently, or nothing if the user is listening to a podcast episodes
/// Returns the track being played currently, or nothing if the user is listening to a podcast episode
pub fn get_current_track(&self) -> Option<Track> {
let playable = self.queue.get_current().unwrap();

match playable {
Playable::Track(track) => Some(track),
Playable::Episode(_) => None,
_ => None,
}
}
}

0 comments on commit 322bdcc

Please sign in to comment.