logic.rs 3.12 KB
use mogwai::prelude::*;
use web_sys::{HtmlInputElement, ImageBitmap, HtmlCanvasElement, CanvasRenderingContext2d};

use super::upload::Upload;

#[derive(Clone, Debug)]
pub(super) enum UploadLogic {
    Add(DomEvent),
    Remove(usize),
}

pub(super) async fn upload_preview_logic( mut rx_canvas :broadcast::Receiver<Dom>
                                        , mut rx_click  :broadcast::Receiver<DomEvent>
                                        ,     upload    :Upload ) {
    if let Some(dom) = rx_canvas.next().await {
        if let Either::Left(c) = dom.inner_read() {
            let canvas = c.to_owned().dyn_into::<HtmlCanvasElement>().unwrap();
            let context = canvas
                        . get_context("2d").unwrap().unwrap()
                        . dyn_into::<CanvasRenderingContext2d>().unwrap();
            context
                . draw_image_with_image_bitmap_and_dw_and_dh(
                      &upload.bitmap()
                    , 0.0, 0.0
                    , canvas.width() as f64, canvas.height() as f64 )
                . unwrap();
        }
    }

    while let Some(event) = rx_click.next().await {
        upload.tx_logic . try_broadcast(UploadLogic::Remove(upload.id))
                        . unwrap();
    }
}

pub(super) async fn upload_logic( mut rx_logic :broadcast::Receiver<UploadLogic>
                                , tx_logic     :broadcast::Sender<UploadLogic>
                                , tx_previews: mpmc::Sender<ListPatch<ViewBuilder<Dom>>>
                                ) {
    let mut uploads: ListPatchModel<Upload> = ListPatchModel::new();

    mogwai::spawn(uploads.stream().for_each(move |patch| {
        let patch = patch.map(|u| u.into());
        let tx_previews = tx_previews.clone();
        async move {
            tx_previews.send(patch).await.unwrap();
        }
    }));

    let mut next_id = 0;

    while let Some(msg) = rx_logic.next().await {
        match msg {
            UploadLogic::Add(event) =>
                if let Either::Left(inner) = event.clone_inner() {
                    let filelist = inner.dyn_into::<Event>().unwrap()
                                 . target().unwrap()
                                 . dyn_into::<HtmlInputElement>().unwrap()
                                 . files().unwrap();

                    for index in 0..filelist.length() {
                        let file = filelist.item(index).unwrap();
                        let tx_logic = tx_logic.clone();
                        uploads.list_patch_push( Upload::new(next_id, file, tx_logic)
                                               . await);
                        next_id += 1;
                    }
                },
            UploadLogic::Remove(id) => {
                let mut found = None;

                for (upload, index) in uploads.read().await.iter().zip(0..) {
                    if upload.id == id {
                        found = Some(index);
                        break;
                    }
                }

                if let Some(index) = found {
                    uploads.list_patch_remove(index).unwrap();
                }
            },
        }
    }
}