Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database startup update #18

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
},
"prettier.tabWidth": 4,
"css.lint.unknownProperties": "ignore",
"rust-analyzer.showUnlinkedFileNotification": false,
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Vizia Sample Browser

An audio sample browser application.

| Header 1 | Header 2 |
| --- | --- |
| Table 1 | Table 2 |
21 changes: 19 additions & 2 deletions src/app_data.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
use std::sync::{Arc, Mutex};

use vizia::prelude::*;

use crate::state::browser::BrowserState;
use crate::{
database::prelude::{AudioFile, CollectionID, Database, DatabaseAudioFileHandler},
state::browser::{BrowserState, Directory},
};

#[derive(Lens)]
pub struct AppData {
pub database: Arc<Mutex<Database>>,
pub browser: BrowserState,
pub browser_width: f32,
pub table_height: f32,
pub table_headers: Vec<String>,
pub table_rows: Vec<Vec<String>>,
pub table_rows: Vec<AudioFile>,
pub search_text: String,
}

pub enum AppEvent {
SetBrowserWidth(f32),
SetTableHeight(f32),
ViewCollection(CollectionID),
}

impl Model for AppData {
Expand All @@ -24,6 +31,16 @@ impl Model for AppData {
event.map(|app_event, _| match app_event {
AppEvent::SetBrowserWidth(width) => self.browser_width = *width,
AppEvent::SetTableHeight(height) => self.table_height = *height,
AppEvent::ViewCollection(id) => {
println!("selected: {}", id);
if let Ok(db) = self.database.lock() {
if let Ok(audio_files) = db.get_all_audio_files() {
println!("ALL {}", audio_files.len());
self.table_rows = audio_files;
}
}
println!("num rows: {}", self.table_rows.len());
}
});
}
}
1 change: 0 additions & 1 deletion src/app_event.rs

This file was deleted.

68 changes: 60 additions & 8 deletions src/database/audio_file.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use super::{CollectionID, Database, DatabaseConnectionHandle, DatabaseError};
use super::prelude::*;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use vizia::prelude::*;

pub type AudioFileID = usize;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Data, Lens)]
pub struct AudioFile {
id: AudioFileID,
name: String,
collection: CollectionID,
pub id: AudioFileID,
pub name: String,
pub collection: CollectionID,
duration: f32,
sample_rate: f32,
bit_depth: f32,
Expand All @@ -16,6 +18,20 @@ pub struct AudioFile {
}

impl AudioFile {
pub fn from_path(path: &PathBuf, id: AudioFileID) -> Option<Self> {
let extension = path.extension().map(|v| v.to_str().unwrap()).unwrap_or("");

if !AUDIO_FILE_EXTENSIONS.contains(&extension) {
return None;
}

let name = path.file_name().unwrap().to_str().unwrap();

let audio_file = AudioFile::new(id, name.to_string(), id, 0.0, 0.0, 0.0, None, None, 0.0);

Some(audio_file)
}

pub fn new(
id: AudioFileID,
name: String,
Expand All @@ -33,8 +49,10 @@ impl AudioFile {

pub trait DatabaseAudioFileHandler {
fn get_all_audio_files(&self) -> Result<Vec<AudioFile>, DatabaseError>;
fn get_audio_file_by_path(&self, path: &PathBuf) -> Result<AudioFile, DatabaseError>;
fn get_child_audio_files(&self, parent: CollectionID) -> Result<Vec<AudioFile>, DatabaseError>;
fn insert_audio_file(&mut self, audio_file: AudioFile) -> Result<(), DatabaseError>;
fn remove_audio_file(&mut self, audio_file: AudioFileID) -> Result<(), DatabaseError>;
}

impl DatabaseAudioFileHandler for Database {
Expand Down Expand Up @@ -63,13 +81,40 @@ impl DatabaseAudioFileHandler for Database {
Err(DatabaseError::ConnectionClosed)
}

fn get_audio_file_by_path(&self, path: &PathBuf) -> Result<AudioFile, DatabaseError> {
if let Some(connection) = self.get_connection() {
let mut query = connection.prepare(
"SELECT id, name, collection, duration, sample_rate, bit_depth, bpm, key, size FROM audio_files WHERE path = (?1)",
)?;

let col: AudioFile = query.query_row([path.to_str().unwrap()], |row| {
let path: String = row.get(3)?;
Ok(AudioFile {
id: row.get(0)?,
name: row.get(1)?,
collection: row.get(2)?,
duration: row.get(3)?,
sample_rate: row.get(4)?,
bit_depth: row.get(5)?,
bpm: row.get(6)?,
key: row.get(7)?,
size: row.get(8)?,
})
})?;

return Ok(col);
}

Err(DatabaseError::ConnectionClosed)
}

fn get_child_audio_files(&self, parent: CollectionID) -> Result<Vec<AudioFile>, DatabaseError> {
if let Some(connection) = self.get_connection() {
let mut query = connection.prepare(
"SELECT id, name, collection, duration, sample_rate, bit_depth, bpm, key, size FROM audio_files WHERE collection = (?1)",
)?;

let collections = query.query_map([parent], |row| {
let audio_files = query.query_map([parent], |row| {
Ok(AudioFile {
id: row.get(0)?,
name: row.get(1)?,
Expand All @@ -83,7 +128,7 @@ impl DatabaseAudioFileHandler for Database {
})
})?;

return Ok(collections.map(|v| v.unwrap()).collect());
return Ok(audio_files.map(|v| v.unwrap()).collect());
}
Err(DatabaseError::ConnectionClosed)
}
Expand All @@ -101,14 +146,21 @@ impl DatabaseAudioFileHandler for Database {
audio_file.bit_depth,
audio_file.bpm,
audio_file.key,
audio_file.key,
audio_file.size,
),
)?;
}

Ok(())
}

fn remove_audio_file(&mut self, audio_file: AudioFileID) -> Result<(), DatabaseError> {
if let Some(connection) = self.get_connection() {
connection.execute("DELETE FROM audio_files WHERE id = (?1)", [audio_file])?;
}

Ok(())
}
}

impl From<AudioFile> for usize {
Expand Down
Loading