use serde::{Deserialize, Serialize};
use spoolframe_types::ArtworkId;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct Previews {
#[serde(rename = "interval_ms", with = "spoolframe_types::serde_time::millis")]
pub interval: std::time::Duration,
pub tile_width: u32,
pub tile_height: u32,
pub columns: u32,
pub rows: u32,
pub count: u32,
pub sheets: Vec<ArtworkId>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PreviewTile {
pub index: u32,
pub sheet: usize,
pub x: u32,
pub y: u32,
pub width: u32,
pub height: u32,
}
impl Previews {
pub fn tile(&self, position: std::time::Duration) -> Option<PreviewTile> {
let per_sheet = self.columns.checked_mul(self.rows)?;
if self.count == 0 || per_sheet == 0 || self.interval.is_zero() {
return None;
}
let index = (position.as_millis() / self.interval.as_millis())
.min(u128::from(self.count - 1)) as u32;
let sheet = (index / per_sheet) as usize;
if sheet >= self.sheets.len() {
return None;
}
let at = index % per_sheet;
Some(PreviewTile {
index,
sheet,
x: (at % self.columns) * self.tile_width,
y: (at / self.columns) * self.tile_height,
width: self.tile_width,
height: self.tile_height,
})
}
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use crate::{PreviewTile, Previews};
use spoolframe_types::ArtworkId;
fn film() -> Previews {
Previews {
interval: std::time::Duration::from_millis(10_000),
tile_width: 320,
tile_height: 134,
columns: 10,
rows: 10,
count: 720,
sheets: (0..8)
.map(|n| format!("preview:abc/{n}").parse::<ArtworkId>().unwrap())
.collect(),
}
}
#[test]
fn the_tile_for_1_23_40_is_on_its_sheet_by_row_and_column() {
assert_eq!(
film().tile(Duration::from_millis(5_020_000)),
Some(PreviewTile {
index: 502,
sheet: 5,
x: 2 * 320,
y: 0,
width: 320,
height: 134,
})
);
let tile = film().tile(Duration::from_millis(5_057_000)).unwrap();
assert_eq!((tile.index, tile.x, tile.y), (505, 5 * 320, 0));
let tile = film().tile(Duration::from_millis(5_799_999)).unwrap();
assert_eq!(
(tile.index, tile.sheet, tile.x, tile.y),
(579, 5, 9 * 320, 7 * 134)
);
}
#[test]
fn a_time_between_tiles_shows_the_one_before() {
assert_eq!(film().tile(Duration::from_millis(0)).unwrap().index, 0);
assert_eq!(film().tile(Duration::from_millis(9_999)).unwrap().index, 0);
assert_eq!(film().tile(Duration::from_millis(10_000)).unwrap().index, 1);
}
#[test]
fn past_the_end_is_the_last_tile() {
let tile = film().tile(Duration::from_millis(10 * 3_600_000)).unwrap();
assert_eq!((tile.index, tile.sheet), (719, 7));
assert_eq!((tile.x, tile.y), (9 * 320, 134));
}
#[test]
fn previews_without_tiles_have_none() {
let empty = Previews {
count: 0,
sheets: vec![],
..film()
};
assert_eq!(empty.tile(Duration::from_millis(0)), None);
}
#[test]
fn a_grid_that_is_not_square_is_read_by_its_columns() {
let previews = Previews {
columns: 4,
rows: 3,
count: 20,
sheets: film().sheets[..2].to_vec(),
..film()
};
let tile = previews.tile(Duration::from_millis(70_000)).unwrap();
assert_eq!((tile.sheet, tile.x, tile.y), (0, 3 * 320, 134));
let tile = previews.tile(Duration::from_millis(130_000)).unwrap();
assert_eq!((tile.index, tile.sheet, tile.x, tile.y), (13, 1, 320, 0));
}
}