Trie les articles par catégorie puis par ordre alphabétique
Les catégories suivent l'ordre de l'enum (parcours du magasin) ; les noms sont comparés sans tenir compte de la casse ni des accents. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2346deab3c
commit
d64aee2d85
2 files changed
+58
-2
No files matched your search
+3
-1
@@ -261,7 +261,9 @@ fn ListsContent(
|
|||||||
.into_any();
|
.into_any();
|
||||||
};
|
};
|
||||||
|
|
||||||
// Items with pending checkbox changes applied.
|
// Items with pending checkbox changes applied, by category then name.
|
||||||
|
let mut items = items;
|
||||||
|
items.sort_by_cached_key(Item::sort_key);
|
||||||
let items = StoredValue::new(items);
|
let items = StoredValue::new(items);
|
||||||
let current = Memo::new(move |_| {
|
let current = Memo::new(move |_| {
|
||||||
let optimistic = ctx.optimistic.get();
|
let optimistic = ctx.optimistic.get();
|
||||||
|
|||||||
+55
-1
@@ -98,6 +98,34 @@ pub struct Item {
|
|||||||
pub checked: bool,
|
pub checked: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Item {
|
||||||
|
/// Display order: by category (aisle order of `Category`), then by name.
|
||||||
|
pub fn sort_key(&self) -> (Category, String) {
|
||||||
|
(self.category, fold_name(&self.name))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Lowercases and strips French accents so "Œufs" sorts with "o" and
|
||||||
|
/// "Épinards" with "e".
|
||||||
|
fn fold_name(name: &str) -> String {
|
||||||
|
let mut folded = String::with_capacity(name.len());
|
||||||
|
for c in name.chars().flat_map(char::to_lowercase) {
|
||||||
|
match c {
|
||||||
|
'à' | 'â' | 'ä' => folded.push('a'),
|
||||||
|
'é' | 'è' | 'ê' | 'ë' => folded.push('e'),
|
||||||
|
'î' | 'ï' => folded.push('i'),
|
||||||
|
'ô' | 'ö' => folded.push('o'),
|
||||||
|
'ù' | 'û' | 'ü' => folded.push('u'),
|
||||||
|
'ÿ' => folded.push('y'),
|
||||||
|
'ç' => folded.push('c'),
|
||||||
|
'œ' => folded.push_str("oe"),
|
||||||
|
'æ' => folded.push_str("ae"),
|
||||||
|
_ => folded.push(c),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
folded
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
|
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct Quantity {
|
pub struct Quantity {
|
||||||
pub amount: f64,
|
pub amount: f64,
|
||||||
@@ -148,7 +176,8 @@ impl Unit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
/// Variant order is the display order of items.
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
|
||||||
pub enum Category {
|
pub enum Category {
|
||||||
FruitsLegumes,
|
FruitsLegumes,
|
||||||
Frais,
|
Frais,
|
||||||
@@ -270,6 +299,31 @@ mod tests {
|
|||||||
assert_eq!(user("élise").initials(), "ÉL");
|
assert_eq!(user("élise").initials(), "ÉL");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn items_sort_by_category_then_name() {
|
||||||
|
let item = |name: &str, category| Item {
|
||||||
|
id: 0,
|
||||||
|
list_id: 0,
|
||||||
|
name: name.into(),
|
||||||
|
quantity: None,
|
||||||
|
icon: Icon::ALL[0],
|
||||||
|
category,
|
||||||
|
checked: false,
|
||||||
|
};
|
||||||
|
let mut items = vec![
|
||||||
|
item("Pain", Category::Boulangerie),
|
||||||
|
item("Yaourts", Category::Frais),
|
||||||
|
item("Œufs", Category::Frais),
|
||||||
|
item("beurre", Category::Frais),
|
||||||
|
item("Épinards", Category::FruitsLegumes),
|
||||||
|
item("Citrons", Category::FruitsLegumes),
|
||||||
|
item("Olives", Category::Frais),
|
||||||
|
];
|
||||||
|
items.sort_by_key(Item::sort_key);
|
||||||
|
let names: Vec<&str> = items.iter().map(|i| i.name.as_str()).collect();
|
||||||
|
assert_eq!(names, ["Citrons", "Épinards", "beurre", "Œufs", "Olives", "Yaourts", "Pain"]);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn text_roundtrip() {
|
fn text_roundtrip() {
|
||||||
for icon in Icon::ALL {
|
for icon in Icon::ALL {
|
||||||
|
|||||||
Reference in new issue
Block a user