1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//! Implementation methods for `AppState`.
use crate::state::app_state::{AppState, recent_capacity};
use crate::state::types::{
NewsBookmark, NewsFeedItem, NewsReadFilter, NewsSortMode, severity_rank,
};
use chrono::{NaiveDate, Utc};
impl AppState {
/// What: Return recent searches in most-recent-first order.
///
/// Inputs:
/// - `self`: Application state containing the recent LRU cache.
///
/// Output:
/// - Vector of recent search strings ordered from most to least recent.
///
/// Details:
/// - Clones stored values; limited to `RECENT_CAPACITY`.
#[must_use]
pub fn recent_values(&self) -> Vec<String> {
self.recent.iter().map(|(_, v)| v.clone()).collect()
}
/// What: Fetch a recent search by positional index.
///
/// Inputs:
/// - `index`: Zero-based position in most-recent-first ordering.
///
/// Output:
/// - `Some(String)` when the index is valid; `None` otherwise.
///
/// Details:
/// - Uses the LRU iterator, so `index == 0` is the most recent entry.
#[must_use]
pub fn recent_value_at(&self, index: usize) -> Option<String> {
self.recent.iter().nth(index).map(|(_, v)| v.clone())
}
/// What: Remove a recent search at the provided position.
///
/// Inputs:
/// - `index`: Zero-based position in most-recent-first ordering.
///
/// Output:
/// - `Some(String)` containing the removed value when found; `None` otherwise.
///
/// Details:
/// - Resolves the cache key via iteration, then pops it to maintain LRU invariants.
pub fn remove_recent_at(&mut self, index: usize) -> Option<String> {
let key = self.recent.iter().nth(index).map(|(k, _)| k.clone())?;
self.recent.pop(&key)
}
/// What: Add or replace a news bookmark, marking state dirty.
///
/// Inputs:
/// - `bookmark`: Bookmark to insert (deduped by `item.id`).
///
/// Output:
/// - None (mutates bookmarks and dirty flag).
pub fn add_news_bookmark(&mut self, bookmark: NewsBookmark) {
if let Some(pos) = self
.news_bookmarks
.iter()
.position(|b| b.item.id == bookmark.item.id)
{
self.news_bookmarks[pos] = bookmark;
} else {
self.news_bookmarks.push(bookmark);
}
self.news_bookmarks_dirty = true;
}
/// What: Remove a news bookmark at a position.
///
/// Inputs:
/// - `index`: Zero-based index into bookmarks vector.
///
/// Output:
/// - Removed bookmark if present.
pub fn remove_news_bookmark_at(&mut self, index: usize) -> Option<NewsBookmark> {
if index >= self.news_bookmarks.len() {
return None;
}
let removed = self.news_bookmarks.remove(index);
self.news_bookmarks_dirty = true;
Some(removed)
}
/// What: Return recent news searches in most-recent-first order.
///
/// Inputs:
/// - `self`: Application state containing the news recent LRU cache.
///
/// Output:
/// - Vector of recent news search strings ordered from most to least recent.
///
/// Details:
/// - Clones stored values; limited by the configured recent capacity.
#[must_use]
pub fn news_recent_values(&self) -> Vec<String> {
self.news_recent.iter().map(|(_, v)| v.clone()).collect()
}
/// What: Fetch a recent news search by positional index.
///
/// Inputs:
/// - `index`: Zero-based position in most-recent-first ordering.
///
/// Output:
/// - `Some(String)` when the index is valid; `None` otherwise.
///
/// Details:
/// - Uses the LRU iterator, so `index == 0` is the most recent entry.
#[must_use]
pub fn news_recent_value_at(&self, index: usize) -> Option<String> {
self.news_recent.iter().nth(index).map(|(_, v)| v.clone())
}
/// What: Replace the news recent cache with the provided most-recent-first entries.
///
/// Inputs:
/// - `items`: Slice of recent news search strings ordered from most to least recent.
///
/// Output:
/// - None (mutates `self.news_recent`).
///
/// Details:
/// - Clears existing entries, enforces configured capacity, and preserves ordering by
/// inserting from least-recent to most-recent.
pub fn load_news_recent_items(&mut self, items: &[String]) {
self.news_recent.clear();
self.news_recent.resize(recent_capacity());
for value in items.iter().rev() {
let stored = value.clone();
let key = stored.to_ascii_lowercase();
self.news_recent.put(key, stored);
}
}
/// What: Remove a recent news search at the provided position.
///
/// Inputs:
/// - `index`: Zero-based position in most-recent-first ordering.
///
/// Output:
/// - `Some(String)` containing the removed value when found; `None` otherwise.
///
/// Details:
/// - Resolves the cache key via iteration, then pops it to maintain LRU invariants.
pub fn remove_news_recent_at(&mut self, index: usize) -> Option<String> {
let key = self.news_recent.iter().nth(index).map(|(k, _)| k.clone())?;
self.news_recent.pop(&key)
}
/// What: Replace the recent cache with the provided most-recent-first entries.
///
/// Inputs:
/// - `items`: Slice of recent search strings ordered from most to least recent.
///
/// Output:
/// - None (mutates `self.recent`).
///
/// Details:
/// - Clears existing entries, enforces configured capacity, and preserves ordering by
/// inserting from least-recent to most-recent.
pub fn load_recent_items(&mut self, items: &[String]) {
self.recent.clear();
self.recent.resize(recent_capacity());
for value in items.iter().rev() {
let stored = value.clone();
let key = stored.to_ascii_lowercase();
self.recent.put(key, stored);
}
}
/// What: Recompute news results applying filters, search, age cutoff, and sorting.
///
/// Inputs:
/// - `self`: Mutable application state containing news items and filter fields.
///
/// Output:
/// - Updates `news_results`, selection state, and recent news searches.
pub fn refresh_news_results(&mut self) {
let query = self.news_search_input.to_lowercase();
if query.is_empty() {
self.news_history_pending = None;
self.news_history_pending_at = None;
} else {
self.news_history_pending = Some(self.news_search_input.clone());
self.news_history_pending_at = Some(std::time::Instant::now());
}
let mut filtered: Vec<NewsFeedItem> = self
.news_items
.iter()
.filter(|it| match it.source {
crate::state::types::NewsFeedSource::ArchNews => self.news_filter_show_arch_news,
crate::state::types::NewsFeedSource::SecurityAdvisory => {
self.news_filter_show_advisories
}
crate::state::types::NewsFeedSource::InstalledPackageUpdate => {
self.news_filter_show_pkg_updates
}
crate::state::types::NewsFeedSource::AurPackageUpdate => {
self.news_filter_show_aur_updates
}
crate::state::types::NewsFeedSource::AurComment => {
self.news_filter_show_aur_comments
}
})
.cloned()
.collect();
// Apply installed-only filter for advisories when enabled.
// When "[Advisories All]" is active (news_filter_show_advisories = true,
// news_filter_installed_only = false), this block does not run, allowing
// all advisories to be shown regardless of installed status.
if self.news_filter_installed_only {
let installed: std::collections::HashSet<String> =
crate::index::explicit_names().into_iter().collect();
filtered.retain(|it| {
!matches!(
it.source,
crate::state::types::NewsFeedSource::SecurityAdvisory
) || it.packages.iter().any(|pkg| installed.contains(pkg))
});
}
if !matches!(self.news_filter_read_status, NewsReadFilter::All) {
filtered.retain(|it| {
let is_read = self.news_read_ids.contains(&it.id)
|| it
.url
.as_ref()
.is_some_and(|u| self.news_read_urls.contains(u));
matches!(self.news_filter_read_status, NewsReadFilter::Read) && is_read
|| matches!(self.news_filter_read_status, NewsReadFilter::Unread) && !is_read
});
}
if !query.is_empty() {
filtered.retain(|it| {
let hay = format!(
"{} {} {}",
it.title,
it.summary.clone().unwrap_or_default(),
it.packages.join(" ")
)
.to_lowercase();
hay.contains(&query)
});
}
if let Some(max_days) = self.news_max_age_days
&& let Some(cutoff_date) = Utc::now()
.date_naive()
.checked_sub_days(chrono::Days::new(u64::from(max_days)))
{
filtered.retain(|it| {
NaiveDate::parse_from_str(&it.date, "%Y-%m-%d").map_or(true, |d| d >= cutoff_date)
});
}
let is_read = |it: &NewsFeedItem| {
self.news_read_ids.contains(&it.id)
|| it
.url
.as_ref()
.is_some_and(|u| self.news_read_urls.contains(u))
};
match self.news_sort_mode {
NewsSortMode::DateDesc => filtered.sort_by(|a, b| b.date.cmp(&a.date)),
NewsSortMode::DateAsc => filtered.sort_by(|a, b| a.date.cmp(&b.date)),
NewsSortMode::Title => {
filtered.sort_by(|a, b| {
a.title
.to_lowercase()
.cmp(&b.title.to_lowercase())
.then(b.date.cmp(&a.date))
});
}
NewsSortMode::SourceThenTitle => filtered.sort_by(|a, b| {
a.source
.cmp(&b.source)
.then(b.date.cmp(&a.date))
.then(a.title.to_lowercase().cmp(&b.title.to_lowercase()))
}),
NewsSortMode::SeverityThenDate => filtered.sort_by(|a, b| {
let sa = severity_rank(a.severity);
let sb = severity_rank(b.severity);
sb.cmp(&sa)
.then(b.date.cmp(&a.date))
.then(a.title.to_lowercase().cmp(&b.title.to_lowercase()))
}),
NewsSortMode::UnreadThenDate => filtered.sort_by(|a, b| {
let ra = is_read(a);
let rb = is_read(b);
ra.cmp(&rb)
.then(b.date.cmp(&a.date))
.then(a.title.to_lowercase().cmp(&b.title.to_lowercase()))
}),
}
self.news_results = filtered;
if self.news_results.is_empty() {
self.news_selected = 0;
self.news_list_state.select(None);
} else {
self.news_selected = self
.news_selected
.min(self.news_results.len().saturating_sub(1));
self.news_list_state.select(Some(self.news_selected));
}
}
}