[habit][library] Habit: the local day with rollover, days read, the forgiving weekly streak, the reminder schedule, medals as a pure function of the log; sessions carry their UTC offset
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
//! Events: what a device did, with a hybrid logical clock. The order of events is total and the same everywhere.
|
||||
|
||||
use crate::library::{BookFile, BookInfo, Colour, Field, GoalKind, Mark, Outcome, Person};
|
||||
use crate::library::{BookFile, BookInfo, Colour, Field, GoalKind, HabitGoal, Mark, Outcome, Person};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::cmp::Ordering;
|
||||
|
||||
@@ -151,10 +151,21 @@ pub enum Op {
|
||||
active_ms: u64,
|
||||
pages: u32,
|
||||
date: String,
|
||||
/// The device's UTC offset at `start`, in minutes (Day 2); `None` for older sessions.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
offset_min: Option<i32>,
|
||||
},
|
||||
Goal {
|
||||
year: i32,
|
||||
kind: GoalKind,
|
||||
target: u32,
|
||||
},
|
||||
/// The daily habit (Day 2): what counts as a day read, and when a day rolls over. Follows the person.
|
||||
HabitGoal {
|
||||
goal: HabitGoal,
|
||||
},
|
||||
/// A medal was shown (Day 2). Medals themselves are computed from the log, never stored.
|
||||
MedalSeen {
|
||||
medal: String,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -177,11 +177,18 @@ pub fn ops_from(lib: &Library) -> Vec<Op> {
|
||||
active_ms: s.active_ms,
|
||||
pages: s.pages,
|
||||
date: s.date.clone(),
|
||||
offset_min: s.offset_min,
|
||||
});
|
||||
}
|
||||
for g in lib.goals.values() {
|
||||
ops.push(Op::Goal { year: g.year, kind: g.kind, target: g.target });
|
||||
}
|
||||
if let Some(h) = lib.habit {
|
||||
ops.push(Op::HabitGoal { goal: h });
|
||||
}
|
||||
for m in &lib.medals_seen {
|
||||
ops.push(Op::MedalSeen { medal: m.clone() });
|
||||
}
|
||||
ops
|
||||
}
|
||||
|
||||
|
||||
@@ -278,8 +278,33 @@ pub struct Session {
|
||||
pub end: u64,
|
||||
pub active_ms: u64,
|
||||
pub pages: u32,
|
||||
/// The local day it counts for (`YYYY-MM-DD`, from the device).
|
||||
/// The local day it counts for (`YYYY-MM-DD`): from `liber_habit::local_day` since Day 2.
|
||||
pub date: String,
|
||||
/// The device's UTC offset at `start`, minutes east of UTC (Day 2).
|
||||
#[serde(default)]
|
||||
pub offset_min: Option<i32>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum DailyKind {
|
||||
Pages,
|
||||
Minutes,
|
||||
}
|
||||
|
||||
/// What counts as a day read (Day 2). Default: one page, days rolling over at 03:00.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct HabitGoal {
|
||||
pub kind: DailyKind,
|
||||
pub amount: u32,
|
||||
pub rollover_hour: u8,
|
||||
}
|
||||
|
||||
impl Default for HabitGoal {
|
||||
fn default() -> Self {
|
||||
HabitGoal { kind: DailyKind::Pages, amount: 1, rollover_hour: 3 }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
@@ -297,6 +322,12 @@ pub struct Library {
|
||||
pub highlights: BTreeMap<String, Highlight>,
|
||||
pub sessions: BTreeMap<String, Session>,
|
||||
pub goals: BTreeMap<i32, Goal>,
|
||||
/// The daily habit; `None` means the default (one page, 03:00).
|
||||
#[serde(default)]
|
||||
pub habit: Option<HabitGoal>,
|
||||
/// Medals already shown (the medals are computed, see `liber_habit::medals`).
|
||||
#[serde(default)]
|
||||
pub medals_seen: BTreeSet<String>,
|
||||
#[serde(skip)]
|
||||
aliases: BTreeMap<String, String>,
|
||||
#[serde(skip)]
|
||||
@@ -313,7 +344,12 @@ pub struct Library {
|
||||
|
||||
impl PartialEq for Library {
|
||||
fn eq(&self, o: &Library) -> bool {
|
||||
self.books == o.books && self.highlights == o.highlights && self.sessions == o.sessions && self.goals == o.goals
|
||||
self.books == o.books
|
||||
&& self.highlights == o.highlights
|
||||
&& self.sessions == o.sessions
|
||||
&& self.goals == o.goals
|
||||
&& self.habit == o.habit
|
||||
&& self.medals_seen == o.medals_seen
|
||||
}
|
||||
}
|
||||
|
||||
@@ -638,7 +674,7 @@ impl Library {
|
||||
self.highlights.remove(id);
|
||||
self.removed_highlights.insert(id.clone());
|
||||
}
|
||||
Op::Session { id, book, start, end, active_ms, pages, date } => {
|
||||
Op::Session { id, book, start, end, active_ms, pages, date, offset_min } => {
|
||||
let c = self.canonical(book);
|
||||
if self.books.contains_key(&c) && end >= start {
|
||||
self.sessions.entry(id.clone()).or_insert(Session {
|
||||
@@ -649,12 +685,23 @@ impl Library {
|
||||
active_ms: (*active_ms).min(end - start),
|
||||
pages: *pages,
|
||||
date: date.clone(),
|
||||
offset_min: offset_min.filter(|o| (-14 * 60..=14 * 60).contains(o)),
|
||||
});
|
||||
}
|
||||
}
|
||||
Op::Goal { year, kind, target } => {
|
||||
self.goals.insert(*year, Goal { year: *year, kind: *kind, target: *target });
|
||||
}
|
||||
Op::HabitGoal { goal } => {
|
||||
if goal.amount >= 1 && goal.amount <= 10_000 && goal.rollover_hour <= 12 {
|
||||
self.habit = Some(*goal);
|
||||
}
|
||||
}
|
||||
Op::MedalSeen { medal } => {
|
||||
if medal.len() <= 64 {
|
||||
self.medals_seen.insert(medal.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user