search function for all models... however...

There is some sort of problem. Somehow I sometimes get a signal 11
SEGV_MAPERR error and then it segfaults. *sigh*... So this function
needs some help yet.
This commit is contained in:
Chris Cochrun 2024-09-15 06:44:47 -05:00
parent 114ffb5bdc
commit 5b0462db65
4 changed files with 120 additions and 15 deletions

View file

@ -96,6 +96,8 @@ mod video_model {
index: i32,
updated_end_time: f32,
) -> bool;
#[qinvokable]
fn search(self: Pin<&mut VideoModel>, search_term: QString);
}
impl cxx_qt::Threading for VideoModel {}
@ -185,6 +187,7 @@ use diesel::sqlite::SqliteConnection;
use diesel::{delete, insert_into, prelude::*, update};
use std::path::PathBuf;
use std::pin::Pin;
use tracing::debug;
use self::video_model::{
QHash_i32_QByteArray, QMap_QString_QVariant, QVector_i32,
@ -206,6 +209,7 @@ pub struct VideoModelRust {
count: i32,
highest_id: i32,
videos: Vec<self::Video>,
inner_videos: Vec<self::Video>,
}
impl video_model::VideoModel {
@ -275,6 +279,10 @@ impl video_model::VideoModel {
.rust_mut()
.videos
.remove(index as usize);
self.as_mut()
.rust_mut()
.inner_videos
.remove(index as usize);
self.as_mut().end_remove_rows();
}
println!("removed-item-at-index: {:?}", video_id);
@ -373,7 +381,8 @@ impl video_model::VideoModel {
index,
index,
);
self.as_mut().rust_mut().videos.push(video);
self.as_mut().rust_mut().videos.push(video.clone());
self.as_mut().rust_mut().inner_videos.push(video);
self.as_mut().end_insert_rows();
}
}
@ -608,6 +617,25 @@ impl video_model::VideoModel {
Err(_e) => false,
}
}
fn search(mut self: Pin<&mut Self>, search_term: QString) {
let search_term = search_term.to_string().to_lowercase();
debug!(search_term);
let searched_videos: Vec<Video> = self
.inner_videos
.iter()
.filter(|video| {
video.title.to_lowercase().contains(&search_term)
})
.cloned()
.collect();
debug!(search = ?&searched_videos);
unsafe {
self.as_mut().begin_reset_model();
self.as_mut().rust_mut().videos = searched_videos;
self.as_mut().end_reset_model();
}
}
}
// QAbstractListModel implementation