Cotton code
113 lines · 4.6 KB · RustRaw
//! The first pass: what the phone asks PhotoKit or MediaStore for, and the//! honest progress line (DECISIONS.md "The first pass, honest").//!//! **Asking for pictures (the Optimise Storage unknown, RESEARCH.md §1).**//! The core never assumes a size is on the phone. Each photo is asked for//! once at [`ASK_PX`] with the network **off** (iPhone: `.fastFormat`,//! `isNetworkAccessAllowed = false`; Android: `loadThumbnail`), and the core//! looks at whatever came back ([`super::look::analyse`] grades it). A photo//! that came back with nothing is counted "only in iCloud" and asked for once//! more at the same small size with the network on, only on Wi-Fi//! ([`next_ask`]). An original is never downloaded to be looked at.//!//! **Time left** is measured from the last [`RATE_WINDOW`] photos looked at,//! never guessed before there are [`RATE_MIN`].use serde::{Deserialize, Serialize};use super::look::Level;use super::words;use super::Platform;pub const ASK_PX: u32 = 360;pub const RATE_WINDOW: usize = 500;pub const RATE_MIN: usize = 50;/// Photos the phone looks at before it saves where it is.pub const CHECKPOINT: usize = 200;/// How to ask for one photo's picture.#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]pub struct Ask {    pub px: u32,    pub network: bool,}/// The next ask for a photo, from what earlier asks gave. `None`: do not ask/// again (seen well enough, or waiting for Wi-Fi).pub fn next_ask(seen: Option<Level>, asked_with_network: bool, on_wifi: bool) -> Option<Ask> {    match seen {        None => Some(Ask { px: ASK_PX, network: false }),        Some(Level::None) if !asked_with_network && on_wifi => Some(Ask { px: ASK_PX, network: true }),        _ => None,    }}#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]#[serde(rename_all = "snake_case")]pub enum State {    /// Not started: the first-run screen.    NotStarted,    Running,    PausedBattery,    PausedClosed,    Done,}/// Where the pass stands, and the times photos were looked at.#[derive(Debug, Clone, Default, Serialize, Deserialize)]pub struct Progress {    pub total: usize,    pub looked: usize,    pub cloud_only: usize,    /// Milliseconds at which the most recent photos were looked at, oldest first.    #[serde(default)]    pub recent_ms: Vec<i64>,}impl Progress {    /// Record `n` photos looked at, at `now_ms`, of which `cloud_only` came back empty.    pub fn looked_at(&mut self, n: usize, cloud_only: usize, now_ms: i64) {        self.looked += n;        self.cloud_only += cloud_only;        self.recent_ms.extend(std::iter::repeat(now_ms).take(n));        let extra = self.recent_ms.len().saturating_sub(RATE_WINDOW);        self.recent_ms.drain(..extra);    }    /// Minutes left at the measured rate; `None` until it can be measured.    pub fn minutes_left(&self) -> Option<u32> {        if self.recent_ms.len() < RATE_MIN || self.looked >= self.total {            return None;        }        let span = (self.recent_ms[self.recent_ms.len() - 1] - self.recent_ms[0]).max(1) as f64;        let per_ms = (self.recent_ms.len() - 1) as f64 / span;        let left = (self.total - self.looked) as f64;        Some(((left / per_ms) / 60_000.0).ceil() as u32)    }}/// The lines at the top of Albums and Tidy while the pass runs.#[derive(Debug, Clone, PartialEq, Serialize)]pub struct Lines {    pub head: String,    pub detail: Option<String>,    pub note: Option<String>,    pub cloud: Option<String>,    /// 0–1 for the thin progress line.    pub fraction: f32,}pub fn lines(p: &Progress, state: State, charging: bool, platform: Platform) -> Option<Lines> {    if matches!(state, State::NotStarted) || p.total == 0 {        return None;    }    let fraction = (p.looked as f32 / p.total as f32).clamp(0.0, 1.0);    let cloud = (p.cloud_only > 0).then(|| words::only_in_icloud(p.cloud_only, platform));    Some(match state {        State::Done => Lines { head: words::done_looking(p.total), detail: None, note: None, cloud, fraction: 1.0 },        State::Running => Lines { head: words::looking(p.total), detail: Some(words::done_and_left(p.looked, p.minutes_left(), charging)), note: Some(words::FILLS_AS_IT_GOES.to_string()), cloud, fraction },        State::PausedBattery => Lines { head: words::looking(p.total), detail: Some(words::done_and_left(p.looked, None, false)), note: Some(words::PAUSED_BATTERY.to_string()), cloud, fraction },        State::PausedClosed => Lines { head: words::looking(p.total), detail: Some(words::done_and_left(p.looked, None, false)), note: Some(words::PAUSED_CLOSED.to_string()), cloud, fraction },        State::NotStarted => unreachable!(),    })}