#Use signal with enums doesnt work as expected

1 messages · Page 1 of 1 (latest)

rustic viper
#

I built an component for barcode scanning:

#[component]
pub fn BarCodeReader(
    mut props: BarcodeReaderProps,
) -> Element {
    let mut stream_started = use_signal(|| false);
    // tofix:
    // If torch_status is set to TorchStatus::Deactivated and is switched to TorchStatus::Disabled because of check_for_torch_capability(), the torch can't be activated
    // If torch_status is set to TorchStatus::Disabled and is switched to TorchStatus::Disabled because of check_for_torch_capability(), the torch can be activated
    let mut torch_status: Signal<TorchStatus> = use_signal(|| TorchStatus::Deactivated);

    rsx!{
        div {
            CamStream {
                width: props.camstream_x,
                height: props.camstream_y,
                poster: props.camstream_placeholder,
            }
            if props.show_scan_button {
                // dioxus button component
                div {
                    if !stream_started() {
                        Button {
                            variant: ButtonVariant::Outline,
                            onclick: move |_| {
                                let formats = props.barcode_formats.clone();
                                async move {
                                    match start_cam_stream().await {
                                        Ok(_) => {
                                            debug!("Camera stream started");
                                            stream_started.set(true);
                                            match check_for_torch_capability().await {
                                                Ok(capability) => {
                                                    debug!("Torch capability: {:?}", capability);
                                                    if capability {
                                                        torch_status.set(TorchStatus::Disabled);
                                                        debug!("Torch status: {:?}", torch_status);
                                                    }
                                                }
                                                Err(e) => {
                                                    error!("Failed getting torch capability: {}", e);
                                                }
                                            }
                                            match scan(formats).await {
                                                Ok(value) => {
                                                    debug!("scanned value is {value:?}");
                                                    props.content.set(value);
                                                    match stop_cam_stream().await {
                                                        Ok(_) => {
                                                            stream_started.set(false);
                                                            torch_status.set(TorchStatus::Deactivated);
                                                            debug!("Camera stream stopped");
                                                        }
                                                        Err(e) => error!("Failed to stop camera: {}", e),
                                                    };
                                                }
                                                Err(e) => {
                                                    error!("Failed to scan barcode: {}", e);
                                                }
                                            }
                                        }
                                        Err(e) => error!("Failed to start camera: {}", e),
                                    };
                                }
                            },
                            "Start Scan"
...

TorchButton {torch_status}
#

the issue i have is pretty easy:

If torch_status is set to TorchStatus::Deactivated and is switched to TorchStatus::Disabled because of check_for_torch_capability(), but the torch can't be activated
If torch_status is set to TorchStatus::Disabled and is switched to TorchStatus::Disabled because of check_for_torch_capability(), so the torch can be activated

My logs are also saying that check_for_torch_capability() is running correctly. If i press the button while torch_status was TorchStatus::Deactivated and switched to TorchStatus::Disabled, there is no reaction on click.

#[component]
pub(crate) fn TorchButton(mut torch_status: Signal<TorchStatus>) -> Element {
    let width = "24em";
    let height = "24em";
    let style = r#"
        padding: 0.2rem;
        display: flex;
        align-items: center;
        justify-content: center;
    "#;

    match *torch_status.read() {
        TorchStatus::Deactivated => {
            rsx! {
                Button {
                    variant: ButtonVariant::Ghost,
                    disabled: true,
                    style,
                    img { width, height, src: TORCH_DISABLED }
                }
            }
        }
        TorchStatus::Disabled => {
            rsx! {
                Button {
                    variant: ButtonVariant::Outline,
                    style,
                    onclick: move |_| {
                        debug!("Torch should get enabled");
                        torch_status.set(TorchStatus::Enabled);
                        async move {
                            let _ = start_torch().await;
                        }
                    },
                    img { width, height, src: TORCH_DISABLED }
                }
            }
        }
        TorchStatus::Enabled => {
            rsx! {
                Button {
                    variant: ButtonVariant::Destructive,
                    style,
                    onclick: move |_| {
                        debug!("Torch should get disabled");
                        torch_status.set(TorchStatus::Disabled);
                        async move {
                            let _ = stop_torch().await;
                        }
                    },
                    img { width, height, src: TORCH_ENABLED }
                }
            }
        }
    }
}