2023-06-01 17:04:03 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-05-24 01:08:32 +00:00
|
|
|
use uuid::Uuid;
|
2023-06-01 17:04:03 +00:00
|
|
|
use yewdux::{prelude::*, storage};
|
2023-05-24 01:08:32 +00:00
|
|
|
|
2023-06-01 17:04:03 +00:00
|
|
|
use crate::data::plate::*;
|
2023-05-22 22:11:49 +00:00
|
|
|
use crate::data::plate_instances::PlateInstance;
|
|
|
|
use crate::data::transfer::Transfer;
|
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")]
|
2023-06-07 21:08:43 +00:00
|
|
|
#[non_exhaustive]
|
2023-05-24 22:39:38 +00:00
|
|
|
pub struct CurrentTransfer {
|
2023-06-01 17:04:03 +00:00
|
|
|
pub transfer: Transfer,
|
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-06-07 21:08:43 +00:00
|
|
|
#[non_exhaustive]
|
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
|
|
|
}
|
|
|
|
|
|
|
|
fn should_notify(&self, old: &Self) -> bool {
|
|
|
|
self != old
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MainState {
|
2023-06-07 21:17:40 +00:00
|
|
|
pub fn _purge_transfers(&mut self) {
|
2023-05-22 22:11:49 +00:00
|
|
|
// Removes any transfers for which the associated plates are gone
|
2023-06-08 15:57:03 +00:00
|
|
|
self.transfers.retain(|tr| {
|
|
|
|
self.source_plates
|
|
|
|
.iter()
|
|
|
|
.any(|spi| spi.get_uuid() == tr.source_id)
|
|
|
|
&& self
|
|
|
|
.destination_plates
|
2023-06-01 17:04:03 +00:00
|
|
|
.iter()
|
2023-06-08 15:57:03 +00:00
|
|
|
.any(|dpi| dpi.get_uuid() == tr.dest_id)
|
|
|
|
});
|
2023-05-22 22:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2023-06-01 17:04:03 +00:00
|
|
|
if let Some(index) = self
|
|
|
|
.source_plates
|
|
|
|
.iter()
|
|
|
|
.position(|spi| spi.get_uuid() == id)
|
|
|
|
{
|
2023-05-24 01:08:32 +00:00
|
|
|
self.source_plates.swap_remove(index);
|
|
|
|
}
|
2023-06-01 17:04:03 +00:00
|
|
|
if let Some(index) = self
|
|
|
|
.destination_plates
|
|
|
|
.iter()
|
|
|
|
.position(|dpi| dpi.get_uuid() == id)
|
|
|
|
{
|
2023-05-24 01:08:32 +00:00
|
|
|
self.destination_plates.swap_remove(index);
|
|
|
|
}
|
|
|
|
}
|
2023-06-06 01:33:23 +00:00
|
|
|
pub fn rename_plate(&mut self, id: Uuid, new_name: &str) {
|
|
|
|
if let Some(index) = self
|
|
|
|
.source_plates
|
|
|
|
.iter()
|
|
|
|
.position(|spi| spi.get_uuid() == id)
|
|
|
|
{
|
|
|
|
self.source_plates[index].change_name(new_name.to_string());
|
|
|
|
}
|
|
|
|
if let Some(index) = self
|
|
|
|
.destination_plates
|
|
|
|
.iter()
|
|
|
|
.position(|dpi| dpi.get_uuid() == id)
|
|
|
|
{
|
|
|
|
self.destination_plates[index].change_name(new_name.to_string());
|
|
|
|
}
|
|
|
|
}
|
2023-05-22 22:11:49 +00:00
|
|
|
}
|