Compare commits

...

3 commits

5 changed files with 239 additions and 23 deletions

View file

@ -168,6 +168,17 @@ impl Model<Image> {
}
}
pub async fn remove_from_db(
db: PoolConnection<Sqlite>,
id: i32,
) -> Result<()> {
query!("DELETE FROM images WHERE id = $1", id)
.execute(&mut db.detach())
.await
.into_diagnostic()
.map(|_| ())
}
pub async fn update_image_in_db(
image: Image,
db: PoolConnection<Sqlite>,

View file

@ -263,6 +263,17 @@ impl Model<Presentation> {
}
}
pub async fn remove_from_db(
db: PoolConnection<Sqlite>,
id: i32,
) -> Result<()> {
query!("DELETE FROM presentations WHERE id = $1", id)
.execute(&mut db.detach())
.await
.into_diagnostic()
.map(|_| ())
}
pub async fn update_presentation_in_db(
presentation: Presentation,
db: PoolConnection<Sqlite>,

View file

@ -407,19 +407,17 @@ impl Model<Song> {
}
}
}
}
pub async fn remove_from_db(
&mut self,
db: &mut SqlitePool,
id: i32,
) -> Result<()> {
let db = db.acquire().await.expect("probs");
query!("delete from songs where id = $1", id)
.execute(&mut db.detach())
.await
.into_diagnostic()
.map(|_| ())
}
pub async fn remove_from_db(
db: PoolConnection<Sqlite>,
id: i32,
) -> Result<()> {
query!("DELETE FROM songs WHERE id = $1", id)
.execute(&mut db.detach())
.await
.into_diagnostic()
.map(|_| ())
}
pub async fn update_song_in_db(

View file

@ -14,7 +14,7 @@ use sqlx::{
pool::PoolConnection, query, query_as, Sqlite, SqliteConnection,
SqlitePool,
};
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use tracing::error;
#[derive(
@ -35,6 +35,25 @@ impl From<&Video> for Value {
}
}
impl From<PathBuf> for Video {
fn from(value: PathBuf) -> Self {
let title: String = value.file_name().map_or_else(|| "Video".into(), |filename| {
filename.to_str().unwrap_or("Video").into()
});
Self {
title,
path: value,
..Default::default()
}
}
}
impl From<&Path> for Video {
fn from(value: &Path) -> Self {
Self::from(value.to_owned())
}
}
impl Content for Video {
fn title(&self) -> String {
self.title.clone()
@ -205,6 +224,17 @@ impl Model<Video> {
}
}
pub async fn remove_from_db(
db: PoolConnection<Sqlite>,
id: i32,
) -> Result<()> {
query!("DELETE FROM videos WHERE id = $1", id)
.execute(&mut db.detach())
.await
.into_diagnostic()
.map(|_| ())
}
pub async fn update_video_in_db(
video: Video,
db: PoolConnection<Sqlite>,

View file

@ -9,7 +9,8 @@ use cosmic::{
iced_widget::{column, row as rowm, text as textm},
theme,
widget::{
button, container, context_menu, horizontal_space, icon,
button, container, context_menu, dnd_destination,
horizontal_space, icon,
menu::{self, Action as MenuAction},
mouse_area, responsive, row, scrollable, text, text_input,
Container, DndSource, Space,
@ -23,12 +24,12 @@ use tracing::{debug, error, warn};
use crate::core::{
content::Content,
images::{update_image_in_db, Image},
images::{self, update_image_in_db, Image},
model::{LibraryKind, Model},
presentations::{update_presentation_in_db, Presentation},
presentations::{self, update_presentation_in_db, Presentation},
service_items::ServiceItem,
songs::{update_song_in_db, Song},
videos::{update_video_in_db, Video},
songs::{self, update_song_in_db, Song},
videos::{self, update_video_in_db, Video},
};
#[derive(Debug, Clone)]
@ -74,8 +75,8 @@ pub enum Action {
}
#[derive(Clone, Debug)]
pub enum Message {
AddItem,
pub(crate) enum Message {
AddItem(LibraryKind),
DeleteItem((LibraryKind, i32)),
OpenItem(Option<(LibraryKind, i32)>),
HoverLibrary(Option<LibraryKind>),
@ -127,36 +128,171 @@ impl<'a> Library {
pub fn update(&'a mut self, message: Message) -> Action {
match message {
Message::AddItem(kind) => match kind {
LibraryKind::Song => todo!(),
LibraryKind::Video => {
let future = cosmic::dialog::file_chooser::open::Dialog::new().filter(cosmic::dialog::file_chooser::FileFilter::new("videos").extension("mp4").extension("mkv").extension("webm").extension(".mpeg")).open_file();
let task = Task::future(future).and_then(|r| {
if let Ok(video) = r
.url()
.to_file_path()
.and_then(|path| Ok(Video::from(path)))
{
self.video_library.add_item(video);
};
Task::none()
});
}
LibraryKind::Image => todo!(),
LibraryKind::Presentation => todo!(),
},
Message::None => (),
Message::DeleteItem((kind, index)) => {
match kind {
LibraryKind::Song => {
let Some(song) =
self.song_library.get_item(index)
else {
error!(
"There appears to not be a song here"
);
return Action::None;
};
let song = song.clone();
if let Err(e) =
self.song_library.remove_item(index)
{
error!(?e);
} else {
let task =
Task::future(self.db.acquire())
.and_then(move |db| {
Task::perform(
songs::remove_from_db(
db, song.id,
),
|r| {
match r {
Err(e) => {
error!(?e)
}
_ => (),
}
Message::None
},
)
});
return Action::Task(task);
}
}
LibraryKind::Video => {
let Some(video) =
self.video_library.get_item(index)
else {
error!(
"There appears to not be a video here"
);
return Action::None;
};
let video = video.clone();
if let Err(e) =
self.video_library.remove_item(index)
{
error!(?e);
} else {
let task =
Task::future(self.db.acquire())
.and_then(move |db| {
Task::perform(
videos::remove_from_db(
db, video.id,
),
|r| {
match r {
Err(e) => {
error!(?e)
}
_ => (),
}
Message::None
},
)
});
return Action::Task(task);
}
}
LibraryKind::Image => {
let Some(image) =
self.image_library.get_item(index)
else {
error!(
"There appears to not be a image here"
);
return Action::None;
};
let image = image.clone();
if let Err(e) =
self.image_library.remove_item(index)
{
error!(?e);
} else {
let task =
Task::future(self.db.acquire())
.and_then(move |db| {
Task::perform(
images::remove_from_db(
db, image.id,
),
|r| {
match r {
Err(e) => {
error!(?e)
}
_ => (),
}
Message::None
},
)
});
return Action::Task(task);
}
}
LibraryKind::Presentation => {
let Some(presentation) =
self.presentation_library.get_item(index)
else {
error!(
"There appears to not be a presentation here"
);
return Action::None;
};
let presentation = presentation.clone();
if let Err(e) = self
.presentation_library
.remove_item(index)
{
error!(?e);
} else {
let task =
Task::future(self.db.acquire())
.and_then(move |db| {
Task::perform(
presentations::remove_from_db(
db,
presentation.id,
),
|r| {
match r {
Err(e) => {
error!(?e)
}
_ => (),
}
Message::None
},
)
});
return Action::Task(task);
}
}
};
@ -572,12 +708,42 @@ impl<'a> Library {
let library_toolbar = rowm!(
text_input("Search...", ""),
button::icon(icon::from_name("add"))
.on_press(Message::AddItem)
.on_press(Message::AddItem(model.kind))
);
let library_column =
column![library_toolbar, items].spacing(3);
Container::new(library_column).padding(5)
let library_dnd = dnd_destination(
library_column,
vec![
"image/png".into(),
"image/jpg".into(),
"image/heif".into(),
"image/gif".into(),
"video/mp4".into(),
"video/AV1".into(),
"video/H264".into(),
"video/H265".into(),
"video/mpeg".into(),
"video/mkv".into(),
"video/webm".into(),
"video/ogg".into(),
"video/vnd.youtube.yt".into(),
"video/x-matroska".into(),
"application/pdf".into(),
"text/html".into(),
"text/md".into(),
"text/org".into(),
],
)
.on_enter(|x, y, mimes| {
warn!(?mimes);
Message::None
})
.on_finish(|mime, data, action, x, y| {
warn!(?mime, ?data, ?action);
Message::None
});
Container::new(library_dnd).padding(5)
} else {
Container::new(Space::new(0, 0))
};