2023-05-23 00:48:03 +00:00
|
|
|
use serde::{Serialize, Deserialize};
|
|
|
|
use yewdux::{prelude::*, storage};
|
2023-05-24 01:08:32 +00:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
2023-05-22 17:25:16 +00:00
|
|
|
use super::transfer_menu::RegionDisplay;
|
2023-05-22 22:11:49 +00:00
|
|
|
use crate::data::plate_instances::PlateInstance;
|
|
|
|
use crate::data::transfer::Transfer;
|
|
|
|
use crate::data::plate::*;
|
2023-05-24 22:39:38 +00:00
|
|
|
use crate::data::transfer_region::TransferRegion;
|
2023-05-22 17:25:16 +00:00
|
|
|
|
2023-05-24 22:39:38 +00:00
|
|
|
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize, Store)]
|
|
|
|
#[store(storage = "session")]
|
|
|
|
pub struct CurrentTransfer {
|
|
|
|
pub transfer: TransferRegion,
|
2023-05-22 17:25:16 +00:00
|
|
|
}
|
2023-05-22 22:11:49 +00:00
|
|
|
|
2023-05-23 00:48:03 +00:00
|
|
|
#[derive(Default, PartialEq, Clone, Serialize, Deserialize)]
|
2023-05-22 22:11:49 +00:00
|
|
|
pub struct MainState {
|
|
|
|
pub source_plates: Vec<PlateInstance>,
|
|
|
|
pub destination_plates: Vec<PlateInstance>,
|
|
|
|
pub transfers: Vec<Transfer>,
|
2023-05-25 16:07:21 +00:00
|
|
|
pub selected_source_plate: Uuid,
|
|
|
|
pub selected_dest_plate: Uuid,
|
|
|
|
pub selected_transfer: Uuid,
|
2023-05-22 22:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Store for MainState {
|
|
|
|
fn new() -> Self {
|
2023-05-23 00:48:03 +00:00
|
|
|
init_listener(storage::StorageListener::<Self>::new(storage::Area::Local));
|
|
|
|
|
|
|
|
storage::load(storage::Area::Local)
|
|
|
|
.expect("Unable to load state")
|
|
|
|
.unwrap_or_default()
|
|
|
|
/*
|
2023-05-22 22:11:49 +00:00
|
|
|
Self {
|
|
|
|
source_plates: Vec::new(),
|
|
|
|
destination_plates: Vec::new(),
|
|
|
|
transfers: Vec::new(),
|
|
|
|
}
|
2023-05-23 00:48:03 +00:00
|
|
|
*/
|
2023-05-22 22:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn should_notify(&self, old: &Self) -> bool {
|
|
|
|
self != old
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MainState {
|
|
|
|
pub fn purge_transfers(&mut self) {
|
|
|
|
// Removes any transfers for which the associated plates are gone
|
|
|
|
self.transfers = self.transfers.iter()
|
|
|
|
.filter(|tr| {
|
|
|
|
self.source_plates.iter().any(|spi| {
|
|
|
|
spi.get_uuid() == tr.source_id
|
|
|
|
}) &&
|
|
|
|
self.destination_plates.iter().any(|dpi| {
|
|
|
|
dpi.get_uuid() == tr.dest_id
|
|
|
|
})
|
|
|
|
})
|
2023-05-27 17:12:58 +00:00
|
|
|
.map(|tr| tr.clone())
|
2023-05-22 22:11:49 +00:00
|
|
|
.collect();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_source_plate(&mut self, plate: PlateInstance) {
|
|
|
|
assert!(plate.plate.plate_type == PlateType::Source);
|
|
|
|
self.source_plates.push(plate);
|
|
|
|
}
|
|
|
|
pub fn add_dest_plate(&mut self, plate: PlateInstance) {
|
|
|
|
assert!(plate.plate.plate_type == PlateType::Destination);
|
|
|
|
self.destination_plates.push(plate);
|
|
|
|
}
|
2023-05-24 01:08:32 +00:00
|
|
|
pub fn del_plate(&mut self, id: Uuid) {
|
|
|
|
if let Some(index) = self.source_plates.iter().position(|spi| {spi.get_uuid() == id}) {
|
|
|
|
self.source_plates.swap_remove(index);
|
|
|
|
}
|
|
|
|
if let Some(index) = self.destination_plates.iter().position(|dpi| {dpi.get_uuid() == id}) {
|
|
|
|
self.destination_plates.swap_remove(index);
|
|
|
|
}
|
|
|
|
}
|
2023-05-22 22:11:49 +00:00
|
|
|
}
|