Refactoring to slide_model and service_item getting the data from db

This is kinda a broken commit. There isn't any order to the adding of
the slide anymore. So it'll need to find a way of keeping track of
where all the current slides are and then insert the new ones in the
correct order while moving the others around as well.
This commit is contained in:
Chris Cochrun 2024-10-02 14:48:16 -05:00
parent 6052cd01ac
commit 2c014e242f
9 changed files with 347 additions and 604 deletions

View file

@ -285,6 +285,97 @@ impl Default for Song {
}
}
impl Song {
pub fn get_lyric_list(&self) -> QList_QString {
let mut lyric_list = QList_QString::default();
if self.lyrics.is_empty() {
return QList_QString::default();
}
let raw_lyrics = self.lyrics.clone();
println!("raw-lyrics: {:?}", raw_lyrics);
let vorder: Vec<&str> =
self.verse_order.split(' ').collect();
let keywords = vec![
"Verse 1", "Verse 2", "Verse 3", "Verse 4",
"Verse 5", "Verse 6", "Verse 7", "Verse 8",
"Chorus 1", "Chorus 2", "Chorus 3", "Chorus 4",
"Bridge 1", "Bridge 2", "Bridge 3", "Bridge 4",
"Intro 1", "Intro 2", "Ending 1", "Ending 2",
"Other 1", "Other 2", "Other 3", "Other 4",
];
let _first_item = true;
let mut lyric_map = HashMap::new();
let mut verse_title = String::from("");
let mut lyric = String::from("");
for (i, line) in raw_lyrics.split('\n').enumerate() {
if keywords.contains(&line) {
if i != 0 {
// println!("{verse_title}");
// println!("{lyric}");
lyric_map.insert(verse_title, lyric);
lyric = String::from("");
verse_title = line.to_string();
// println!("{line}");
// println!("\n");
} else {
verse_title = line.to_string();
// println!("{line}");
// println!("\n");
}
} else {
lyric.push_str(line);
lyric.push('\n');
}
}
lyric_map.insert(verse_title, lyric);
// println!("da-map: {:?}", lyric_map);
for verse in vorder {
let mut verse_name = "";
// debug!(verse = verse);
for word in keywords.clone() {
let end_verse =
verse.get(1..2).unwrap_or_default();
let beg_verse =
verse.get(0..1).unwrap_or_default();
// println!(
// "verse: {:?}, beginning: {:?}, end: {:?}, word: {:?}",
// verse, beg_verse, end_verse, word
// );
if word.starts_with(beg_verse)
&& word.ends_with(end_verse)
{
verse_name = word;
// println!("TITLE: {verse_name}");
continue;
}
}
if let Some(lyric) = lyric_map.get(verse_name) {
if lyric.contains("\n\n") {
let split_lyrics: Vec<&str> =
lyric.split("\n\n").collect();
for lyric in split_lyrics {
if lyric.is_empty() {
continue;
}
lyric_list.append(QString::from(lyric));
}
continue;
}
lyric_list.append(QString::from(lyric));
} else {
println!("NOT WORKING!");
};
}
for lyric in lyric_list.iter() {
// println!("da-list: {:?}", lyric);
debug!(lyric = ?lyric)
}
lyric_list
}
}
#[derive(Debug)]
pub struct SongModelRust {
count: i32,
@ -316,27 +407,24 @@ impl Default for SongModelRust {
}
}
const SELECT_SINGLE_SONG_STATEMENT: &'static str = r#"SELECT vorder as "verse_order!", fontSize as "font_size!: i32", backgroundType as "background_type!", horizontalTextAlignment as "horizontal_text_alignment!", verticalTextAlignment as "vertical_text_alignment!", title as "title!", font as "font!", background as "background!", lyrics as "lyrics!", ccli as "ccli!", author as "author!", audio as "audio!", id as "id: i32" from songs where id = ?"#;
impl SongModelRust {
pub fn get_song(id: i32) -> Result<Song> {
let rt = tokio::runtime::Runtime::new().unwrap();
let mut db = {
let mut data = dirs::data_local_dir().unwrap();
data.push("lumina");
data.push("library-db.sqlite3");
let mut db_url = String::from("sqlite://");
db_url.push_str(data.to_str().unwrap());
rt.block_on(async {
SqliteConnection::connect(&db_url).await.expect("problems")
})
};
pub fn get_song(id: i32) -> Result<Song> {
let rt = tokio::runtime::Runtime::new().unwrap();
let mut db = {
let mut data = dirs::data_local_dir().unwrap();
data.push("lumina");
data.push("library-db.sqlite3");
let mut db_url = String::from("sqlite://");
db_url.push_str(data.to_str().unwrap());
rt.block_on(async {
let statement = format!("{} where id = ?", SELECT_SONG_STATEMENT).as_str();
let result = query_as!(Song, SELECT_SINGLE_SONG_STATEMENT, id).fetch_one(&mut db).await?;
Ok(result)
SqliteConnection::connect(&db_url).await.expect("problems")
})
}
};
debug!("getting song with id: {id}");
rt.block_on(async {
let result = query_as!(Song, r#"SELECT vorder as "verse_order!", fontSize as "font_size!: i32", backgroundType as "background_type!", horizontalTextAlignment as "horizontal_text_alignment!", verticalTextAlignment as "vertical_text_alignment!", title as "title!", font as "font!", background as "background!", lyrics as "lyrics!", ccli as "ccli!", author as "author!", audio as "audio!", id as "id: i32" from songs where id = ?"#, id).fetch_one(&mut db).await?;
debug!(?result);
Ok(result)
})
}
impl song_model::SongModel {
@ -1050,90 +1138,7 @@ impl song_model::SongModel {
return QStringList::default();
}
if let Some(song) = self.rust().songs.get(index as usize) {
if song.lyrics.is_empty() {
return QStringList::default();
}
let raw_lyrics = song.lyrics.clone();
println!("raw-lyrics: {:?}", raw_lyrics);
let vorder: Vec<&str> =
song.verse_order.split(' ').collect();
let keywords = vec![
"Verse 1", "Verse 2", "Verse 3", "Verse 4",
"Verse 5", "Verse 6", "Verse 7", "Verse 8",
"Chorus 1", "Chorus 2", "Chorus 3", "Chorus 4",
"Bridge 1", "Bridge 2", "Bridge 3", "Bridge 4",
"Intro 1", "Intro 2", "Ending 1", "Ending 2",
"Other 1", "Other 2", "Other 3", "Other 4",
];
let _first_item = true;
let mut lyric_map = HashMap::new();
let mut verse_title = String::from("");
let mut lyric = String::from("");
for (i, line) in raw_lyrics.split('\n').enumerate() {
if keywords.contains(&line) {
if i != 0 {
// println!("{verse_title}");
// println!("{lyric}");
lyric_map.insert(verse_title, lyric);
lyric = String::from("");
verse_title = line.to_string();
// println!("{line}");
// println!("\n");
} else {
verse_title = line.to_string();
// println!("{line}");
// println!("\n");
}
} else {
lyric.push_str(line);
lyric.push('\n');
}
}
lyric_map.insert(verse_title, lyric);
// println!("da-map: {:?}", lyric_map);
for verse in vorder {
let mut verse_name = "";
// debug!(verse = verse);
for word in keywords.clone() {
let end_verse =
verse.get(1..2).unwrap_or_default();
let beg_verse =
verse.get(0..1).unwrap_or_default();
// println!(
// "verse: {:?}, beginning: {:?}, end: {:?}, word: {:?}",
// verse, beg_verse, end_verse, word
// );
if word.starts_with(beg_verse)
&& word.ends_with(end_verse)
{
verse_name = word;
// println!("TITLE: {verse_name}");
continue;
}
}
if let Some(lyric) = lyric_map.get(verse_name) {
if lyric.contains("\n\n") {
let split_lyrics: Vec<&str> =
lyric.split("\n\n").collect();
for lyric in split_lyrics {
if lyric.is_empty() {
continue;
}
lyric_list.append(QString::from(lyric));
}
continue;
}
lyric_list.append(QString::from(lyric));
} else {
println!("NOT WORKING!");
};
}
for lyric in lyric_list.iter() {
// println!("da-list: {:?}", lyric);
debug!(lyric = ?lyric)
}
lyric_list = song.get_lyric_list();
}
QStringList::from(&lyric_list)
}