WIP: Add new station_model, implement asynchronous station searching

parent b4867f67
Pipeline #89733 passed with stages
in 23 minutes and 15 seconds
......@@ -92,12 +92,12 @@ impl Library {
let sender = self.sender.clone();
// TODO: Re-implement library
// self.library_model.borrow().model.connect_items_changed(move |model, _, removed, added| {
// Check if data got changed
// Check if data got changed
// if removed == 1 || added == 1 {
// Convert gio::ListStore into Vec<Station>
// Convert gio::ListStore into Vec<Station>
// let stations = model.export();
// Write new data to disk
// Write new data to disk
// match Self::write(stations, LIBRARY_PATH.to_path_buf()) {
// Ok(()) => (),
// Err(error) => {
......
......@@ -22,8 +22,8 @@ mod app;
mod config;
mod library;
mod static_resource;
mod window;
mod utils;
mod window;
use crate::app::App;
......
......@@ -3,9 +3,9 @@ mod object_wrapper;
mod song_model;
mod station_model;
pub use model_handler::ModelHandler;
pub use object_wrapper::ObjectWrapper; // TODO: KILL THIS
pub use song_model::SongModel;
pub use station_model::Order;
pub use station_model::Sorting;
pub use station_model::StationModel;
pub use model_handler::ModelHandler;
use crate::api::Station;
use crate::widgets::StationFlowBox;
use crate::model::ModelHandler;
use crate::widgets::StationFlowBox;
#[derive(Clone, Debug)]
pub enum Sorting {
......@@ -34,33 +34,28 @@ impl StationModel {
let order = Order::Ascending;
let handler: Vec<Box<ModelHandler>> = Vec::new();
Self {
data,
sorting,
order,
handler,
}
Self { data, sorting, order, handler }
}
pub fn add_stations(&mut self, stations: Vec<Station>) {
for station in &stations{
for station in &stations {
if !self.data.contains(&station) {
self.data.push(station.clone());
}
}
for h in &*self.handler{
for h in &*self.handler {
h.add_stations(stations.clone());
}
}
pub fn remove_stations(&mut self, stations: Vec<Station>) {
for station in &stations{
for station in &stations {
let index = self.data.iter().position(|s| s == station).unwrap();
self.data.remove(index);
}
for h in &*self.handler{
for h in &*self.handler {
h.remove_stations(stations.clone());
}
}
......@@ -68,7 +63,7 @@ impl StationModel {
pub fn clear(&mut self) {
self.data.clear();
for h in &*self.handler{
for h in &*self.handler {
h.clear();
}
}
......@@ -78,12 +73,12 @@ impl StationModel {
self.order = order.clone();
}
pub fn export(&self) -> Vec<Station>{
pub fn export(&self) -> Vec<Station> {
self.data.clone()
}
/// Bind to a struct which implements the trait ModelHandler
pub fn bind(&mut self, handler: Box<ModelHandler>){
pub fn bind(&mut self, handler: Box<ModelHandler>) {
self.handler.push(handler);
}
......
use glib::{self, object::WeakRef};
use gio::prelude::*;
use glib::prelude::*;
use glib::{self, object::WeakRef};
use gtk::prelude::*;
use gio::prelude::*;
// If you want to know more about lazy loading, you should read these:
// - https://en.wikipedia.org/wiki/Lazy_loading
......@@ -9,12 +9,8 @@ use gio::prelude::*;
//
// Source: gnome-podcasts (GPLv3)
// https://gitlab.gnome.org/World/podcasts/blob/7856b6fd27cb071583b87f55f3e47d9d8af9acb6/podcasts-gtk/src/utils.rs
pub fn lazy_load<T, C, F, W>(
data: T,
container: WeakRef<C>,
mut contructor: F,
cancellable: gio::Cancellable,
) where
pub fn lazy_load<T, C, F, W>(data: T, container: WeakRef<C>, mut contructor: F, cancellable: gio::Cancellable)
where
T: IntoIterator + 'static,
T::Item: 'static,
C: IsA<glib::Object> + ContainerExt + 'static,
......@@ -44,13 +40,10 @@ where
gtk::idle_add(move || {
// If the Cancellable got activated, cancel the lazy loading
// by stopping this idle_add "loop".
if cancellable.is_cancelled(){
if cancellable.is_cancelled() {
return glib::Continue(false);
}
data.next()
.map(|x| func(x))
.map(|_| glib::Continue(true) )
.unwrap_or_else(|| glib::Continue(false))
data.next().map(|x| func(x)).map(|_| glib::Continue(true)).unwrap_or_else(|| glib::Continue(false))
});
}
......@@ -2,14 +2,14 @@ use gio::prelude::*;
use glib::Sender;
use gtk::prelude::*;
use std::rc::Rc;
use std::cell::RefCell;
use std::rc::Rc;
use crate::api::Station;
use crate::app::Action;
use crate::model::{ModelHandler, StationModel};
use crate::widgets::station_row::StationRow;
use crate::utils::*;
use crate::widgets::station_row::StationRow;
pub struct StationFlowBox {
pub widget: gtk::FlowBox,
......@@ -43,14 +43,12 @@ impl StationFlowBox {
}
}
impl ModelHandler for StationFlowBox{
fn add_stations(&self, stations: Vec<Station>){
impl ModelHandler for StationFlowBox {
fn add_stations(&self, stations: Vec<Station>) {
let lazy_loading = self.lazy_loading.clone();
let widget = self.widget.downgrade();
let sender = self.sender.clone();
let constructor = move |station| {
StationRow::new(sender.clone(), station).widget.clone()
};
let constructor = move |station| StationRow::new(sender.clone(), station).widget.clone();
// Create new cancellable which we can use to cancel
// the lazy loading
......@@ -61,21 +59,20 @@ impl ModelHandler for StationFlowBox{
lazy_load(stations.clone(), widget.clone(), constructor.clone(), cancellable.clone());
}
fn remove_stations(&self, stations: Vec<Station>){
fn remove_stations(&self, stations: Vec<Station>) {
// TODO: implement remove
}
fn clear(&self){
fn clear(&self) {
// Cancel previous lazy loading, since we don't need
// the content anymore because we're clearing everything.
self.lazy_loading.borrow().cancel();
// Fetch all children and destroy them
let children = self.widget.get_children();
for widget in children{
for widget in children {
self.widget.remove(&widget);
widget.destroy();
}
}
}
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