lib: Make loading issues and patches optional

This commit is contained in:
Erik Kundt
2024-01-03 17:51:24 +01:00
parent 9ea4b54993
commit c0ad27397c
5 changed files with 40 additions and 20 deletions
+5 -4
View File
@@ -54,7 +54,8 @@ impl IssueBrowser {
let repo = context.repository();
let mut items = vec![];
for (id, issue) in context.issues() {
let issues = context.issues().as_ref().unwrap();
for (id, issue) in issues {
if let Ok(item) = IssueItem::try_from((context.profile(), repo, *id, issue.clone())) {
items.push(item);
}
@@ -107,8 +108,8 @@ impl LargeList {
pub fn new(context: &Context, theme: &Theme, selected: Option<(IssueId, Issue)>) -> Self {
let repo = context.repository();
let mut items = context
.issues()
let issues = context.issues().as_ref().unwrap();
let mut items = issues
.iter()
.map(|(id, issue)| IssueItem::from((context.profile(), repo, *id, issue.clone())))
.collect::<Vec<_>>();
@@ -392,7 +393,7 @@ pub fn details(
pub fn browse_context(context: &Context, theme: &Theme, progress: Progress) -> Widget<ContextBar> {
use radicle::cob::issue::State;
let issues = context.issues();
let issues = context.issues().as_ref().unwrap();
let open = issues
.iter()
.filter(|issue| *issue.1.state() == State::Open)
+2 -2
View File
@@ -73,7 +73,7 @@ pub fn run(options: Options, _ctx: impl terminal::Context) -> anyhow::Result<()>
let (_, id) = radicle::rad::cwd()
.map_err(|_| anyhow!("this command must be run in the context of a project"))?;
let context = context::Context::new(id)?;
let context = context::Context::new(id)?.with_patches();
match options.op {
Operation::List => {
@@ -81,7 +81,7 @@ pub fn run(options: Options, _ctx: impl terminal::Context) -> anyhow::Result<()>
let patch_id = Window::default()
.run(&mut list::App::new(context), 1000 / FPS)?
.ok_or_else(|| anyhow!("expected patch id"))?;
.ok_or_else(|| anyhow!("expected a patch id"))?;
eprint!("{patch_id}");
}
+3 -2
View File
@@ -45,9 +45,10 @@ impl PatchBrowser {
];
let repo = context.repository();
let patches = context.patches().as_ref().unwrap();
let mut items = vec![];
for (id, patch) in context.patches() {
for (id, patch) in patches {
if let Ok(item) = PatchItem::try_from((context.profile(), repo, *id, patch.clone())) {
items.push(item);
}
@@ -111,12 +112,12 @@ pub fn patches(
pub fn browse_context(context: &Context, theme: &Theme, progress: Progress) -> Widget<ContextBar> {
use radicle::cob::patch::State;
let patches = context.patches();
let mut draft = 0;
let mut open = 0;
let mut archived = 0;
let mut merged = 0;
let patches = context.patches().as_ref().unwrap();
for (_, patch) in patches {
match patch.state() {
State::Draft => draft += 1,
+3 -2
View File
@@ -49,8 +49,9 @@ impl PatchBrowser {
let repo = context.repository();
let mut items = vec![];
let patches = context.patches().as_ref().unwrap();
for (id, patch) in context.patches() {
for (id, patch) in patches {
if let Ok(item) = PatchItem::try_from((context.profile(), repo, *id, patch.clone())) {
items.push(item);
}
@@ -213,12 +214,12 @@ pub fn context(context: &Context, theme: &Theme, patch: (PatchId, Patch)) -> Wid
pub fn browse_context(context: &Context, theme: &Theme, progress: Progress) -> Widget<ContextBar> {
use radicle::cob::patch::State;
let patches = context.patches();
let mut draft = 0;
let mut open = 0;
let mut archived = 0;
let mut merged = 0;
let patches = context.patches().as_ref().unwrap();
for (_, patch) in patches {
match patch.state() {
State::Draft => draft += 1,
+27 -10
View File
@@ -38,8 +38,8 @@ pub struct Context {
id: Id,
project: Project,
repository: Repository,
issues: Vec<(IssueId, Issue)>,
patches: Vec<(PatchId, Patch)>,
issues: Option<Vec<(IssueId, Issue)>>,
patches: Option<Vec<(PatchId, Patch)>>,
signer: Box<dyn Signer>,
}
@@ -47,12 +47,10 @@ impl Context {
pub fn new(id: Id) -> Result<Self, anyhow::Error> {
let profile = profile()?;
let signer = signer(&profile)?;
let repository = profile.storage.repository(id).unwrap();
let issues = crate::cob::issue::all(&repository).unwrap_or_default();
let patches = crate::cob::patch::all(&repository).unwrap_or_default();
let project = repository.identity_doc()?.project()?;
let issues = None;
let patches = None;
Ok(Self {
id,
@@ -65,6 +63,18 @@ impl Context {
})
}
pub fn with_issues(mut self) -> Self {
use crate::cob::issue;
self.issues = Some(issue::all(&self.repository).unwrap_or_default());
self
}
pub fn with_patches(mut self) -> Self {
use crate::cob::patch;
self.patches = Some(patch::all(&self.repository).unwrap_or_default());
self
}
pub fn profile(&self) -> &Profile {
&self.profile
}
@@ -81,11 +91,11 @@ impl Context {
&self.repository
}
pub fn issues(&self) -> &Vec<(IssueId, Issue)> {
pub fn issues(&self) -> &Option<Vec<(IssueId, Issue)>> {
&self.issues
}
pub fn patches(&self) -> &Vec<(PatchId, Patch)> {
pub fn patches(&self) -> &Option<Vec<(PatchId, Patch)>> {
&self.patches
}
@@ -95,8 +105,15 @@ impl Context {
}
pub fn reload(&mut self) {
self.issues = crate::cob::issue::all(&self.repository).unwrap_or_default();
self.patches = crate::cob::patch::all(&self.repository).unwrap_or_default();
use crate::cob::issue;
use crate::cob::patch;
if self.issues.is_some() {
self.issues = Some(issue::all(&self.repository).unwrap_or_default());
}
if self.patches.is_some() {
self.patches = Some(patch::all(&self.repository).unwrap_or_default());
}
}
}