Skip to content

Commit

Permalink
Fix a problem where body wouldn't be updated after adding or removing…
Browse files Browse the repository at this point in the history
… a collection
  • Loading branch information
ghasemi committed Aug 6, 2023
1 parent 9526ea8 commit 2b4bfb5
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 27 deletions.
23 changes: 16 additions & 7 deletions src/body/collection/box.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
use diesel::{delete, QueryDsl, RunQueryDsl};
use std::cell::RefCell;
use std::rc::Rc;
use diesel::{Connection, delete, QueryDsl, RunQueryDsl};
use gtk::{Button, Label, prelude};
use gtk::Orientation::{Horizontal, Vertical};
use prelude::*;
use crate::body::Body;
use crate::body::collection::model::Collection;
use crate::common::{StyledLabelBuilder, gtk_box};
use crate::common::util::PathString;
use crate::db::get_connection;
use crate::schema::bodies::dsl::bodies;
use crate::schema::collections::dsl::collections;

pub(in crate::body::collection) trait CollectionBox {
fn new() -> Self;
fn add(&self, id: i32, path: &String);
fn new(history: Rc<RefCell<Vec<(Rc<Body>, bool)>>>) -> Self;
fn add(&self, id: i32, path: &String, history: Rc<RefCell<Vec<(Rc<Body>, bool)>>>);
}

impl CollectionBox for gtk::Box {
fn new() -> Self {
fn new(history: Rc<RefCell<Vec<(Rc<Body>, bool)>>>) -> Self {
let gtk_box = gtk_box(Vertical);
for collection in collections.load::<Collection>(&mut get_connection()).unwrap() {
gtk_box.clone().add(collection.id, &collection.path);
gtk_box.clone().add(collection.id, &collection.path, history.clone());
}
gtk_box
}
fn add(&self, id: i32, path: &String) {
fn add(&self, id: i32, path: &String, history: Rc<RefCell<Vec<(Rc<Body>, bool)>>>) {
let remove_button = Button::builder().icon_name("list-remove").build();
remove_button.add_css_class("destructive-action");
let inner_box = gtk_box(Horizontal);
Expand All @@ -30,9 +34,14 @@ impl CollectionBox for gtk::Box {
inner_box.append(&remove_button);
self.append(&inner_box);
remove_button.connect_clicked({
let history = history.clone();
let this = self.clone();
move |_| {
delete(collections.find(id)).execute(&mut get_connection()).unwrap();
get_connection().transaction(|connection| {
delete(collections.find(id)).execute(connection)?;
delete(bodies).execute(connection)
}).unwrap();
history.borrow_mut().clear();
this.remove(&inner_box);
}
});
Expand Down
16 changes: 12 additions & 4 deletions src/body/collection/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::mpsc::{channel, TryRecvError};
use std::thread;
use std::time::{Duration, UNIX_EPOCH};
Expand All @@ -7,25 +9,28 @@ use adw::ApplicationWindow;
use adw::gio::{Cancellable, File};
use adw::glib::{ControlFlow, timeout_add_local};
use adw::prelude::*;
use diesel::{ExpressionMethods, insert_or_ignore_into, QueryDsl, RunQueryDsl, update};
use diesel::{delete, ExpressionMethods, insert_or_ignore_into, QueryDsl, RunQueryDsl, update};
use diesel::prelude::*;
use diesel::result::Error;
use gtk::{Button, FileDialog, ProgressBar};
use gtk::Orientation::Vertical;
use crate::body::Body;
use crate::body::collection::model::Collection;
use crate::body::collection::r#box::CollectionBox;
use crate::common::gtk_box;
use crate::db::get_connection;
use crate::schema::bodies::dsl::bodies;
use crate::schema::collections::{modified, path};
use crate::schema::collections::dsl::collections;
use crate::song::{import_songs, ImportProgress};

mod r#box;
pub mod model;

pub(in crate::body) fn add_collection_box(window: &ApplicationWindow) -> gtk::Box {
pub(in crate::body) fn add_collection_box(window: &ApplicationWindow, history: Rc<RefCell<Vec<(Rc<Body>, bool)>>>)
-> gtk::Box {
let add_collection_box = gtk_box(Vertical);
let collection_box: gtk::Box = CollectionBox::new();
let collection_box: gtk::Box = CollectionBox::new(history.clone());
add_collection_box.append(&collection_box);
let browse_button = Button::builder().label("Browse").build();
browse_button.add_css_class("suggested-action");
Expand All @@ -36,8 +41,11 @@ pub(in crate::body) fn add_collection_box(window: &ApplicationWindow) -> gtk::Bo
FileDialog::builder().title("Collection directories").accept_label("Choose").build()
.select_multiple_folders(Some(&window), Cancellable::NONE, {
let collection_box = collection_box.clone();
let history = history.clone();
move |files| {
if let Ok(Some(files)) = files {
delete(bodies).execute(&mut get_connection()).unwrap();
history.borrow_mut().clear();
let (sender, receiver) = channel::<ImportProgress>();
let mut last_id: Option<i32> = None;
let mut progress_bar_map = HashMap::new();
Expand All @@ -62,7 +70,7 @@ pub(in crate::body) fn add_collection_box(window: &ApplicationWindow) -> gtk::Bo
}
ImportProgress::CollectionEnd(id, collection_path) => {
collection_box.remove(&progress_bar_map[&last_id.unwrap()]);
collection_box.add(id, &collection_path);
collection_box.add(id, &collection_path, history.clone());
true
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/body/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl Body {
BodyType::Songs => {
Body::songs(body_table.query1.clone(), body_table.query2.clone().map(Rc::new), &now_playing)
}
BodyType::Collections => { Body::collections(&window) }
BodyType::Collections => { Body::collections(&window, history) }
}
}
pub fn put_to_history(self, scroll_adjustment: Option<f32>, history: Rc<RefCell<Vec<(Rc<Body>, bool)>>>) {
Expand All @@ -167,15 +167,15 @@ impl Body {
scrolled_window.set_child(Some((*self.widget).as_ref()));
history.push((self, false));
}
pub fn collections(window: &ApplicationWindow) -> Self {
pub fn collections(window: &ApplicationWindow, history: Rc<RefCell<Vec<(Rc<Body>, bool)>>>) -> Self {
Self {
title: Rc::new(String::from("Harborz")),
subtitle: Rc::new(String::from("Collection")),
body_type: BodyType::Collections,
query1: None,
query2: None,
scroll_adjustment: Cell::new(None),
widget: Box::new(add_collection_box(&window)),
widget: Box::new(add_collection_box(&window, history)),
}
}
pub fn artists(window_title: &WindowTitle, scrolled_window: &ScrolledWindow,
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fn main() -> Result<ExitCode> {
let back_button = back_button.clone();
move |_| {
if history.borrow().last().unwrap().0.body_type != BodyType::Collections {
Rc::new(Body::collections(&window))
Rc::new(Body::collections(&window, history.clone()))
.set(&window_title, &scrolled_window, history.clone(), &Some(back_button.clone()));
}
menu_button.popdown();
Expand Down
32 changes: 20 additions & 12 deletions src/now_playing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,35 @@ pub fn create(song_selected_body: Rc<RefCell<Option<Rc<Body>>>>, window_title: &
now_playing.borrow().update_other(&window_title, &back_button, "go-down", &header_body, &now_playing_body);
}
});
let wrapper = Wrapper::new(&bottom_widget);
back_button.connect_clicked({
let history = history.clone();
let now_playing = now_playing.clone();
let header_body = header_body.clone();
let body = body.clone();
let window_title = window_title.clone();
let scrolled_window = scrolled_window.clone();
let wrapper = wrapper.clone();
move |back_button| {
let mut history = history.borrow_mut();
if back_button.icon_name().unwrap() == BACK_ICON {
history.pop();
if history.borrow().is_empty() {
back_button.set_visible(false);
Rc::new(Body::artists(&window_title, &scrolled_window, history.clone(), &wrapper,
&Some(back_button.clone()))
).set(&window_title, &scrolled_window, history.clone(), &None);
} else {
now_playing.borrow().update_other(&window_title, &back_button, BACK_ICON, &header_body, &body);
}
back_button.set_visible(history.len() > 1);
if let Some((body, adjust_scroll)) = history.last() {
body.set_window_title(&window_title);
let Body { widget, scroll_adjustment: body_scroll_adjustment, .. } = body.deref();
scrolled_window.set_child(Some((**widget).as_ref()));
if *adjust_scroll { scrolled_window.adjust(&body_scroll_adjustment); }
let mut history = history.borrow_mut();
if back_button.icon_name().unwrap() == BACK_ICON {
history.pop();
} else {
now_playing.borrow().update_other(&window_title, &back_button, BACK_ICON, &header_body, &body);
}
back_button.set_visible(history.len() > 1);
if let Some((body, adjust_scroll)) = history.last() {
body.set_window_title(&window_title);
let Body { widget, scroll_adjustment: body_scroll_adjustment, .. } = body.deref();
scrolled_window.set_child(Some((**widget).as_ref()));
if *adjust_scroll { scrolled_window.adjust(&body_scroll_adjustment); }
}
}
}
});
Expand Down Expand Up @@ -136,7 +145,6 @@ pub fn create(song_selected_body: Rc<RefCell<Option<Rc<Body>>>>, window_title: &
});
let tracking_position = Rc::new(Cell::new(false));
let once = Once::new();
let wrapper = Wrapper::new(&bottom_widget);
wrapper.connect_local(SONG_SELECTED, true, {
let now_playing = now_playing.clone();
move |params| {
Expand Down

0 comments on commit 2b4bfb5

Please sign in to comment.