Simplify `Event` abstraction in `Search` screen

This commit is contained in:
Héctor Ramón Jiménez 2024-07-13 13:22:01 +02:00
parent 63106e2ad0
commit 3fe30c91aa
No known key found for this signature in database
GPG Key ID: 7CC46565708259A7
2 changed files with 26 additions and 26 deletions

View File

@ -60,17 +60,18 @@ impl Chat {
match message {
Message::Search(message) => {
if let Screen::Search(search) = &mut self.screen {
let (task, event) = search.update(message);
let action = search.update(message);
match event {
search::Event::None => {}
search::Event::ModelSelected(model) => {
match action {
search::Action::None => Task::none(),
search::Action::Run(task) => task.map(Message::Search),
search::Action::Boot(model) => {
self.screen =
Screen::Boot(screen::Boot::new(model, self.system.as_ref()));
}
};
task.map(Message::Search)
Task::none()
}
}
} else {
Task::none()
}

View File

@ -37,9 +37,10 @@ pub enum Link {
LlamaCpp,
}
pub enum Event {
pub enum Action {
None,
ModelSelected(Model),
Run(Task<Message>),
Boot(Model),
}
impl Search {
@ -67,29 +68,27 @@ impl Search {
"Icebreaker".to_owned()
}
pub fn update(&mut self, message: Message) -> (Task<Message>, Event) {
pub fn update(&mut self, message: Message) -> Action {
match message {
Message::ModelsListed(Ok(models)) => {
self.models = models;
self.is_searching = false;
(Task::none(), Event::None)
Action::None
}
Message::ModelsListed(Err(error)) => {
self.error = Some(dbg!(error));
(Task::none(), Event::None)
Action::None
}
Message::SearchChanged(search) => {
self.search = search;
self.search_temperature += 1;
(
Task::perform(tokio::time::sleep(Duration::from_secs(1)), |_| {
Message::SearchCooled
}),
Event::None,
)
Action::Run(Task::perform(
tokio::time::sleep(Duration::from_secs(1)),
|_| Message::SearchCooled,
))
}
Message::SearchCooled => {
self.search_temperature = self.search_temperature.saturating_sub(1);
@ -97,15 +96,15 @@ impl Search {
if self.search_temperature == 0 {
self.is_searching = true;
(
Task::perform(Model::search(self.search.clone()), Message::ModelsListed),
Event::None,
)
Action::Run(Task::perform(
Model::search(self.search.clone()),
Message::ModelsListed,
))
} else {
(Task::none(), Event::None)
Action::None
}
}
Message::RunModel(model) => (Task::none(), Event::ModelSelected(model)),
Message::RunModel(model) => Action::Boot(model),
Message::LinkPressed(link) => {
let _ = open::that(match link {
Link::Rust => "https://rust-lang.org",
@ -114,12 +113,12 @@ impl Search {
Link::LlamaCpp => "https://github.com/ggerganov/llama.cpp",
});
(Task::none(), Event::None)
Action::None
}
Message::WindowResized(size) => {
self.window_size = size;
(Task::none(), Event::None)
Action::None
}
}
}