Commit 5387c761 authored by Felix Häcker's avatar Felix Häcker

implement new rustio crate for search page

parent 331b6b8d
This diff is collapsed.
......@@ -16,7 +16,6 @@ url = "1.7.0"
gstreamer = "0.11.1"
dbus = "0.6.1"
dbus-macros = "0.2.3"
libhandy = { git = "https://gitlab.gnome.org/jsparber/libhandy-rs" }
mdl = "1.0.2"
serde_derive = "1.0.70"
serde = "1.0.70"
......
......@@ -102,7 +102,7 @@ impl AudioPlayer{
playbin.set_state(gstreamer::State::Null);
let p = playbin.clone();
thread::spawn(move||{
let client = Client::new("http://www.radio-browser.info");
let mut client = Client::new("http://www.radio-browser.info");
let station_url = client.get_playable_station_url(new_station).unwrap();
p.set_property("uri", &station_url);
p.set_state(gstreamer::State::Playing);
......
......@@ -53,7 +53,7 @@ impl Library {
}
fn import_db(&mut self, path: String){
let client = Client::new("http://www.radio-browser.info");
let mut client = Client::new("http://www.radio-browser.info");
let connection = Connection::open(path).unwrap();
// Read database itself
......
......@@ -16,7 +16,6 @@ extern crate gstreamer;
extern crate rusqlite;
extern crate rustio;
extern crate url;
extern crate libhandy;
extern crate mdl;
#[macro_use]
......
......@@ -34,12 +34,12 @@ impl LibraryPage {
for (station, collection_name) in &app_state.library.stations {
match station_listboxes.borrow_mut().get_mut(&collection_name) {
Some(station_listbox) => station_listbox.add_station(&station),
Some(station_listbox) => /*station_listbox.add_station(&station)*/(),
None => {
let mut station_listbox = StationListBox::new(app_cache.clone());
station_listbox.set_title("name".to_string());
//let mut station_listbox = StationListBox::new(app_cache.clone());
//station_listbox.set_title("name".to_string());
//station_listbox.add_station(&station);
library_box.add(&station_listbox.container);
//library_box.add(&station_listbox.container);
},
};
}
......
......@@ -3,7 +3,7 @@ extern crate gtk;
use app_cache::AppCache;
use gtk::prelude::*;
use page::Page;
use rustio::Client;
use rustio::AsyncClient;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
......@@ -11,6 +11,8 @@ use std::sync::mpsc::{channel, Sender};
use widgets::station_listbox::StationListBox;
use rustio::Station;
use rustio::Message;
use rustio::StationSearch;
use rustio::Task;
pub struct SearchPage {
app_cache: AppCache,
......@@ -22,26 +24,28 @@ pub struct SearchPage {
container: gtk::Box,
result_listbox: Rc<RefCell<StationListBox>>,
search_sender: Sender<Message>,
client_sender: Sender<Message>,
}
impl SearchPage {
fn connect_signals(&self) {
let mut client = Rc::new(RefCell::new(Client::new()));
let search_entry: gtk::SearchEntry = self.builder.get_object("search_entry").unwrap();
let search_sender = self.search_sender.clone();
let mut search_entry: gtk::SearchEntry = self.builder.get_object("search_entry").unwrap();
// create and start async client
let mut async_client = RefCell::new(AsyncClient::new("http://www.radio-browser.info".to_string(), self.client_sender.clone()));
async_client.borrow_mut().start_loop();
search_entry.connect_search_changed(move |search_entry| {
// Get search term
let search_term = search_entry.get_text().unwrap();
debug!("Search for {}", search_term);
// prepare search params
let mut params = HashMap::new();
params.insert("name".to_string(), search_term);
params.insert("limit".to_string(), "150".to_string());
// create search task
let search_data = StationSearch::search_for_name(search_term, false, 20);
let task = Task::Search(search_data);
// do the search itself
client.borrow_mut().search(params, search_sender.clone());
// set task for async_client
async_client.borrow_mut().set_task(task);
});
}
}
......@@ -54,31 +58,13 @@ impl Page for SearchPage {
let builder = gtk::Builder::new_from_string(include_str!("search_page.ui"));
let container: gtk::Box = builder.get_object("search_page").unwrap();
let result_listbox: Rc<RefCell<StationListBox>> = Rc::new(RefCell::new(StationListBox::new(app_cache.clone())));
let (client_sender, client_receiver) = channel();
let result_listbox: Rc<RefCell<StationListBox>> = Rc::new(RefCell::new(StationListBox::new(app_cache.clone(), client_receiver)));
let results_box: gtk::Box = builder.get_object("results_box").unwrap();
let results_stack: gtk::Stack = builder.get_object("results_stack").unwrap();
results_box.add(&result_listbox.borrow().container);
let (search_sender, search_receiver) = channel();
let result_listbox_clone = result_listbox.clone();
gtk::timeout_add(100, move || {
match search_receiver.try_recv() {
Ok(Message::StationAdd(stations)) => {
for station in stations {
result_listbox_clone.borrow_mut().add_station(&station);
}
results_stack.set_visible_child_name("results");
}
Ok(Message::Clear) => {
results_stack.set_visible_child_name("loading");
result_listbox_clone.borrow_mut().clear();
}
Err(_) => (),
}
Continue(true)
});
let searchpage = SearchPage {
app_cache,
title,
......@@ -86,7 +72,7 @@ impl Page for SearchPage {
builder,
container,
result_listbox,
search_sender,
client_sender,
};
searchpage.connect_signals();
searchpage
......
......@@ -5,10 +5,12 @@ use app_cache::AppCache;
use rustio::Station;
use widgets::station_row::StationRow;
use gtk::WidgetExt;
use libhandy::{Column, ColumnExt};
use std::collections::HashMap;
use std::rc::Rc;
use favicon_downloader::FaviconDownloader;
use rustio::Message;
use std::sync::mpsc::Receiver;
use std::cell::RefCell;
pub struct StationListBox {
app_cache: AppCache,
......@@ -17,25 +19,26 @@ pub struct StationListBox {
builder: gtk::Builder,
fdl: Rc<FaviconDownloader>,
receiver: Rc<Receiver<Message>>,
// We need to track which station is which listboxrow, otherwise we cannot remove them
// station_id (string), StationRow
station_rows: HashMap<String, StationRow>,
station_rows: Rc<RefCell<HashMap<String, StationRow>>>,
}
impl StationListBox {
pub fn new(app_cache: AppCache) -> Self {
pub fn new(app_cache: AppCache, client_receiver: Receiver<Message>) -> Self {
let builder = gtk::Builder::new_from_string(include_str!("station_listbox.ui"));
let container: gtk::Box = builder.get_object("station_listbox").unwrap();
let column: Column = builder.get_object("column").unwrap();
column.set_maximum_width(600);
let fdl = Rc::new(FaviconDownloader::new());
let receiver = Rc::new(client_receiver);
let mut station_rows = HashMap::new();
let mut station_rows = Rc::new(RefCell::new(HashMap::new()));
Self { app_cache, container, builder, fdl, station_rows }
let slb = Self { app_cache, container, builder, fdl, receiver, station_rows };
slb.start_loop();
slb
}
pub fn set_title(&self, title: String) {
......@@ -44,28 +47,45 @@ impl StationListBox {
title_label.set_visible(true);
}
pub fn clear(&mut self) {
fn start_loop(&self){
let listbox: gtk::ListBox = self.builder.get_object("listbox").unwrap();
for row in listbox.get_children().iter() {
listbox.remove(row);
}
self.station_rows.clear();
}
let receiver = self.receiver.clone();
let station_rows = self.station_rows.clone();
let app_cache = self.app_cache.clone();
let fdl = self.fdl.clone();
pub fn add_station(&mut self, station: &Station){
let listbox: gtk::ListBox = self.builder.get_object("listbox").unwrap();
let row = StationRow::new(self.app_cache.clone(), &station, self.fdl.clone());
listbox.add(&row.container);
self.station_rows.insert(station.id.clone(), row);
}
gtk::timeout_add(100, move || {
match receiver.try_recv() {
// Insert new rows
Ok(Message::StationAdd(stations)) => {
for station in stations{
let row = StationRow::new(app_cache.clone(), &station, fdl.clone());
listbox.add(&row.container);
station_rows.borrow_mut().insert(station.id.clone(), row);
}
}
pub fn remove_station(&mut self, station: &Station){
let listbox: gtk::ListBox = self.builder.get_object("listbox").unwrap();
match self.station_rows.remove(&station.id) {
Some(row) => {
listbox.remove(&row.container);
},
None => warn!("Cannot remove not existing row."),
};
// Remove rows
Ok(Message::StationAdd(stations)) => {
for station in stations{
match station_rows.borrow_mut().remove(&station.id) {
Some(row) => listbox.remove(&row.container),
None => warn!("Cannot remove not existing row."),
};
}
}
// Clear all rows
Ok(Message::Clear) => {
for row in listbox.get_children().iter() {
listbox.remove(row);
}
station_rows.borrow_mut().clear();
}
Ok(_) => (),
Err(_) => (),
}
Continue(true)
});
}
}
......@@ -8,7 +8,7 @@
<property name="vexpand">True</property>
<property name="orientation">vertical</property>
<child>
<object class="HdyColumn" id="column">
<object class="GtkBox" id="column">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment