Refresh issue and patch browser on page leave

This commit is contained in:
Erik Kundt
2023-08-22 13:28:49 +02:00
parent aa0fe2336c
commit dae897f8cc
6 changed files with 105 additions and 55 deletions
+6
View File
@@ -103,6 +103,12 @@ impl PatchItem {
}
}
impl PartialEq for PatchItem {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl TryFrom<(&Profile, &Repository, PatchId, Patch)> for PatchItem {
type Error = anyhow::Error;
+10 -4
View File
@@ -182,7 +182,7 @@ impl WidgetComponent for PropertyTable {
/// A table component that can display a list of [`TableItem`]s.
pub struct Table<V, const W: usize>
where
V: TableItem<W> + Clone,
V: TableItem<W> + Clone + PartialEq,
{
/// Items hold by this model.
items: Vec<V>,
@@ -198,19 +198,25 @@ where
impl<V, const W: usize> Table<V, W>
where
V: TableItem<W> + Clone,
V: TableItem<W> + Clone + PartialEq,
{
pub fn new(
items: &[V],
selected: Option<V>,
header: [Widget<Label>; W],
widths: [ColumnWidth; W],
theme: Theme,
) -> Self {
let selected = match selected {
Some(item) => items.iter().position(|i| i == &item),
_ => None,
};
Self {
items: items.to_vec(),
header,
widths,
state: ItemState::new(Some(0), items.len()),
state: ItemState::new(selected, items.len()),
theme,
}
}
@@ -218,7 +224,7 @@ where
impl<V, const W: usize> WidgetComponent for Table<V, W>
where
V: TableItem<W> + Clone,
V: TableItem<W> + Clone + PartialEq,
{
fn view(&mut self, properties: &Props, frame: &mut Frame, area: Rect) {
let highlight = properties
+27 -14
View File
@@ -1,3 +1,5 @@
use radicle::cob::issue::{Issue, IssueId};
use radicle::cob::patch::{Patch, PatchId};
use tuirealm::command::{Cmd, CmdResult};
use tuirealm::tui::layout::Rect;
use tuirealm::{AttrValue, Attribute, Frame, MockComponent, Props, State};
@@ -8,7 +10,6 @@ use super::common::list::{ColumnWidth, Table};
use super::{Widget, WidgetComponent};
use crate::cob;
use crate::ui::cob::{IssueItem, PatchItem};
use crate::ui::context::Context;
use crate::ui::theme::Theme;
@@ -43,7 +44,7 @@ pub struct IssueBrowser {
}
impl IssueBrowser {
pub fn new(context: &Context, theme: &Theme) -> Self {
pub fn new(context: &Context, theme: &Theme, selected: Option<(IssueId, Issue)>) -> Self {
let header = [
common::label(" ● "),
common::label("ID"),
@@ -76,7 +77,10 @@ impl IssueBrowser {
items.sort_by(|a, b| b.timestamp().cmp(a.timestamp()));
items.sort_by(|a, b| b.state().cmp(a.state()));
let table = Widget::new(Table::new(&items, header, widths, theme.clone()))
let selected =
selected.map(|(id, issue)| IssueItem::from((context.profile(), repo, id, issue)));
let table = Widget::new(Table::new(&items, selected, header, widths, theme.clone()))
.highlight(theme.colors.item_list_highlighted_bg);
Self { items, table }
@@ -112,7 +116,7 @@ pub struct PatchBrowser {
}
impl PatchBrowser {
pub fn new(context: &Context, theme: &Theme) -> Self {
pub fn new(context: &Context, theme: &Theme, selected: Option<(PatchId, Patch)>) -> Self {
let header = [
common::label(" ● "),
common::label("ID"),
@@ -138,18 +142,19 @@ impl PatchBrowser {
let repo = context.repository();
let mut items = vec![];
if let Ok(patches) = cob::patch::all(repo) {
for (id, patch) in patches {
if let Ok(item) = PatchItem::try_from((context.profile(), repo, id, patch)) {
items.push(item);
}
for (id, patch) in context.patches() {
if let Ok(item) = PatchItem::try_from((context.profile(), repo, *id, patch.clone())) {
items.push(item);
}
}
items.sort_by(|a, b| b.timestamp().cmp(a.timestamp()));
items.sort_by(|a, b| a.state().cmp(b.state()));
let table = Widget::new(Table::new(&items, header, widths, theme.clone()))
let selected = selected
.map(|(id, patch)| PatchItem::try_from((context.profile(), repo, id, patch)).unwrap());
let table = Widget::new(Table::new(&items, selected, header, widths, theme.clone()))
.highlight(theme.colors.item_list_highlighted_bg);
Self { items, table }
@@ -209,10 +214,18 @@ pub fn dashboard(context: &Context, theme: &Theme) -> Widget<Dashboard> {
Widget::new(dashboard)
}
pub fn patches(context: &Context, theme: &Theme) -> Widget<PatchBrowser> {
Widget::new(PatchBrowser::new(context, theme))
pub fn patches(
context: &Context,
theme: &Theme,
selected: Option<(PatchId, Patch)>,
) -> Widget<PatchBrowser> {
Widget::new(PatchBrowser::new(context, theme, selected))
}
pub fn issues(context: &Context, theme: &Theme) -> Widget<IssueBrowser> {
Widget::new(IssueBrowser::new(context, theme))
pub fn issues(
context: &Context,
theme: &Theme,
selected: Option<(IssueId, Issue)>,
) -> Widget<IssueBrowser> {
Widget::new(IssueBrowser::new(context, theme, selected))
}