From c0ad27397c99cde96b97e63fb95a0b6a3fa68d85 Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Wed, 3 Jan 2024 17:51:24 +0100 Subject: [PATCH] lib: Make loading issues and patches optional --- bin/commands/issue/suite/ui.rs | 9 +++++---- bin/commands/patch.rs | 4 ++-- bin/commands/patch/list/ui.rs | 5 +++-- bin/commands/patch/suite/ui.rs | 5 +++-- src/context.rs | 37 +++++++++++++++++++++++++--------- 5 files changed, 40 insertions(+), 20 deletions(-) diff --git a/bin/commands/issue/suite/ui.rs b/bin/commands/issue/suite/ui.rs index 54c6265e..8530c576 100644 --- a/bin/commands/issue/suite/ui.rs +++ b/bin/commands/issue/suite/ui.rs @@ -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::>(); @@ -392,7 +393,7 @@ pub fn details( pub fn browse_context(context: &Context, theme: &Theme, progress: Progress) -> Widget { 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) diff --git a/bin/commands/patch.rs b/bin/commands/patch.rs index 4782e562..4d71e271 100644 --- a/bin/commands/patch.rs +++ b/bin/commands/patch.rs @@ -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}"); } diff --git a/bin/commands/patch/list/ui.rs b/bin/commands/patch/list/ui.rs index e24711d5..f4bb9f70 100644 --- a/bin/commands/patch/list/ui.rs +++ b/bin/commands/patch/list/ui.rs @@ -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 { 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, diff --git a/bin/commands/patch/suite/ui.rs b/bin/commands/patch/suite/ui.rs index 1216d2e7..6a2826d7 100644 --- a/bin/commands/patch/suite/ui.rs +++ b/bin/commands/patch/suite/ui.rs @@ -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 { 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, diff --git a/src/context.rs b/src/context.rs index e99cdeb6..9dbff4ed 100644 --- a/src/context.rs +++ b/src/context.rs @@ -38,8 +38,8 @@ pub struct Context { id: Id, project: Project, repository: Repository, - issues: Vec<(IssueId, Issue)>, - patches: Vec<(PatchId, Patch)>, + issues: Option>, + patches: Option>, signer: Box, } @@ -47,12 +47,10 @@ impl Context { pub fn new(id: Id) -> Result { 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> { &self.issues } - pub fn patches(&self) -> &Vec<(PatchId, Patch)> { + pub fn patches(&self) -> &Option> { &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()); + } } }