mecomp_tui/state/
audio.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//! This module contains the implementation of audio state store.
//! which is updated every tick and used by views to render the audio playback and queue state.
//!
//! The audio state store is responsible for maintaining the audio state, and for handling audio related actions.

use std::{sync::Arc, time::Duration};

use tokio::sync::{
    broadcast,
    mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender},
};

use mecomp_core::rpc::MusicPlayerClient;
use mecomp_core::state::StateAudio;

use crate::termination::Interrupted;

use super::action::{AudioAction, PlaybackAction, QueueAction, VolumeAction};

pub const TICK_RATE: Duration = Duration::from_millis(100);

/// The audio state store.
#[derive(Debug, Clone)]
#[allow(clippy::module_name_repetitions)]
pub struct AudioState {
    state_tx: UnboundedSender<StateAudio>,
}

impl AudioState {
    /// create a new audio state store, and return the receiver for listening to state updates.
    #[must_use]
    pub fn new() -> (Self, UnboundedReceiver<StateAudio>) {
        let (state_tx, state_rx) = unbounded_channel::<StateAudio>();

        (Self { state_tx }, state_rx)
    }

    /// a loop that updates the audio state every tick.
    ///
    /// # Errors
    ///
    /// Fails if the state cannot be sent
    pub async fn main_loop(
        &self,
        daemon: Arc<MusicPlayerClient>,
        mut action_rx: UnboundedReceiver<AudioAction>,
        mut interrupt_rx: broadcast::Receiver<Interrupted>,
    ) -> anyhow::Result<Interrupted> {
        let mut state = get_state(daemon.clone()).await?;

        // the initial state once
        self.state_tx.send(state.clone())?;

        // the ticker
        let mut ticker = tokio::time::interval(TICK_RATE);

        let result = loop {
            tokio::select! {
                // Handle the actions coming from the UI
                // and process them to do async operations
                Some(action) = action_rx.recv() => {
                    self.handle_action(daemon.clone(), action).await?;
                },
                // Tick to terminate the select every N milliseconds
                _ = ticker.tick() => {},
                // Catch and handle interrupt signal to gracefully shutdown
                Ok(interrupted) = interrupt_rx.recv() => {
                    break interrupted;
                }
            }

            state = get_state(daemon.clone()).await?;
            self.state_tx.send(state.clone())?;
        };

        Ok(result)
    }

    async fn handle_action(
        &self,
        daemon: Arc<MusicPlayerClient>,
        action: AudioAction,
    ) -> anyhow::Result<()> {
        match action {
            AudioAction::Playback(action) => handle_playback(daemon, action).await?,
            AudioAction::Queue(action) => handle_queue(daemon, action).await?,
        }

        Ok(())
    }
}

/// get the audio state from the daemon.
async fn get_state(daemon: Arc<MusicPlayerClient>) -> anyhow::Result<StateAudio> {
    let ctx = tarpc::context::current();
    Ok(daemon.state_audio(ctx).await?.unwrap_or_default())
}

/// handle a playback action
async fn handle_playback(
    daemon: Arc<MusicPlayerClient>,
    action: PlaybackAction,
) -> anyhow::Result<()> {
    let ctx = tarpc::context::current();

    match action {
        PlaybackAction::Toggle => daemon.playback_toggle(ctx).await?,
        PlaybackAction::Next => daemon.playback_skip_forward(ctx, 1).await?,
        PlaybackAction::Previous => daemon.playback_skip_backward(ctx, 1).await?,
        PlaybackAction::Seek(seek_type, duration) => {
            daemon.playback_seek(ctx, seek_type, duration).await?;
        }
        PlaybackAction::Volume(VolumeAction::Increase(amount)) => {
            daemon.playback_volume_up(ctx, amount).await?;
        }
        PlaybackAction::Volume(VolumeAction::Decrease(amount)) => {
            daemon.playback_volume_down(ctx, amount).await?;
        }
        PlaybackAction::ToggleMute => daemon.playback_volume_toggle_mute(ctx).await?,
    }

    Ok(())
}

/// handle a queue action
async fn handle_queue(daemon: Arc<MusicPlayerClient>, action: QueueAction) -> anyhow::Result<()> {
    let ctx = tarpc::context::current();

    match action {
        QueueAction::Add(ids) => daemon.queue_add_list(ctx, ids).await??,
        QueueAction::Remove(index) => {
            #[allow(clippy::range_plus_one)]
            daemon.queue_remove_range(ctx, index..index + 1).await?;
        }
        QueueAction::SetPosition(index) => daemon.queue_set_index(ctx, index).await?,
        QueueAction::Shuffle => daemon.playback_shuffle(ctx).await?,
        QueueAction::Clear => daemon.playback_clear(ctx).await?,
        QueueAction::SetRepeatMode(mode) => daemon.playback_repeat(ctx, mode).await?,
    }

    Ok(())
}