Skip to content

Commit

Permalink
refactor: add SearchContinue data struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Builditluc committed Aug 14, 2023
1 parent 3a05d9a commit 5a1f133
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
16 changes: 10 additions & 6 deletions src/ui/search/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ pub fn display_search_results(siv: &mut Cursive, search: Search) -> Result<()> {

// create the continue button (Button)
let search_continue_button = {
let search = search;
Button::new("Show more results...", move |s| {
on_continue_submit(s, search.clone())
})
.with_name(search_continue_button_name)
let callback: Box<dyn 'static + Fn(&mut Cursive)> = match search.continue_data() {
Some(data) => Box::new(move |s| on_continue_submit(s, data.clone())),
None => Box::new(|_| {}),
};
Button::new("Show more results...", callback).with_name(search_continue_button_name)
};

debug!("created the views for the search results layout");
Expand Down Expand Up @@ -195,7 +195,11 @@ pub fn display_more_search_results(siv: &mut Cursive, search: Search) -> Result<

// modify the callback of the continue button so we don't search for the same thing again
{
search_continue_button.set_callback(move |s| on_continue_submit(s, search.clone()));
let callback: Box<dyn 'static + Fn(&mut Cursive)> = match search.continue_data() {
Some(data) => Box::new(move |s| on_continue_submit(s, data.clone())),
None => Box::new(|_| {}),
};
search_continue_button.set_callback(callback);
}
debug!("set the new callback of the continue button");

Expand Down
17 changes: 6 additions & 11 deletions src/ui/search/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
config::Config,
ui::utils::display_error,
wiki::search::{Search, SearchResult},
wiki::search::{Search, SearchContinue, SearchResult},
};

use anyhow::Context;
Expand Down Expand Up @@ -108,18 +108,13 @@ fn on_result_select(siv: &mut Cursive, item: &SearchResult) {

/// Searches for more results at a given offset and adds them to the results view. It's a callback
/// for the continue button and displays an error if something went wrong
fn on_continue_submit(siv: &mut Cursive, search: Search) {
let offset = match search.continue_offset {
Some(ref offset) => offset,
None => return,
};

fn on_continue_submit(siv: &mut Cursive, continue_data: SearchContinue) {
// continue the search and fetch more results
let continue_search = match Search::builder()
.query(search.query)
.endpoint(search.endpoint)
.language(search.language)
.offset(*offset)
.query(continue_data.query)
.endpoint(continue_data.endpoint)
.language(continue_data.language)
.offset(continue_data.offset)
.search()
.context("failed to fetch more search results")
{
Expand Down
21 changes: 21 additions & 0 deletions src/wiki/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,27 @@ impl Search {
pub fn builder() -> SearchBuilder<NoQuery, NoEndpoint, NoLanguage> {
SearchBuilder::default()
}

pub fn continue_data(&self) -> Option<SearchContinue> {
if let Some(ref offset) = self.continue_offset {
return Some(SearchContinue {
query: self.query.clone(),
endpoint: self.endpoint.clone(),
language: self.language.clone(),
offset: *offset,
});
}
None
}
}

/// Struct containing all the necessary data for continuing the search
#[derive(Debug, Clone)]
pub struct SearchContinue {
pub query: String,
pub endpoint: Url,
pub language: Language,
pub offset: usize,
}

#[derive(Deserialize, Debug, Clone)]
Expand Down

0 comments on commit 5a1f133

Please sign in to comment.