Skip to main content

rustriff_lib/commands/
channels.rs

1use crate::commands::helpers::persist_amp_config;
2use crate::domain::dto::channel_dto::ChannelDto;
3use crate::services::amp_config_service::AmpConfigPersistenceService;
4use crate::services::audio_service::AudioService;
5use std::sync::Mutex;
6use tauri::{AppHandle, Emitter};
7use tracing::info;
8
9/// Sets the active channel ID in the audio service.
10///
11/// Updates the currently selected [`Channel`] within the [`AudioService`]
12/// by assigning the provided channel ID.
13///
14/// # Arguments
15///
16/// * `audio_service` - The shared [`AudioService`] state.
17/// * `channel_id` - The identifier of the channel to activate.
18///
19/// [`Channel`]: crate::domain::channel::Channel
20/// [`AudioService`]: crate::services::audio_service::AudioService
21#[tauri::command]
22pub(crate) fn set_channel_id(
23    audio_service: tauri::State<Mutex<AudioService>>,
24    persistence_service: tauri::State<Mutex<AmpConfigPersistenceService>>,
25    channel_id: u32,
26) {
27    let mut service = audio_service.inner().lock().unwrap();
28    service.set_current_channel_id(channel_id);
29    persist_amp_config(&service, &persistence_service);
30}
31
32/// Returns the currently active channel ID.
33///
34/// Retrieves the identifier of the [`Channel`] that is currently selected
35/// within the [`AudioService`].
36///
37/// # Arguments
38///
39/// * `audio_service` - The shared [`AudioService`] state.
40///
41/// # Returns
42///
43/// The ID of the active channel.
44///
45/// [`Channel`]: crate::domain::channel::Channel
46/// [`AudioService`]: crate::services::audio_service::AudioService
47#[tauri::command]
48pub(crate) fn get_channel_id(audio_service: tauri::State<Mutex<AudioService>>) -> u32 {
49    let service = audio_service.inner().lock().unwrap();
50    *service.current_channel_id()
51}
52
53/// Adds a new channel to the audio service.
54///
55/// Creates a new [`Channel`] with the given name, registers it with the
56/// [`AudioService`], and emits a `channel-added` event to the frontend
57/// containing the newly created [`ChannelDto`].
58///
59/// # Arguments
60///
61/// * `app` - The Tauri application handle used to emit events.
62/// * `audio_service` - The shared [`AudioService`] state.
63/// * `channel_name` - The display name of the new channel.
64///
65/// # Returns
66///
67/// * `Ok(())` if the channel was created and the event was emitted successfully.
68/// * `Err(String)` if emitting the event failed.
69///
70/// [`Channel`]: crate::domain::channel::Channel
71/// [`ChannelDto`]: crate::dto::channel_dto::ChannelDto
72/// [`AudioService`]: crate::services::audio_service::AudioService
73#[tauri::command]
74pub(crate) fn add_channel(
75    app: AppHandle,
76    audio_service: tauri::State<Mutex<AudioService>>,
77    persistence_service: tauri::State<Mutex<AmpConfigPersistenceService>>,
78    channel_name: String,
79) -> Result<(), String> {
80    info!("add_channel command received: {channel_name}");
81
82    let mut service = audio_service.inner().lock().unwrap();
83    let channel_id = service.add_channel(channel_name.clone());
84    let channel = service
85        .channels()
86        .iter()
87        .find(|c| c.id() == channel_id)
88        .unwrap();
89    let channel_dto = ChannelDto::from(channel);
90    persist_amp_config(&service, &persistence_service);
91
92    info!(
93        "emitting channel-added event for id={} name={}",
94        channel_dto.id, channel_dto.name
95    );
96
97    app.emit("channel-added", channel_dto)
98        .map_err(|e| e.to_string())?;
99
100    info!("channel-added event emitted successfully");
101
102    Ok(())
103}
104
105/// Returns all available channels.
106///
107/// Retrieves all [`Channel`] instances managed by the [`AudioService`] and
108/// converts them into [`ChannelDto`] representations, including their gain,
109/// tone stack settings, and volume.
110///
111/// # Arguments
112///
113/// * `audio_service` - The shared [`AudioService`] state.
114///
115/// # Returns
116///
117/// A vector of [`ChannelDto`] objects representing all configured channels.
118///
119/// [`Channel`]: crate::domain::channel::Channel
120/// [`ChannelDto`]: crate::dto::channel_dto::ChannelDto
121/// [`AudioService`]: crate::services::audio_service::AudioService
122#[tauri::command]
123pub(crate) fn get_all_channels(
124    audio_service: tauri::State<Mutex<AudioService>>,
125) -> Vec<ChannelDto> {
126    let service = audio_service.inner().lock().unwrap();
127    service.channels().iter().map(ChannelDto::from).collect()
128}
129
130/// Removes a channel from the audio service.
131///
132/// Deletes the [`Channel`] with the specified ID from the [`AudioService`].
133///
134/// # Arguments
135///
136/// * `audio_service` - The shared [`AudioService`] state.
137/// * `channel_id` - The identifier of the channel to remove.
138///
139/// # Returns
140///
141/// * `Ok(())` if the channel was removed successfully.
142/// * `Err(String)` if removal fails.
143///
144/// [`Channel`]: crate::domain::channel::Channel
145/// [`AudioService`]: crate::services::audio_service::AudioService
146#[tauri::command]
147pub(crate) fn remove_channel(
148    audio_service: tauri::State<Mutex<AudioService>>,
149    persistence_service: tauri::State<Mutex<AmpConfigPersistenceService>>,
150    channel_id: u32,
151) -> Result<(), String> {
152    let mut service = audio_service.inner().lock().unwrap();
153    service.remove_channel(channel_id);
154    persist_amp_config(&service, &persistence_service);
155    info!("remove channel {channel_id}");
156    Ok(())
157}