From e35e6d8ba4a7685af6a24b7cd3e77ff19e5f7b80 Mon Sep 17 00:00:00 2001 From: Joe Carstairs Date: Sun, 25 Jan 2026 18:18:35 +0000 Subject: refactor bucket_navigation_entry --- schist_desktop_gui/src/gui/components.rs | 4 +-- .../src/gui/components/bucket_name_and_balance.rs | 38 -------------------- .../src/gui/components/bucket_navigation_entry.rs | 28 +++++++++++++++ .../bucket_name_and_balance.rs | 42 ++++++++++++++++++++++ .../src/gui/components/buckets_view.rs | 34 ++++++++++-------- 5 files changed, 92 insertions(+), 54 deletions(-) delete mode 100644 schist_desktop_gui/src/gui/components/bucket_name_and_balance.rs create mode 100644 schist_desktop_gui/src/gui/components/bucket_navigation_entry.rs create mode 100644 schist_desktop_gui/src/gui/components/bucket_navigation_entry/bucket_name_and_balance.rs (limited to 'schist_desktop_gui') diff --git a/schist_desktop_gui/src/gui/components.rs b/schist_desktop_gui/src/gui/components.rs index a242b0b..b0f8faf 100644 --- a/schist_desktop_gui/src/gui/components.rs +++ b/schist_desktop_gui/src/gui/components.rs @@ -1,5 +1,5 @@ pub mod balances_view; -pub mod bucket_name_and_balance; +pub mod bucket_navigation_entry; pub mod buckets_view; pub mod button; pub mod navigation; @@ -7,7 +7,7 @@ pub mod text; pub mod transactions_view; pub use balances_view::BalancesView; -pub use bucket_name_and_balance::BucketNameAndBalance; +pub use bucket_navigation_entry::BucketNavigationEntry; pub use buckets_view::BucketsView; pub use button::{active_button, inactive_button, panel_button}; pub use navigation::Navigation; diff --git a/schist_desktop_gui/src/gui/components/bucket_name_and_balance.rs b/schist_desktop_gui/src/gui/components/bucket_name_and_balance.rs deleted file mode 100644 index 8a5a16e..0000000 --- a/schist_desktop_gui/src/gui/components/bucket_name_and_balance.rs +++ /dev/null @@ -1,38 +0,0 @@ -use iced::{ - widget::{row, text}, - Element, Length, -}; -use schist_models::Bucket; - -use crate::gui::components::navigation; - -#[derive(Clone, Debug, PartialEq)] -pub struct BucketNameAndBalance { - pub bucket: Bucket, -} - -impl BucketNameAndBalance { - pub fn new(bucket: Bucket) -> Self { - Self { bucket } - } -} - -impl<'a> From<&'a BucketNameAndBalance> for Element<'a, navigation::Message> { - fn from(bucket_name_and_balance: &'a BucketNameAndBalance) -> Self { - let balance_style = if bucket_name_and_balance - .bucket - .balance - .is_none_or(|balance| balance >= 0) - { - text::default - } else { - text::danger - }; - - row![ - text(bucket_name_and_balance.bucket.name.clone()).width(Length::Fill), - text(bucket_name_and_balance.bucket.pretty_balance_per_100()).style(balance_style), - ] - .into() - } -} diff --git a/schist_desktop_gui/src/gui/components/bucket_navigation_entry.rs b/schist_desktop_gui/src/gui/components/bucket_navigation_entry.rs new file mode 100644 index 0000000..79b0ba9 --- /dev/null +++ b/schist_desktop_gui/src/gui/components/bucket_navigation_entry.rs @@ -0,0 +1,28 @@ +use bucket_name_and_balance::BucketNameAndBalance; +use iced::Element; +use schist_models::Bucket; + +use crate::gui::components::navigation; + +mod bucket_name_and_balance; + +#[derive(Clone, Debug, PartialEq)] +pub struct BucketNavigationEntry(BucketNameAndBalance); + +impl BucketNavigationEntry { + pub fn from_bucket(bucket: &Bucket) -> Self { + Self(BucketNameAndBalance::new(bucket)) + } + + pub fn to_string(&self) -> String { + self.0.bucket.name.clone() + } +} + +impl<'a> From<&'a BucketNavigationEntry> + for Element<'a, navigation::Message> +{ + fn from(bucket_navigation_entry: &'a BucketNavigationEntry) -> Self { + (&bucket_navigation_entry.0).into() + } +} diff --git a/schist_desktop_gui/src/gui/components/bucket_navigation_entry/bucket_name_and_balance.rs b/schist_desktop_gui/src/gui/components/bucket_navigation_entry/bucket_name_and_balance.rs new file mode 100644 index 0000000..7abca57 --- /dev/null +++ b/schist_desktop_gui/src/gui/components/bucket_navigation_entry/bucket_name_and_balance.rs @@ -0,0 +1,42 @@ +use iced::{ + widget::{row, text}, + Element, Length, +}; +use schist_models::Bucket; + +use crate::gui::components::{navigation, BucketNavigationEntry}; + +#[derive(Clone, Debug, PartialEq)] +pub struct BucketNameAndBalance { + pub bucket: Bucket, +} + +impl BucketNameAndBalance { + pub fn new(bucket: &Bucket) -> Self { + Self { + bucket: bucket.clone(), + } + } +} + +impl<'a> From<&'a BucketNameAndBalance> + for Element<'a, navigation::Message> +{ + fn from(bucket_name_and_balance: &'a BucketNameAndBalance) -> Self { + let balance_style = if bucket_name_and_balance + .bucket + .balance + .is_none_or(|balance| balance >= 0) + { + text::default + } else { + text::danger + }; + + row![ + text(bucket_name_and_balance.bucket.name.clone()).width(Length::Fill), + text(bucket_name_and_balance.bucket.pretty_balance_per_100()).style(balance_style), + ] + .into() + } +} diff --git a/schist_desktop_gui/src/gui/components/buckets_view.rs b/schist_desktop_gui/src/gui/components/buckets_view.rs index cbf7abd..01c91db 100644 --- a/schist_desktop_gui/src/gui/components/buckets_view.rs +++ b/schist_desktop_gui/src/gui/components/buckets_view.rs @@ -6,7 +6,7 @@ use iced::{ use schist_models::Bucket; use crate::{ - gui::components::{navigation, BucketNameAndBalance, Navigation, Text}, + gui::components::{navigation, BucketNavigationEntry, Navigation, Text}, style::SPACING_LG, traits::{Component, Viewable}, }; @@ -14,12 +14,12 @@ use crate::{ #[derive(Clone, Debug)] pub struct BucketsView { greeting: String, - navigation: Navigation, + navigation: Navigation, } #[derive(Clone, Debug)] pub enum Message { - NavigationMessage(navigation::Message), + NavigationMessage(navigation::Message), SelectNextBucket, SelectPrevBucket, SetBuckets(Vec), @@ -34,14 +34,17 @@ impl BucketsView { Self { greeting: greeting.to_owned(), navigation: Navigation::new( - buckets.first().cloned().map(BucketNameAndBalance::new), - buckets.into_iter().map(BucketNameAndBalance::new).collect(), + buckets.first().map(BucketNavigationEntry::from_bucket), + buckets + .iter() + .map(BucketNavigationEntry::from_bucket) + .collect(), ), } } - fn update_greeting(&mut self, active_bucket: &Bucket) { - self.greeting = format!("Hello, {} bucket!", active_bucket.name); + fn update_greeting(&mut self, active_entry: &str) { + self.greeting = format!("Hello, {}!", active_entry); } } @@ -68,21 +71,24 @@ impl<'a> Component<'a, Message, Action> for BucketsView { match message { Message::SetBuckets(buckets) => { self.navigation.update(navigation::Message::SetOptions( - buckets.into_iter().map(BucketNameAndBalance::new).collect(), + buckets + .iter() + .map(BucketNavigationEntry::from_bucket) + .collect(), )); Action::None } Message::NavigationMessage(message) => match self.navigation.update(message) { - navigation::Action::SelectOption(bucket) => { - self.update_greeting(&bucket.bucket); + navigation::Action::SelectOption(bucket_navigation_entry) => { + self.update_greeting(&bucket_navigation_entry.to_string()); Action::None } navigation::Action::None => Action::None, }, Message::SelectNextBucket => { match self.navigation.update(navigation::Message::SelectNext) { - navigation::Action::SelectOption(bucket) => { - self.update_greeting(&bucket.bucket); + navigation::Action::SelectOption(bucket_navigation_entry) => { + self.update_greeting(&bucket_navigation_entry.to_string()); Action::None } navigation::Action::None => Action::None, @@ -90,8 +96,8 @@ impl<'a> Component<'a, Message, Action> for BucketsView { } Message::SelectPrevBucket => { match self.navigation.update(navigation::Message::SelectPrev) { - navigation::Action::SelectOption(bucket) => { - self.update_greeting(&bucket.bucket); + navigation::Action::SelectOption(bucket_navigation_entry) => { + self.update_greeting(&bucket_navigation_entry.to_string()); Action::None } navigation::Action::None => Action::None, -- cgit v1.2.3