refactor: extract sorting logic for startup applications and all apps

- Introduced a new function `sort_entries_by_name` to encapsulate the sorting logic for desktop entries by name, improving code readability and maintainability.
- Updated existing sorting implementations to use the new function, ensuring consistent behavior across the application.
This commit is contained in:
manascb1344 2026-05-08 15:52:34 +05:30 committed by Michael Murphy
parent d6bf77bf3a
commit 1f225d19ce

View file

@ -143,17 +143,7 @@ impl page::Page<crate::pages::Message> for Page {
freedesktop_desktop_entry::Iter::new(user_dirs.into_iter()).entries(Some(&locales));
let mut user_entries_vec = user_entries.collect_vec();
user_entries_vec.sort_by(|a, b| {
let name_a = a
.name(&locales)
.map(|n| n.to_lowercase())
.unwrap_or_else(|| a.appid.to_lowercase());
let name_b = b
.name(&locales)
.map(|n| n.to_lowercase())
.unwrap_or_else(|| b.appid.to_lowercase());
name_a.cmp(&name_b)
});
sort_entries_by_name(&mut user_entries_vec, &locales);
let mut apps_hash = HashMap::with_capacity(1);
apps_hash.insert(DirectoryType::User, user_entries_vec);
@ -259,17 +249,10 @@ impl Page {
if let Some(target_apps) = target_apps {
let mut new_apps = target_apps.clone();
new_apps.push(app.clone());
new_apps.sort_by(|a, b| {
let name_a = a
.name(&cached_startup_apps.locales)
.map(|n| n.to_lowercase())
.unwrap_or_else(|| a.appid.to_lowercase());
let name_b = b
.name(&cached_startup_apps.locales)
.map(|n| n.to_lowercase())
.unwrap_or_else(|| b.appid.to_lowercase());
name_a.cmp(&name_b)
});
sort_entries_by_name(
&mut new_apps,
&cached_startup_apps.locales,
);
cached_startup_apps
.apps
@ -457,6 +440,14 @@ fn apps() -> Section<crate::pages::Message> {
})
}
fn sort_entries_by_name(entries: &mut [DesktopEntry], locales: &[String]) {
entries.sort_by_cached_key(|e| {
e.name(locales)
.map(|n| n.to_lowercase())
.unwrap_or_else(|| e.appid.to_lowercase())
});
}
fn get_all_apps(locales: Vec<String>) -> Vec<DesktopEntry> {
let mut dedupe = HashSet::new();
@ -497,17 +488,7 @@ fn get_all_apps(locales: Vec<String>) -> Vec<DesktopEntry> {
dedupe.insert(app_id.to_owned());
}
result.sort_by(|a, b| {
let name_a = a
.name(&locales)
.map(|n| n.to_lowercase())
.unwrap_or_else(|| a.appid.to_lowercase());
let name_b = b
.name(&locales)
.map(|n| n.to_lowercase())
.unwrap_or_else(|| b.appid.to_lowercase());
name_a.cmp(&name_b)
});
sort_entries_by_name(&mut result, &locales);
result
}