feat(content): update installed content atomically
This commit is contained in:
1 parent
aa03dac34d
commit
bbf0317775
14 files changed
+877
-101
No files matched your search
@@ -135,14 +135,6 @@ struct PendingProviderIcon {
|
||||
project_id: String,
|
||||
bytes: Vec<u8>,
|
||||
description: String,
|
||||
update_available: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
struct ProviderUpdateContext {
|
||||
game_version: String,
|
||||
loader: crate::instance::ModLoader,
|
||||
kind: crate::instance::ContentKind,
|
||||
}
|
||||
|
||||
struct DisplayMetadata {
|
||||
@@ -189,7 +181,6 @@ pub struct ContentListState {
|
||||
requested_provider_icons: HashSet<(String, String)>,
|
||||
provider_icon_meta_dir: Option<std::path::PathBuf>,
|
||||
provider_icon_client: Option<crate::net::HttpClient>,
|
||||
provider_update_context: Option<ProviderUpdateContext>,
|
||||
images_dirty: bool,
|
||||
display_metadata: HashMap<String, DisplayMetadata>,
|
||||
pub search: crate::tui::widgets::search::SearchState,
|
||||
@@ -231,7 +222,6 @@ impl Default for ContentListState {
|
||||
requested_provider_icons: HashSet::new(),
|
||||
provider_icon_meta_dir: None,
|
||||
provider_icon_client: None,
|
||||
provider_update_context: None,
|
||||
images_dirty: true,
|
||||
display_metadata: HashMap::new(),
|
||||
search: crate::tui::widgets::search::SearchState::default(),
|
||||
@@ -330,25 +320,27 @@ impl ContentListState {
|
||||
self.provider_icon_client = Some(client);
|
||||
}
|
||||
|
||||
pub fn set_provider_update_context(
|
||||
pub fn apply_update_snapshot(
|
||||
&mut self,
|
||||
instance: &crate::instance::InstanceConfig,
|
||||
kind: crate::instance::ContentKind,
|
||||
) {
|
||||
let context = ProviderUpdateContext {
|
||||
game_version: instance.game_version.clone(),
|
||||
loader: instance.loader,
|
||||
kind,
|
||||
};
|
||||
if self.provider_update_context.as_ref() != Some(&context) {
|
||||
self.provider_update_context = Some(context);
|
||||
self.requested_provider_icons.clear();
|
||||
for entry in &mut self.entries {
|
||||
if entry.title_suffix.as_deref() == Some("Update") {
|
||||
entry.title_suffix = None;
|
||||
}
|
||||
snapshot: Option<&crate::instance::content::updates::UpdateSnapshot>,
|
||||
) -> bool {
|
||||
let mut changed = false;
|
||||
for entry in &mut self.entries {
|
||||
let update = entry.provider_project.as_ref().is_some_and(|installed| {
|
||||
snapshot
|
||||
.and_then(|snapshot| snapshot.update_for(installed))
|
||||
.is_some()
|
||||
});
|
||||
let suffix = update.then(|| "Update".to_owned());
|
||||
if entry.title_suffix != suffix {
|
||||
entry.title_suffix = suffix;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if changed {
|
||||
crate::feedback::request_redraw();
|
||||
}
|
||||
changed
|
||||
}
|
||||
|
||||
pub fn drain_provider_icons(&mut self) -> bool {
|
||||
@@ -378,11 +370,6 @@ impl ContentListState {
|
||||
.insert(entry.file_stem.clone(), display_metadata(entry));
|
||||
changed = true;
|
||||
}
|
||||
let suffix = metadata.update_available.then(|| "Update".to_owned());
|
||||
if entry.title_suffix != suffix {
|
||||
entry.title_suffix = suffix;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if changed {
|
||||
@@ -409,22 +396,18 @@ impl ContentListState {
|
||||
let slots = PROVIDER_ICON_SLOTS.clone();
|
||||
let meta_dir = meta_dir.clone();
|
||||
let client = client.clone();
|
||||
let update_context = self.provider_update_context.clone();
|
||||
tokio::spawn(async move {
|
||||
let Ok(_permit) = slots.acquire_owned().await else {
|
||||
return;
|
||||
};
|
||||
match load_provider_metadata(&client, &meta_dir, &project, update_context.as_ref())
|
||||
.await
|
||||
{
|
||||
Ok((bytes, description, update_available)) => {
|
||||
match load_provider_metadata(&client, &meta_dir, &project).await {
|
||||
Ok((bytes, description)) => {
|
||||
if let Ok(mut pending) = pending.lock() {
|
||||
pending.push(PendingProviderIcon {
|
||||
provider: project.provider,
|
||||
project_id: project.project_id,
|
||||
bytes,
|
||||
description,
|
||||
update_available,
|
||||
});
|
||||
crate::feedback::request_redraw();
|
||||
}
|
||||
@@ -463,9 +446,7 @@ impl ContentListState {
|
||||
if visible_height == 0 {
|
||||
continue;
|
||||
}
|
||||
if (entry.icon_bytes.is_none()
|
||||
|| entry.description.trim().is_empty()
|
||||
|| self.provider_update_context.is_some())
|
||||
if (entry.icon_bytes.is_none() || entry.description.trim().is_empty())
|
||||
&& let Some(project) = entry.provider_project.clone()
|
||||
{
|
||||
projects.push(project);
|
||||
@@ -1346,8 +1327,7 @@ async fn load_provider_metadata(
|
||||
client: &crate::net::HttpClient,
|
||||
meta_dir: &Path,
|
||||
installed: &crate::instance::ProviderProject,
|
||||
update_context: Option<&ProviderUpdateContext>,
|
||||
) -> Result<(Vec<u8>, String, bool), crate::net::NetError> {
|
||||
) -> Result<(Vec<u8>, String), crate::net::NetError> {
|
||||
let provider_id = &installed.provider;
|
||||
let project_id = &installed.project_id;
|
||||
let metadata = crate::storage::MetadataPaths::new(meta_dir);
|
||||
@@ -1376,9 +1356,7 @@ async fn load_provider_metadata(
|
||||
let project = match provider.project(project_id).await {
|
||||
Ok(project) => project,
|
||||
Err(error) => {
|
||||
return cached_icon
|
||||
.map(|bytes| (bytes, String::new(), false))
|
||||
.ok_or(error);
|
||||
return cached_icon.map(|bytes| (bytes, String::new())).ok_or(error);
|
||||
}
|
||||
};
|
||||
crate::storage::write_atomic(
|
||||
@@ -1389,27 +1367,6 @@ async fn load_provider_metadata(
|
||||
project
|
||||
}
|
||||
};
|
||||
let update_available = match update_context {
|
||||
Some(context) => match provider
|
||||
.compatible_versions(
|
||||
project_id,
|
||||
context.kind,
|
||||
&context.game_version,
|
||||
context.loader,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(versions) => crate::instance::content::provider::has_newer_compatible_version(
|
||||
&versions,
|
||||
&installed.version_id,
|
||||
),
|
||||
Err(error) => {
|
||||
tracing::debug!("Could not check updates for project '{project_id}': {error}");
|
||||
false
|
||||
}
|
||||
},
|
||||
None => false,
|
||||
};
|
||||
let bytes = match (cached_icon, project.icon_url.as_deref()) {
|
||||
(Some(bytes), _) => bytes,
|
||||
(None, Some(url)) => match provider.icon(url).await {
|
||||
@@ -1428,7 +1385,7 @@ async fn load_provider_metadata(
|
||||
},
|
||||
(None, None) => Vec::new(),
|
||||
};
|
||||
Ok((bytes, project.description, update_available))
|
||||
Ok((bytes, project.description))
|
||||
}
|
||||
|
||||
pub fn handle_key_no_toggle(key_event: &KeyEvent, state: &mut ContentListState) -> bool {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
pub mod discovery;
|
||||
pub mod list;
|
||||
pub mod tabs;
|
||||
pub mod update;
|
||||
|
||||
pub use discovery::{ContentMode, DiscoveryState};
|
||||
pub use list::{ContentListState, handle_key, handle_key_no_toggle};
|
||||
|
||||
@@ -0,0 +1,202 @@
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use ratatui::{
|
||||
Frame,
|
||||
layout::{Constraint, Flex, Layout},
|
||||
style::{Modifier, Style},
|
||||
text::{Line, Span},
|
||||
widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph},
|
||||
};
|
||||
|
||||
use crate::config::theme::{BORDER_STYLE, THEME};
|
||||
use crate::instance::ContentKind;
|
||||
use crate::instance::content::updates::{BulkUpdatePlan, UpdateSnapshot};
|
||||
|
||||
pub enum PendingResult {
|
||||
Prepared(UpdateSnapshot, BulkUpdatePlan),
|
||||
Applied(Result<(), String>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Phase {
|
||||
Checking,
|
||||
Conflicts,
|
||||
Review,
|
||||
Applying,
|
||||
}
|
||||
|
||||
pub struct State {
|
||||
pub phase: Phase,
|
||||
pub plan: Option<BulkUpdatePlan>,
|
||||
pub snapshot: Option<UpdateSnapshot>,
|
||||
pub selected: usize,
|
||||
pub retry_conflicts: Vec<bool>,
|
||||
pub error: Option<String>,
|
||||
pub pending: Arc<Mutex<Vec<PendingResult>>>,
|
||||
pub kind: ContentKind,
|
||||
pub target_world: Option<(String, std::path::PathBuf)>,
|
||||
pub completed: bool,
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub fn checking(kind: ContentKind, target_world: Option<(String, std::path::PathBuf)>) -> Self {
|
||||
Self {
|
||||
phase: Phase::Checking,
|
||||
plan: None,
|
||||
snapshot: None,
|
||||
selected: 0,
|
||||
retry_conflicts: Vec::new(),
|
||||
error: None,
|
||||
pending: Arc::new(Mutex::new(Vec::new())),
|
||||
kind,
|
||||
target_world,
|
||||
completed: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn drain(&mut self) -> bool {
|
||||
let pending = match self.pending.lock() {
|
||||
Ok(mut pending) => pending.drain(..).collect::<Vec<_>>(),
|
||||
Err(_) => return false,
|
||||
};
|
||||
let mut changed = false;
|
||||
for result in pending {
|
||||
changed = true;
|
||||
match result {
|
||||
PendingResult::Prepared(snapshot, plan) => {
|
||||
self.retry_conflicts = vec![false; plan.conflicts.len()];
|
||||
self.phase = if plan.conflicts.is_empty() {
|
||||
Phase::Review
|
||||
} else {
|
||||
Phase::Conflicts
|
||||
};
|
||||
self.selected = 0;
|
||||
self.snapshot = Some(snapshot);
|
||||
self.plan = Some(plan);
|
||||
self.error = None;
|
||||
}
|
||||
PendingResult::Applied(Ok(())) => self.completed = true,
|
||||
PendingResult::Applied(Err(error)) => {
|
||||
self.phase = Phase::Review;
|
||||
self.error = Some(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
changed
|
||||
}
|
||||
|
||||
pub fn push(&self, result: PendingResult) {
|
||||
if let Ok(mut pending) = self.pending.lock() {
|
||||
pending.push(result);
|
||||
crate::feedback::request_redraw();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn has_retry(&self) -> bool {
|
||||
self.retry_conflicts.iter().any(|retry| *retry)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(frame: &mut Frame, state: &State) {
|
||||
let theme = THEME.as_ref();
|
||||
let item_count = match state.phase {
|
||||
Phase::Conflicts => state.plan.as_ref().map_or(0, |plan| plan.conflicts.len()),
|
||||
Phase::Review => state.plan.as_ref().map_or(0, |plan| plan.roots.len()),
|
||||
_ => 1,
|
||||
};
|
||||
let height = (item_count as u16 + 5).clamp(7, 20);
|
||||
let [area] = Layout::vertical([Constraint::Length(height)])
|
||||
.flex(Flex::Center)
|
||||
.areas(frame.area());
|
||||
let [area] = Layout::horizontal([Constraint::Percentage(62)])
|
||||
.flex(Flex::Center)
|
||||
.areas(area);
|
||||
frame.render_widget(Clear, area);
|
||||
|
||||
let (title, footer) = match state.phase {
|
||||
Phase::Checking => (" Check for updates ", " [Esc] close "),
|
||||
Phase::Conflicts => (
|
||||
" Resolve update conflicts ",
|
||||
" [j/k] select [Space] keep/retry [Enter] continue [Esc] close ",
|
||||
),
|
||||
Phase::Review => (
|
||||
" Update installed content ",
|
||||
" [Enter] update all [Esc] close ",
|
||||
),
|
||||
Phase::Applying => (" Updating installed content ", " Please wait "),
|
||||
};
|
||||
let block = Block::default()
|
||||
.title(title)
|
||||
.title_bottom(Line::from(footer).centered())
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BORDER_STYLE.to_border_type())
|
||||
.border_style(Style::default().fg(theme.accent()))
|
||||
.style(Style::default().fg(theme.text()).bg(theme.surface()));
|
||||
|
||||
match state.phase {
|
||||
Phase::Checking | Phase::Applying => {
|
||||
let message = if state.phase == Phase::Checking {
|
||||
"Checking compatible versions and dependencies…"
|
||||
} else {
|
||||
"Installing the accepted update set…"
|
||||
};
|
||||
frame.render_widget(Paragraph::new(message).block(block), area);
|
||||
}
|
||||
Phase::Conflicts => {
|
||||
let items = state
|
||||
.plan
|
||||
.as_ref()
|
||||
.into_iter()
|
||||
.flat_map(|plan| plan.conflicts.iter())
|
||||
.enumerate()
|
||||
.map(|(index, conflict)| {
|
||||
let action = if state.retry_conflicts.get(index) == Some(&true) {
|
||||
"Retry"
|
||||
} else {
|
||||
"Keep"
|
||||
};
|
||||
ListItem::new(vec![
|
||||
Line::from(vec![
|
||||
Span::styled(
|
||||
conflict.title.clone(),
|
||||
Style::default().add_modifier(Modifier::BOLD),
|
||||
),
|
||||
Span::raw(" "),
|
||||
Span::styled(action, Style::default().fg(theme.accent())),
|
||||
]),
|
||||
Line::from(conflict.reason.clone())
|
||||
.style(Style::default().fg(theme.text_dim())),
|
||||
])
|
||||
});
|
||||
let mut selected = ListState::default().with_selected(Some(state.selected));
|
||||
let list = List::new(items)
|
||||
.block(block)
|
||||
.highlight_symbol("▌")
|
||||
.highlight_style(Style::default().bg(theme.stripe()));
|
||||
frame.render_stateful_widget(list, area, &mut selected);
|
||||
}
|
||||
Phase::Review => {
|
||||
let mut items = state
|
||||
.plan
|
||||
.as_ref()
|
||||
.into_iter()
|
||||
.flat_map(|plan| plan.roots.iter())
|
||||
.map(|root| {
|
||||
ListItem::new(Line::from(format!(
|
||||
"• {} {} → {}",
|
||||
root.title, root.current_version, root.target.version_number
|
||||
)))
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
if items.is_empty() {
|
||||
items.push(ListItem::new("No compatible updates available."));
|
||||
}
|
||||
if let Some(error) = &state.error {
|
||||
items.push(ListItem::new(
|
||||
Line::from(error.clone()).style(Style::default().fg(theme.error())),
|
||||
));
|
||||
}
|
||||
frame.render_widget(List::new(items).block(block), area);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user