Skip to content

Commit 1f225d1

Browse files
manascb1344mmstick
authored andcommitted
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.
1 parent d6bf77b commit 1f225d1

1 file changed

Lines changed: 14 additions & 33 deletions

File tree

cosmic-settings/src/pages/applications/startup_apps.rs

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,7 @@ impl page::Page<crate::pages::Message> for Page {
143143
freedesktop_desktop_entry::Iter::new(user_dirs.into_iter()).entries(Some(&locales));
144144

145145
let mut user_entries_vec = user_entries.collect_vec();
146-
user_entries_vec.sort_by(|a, b| {
147-
let name_a = a
148-
.name(&locales)
149-
.map(|n| n.to_lowercase())
150-
.unwrap_or_else(|| a.appid.to_lowercase());
151-
let name_b = b
152-
.name(&locales)
153-
.map(|n| n.to_lowercase())
154-
.unwrap_or_else(|| b.appid.to_lowercase());
155-
name_a.cmp(&name_b)
156-
});
146+
sort_entries_by_name(&mut user_entries_vec, &locales);
157147

158148
let mut apps_hash = HashMap::with_capacity(1);
159149
apps_hash.insert(DirectoryType::User, user_entries_vec);
@@ -259,17 +249,10 @@ impl Page {
259249
if let Some(target_apps) = target_apps {
260250
let mut new_apps = target_apps.clone();
261251
new_apps.push(app.clone());
262-
new_apps.sort_by(|a, b| {
263-
let name_a = a
264-
.name(&cached_startup_apps.locales)
265-
.map(|n| n.to_lowercase())
266-
.unwrap_or_else(|| a.appid.to_lowercase());
267-
let name_b = b
268-
.name(&cached_startup_apps.locales)
269-
.map(|n| n.to_lowercase())
270-
.unwrap_or_else(|| b.appid.to_lowercase());
271-
name_a.cmp(&name_b)
272-
});
252+
sort_entries_by_name(
253+
&mut new_apps,
254+
&cached_startup_apps.locales,
255+
);
273256

274257
cached_startup_apps
275258
.apps
@@ -457,6 +440,14 @@ fn apps() -> Section<crate::pages::Message> {
457440
})
458441
}
459442

443+
fn sort_entries_by_name(entries: &mut [DesktopEntry], locales: &[String]) {
444+
entries.sort_by_cached_key(|e| {
445+
e.name(locales)
446+
.map(|n| n.to_lowercase())
447+
.unwrap_or_else(|| e.appid.to_lowercase())
448+
});
449+
}
450+
460451
fn get_all_apps(locales: Vec<String>) -> Vec<DesktopEntry> {
461452
let mut dedupe = HashSet::new();
462453

@@ -497,17 +488,7 @@ fn get_all_apps(locales: Vec<String>) -> Vec<DesktopEntry> {
497488
dedupe.insert(app_id.to_owned());
498489
}
499490

500-
result.sort_by(|a, b| {
501-
let name_a = a
502-
.name(&locales)
503-
.map(|n| n.to_lowercase())
504-
.unwrap_or_else(|| a.appid.to_lowercase());
505-
let name_b = b
506-
.name(&locales)
507-
.map(|n| n.to_lowercase())
508-
.unwrap_or_else(|| b.appid.to_lowercase());
509-
name_a.cmp(&name_b)
510-
});
491+
sort_entries_by_name(&mut result, &locales);
511492

512493
result
513494
}

0 commit comments

Comments
 (0)