feat(slint): Blacklist Manager view (list/search/toggle/remove/clear)

cc
This commit is contained in:
vicrodh committed 2026-06-17 01:51:08 -06:00
1 parent 67d82efd30
commit dbc62be90e
7 files changed
+918 -2

No files matched your search

+170
View File
@@ -0,0 +1,170 @@
//! Blacklist Manager controller — the Rust side of the Slint port of Tauri's
//! BlacklistManagerView (Task 11). Loads the per-user blacklist into
//! `BlacklistState`, applies search-as-you-type filtering controller-side, and
//! runs the toggle / remove / clear mutations against the
//! `crate::artist_blacklist` wrapper (the same fail-open singleton the artist
//! toggle in T9 mutates).
//!
//! Mirrors `crate::offline_manager`'s shape: a `load` entry point invoked on
//! navigation + an action set wired in `main.rs`. There is no change-notify on
//! the blacklist store (the fav_cache pattern), so the manager reloads on every
//! `open` — a mutation from elsewhere (the T9 artist toggle) is reflected the
//! next time the manager is opened, and the manager's own mutations re-push the
//! filtered list in place.
//!
//! Search filter (Tauri parity §7): trim the query; empty → the full list;
//! else a case-insensitive substring match on `artist_name` ONLY (notes are not
//! searched), preserving the backend's name-sorted order. `count` always
//! carries the FULL list length so the view can tell "empty blacklist"
//! (count==0) from "no search results" (count>0, filtered list empty).
//!
//! Date: `added_at` is unix SECONDS; formatted controller-side to "MMM D, YYYY"
//! (English; the Slint build has no gettext for Rust strings — matches T9
//! toasts being `format!` English).
use std::sync::Mutex as StdMutex;
use chrono::{DateTime, Utc};
use slint::{ComponentHandle, ModelRc, SharedString, VecModel};
use crate::{AppWindow, BlacklistState, BlacklistedArtistItem};
/// The live search query (Rust-side source of truth). The view echoes it back
/// from `BlacklistState.search-query`; this is what `refilter` reads so a
/// toggle/remove/clear re-push keeps the current filter applied.
static QUERY: StdMutex<String> = StdMutex::new(String::new());
fn current_query() -> String {
QUERY.lock().map(|q| q.clone()).unwrap_or_default()
}
fn set_query(q: String) {
if let Ok(mut guard) = QUERY.lock() {
*guard = q;
}
}
/// Format a unix-seconds timestamp as "MMM D, YYYY" (English). Falls back to an
/// empty string for a non-representable value.
fn format_added(secs: i64) -> String {
DateTime::<Utc>::from_timestamp(secs, 0)
.map(|dt| dt.format("%b %-d, %Y").to_string())
.unwrap_or_default()
}
/// Build the visible (filtered) `BlacklistedArtistItem`s from the full
/// name-sorted snapshot, applying the current query.
fn build_items() -> (Vec<BlacklistedArtistItem>, i32) {
let all = crate::artist_blacklist::get_all();
let count = all.len() as i32;
let query = current_query();
let needle = query.trim().to_lowercase();
let items: Vec<BlacklistedArtistItem> = all
.into_iter()
.filter(|a| needle.is_empty() || a.artist_name.to_lowercase().contains(&needle))
.map(|a| {
let notes = a.notes.clone().unwrap_or_default();
BlacklistedArtistItem {
// artist_id is u64; an int holds Qobuz ids comfortably for
// display + passing back on click/remove (matches OfflineRow
// carrying ids as strings — here the spec uses int).
artist_id: a.artist_id as i32,
artist_name: a.artist_name.into(),
added_at: a.added_at as i32,
added_display: format_added(a.added_at).into(),
has_notes: !notes.is_empty(),
notes: notes.into(),
}
})
.collect();
(items, count)
}
/// Push the filtered items + full count + enabled flag + query into Slint.
fn push(w: &AppWindow) {
let (items, count) = build_items();
let st = w.global::<BlacklistState>();
st.set_items(ModelRc::new(VecModel::from(items)));
st.set_count(count);
st.set_enabled(crate::artist_blacklist::is_enabled());
st.set_search_query(SharedString::from(current_query()));
}
/// Load (or refresh) the manager: mark loading, read the store, push state,
/// clear loading. Synchronous — the wrapper reads are in-memory / a single
/// SQLite query, so there is no worker hop (unlike the offline manager's
/// index scan).
pub fn load(weak: slint::Weak<AppWindow>) {
let _ = weak.upgrade_in_event_loop(|w| {
w.global::<BlacklistState>().set_loading(true);
push(&w);
w.global::<BlacklistState>().set_loading(false);
});
}
// --- Actions ------------------------------------------------------------
/// Search-as-you-type: store the query and re-push the filtered list. `count`
/// stays the full length (so the empty-vs-no-results split stays correct).
pub fn search_changed(w: &AppWindow, query: String) {
set_query(query);
push(w);
}
/// Toggle the global enable flag; on success re-read + re-push and info-toast.
/// On error, surface the wrapper's message (no state change).
pub fn toggle_enabled(w: &AppWindow) {
let new_state = !crate::artist_blacklist::is_enabled();
match crate::artist_blacklist::set_enabled(new_state) {
Ok(()) => {
push(w);
let msg = if new_state {
"Blacklist enabled"
} else {
"Blacklist disabled"
};
crate::toast::info(w, msg);
}
Err(e) => {
log::error!("[qbz-slint] blacklist toggle-enabled failed: {e}");
crate::toast::error(w, "Failed to toggle blacklist");
}
}
}
/// Remove one artist (optimistic — re-push drops the row immediately) + toast.
pub fn remove(w: &AppWindow, artist_id: i32) {
// Capture the name before removing, for the toast.
let name = crate::artist_blacklist::get_all()
.into_iter()
.find(|a| a.artist_id == artist_id as u64)
.map(|a| a.artist_name)
.unwrap_or_else(|| "Artist".to_string());
match crate::artist_blacklist::remove(artist_id as u64) {
Ok(()) => {
push(w);
crate::toast::success(w, format!("{name} removed from blacklist"));
}
Err(e) => {
log::error!("[qbz-slint] blacklist remove failed: {e}");
crate::toast::error(w, "Failed to remove artist");
}
}
}
/// Clear every blacklisted artist + toast (the count is captured before).
pub fn clear_all(w: &AppWindow) {
let count = crate::artist_blacklist::count();
match crate::artist_blacklist::clear_all() {
Ok(()) => {
push(w);
crate::toast::success(w, format!("Removed {count} artists from blacklist"));
}
Err(e) => {
log::error!("[qbz-slint] blacklist clear-all failed: {e}");
crate::toast::error(w, "Failed to clear blacklist");
}
}
}
+111
View File
@@ -21,6 +21,7 @@ mod artist;
mod artist_blacklist;
mod artwork;
mod auth;
mod blacklist_manager;
mod commands;
mod custom_artwork;
mod dates;
@@ -1197,6 +1198,7 @@ fn scope_for(entry: &nav::NavEntry) -> String {
nav::NavEntry::Playlist(_) => "playlist".into(),
nav::NavEntry::PlaylistManager => "playlist-manager".into(),
nav::NavEntry::OfflineManager => "offline-manager".into(),
nav::NavEntry::BlacklistManager => "blacklist-manager".into(),
nav::NavEntry::Mixtapes => "mixtapes".into(),
nav::NavEntry::Collections => "collections".into(),
nav::NavEntry::MixtapeDetail(_) => "mixtape-detail".into(),
@@ -1346,6 +1348,13 @@ fn apply_entry(
});
offline_manager::load(w2, handle.clone());
}
nav::NavEntry::BlacklistManager => {
let w2 = weak.clone();
let _ = weak.upgrade_in_event_loop(|w| {
w.global::<NavState>().set_view(ContentView::BlacklistManager);
});
blacklist_manager::load(w2);
}
nav::NavEntry::Mixtapes => {
myqbz::navigate(
weak.clone(),
@@ -7695,6 +7704,108 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
});
}
// Artist Blacklist Manager actions (Task 11). Mutations are synchronous
// (in-memory set + single SQLite ops via the artist_blacklist wrapper), so
// no tokio handle is needed; each callback runs on the event-loop thread.
{
// open() — the forward-open seam (T10's Settings content-filtering row
// calls this). Records the nav entry, swaps the view, then loads the
// blacklist. Mirrors OfflineManagerActions.on_open.
let weak = window.as_weak();
window.global::<BlacklistActions>().on_open(move || {
nav::record(nav::NavEntry::BlacklistManager);
if let Some(w) = weak.upgrade() {
w.global::<NavState>()
.set_view(ContentView::BlacklistManager);
update_nav_flags(&w);
}
blacklist_manager::load(weak.clone());
});
}
{
// back() — declared per the spec; the actual back chrome is the shared
// header NavButtons (which drives nav::go_back). Wired here for any
// future in-view trigger; routes through the same go-back path.
let weak = window.as_weak();
let app_runtime_bl = app_runtime.clone();
let bl_handle = tokio_rt.handle().clone();
let image_cache_bl = image_cache.clone();
window.global::<BlacklistActions>().on_back(move || {
if let Some((entry, scroll)) = nav::go_back() {
let weak2 = weak.clone();
arm_scroll_restore(&weak2, &entry, scroll);
apply_entry(
entry,
&app_runtime_bl,
&weak2,
&bl_handle,
&image_cache_bl,
);
if let Some(w) = weak.upgrade() {
update_nav_flags(&w);
}
}
});
}
{
let weak = window.as_weak();
let bl_runtime_a = app_runtime.clone();
let bl_handle_a = tokio_rt.handle().clone();
let bl_image_cache_a = image_cache.clone();
window
.global::<BlacklistActions>()
.on_artist_select(move |id| {
let artist_id = id.to_string();
nav::record(nav::NavEntry::Artist(artist_id.clone()));
navigate_artist(
bl_runtime_a.clone(),
weak.clone(),
&bl_handle_a,
bl_image_cache_a.clone(),
artist_id,
);
if let Some(w) = weak.upgrade() {
update_nav_flags(&w);
}
});
}
{
let weak = window.as_weak();
window
.global::<BlacklistActions>()
.on_toggle_enabled(move || {
if let Some(w) = weak.upgrade() {
blacklist_manager::toggle_enabled(&w);
}
});
}
{
let weak = window.as_weak();
window.global::<BlacklistActions>().on_remove(move |id| {
if let Some(w) = weak.upgrade() {
blacklist_manager::remove(&w, id);
}
});
}
{
let weak = window.as_weak();
window.global::<BlacklistActions>().on_clear_all(move || {
if let Some(w) = weak.upgrade() {
blacklist_manager::clear_all(&w);
}
});
}
{
let weak = window.as_weak();
window
.global::<BlacklistActions>()
.on_search_changed(move |q| {
if let Some(w) = weak.upgrade() {
blacklist_manager::search_changed(&w, q.to_string());
}
});
}
// Offline Cache Manager actions.
{
let runtime = app_runtime.clone();
+4
View File
@@ -59,6 +59,10 @@ pub enum NavEntry {
/// The Offline Cache Manager — the manage-downloads surface (Tauri's
/// OfflineCacheManagerView). Session-scoped; no payload.
OfflineManager,
/// The Artist Blacklist Manager — the manage-blacklist surface (Tauri's
/// BlacklistManagerView). Reached from the Settings content-filtering row.
/// Session-scoped (the search query lives in the controller); no payload.
BlacklistManager,
/// The My QBZ > Mixtapes index grid (read-only in this slice). Toolbar
/// state (sort/view/search) is session-scoped in the controller, so the
/// entry carries no payload.