refactor(content): keep installed sorting file-based
This commit is contained in:
1 parent
8c7bbb9a79
commit
dcefb3b7ba
9 files changed
+56
-215
No files matched your search
@@ -735,7 +735,6 @@ pub struct DiscoveryState {
|
||||
pub sort_panel_page: DiscoveryPanelPage,
|
||||
pub sort_panel_selected: usize,
|
||||
pub local_mode: bool,
|
||||
pub local_ranking_index: usize,
|
||||
pub local_sort_index: usize,
|
||||
pub local_sort_descending: bool,
|
||||
pub filter_panel_selected: usize,
|
||||
@@ -859,7 +858,6 @@ impl DiscoveryState {
|
||||
sort_panel_page: DiscoveryPanelPage::default(),
|
||||
sort_panel_selected: 0,
|
||||
local_mode: false,
|
||||
local_ranking_index: 0,
|
||||
local_sort_index: 5,
|
||||
local_sort_descending: false,
|
||||
filter_panel_selected: 0,
|
||||
@@ -931,6 +929,14 @@ impl DiscoveryState {
|
||||
return;
|
||||
}
|
||||
self.local_mode = installed;
|
||||
self.sort_panel_selected = if installed {
|
||||
self.local_sort_index - 5
|
||||
} else {
|
||||
crate::instance::content::provider::DiscoverySort::ALL
|
||||
.iter()
|
||||
.position(|sort| *sort == self.sort)
|
||||
.unwrap_or(0)
|
||||
};
|
||||
if installed {
|
||||
let local = self.installed_filters.take().unwrap_or_default();
|
||||
self.discovery_filters = Some(std::mem::replace(&mut self.filters, local));
|
||||
@@ -964,7 +970,6 @@ impl DiscoveryState {
|
||||
|
||||
fn reset_sort(&mut self) {
|
||||
if self.local_mode {
|
||||
self.local_ranking_index = 0;
|
||||
self.local_sort_index = 5;
|
||||
self.local_sort_descending = false;
|
||||
return;
|
||||
@@ -1617,21 +1622,18 @@ impl DiscoveryState {
|
||||
|
||||
fn apply_selected_sort(&mut self) {
|
||||
if self.local_mode {
|
||||
if self.sort_panel_selected >= 5 {
|
||||
if self.local_sort_index == self.sort_panel_selected {
|
||||
if self.local_sort_descending {
|
||||
self.local_sort_index = 5;
|
||||
self.local_sort_descending = false;
|
||||
} else {
|
||||
self.local_sort_descending = true;
|
||||
}
|
||||
} else {
|
||||
self.local_sort_index = self.sort_panel_selected;
|
||||
let selected = self.sort_panel_selected + 5;
|
||||
if self.local_sort_index == selected {
|
||||
if self.local_sort_descending {
|
||||
self.local_sort_index = 5;
|
||||
self.local_sort_descending = false;
|
||||
} else {
|
||||
self.local_sort_descending = true;
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
self.local_sort_index = selected;
|
||||
self.local_sort_descending = false;
|
||||
}
|
||||
self.local_ranking_index = self.sort_panel_selected;
|
||||
return;
|
||||
}
|
||||
let sort = crate::instance::content::provider::DiscoverySort::ALL[self.sort_panel_selected];
|
||||
@@ -2313,7 +2315,7 @@ pub fn handle_key(key_event: &KeyEvent, state: &mut DiscoveryState) -> bool {
|
||||
KeyCode::Char('j') | KeyCode::Down => {
|
||||
state.sort_panel_selected =
|
||||
(state.sort_panel_selected + 1).min(if state.local_mode {
|
||||
7
|
||||
2
|
||||
} else {
|
||||
crate::instance::content::provider::DiscoverySort::ALL.len() - 1
|
||||
});
|
||||
@@ -2343,7 +2345,7 @@ pub fn handle_key(key_event: &KeyEvent, state: &mut DiscoveryState) -> bool {
|
||||
state.sort_panel_focused = true;
|
||||
state.sort_panel_page = DiscoveryPanelPage::Filters;
|
||||
state.sort_panel_selected = if state.local_mode {
|
||||
state.local_sort_index
|
||||
state.local_sort_index - 5
|
||||
} else {
|
||||
crate::instance::content::provider::DiscoverySort::ALL
|
||||
.iter()
|
||||
|
||||
@@ -190,7 +190,6 @@ pub struct ContentListState {
|
||||
version_metadata: HashMap<(String, String), crate::net::modrinth::VersionInfo>,
|
||||
requested_provider_icons: HashSet<(String, String, String)>,
|
||||
pub local_panel_open: bool,
|
||||
pub local_ranking_index: usize,
|
||||
pub local_sort_index: usize,
|
||||
pub local_sort_descending: bool,
|
||||
local_filters: crate::tui::widgets::content::discovery::DiscoveryFilters,
|
||||
@@ -241,7 +240,6 @@ impl Default for ContentListState {
|
||||
requested_provider_icons: HashSet::new(),
|
||||
local_panel_open: false,
|
||||
local_sort_index: 0,
|
||||
local_ranking_index: 0,
|
||||
local_sort_descending: false,
|
||||
local_filters: crate::tui::widgets::content::discovery::DiscoveryFilters {
|
||||
game_version: crate::tui::widgets::content::discovery::GameVersionFilter::Any,
|
||||
@@ -1160,27 +1158,20 @@ impl ContentListState {
|
||||
})
|
||||
.map(|(i, _)| i)
|
||||
.collect();
|
||||
if self.local_sort_index != 0 || self.local_ranking_index != 0 {
|
||||
if self.local_sort_index != 0 {
|
||||
indices.sort_by(|&a, &b| {
|
||||
let left = &self.entries[a];
|
||||
let right = &self.entries[b];
|
||||
let compare = |index, descending| {
|
||||
let left_key = self.installed_sort_key(left, index);
|
||||
let right_key = self.installed_sort_key(right, index);
|
||||
match (left_key, right_key) {
|
||||
(Some(left), Some(right)) if descending => right.cmp(&left),
|
||||
(Some(left), Some(right)) => left.cmp(&right),
|
||||
(Some(_), None) => std::cmp::Ordering::Less,
|
||||
(None, Some(_)) => std::cmp::Ordering::Greater,
|
||||
(None, None) => std::cmp::Ordering::Equal,
|
||||
}
|
||||
let order = match (
|
||||
self.installed_sort_key(left),
|
||||
self.installed_sort_key(right),
|
||||
) {
|
||||
(Some(left), Some(right)) if self.local_sort_descending => right.cmp(&left),
|
||||
(Some(left), Some(right)) => left.cmp(&right),
|
||||
(Some(_), None) => std::cmp::Ordering::Less,
|
||||
(None, Some(_)) => std::cmp::Ordering::Greater,
|
||||
(None, None) => std::cmp::Ordering::Equal,
|
||||
};
|
||||
let order = if self.local_sort_index != 0 {
|
||||
compare(self.local_sort_index, self.local_sort_descending)
|
||||
} else {
|
||||
std::cmp::Ordering::Equal
|
||||
}
|
||||
.then_with(|| compare(self.local_ranking_index, true));
|
||||
order.then_with(|| left.name.to_lowercase().cmp(&right.name.to_lowercase()))
|
||||
});
|
||||
}
|
||||
@@ -1190,7 +1181,6 @@ impl ContentListState {
|
||||
fn needs_local_metadata(&self) -> bool {
|
||||
use crate::tui::widgets::content::discovery::{EnvironmentFilter, GameVersionFilter};
|
||||
self.local_panel_open
|
||||
|| self.local_ranking_index != 0
|
||||
|| !self.local_filters.categories.is_empty()
|
||||
|| self.local_filters.environment != EnvironmentFilter::Any
|
||||
|| self.local_filters.game_version != GameVersionFilter::Any
|
||||
@@ -1199,7 +1189,6 @@ impl ContentListState {
|
||||
pub fn set_installed_options(
|
||||
&mut self,
|
||||
filters: &crate::tui::widgets::content::discovery::DiscoveryFilters,
|
||||
ranking_index: usize,
|
||||
sort_index: usize,
|
||||
descending: bool,
|
||||
game_version: &str,
|
||||
@@ -1208,7 +1197,6 @@ impl ContentListState {
|
||||
let selected = self.selected_file_stem();
|
||||
let previously_needed_metadata = self.needs_local_metadata();
|
||||
self.local_filters = filters.clone();
|
||||
self.local_ranking_index = ranking_index;
|
||||
self.local_sort_index = sort_index;
|
||||
self.local_sort_descending = descending;
|
||||
self.local_game_version = game_version.to_owned();
|
||||
@@ -1289,35 +1277,8 @@ impl ContentListState {
|
||||
true
|
||||
}
|
||||
|
||||
fn installed_sort_key(&self, entry: &ContentEntry, index: usize) -> Option<String> {
|
||||
let project = entry.provider_project.as_ref().and_then(|installed| {
|
||||
self.project_metadata
|
||||
.get(&(installed.provider.clone(), installed.project_id.clone()))
|
||||
});
|
||||
match index {
|
||||
1 => project.map(|project| {
|
||||
let popularity = if entry
|
||||
.provider_project
|
||||
.as_ref()
|
||||
.is_some_and(|source| source.provider == "modrinth")
|
||||
{
|
||||
project.followers
|
||||
} else {
|
||||
project.downloads
|
||||
};
|
||||
format!("{popularity:020}")
|
||||
}),
|
||||
2 => project
|
||||
.and_then(|project| {
|
||||
chrono::DateTime::parse_from_rfc3339(&project.date_created).ok()
|
||||
})
|
||||
.map(|date| format!("{:020}", date.timestamp())),
|
||||
3 => project.map(|project| format!("{:020}", project.downloads)),
|
||||
4 => project
|
||||
.and_then(|project| {
|
||||
chrono::DateTime::parse_from_rfc3339(&project.date_modified).ok()
|
||||
})
|
||||
.map(|date| format!("{:020}", date.timestamp())),
|
||||
fn installed_sort_key(&self, entry: &ContentEntry) -> Option<String> {
|
||||
match self.local_sort_index {
|
||||
5 => Some(entry.name.to_lowercase()),
|
||||
// ponytail: file metadata is read during a local sort; cache it if large installed lists make this slow.
|
||||
6 => std::fs::metadata(&entry.path)
|
||||
|
||||
@@ -797,7 +797,6 @@ fn render_installed_list_panel(
|
||||
discovery_state.set_filter_loader(instance.loader);
|
||||
state.set_installed_options(
|
||||
&discovery_state.filters,
|
||||
discovery_state.local_ranking_index,
|
||||
discovery_state.local_sort_index,
|
||||
discovery_state.local_sort_descending,
|
||||
&instance.game_version,
|
||||
@@ -949,13 +948,9 @@ fn render_sort_panel(frame: &mut Frame, area: Rect, state: &mut DiscoveryState)
|
||||
render_discovery_panel_title(frame, area, state);
|
||||
return;
|
||||
}
|
||||
let selected_row = if state.local_mode && state.sort_panel_selected >= 5 {
|
||||
state.sort_panel_selected + 3
|
||||
} else {
|
||||
state.sort_panel_selected + 1
|
||||
};
|
||||
let selected_row = state.sort_panel_selected + 1;
|
||||
let offset = selected_row.saturating_sub(usize::from(inner.height).saturating_sub(1));
|
||||
let row_count = if state.local_mode { 11 } else { 6 };
|
||||
let row_count = if state.local_mode { 4 } else { 6 };
|
||||
for row in offset..row_count.min(offset + usize::from(inner.height)) {
|
||||
let rect = Rect {
|
||||
x: inner.x,
|
||||
@@ -963,18 +958,9 @@ fn render_sort_panel(frame: &mut Frame, area: Rect, state: &mut DiscoveryState)
|
||||
width: inner.width,
|
||||
height: 1,
|
||||
};
|
||||
if row == 0 || (state.local_mode && row == 7) {
|
||||
if row == 0 {
|
||||
frame.render_widget(
|
||||
Paragraph::new(if row == 0 {
|
||||
if state.local_mode {
|
||||
" Project ranking"
|
||||
} else {
|
||||
" Sort by"
|
||||
}
|
||||
} else {
|
||||
" File order"
|
||||
})
|
||||
.style(
|
||||
Paragraph::new(" Sort by").style(
|
||||
Style::default()
|
||||
.fg(theme.text())
|
||||
.add_modifier(Modifier::BOLD),
|
||||
@@ -983,26 +969,15 @@ fn render_sort_panel(frame: &mut Frame, area: Rect, state: &mut DiscoveryState)
|
||||
);
|
||||
continue;
|
||||
}
|
||||
if row == 6 {
|
||||
continue;
|
||||
}
|
||||
let index = if state.local_mode && row >= 8 {
|
||||
row - 3
|
||||
let index = row - 1;
|
||||
let label = if state.local_mode {
|
||||
["Name", "File size", "Date modified"][index]
|
||||
} else {
|
||||
row - 1
|
||||
};
|
||||
let label = if index < 5 {
|
||||
crate::instance::content::provider::DiscoverySort::ALL[index].label()
|
||||
} else {
|
||||
["Name", "File size", "Date modified"][index - 5]
|
||||
};
|
||||
let selected = state.sort_panel_focused && index == state.sort_panel_selected;
|
||||
let active = if state.local_mode {
|
||||
if index < 5 {
|
||||
index == state.local_ranking_index
|
||||
} else {
|
||||
index == state.local_sort_index
|
||||
}
|
||||
index + 5 == state.local_sort_index
|
||||
} else {
|
||||
crate::instance::content::provider::DiscoverySort::ALL[index] == state.sort
|
||||
};
|
||||
@@ -1012,7 +987,7 @@ fn render_sort_panel(frame: &mut Frame, area: Rect, state: &mut DiscoveryState)
|
||||
Style::default().fg(theme.accent()),
|
||||
),
|
||||
Span::styled(
|
||||
if active && index >= 5 {
|
||||
if active && state.local_mode {
|
||||
if state.local_sort_descending {
|
||||
"▼ "
|
||||
} else {
|
||||
|
||||
Reference in new issue
Block a user