simplified logic a bit

This commit is contained in:
Thomas Eppers 2023-02-20 14:14:16 +01:00
parent 70b163cf81
commit 445b21975a
2 changed files with 29 additions and 59 deletions

View File

@ -53,8 +53,7 @@ impl std::iter::Iterator for State {
pub enum DeferredEvent {
Quit,
NewRepo(String),
TagPrevious,
TagNext,
LoadMoreTags,
}
impl Ui {
@ -88,35 +87,15 @@ impl Ui {
let mut ui = ui.lock().unwrap();
ui.tags = list;
}
Ok(DeferredEvent::TagPrevious) => {
let mut ui = ui.lock().unwrap();
ui.tags.previous();
ui.details = ui.tags.create_detail_widget();
}
Ok(DeferredEvent::TagNext) => {
Ok(DeferredEvent::LoadMoreTags) if !fetching_tags.load(Ordering::Relaxed) => {
fetching_tags.store(true, Ordering::Relaxed);
let mut tags_copy = {
let mut ui = ui.lock().unwrap();
match ui.tags.next() {
None => {
// return early, also releases lock
ui.details = ui.tags.create_detail_widget();
sender.send(UiEvent::RefreshOnNewData)?;
continue;
}
Some(_) if !fetching_tags.load(Ordering::Relaxed) => {
fetching_tags.store(true, Ordering::Relaxed);
ui.info.set_text("Fetching more tags...");
sender.send(UiEvent::RefreshOnNewData)?;
ui.tags.clone()
}
Some(_) => {
// do nothing, as we are already fetching for new tags
continue;
}
}
};
// fetching new tags
let sender_copy = sender.clone();
let ui_copy = ui.clone();
let fetching_tags_copy = fetching_tags.clone();
@ -139,6 +118,9 @@ impl Ui {
})
});
}
Ok(DeferredEvent::LoadMoreTags) => {
//do nothing, as we are fetching tags
}
Err(e) => {
let mut ui = ui.lock().unwrap();
ui.info.set_info(&e);
@ -210,7 +192,7 @@ impl Ui {
let (tags, state) = ui_data.tags.render(render_state);
let more_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Min(15), Constraint::Length(28)].as_ref())
.constraints([Constraint::Min(15), Constraint::Length(29)].as_ref())
.split(chunks[1]);
rect.render_stateful_widget(tags, more_chunks[0], state);
rect.render_widget(ui_data.details.render(), more_chunks[1]);
@ -252,12 +234,14 @@ impl Ui {
}
//moving up on selecting tags
Key::Up | Key::Char('k') if ui_data.state == State::SelectTag => {
deferred_sender.send(DeferredEvent::TagPrevious)?;
ui_data.tags.previous();
ui_data.details = ui_data.tags.create_detail_widget();
}
//moving down on selecting tags
Key::Down | Key::Char('j') if ui_data.state == State::SelectTag => {
deferred_sender.send(DeferredEvent::TagNext)?;
if ui_data.tags.next().is_some() {
deferred_sender.send(DeferredEvent::LoadMoreTags).unwrap();
}
ui_data.details = ui_data.tags.create_detail_widget();
}
//append character on editing repository

View File

@ -58,8 +58,7 @@ impl std::iter::Iterator for State {
pub enum DeferredEvent {
Quit,
NewRepo(String),
TagPrevious,
TagNext,
LoadMoreTags,
}
impl Ui {
@ -93,35 +92,15 @@ impl Ui {
let mut ui = ui.lock().unwrap();
ui.tags = list;
}
Ok(DeferredEvent::TagPrevious) => {
let mut ui = ui.lock().unwrap();
ui.tags.previous();
ui.details = ui.tags.create_detail_widget();
}
Ok(DeferredEvent::TagNext) => {
Ok(DeferredEvent::LoadMoreTags) if !fetching_tags.load(Ordering::Relaxed) => {
fetching_tags.store(true, Ordering::Relaxed);
let mut tags_copy = {
let mut ui = ui.lock().unwrap();
match ui.tags.next() {
None => {
// return early, also releases lock
ui.details = ui.tags.create_detail_widget();
sender.send(UiEvent::RefreshOnNewData)?;
continue;
}
Some(_) if !fetching_tags.load(Ordering::Relaxed) => {
fetching_tags.store(true, Ordering::Relaxed);
ui.info.set_text("Fetching more tags...");
sender.send(UiEvent::RefreshOnNewData)?;
ui.tags.clone()
}
Some(_) => {
// do nothing, as we are already fetching for new tags
continue;
}
}
};
// fetching new tags
let sender_copy = sender.clone();
let ui_copy = ui.clone();
let fetching_tags_copy = fetching_tags.clone();
@ -144,6 +123,9 @@ impl Ui {
})
});
}
Ok(DeferredEvent::LoadMoreTags) => {
//do nothing, as we are fetching tags
}
Err(e) => {
let mut ui = ui.lock().unwrap();
ui.info.set_info(&e);
@ -341,11 +323,15 @@ impl Ui {
}
//moving up on selecting tags
Key::Up | Key::Char('k') if ui_data.state == State::SelectTag => {
deferred_sender.send(DeferredEvent::TagPrevious).unwrap();
ui_data.tags.previous();
ui_data.details = ui_data.tags.create_detail_widget();
}
//moving down on selecting tags
Key::Down | Key::Char('j') if ui_data.state == State::SelectTag => {
deferred_sender.send(DeferredEvent::TagNext).unwrap();
if ui_data.tags.next().is_some() {
deferred_sender.send(DeferredEvent::LoadMoreTags).unwrap();
}
ui_data.details = ui_data.tags.create_detail_widget();
}
//append character on editing repository
Key::Char(key) if ui_data.state == State::EditRepo => {