Use seperate threads for search

This commit is contained in:
KoffeinFlummi
2019-03-31 21:53:33 +02:00
parent 3c7b3e3618
commit c42c144ccf

View File

@@ -66,36 +66,69 @@ impl SearchView {
}); });
} }
pub fn run_search<S: Into<String>>(&mut self, query: S) { fn search_track(
let query = query.into(); spotify: Arc<Spotify>,
let q = query.clone(); tracks: Arc<RwLock<Vec<Track>>>,
self.edit query: String,
.call_on(&Selector::Id(EDIT_ID), |v: &mut EditView| { ) {
v.set_content(q); if let Some(results) = spotify.search_track(&query, 50, 0) {
}); let t = results
if let Some(results) = self.spotify.search_track(&query, 50, 0) {
let tracks = results
.tracks .tracks
.items .items
.iter() .iter()
.map(|ft| Track::new(ft)) .map(|ft| Track::new(ft))
.collect(); .collect();
let mut r = self.results_tracks.write().unwrap(); let mut r = tracks.write().unwrap();
*r = tracks; *r = t;
self.edit_focused = false;
} }
}
if let Some(results) = self.spotify.search_playlist(&query, 50, 0) { fn search_playlist(
spotify: Arc<Spotify>,
playlists: Arc<RwLock<Vec<Playlist>>>,
query: String,
) {
if let Some(results) = spotify.search_playlist(&query, 50, 0) {
let pls = results let pls = results
.playlists .playlists
.items .items
.iter() .iter()
.map(|sp| Playlists::process_playlist(sp, &&self.spotify)) .map(|sp| Playlists::process_playlist(sp, &&spotify))
.collect(); .collect();
let mut r = self.results_playlists.write().unwrap(); let mut r = playlists.write().unwrap();
*r = pls; *r = pls;
self.edit_focused = false; }
}
pub fn run_search<S: Into<String>>(&mut self, query: S) {
let query = query.into();
self.edit_focused = false;
{
let query = query.clone();
self.edit
.call_on(&Selector::Id(EDIT_ID), |v: &mut EditView| {
v.set_content(query);
});
}
{
let spotify = self.spotify.clone();
let results = self.results_tracks.clone();
let query = query.clone();
std::thread::spawn(|| {
Self::search_track(spotify, results, query);
});
}
{
let spotify = self.spotify.clone();
let results = self.results_playlists.clone();
let query = query.clone();
std::thread::spawn(|| {
Self::search_playlist(spotify, results, query);
});
} }
} }
} }