Tell why the home has no cards yet and tidy the cards code after review (#72)
Without statistics the home said « Aucun champion trouvé » under a count of 0; it now shows the same notice as the champ select (loading, no Account), from a small StatsNotice enum instead of compared strings. Also from the review: a structural test that hovering or scrolling « Autres champions » recomputes no statistics, as docs/performance.md says; English comments; one card aspect (core's CARD_ASPECT) and one cards_dir; one builder for the champ select cards, one count badge, one test temp dir; clearer names (with_cards_behaviour, home_has_own_scope). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
213d693ab8
commit
72f7a239c0
8 files changed
+303
-106
No files matched your search
+182
-43
@@ -136,9 +136,6 @@ pub struct Page {
|
||||
champ_select_account: Option<String>,
|
||||
/// The Riot ID `champ_select_account` was resolved for.
|
||||
champ_select_riot_id: Option<String>,
|
||||
/// What the Anvil champions area says while the statistics are missing, else empty; prepared
|
||||
/// when the champ select, the statistics or the status change.
|
||||
champ_select_notice: SharedString,
|
||||
accounts: Vec<Account>,
|
||||
last_active: Option<Account>,
|
||||
/// The Account the user picked instead of following the one being updated.
|
||||
@@ -381,7 +378,6 @@ impl Page {
|
||||
champ_select: None,
|
||||
champ_select_account: None,
|
||||
champ_select_riot_id: None,
|
||||
champ_select_notice: SharedString::default(),
|
||||
accounts: Vec::new(),
|
||||
last_active: None,
|
||||
pinned: None,
|
||||
@@ -519,6 +515,33 @@ impl Page {
|
||||
}
|
||||
}
|
||||
|
||||
/// Why the home has no statistics to show yet, if it has none: the same for the Défi tab
|
||||
/// and the champ select. Cheap enough to ask at each render.
|
||||
fn stats_notice(&self) -> Option<home::StatsNotice> {
|
||||
use home::StatsNotice;
|
||||
if self.error().is_some() {
|
||||
return Some(StatsNotice::Error);
|
||||
}
|
||||
if self.home_statistics().is_some() {
|
||||
return None;
|
||||
}
|
||||
if self.home_season.is_none() {
|
||||
return Some(StatsNotice::NoSeason);
|
||||
}
|
||||
// During a champ select the Account is the one signed in to the client, known by the
|
||||
// follower's status; else it is the one viewed.
|
||||
let no_account = if self.in_champ_select() {
|
||||
self.status.updating.is_none()
|
||||
} else {
|
||||
self.viewed().is_none()
|
||||
};
|
||||
Some(if no_account {
|
||||
StatsNotice::NoAccount
|
||||
} else {
|
||||
StatsNotice::Loading
|
||||
})
|
||||
}
|
||||
|
||||
/// The tab's own statistics error: the home may have computed its own.
|
||||
fn error(&self) -> Option<&String> {
|
||||
match (self.tab, &self.home_scope) {
|
||||
@@ -1004,32 +1027,20 @@ impl Page {
|
||||
self.champ_select.as_ref().map(|(_, pick)| pick)
|
||||
}
|
||||
|
||||
/// What the Anvil champions area shows: the row's cards, or the notice that stands in
|
||||
/// for them. For when the champ select, the statistics or the status change.
|
||||
/// The cards of the Anvil champions row (`stats_notice` says why there are none when the
|
||||
/// statistics are missing). For when the champ select, the statistics or the status change.
|
||||
fn prepare_champ_select(&mut self, cx: &mut Context<Self>) {
|
||||
// Statistics are missing rather than absent when they are still on their way.
|
||||
self.champ_select_notice = match (&self.stats_error, self.stats.is_some()) {
|
||||
(Some(error), _) => error.clone().into(),
|
||||
(None, true) => SharedString::default(),
|
||||
(None, false) if self.current_season.is_none() => home::NO_SEASON.into(),
|
||||
(None, false) if self.status.updating.is_none() => "Compte indisponible".into(),
|
||||
(None, false) => "Chargement des statistiques…".into(),
|
||||
};
|
||||
// The same rows in the same order as the home grid (`ordered_rows`): the
|
||||
// cards keep their place whatever the champ select does. An id the rows do not know is
|
||||
// skipped.
|
||||
let cards: Rc<[Card]> = match self.live_champ_select() {
|
||||
Some(pick) => {
|
||||
let anvil = pick.anvil_champions.as_deref().unwrap_or_default();
|
||||
self.ordered_rows()
|
||||
.filter(|row| anvil.contains(&row.champion.id))
|
||||
.map(|row| {
|
||||
Card::from_row(row).with_unpickable(pick.unpickable(row.champion.id))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
None => Rc::new([]),
|
||||
};
|
||||
let anvil = self
|
||||
.live_champ_select()
|
||||
.and_then(|pick| pick.anvil_champions.as_deref())
|
||||
.unwrap_or_default();
|
||||
let cards = self.champ_select_cards(
|
||||
self.ordered_rows()
|
||||
.filter(|row| anvil.contains(&row.champion.id)),
|
||||
);
|
||||
self.anvil_count = cards.len().to_string().into();
|
||||
let shown = (0..cards.len()).collect();
|
||||
self.anvil_grid.update(cx, |grid, cx| {
|
||||
@@ -1039,6 +1050,17 @@ impl Page {
|
||||
self.prepare_others(cx);
|
||||
}
|
||||
|
||||
/// The cards of `rows` during the champ select, each with the reasons it cannot be picked;
|
||||
/// none outside a champ select.
|
||||
fn champ_select_cards<'a>(&self, rows: impl Iterator<Item = &'a ChampionRow>) -> Rc<[Card]> {
|
||||
match self.live_champ_select() {
|
||||
Some(pick) => rows
|
||||
.map(|row| Card::from_row(row).with_unpickable(pick.unpickable(row.champion.id)))
|
||||
.collect(),
|
||||
None => Rc::new([]),
|
||||
}
|
||||
}
|
||||
|
||||
/// The rows of « Autres champions »: those whose id is not an Anvil one, in the master
|
||||
/// order; all of them when the client gave no list.
|
||||
fn other_rows(&self) -> impl Iterator<Item = &ChampionRow> {
|
||||
@@ -1055,13 +1077,7 @@ impl Page {
|
||||
if !self.others_open {
|
||||
return;
|
||||
}
|
||||
let cards: Rc<[Card]> = match self.live_champ_select() {
|
||||
Some(pick) => self
|
||||
.other_rows()
|
||||
.map(|row| Card::from_row(row).with_unpickable(pick.unpickable(row.champion.id)))
|
||||
.collect(),
|
||||
None => Rc::new([]),
|
||||
};
|
||||
let cards = self.champ_select_cards(self.other_rows());
|
||||
self.others_grid
|
||||
.update(cx, |grid, cx| grid.set_cards(cards, cx));
|
||||
self.refresh_others(cx);
|
||||
@@ -1771,6 +1787,83 @@ mod tests {
|
||||
});
|
||||
}
|
||||
|
||||
/// Without statistics the home says why there is nothing, rather than count « 0 » champions
|
||||
/// above a grid that says none matches.
|
||||
fn assert_home_says(window: &mut VisualTestContext, notice: super::home::StatsNotice) {
|
||||
draw(window);
|
||||
assert_eq!(
|
||||
window.debug_bounds("home-notice").is_some(),
|
||||
!matches!(notice, super::home::StatsNotice::Error)
|
||||
);
|
||||
assert!(window.debug_bounds("home-tools").is_none());
|
||||
assert!(window.debug_bounds("card-list").is_none());
|
||||
assert!(window.debug_bounds("no-champion").is_none());
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn the_home_says_why_it_has_no_cards_until_the_statistics_arrive(cx: &mut TestAppContext) {
|
||||
use super::home::StatsNotice;
|
||||
// A Season but no Account at all.
|
||||
let (page, cx) = open_page_in_season(cx);
|
||||
let cx = &mut { cx };
|
||||
page.read_with(cx, |p, _| {
|
||||
assert_eq!(p.stats_notice(), Some(StatsNotice::NoAccount));
|
||||
assert_eq!(
|
||||
p.anvil_line().as_deref(),
|
||||
Some("Compte indisponible"),
|
||||
"the champ select says it the same way"
|
||||
);
|
||||
});
|
||||
assert_home_says(cx, StatsNotice::NoAccount);
|
||||
|
||||
// The statistics arrive: the heading and the grid show, and the notice goes.
|
||||
show(&page, cx, statistics(0, 3, champions(3)));
|
||||
draw(cx);
|
||||
page.read_with(cx, |p, _| assert_eq!(p.stats_notice(), None));
|
||||
assert!(cx.debug_bounds("home-notice").is_none());
|
||||
assert!(cx.debug_bounds("home-tools").is_some());
|
||||
assert!(cx.debug_bounds("card-1").is_some());
|
||||
assert!(cx.debug_bounds("no-champion").is_none());
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn the_home_says_the_statistics_are_loading(cx: &mut TestAppContext) {
|
||||
use super::home::StatsNotice;
|
||||
cx.executor().allow_parking();
|
||||
let (page, mut window) = open_page_with_tracker(cx, joueur_tracker());
|
||||
cx.condition(&page, |p, _| p.stats.is_some()).await;
|
||||
// The job of an Account is running: nothing is there yet.
|
||||
window.update(|_, cx| {
|
||||
page.update(cx, |p, cx| {
|
||||
p.stats = None;
|
||||
p.stats_error = None;
|
||||
p.prepare_home_cards(cx);
|
||||
cx.notify();
|
||||
})
|
||||
});
|
||||
page.read_with(&window, |p, _| {
|
||||
assert!(p.home_season.is_some());
|
||||
assert_eq!(p.stats_notice(), Some(StatsNotice::Loading));
|
||||
assert_eq!(
|
||||
p.stats_notice().map(|n| n.label(None)).as_deref(),
|
||||
Some("Chargement des statistiques…")
|
||||
);
|
||||
});
|
||||
assert_home_says(&mut window, StatsNotice::Loading);
|
||||
|
||||
// Its error is the banner's alone.
|
||||
window.update(|_, cx| {
|
||||
page.update(cx, |p, cx| {
|
||||
p.stats_error = Some("Statistiques indisponibles : disque plein".into());
|
||||
cx.notify();
|
||||
})
|
||||
});
|
||||
page.read_with(&window, |p, _| {
|
||||
assert_eq!(p.stats_notice(), Some(StatsNotice::Error));
|
||||
});
|
||||
assert_home_says(&mut window, StatsNotice::Error);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn the_counter_keeps_its_total_while_a_search_and_a_filter_reduce_the_list(
|
||||
cx: &mut TestAppContext,
|
||||
@@ -2607,14 +2700,10 @@ mod tests {
|
||||
let tracker = Tracker::open(Path::new(":memory:"), Box::new(JoueurHistory))
|
||||
.unwrap()
|
||||
.with_seasons(vec![season("Saison", 2020)]);
|
||||
// One folder per tracker: the tests run in parallel.
|
||||
static TRACKERS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||
let n = TRACKERS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
let portraits =
|
||||
std::env::temp_dir().join(format!("arena-tracker-74-{}-{n}", std::process::id()));
|
||||
let tracker = tracker.with_champions(Box::new(Catalog), portraits.clone());
|
||||
// One folder per tracker (the tests run in parallel), removed once the catalog is stored.
|
||||
let portraits = crate::card_images::tests::TestDir::new("page-tracker");
|
||||
let tracker = tracker.with_champions(Box::new(Catalog), portraits.path().to_path_buf());
|
||||
tracker.refresh_champions().unwrap();
|
||||
let _ = std::fs::remove_dir(portraits);
|
||||
if mastery {
|
||||
tracker.sync("Autre#EUW", &mut |_| {}).unwrap();
|
||||
// Without a client, a round adopts the Riot ID it is given, then asks for the points.
|
||||
@@ -2911,7 +3000,7 @@ mod tests {
|
||||
page.read_with(&window, |p, _| {
|
||||
assert_eq!(p.anvil_line().as_deref(), Some(line))
|
||||
});
|
||||
// No row, and no champion is presented as « sans enclume ».
|
||||
// No row, and no champion is presented as without an anvil.
|
||||
assert!(anvil_cards(&page, &mut window).is_empty());
|
||||
assert!(window.debug_bounds("anvil-notice").is_some());
|
||||
assert!(window.debug_bounds("anvil-row").is_none());
|
||||
@@ -2928,7 +3017,7 @@ mod tests {
|
||||
send(&page, &mut window, &frames()[0]);
|
||||
draw(&mut window);
|
||||
page.read_with(&window, |p, _| {
|
||||
assert_eq!(p.champ_select_notice, "Aucune Saison en cours");
|
||||
assert_eq!(p.stats_notice(), Some(super::home::StatsNotice::NoSeason));
|
||||
});
|
||||
assert!(window.debug_bounds("no-season").is_some());
|
||||
assert!(window.debug_bounds("anvil-notice").is_none());
|
||||
@@ -3110,6 +3199,56 @@ mod tests {
|
||||
assert_eq!(ids(&anvil_cards(&page, &mut window)), [233]);
|
||||
}
|
||||
|
||||
// As the home grid's test: hovering the cards of « Autres champions » or scrolling it notifies
|
||||
// the grid, and costs no statistics and no cards. (The capture's catalog is too short for
|
||||
// the list to scroll: the wheel still reaches the grid, which is what clears its hover.)
|
||||
#[gpui::test]
|
||||
async fn hovering_or_scrolling_others_does_not_recompute_the_statistics(
|
||||
cx: &mut TestAppContext,
|
||||
) {
|
||||
let (page, mut window) = page_in_champ_select(cx, &frames()[0]).await;
|
||||
click_others(&mut window);
|
||||
let cx = &mut window;
|
||||
let grid = page.read_with(cx, |p, _| p.others_grid.clone());
|
||||
let generation = page.read_with(cx, |p, _| p.stats_generation);
|
||||
let set_cards = grid.read_with(cx, |grid, _| grid.set_cards_calls);
|
||||
let renders = grid.read_with(cx, |grid, _| grid.renders);
|
||||
assert!(set_cards > 0 && renders > 0);
|
||||
|
||||
let hovers = Rc::new(Cell::new(0));
|
||||
let _subscription = cx.update(|_, cx| {
|
||||
let hovers = hovers.clone();
|
||||
cx.observe(&grid, move |_, _| hovers.set(hovers.get() + 1))
|
||||
});
|
||||
for id in ["card-103", "card-12", "card-103"] {
|
||||
let card = center(cx, id);
|
||||
cx.simulate_mouse_move(card, None, Modifiers::none());
|
||||
draw(cx);
|
||||
}
|
||||
assert!(hovers.get() > 0, "no card was hovered");
|
||||
assert!(grid.read_with(cx, |grid, _| grid.renders) > renders);
|
||||
|
||||
let over = center(cx, "card-103");
|
||||
cx.simulate_event(ScrollEvent {
|
||||
position: over,
|
||||
delta: ScrollDelta::Pixels(point(px(0.), px(-400.))),
|
||||
..Default::default()
|
||||
});
|
||||
draw(cx);
|
||||
|
||||
assert_eq!(
|
||||
grid.read_with(cx, |grid, _| grid.set_cards_calls),
|
||||
set_cards,
|
||||
"the cards were built again"
|
||||
);
|
||||
assert_eq!(
|
||||
page.read_with(cx, |p, _| p.stats_generation),
|
||||
generation,
|
||||
"the statistics were computed again"
|
||||
);
|
||||
assert!(!page.read_with(cx, |p, _| p.stats_job.running()));
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn the_search_of_others_filters_only_that_section(cx: &mut TestAppContext) {
|
||||
let (page, mut window) = page_in_champ_select(cx, &frames()[0]).await;
|
||||
@@ -3517,7 +3656,7 @@ mod tests {
|
||||
});
|
||||
draw(&mut window);
|
||||
page.read_with(&window, |p, _| {
|
||||
assert_eq!(p.champ_select_notice, error);
|
||||
assert_eq!(p.stats_notice(), Some(super::home::StatsNotice::Error));
|
||||
assert_eq!(p.error().map(String::as_str), Some(error));
|
||||
});
|
||||
// The banner says it; the area says nothing and offers nothing to browse.
|
||||
|
||||
Reference in new issue
Block a user