Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::database::Database;
use crate::logging::initialize_logger;
use crate::platform::{BackendTask, BackendTaskSuccessResult};
use crate::ui::document_query_screen::DocumentQueryScreen;
use crate::ui::dpns_contested_names_screen::DPNSContestedNamesScreen;
use crate::ui::dpns_contested_names_screen::{DPNSContestedNamesScreen, DPNSSubscreen};
use crate::ui::identities::identities_screen::IdentitiesScreen;
use crate::ui::network_chooser_screen::NetworkChooserScreen;
use crate::ui::transition_visualizer_screen::TransitionVisualizerScreen;
Expand Down Expand Up @@ -116,7 +116,12 @@ impl AppState {
let testnet_app_context = AppContext::new(Network::Testnet, db.clone());

let mut identities_screen = IdentitiesScreen::new(&mainnet_app_context);
let mut dpns_contested_names_screen = DPNSContestedNamesScreen::new(&mainnet_app_context);
let mut dpns_active_contests_screen =
DPNSContestedNamesScreen::new(&mainnet_app_context, DPNSSubscreen::Active);
let mut dpns_past_contests_screen =
DPNSContestedNamesScreen::new(&mainnet_app_context, DPNSSubscreen::Past);
let mut dpns_my_usernames_screen =
DPNSContestedNamesScreen::new(&mainnet_app_context, DPNSSubscreen::Owned);
Comment on lines +119 to +124

@coderabbitai coderabbitai Bot Oct 23, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider extracting DPNS screen initialization logic

While the initialization is correct, consider extracting this into a helper method to reduce code duplication and improve maintainability.

+ fn create_dpns_screens(app_context: &Arc<AppContext>) -> (
+     DPNSContestedNamesScreen,
+     DPNSContestedNamesScreen,
+     DPNSContestedNamesScreen,
+ ) {
+     (
+         DPNSContestedNamesScreen::new(app_context, DPNSSubscreen::Active),
+         DPNSContestedNamesScreen::new(app_context, DPNSSubscreen::Past),
+         DPNSContestedNamesScreen::new(app_context, DPNSSubscreen::Owned),
+     )
+ }

- let mut dpns_active_contests_screen =
-     DPNSContestedNamesScreen::new(&mainnet_app_context, DPNSSubscreen::Active);
- let mut dpns_past_contests_screen =
-     DPNSContestedNamesScreen::new(&mainnet_app_context, DPNSSubscreen::Past);
- let mut dpns_my_usernames_screen =
-     DPNSContestedNamesScreen::new(&mainnet_app_context, DPNSSubscreen::Owned);
+ let (mut dpns_active_contests_screen, mut dpns_past_contests_screen, mut dpns_my_usernames_screen) =
+     create_dpns_screens(&mainnet_app_context);

Committable suggestion was skipped due to low confidence.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's fine

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@QuantumExplorer, understood.

(_/)
( •_• )

let mut transition_visualizer_screen =
TransitionVisualizerScreen::new(&mainnet_app_context);
let mut document_query_screen = DocumentQueryScreen::new(&mainnet_app_context);
Expand All @@ -136,7 +141,12 @@ impl AppState {
if network == Network::Testnet && testnet_app_context.is_some() {
let testnet_app_context = testnet_app_context.as_ref().unwrap();
identities_screen = IdentitiesScreen::new(testnet_app_context);
dpns_contested_names_screen = DPNSContestedNamesScreen::new(testnet_app_context);
dpns_active_contests_screen =
DPNSContestedNamesScreen::new(&testnet_app_context, DPNSSubscreen::Active);
dpns_past_contests_screen =
DPNSContestedNamesScreen::new(&testnet_app_context, DPNSSubscreen::Past);
dpns_my_usernames_screen =
DPNSContestedNamesScreen::new(&testnet_app_context, DPNSSubscreen::Owned);
transition_visualizer_screen = TransitionVisualizerScreen::new(testnet_app_context);
document_query_screen = DocumentQueryScreen::new(testnet_app_context);
}
Expand All @@ -156,8 +166,16 @@ impl AppState {
Screen::IdentitiesScreen(identities_screen),
),
(
RootScreenType::RootScreenDPNSContestedNames,
Screen::DPNSContestedNamesScreen(dpns_contested_names_screen),
RootScreenType::RootScreenDPNSActiveContests,
Screen::DPNSContestedNamesScreen(dpns_active_contests_screen),
),
(
RootScreenType::RootScreenDPNSPastContests,
Screen::DPNSContestedNamesScreen(dpns_past_contests_screen),
),
(
RootScreenType::RootScreenDPNSOwnedNames,
Screen::DPNSContestedNamesScreen(dpns_my_usernames_screen),
),
(
RootScreenType::RootScreenTransitionVisualizerScreen,
Expand Down
33 changes: 33 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::sdk_wrapper::initialize_sdk;
use crate::ui::RootScreenType;
use dash_sdk::dashcore_rpc::{Auth, Client};
use dash_sdk::dpp::dashcore::Network;
use dash_sdk::dpp::identity::accessors::IdentityGettersV0;
use dash_sdk::dpp::identity::Identity;
use dash_sdk::dpp::system_data_contracts::{load_system_data_contract, SystemDataContract};
use dash_sdk::dpp::version::PlatformVersion;
Expand Down Expand Up @@ -130,6 +131,38 @@ impl AppContext {
self.db.get_ongoing_contested_names(self)
}

pub fn owned_contested_names(&self) -> Result<Vec<ContestedName>> {
let all_contested_names = self.all_contested_names().unwrap_or_else(|e| {
tracing::error!("Failed to load contested names: {:?}", e);
Vec::new() // Use default value if loading fails
});
let local_qualified_identities =
self.load_local_qualified_identities().unwrap_or_else(|e| {
tracing::error!("Failed to load local qualified identities: {:?}", e);
Vec::new() // Use default value if loading fails
});

// Collect the identifiers from local qualified identities
let local_identifiers: Vec<_> = local_qualified_identities
.iter()
.map(|identity| identity.identity.id())
.collect();

// Filter the contested names by checking if they were awarded to any local identifier
let owned_contested_names: Vec<ContestedName> = all_contested_names
.into_iter()
.filter(|contested_name| {
if let Some(awarded_to) = &contested_name.awarded_to {
local_identifiers.contains(awarded_to)
} else {
false
}
})
.collect();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider optimizing the filtering logic.

The current implementation creates intermediate vectors which could be avoided for better performance.

Consider this more efficient implementation using iterator chaining:

-        // Collect the identifiers from local qualified identities
-        let local_identifiers: Vec<_> = local_qualified_identities
-            .iter()
-            .map(|identity| identity.identity.id())
-            .collect();
-
-        // Filter the contested names by checking if they were awarded to any local identifier
-        let owned_contested_names: Vec<ContestedName> = all_contested_names
-            .into_iter()
-            .filter(|contested_name| {
-                if let Some(awarded_to) = &contested_name.awarded_to {
-                    local_identifiers.contains(awarded_to)
-                } else {
-                    false
-                }
-            })
-            .collect();
+        let owned_contested_names = all_contested_names
+            .into_iter()
+            .filter(|contested_name| {
+                contested_name.awarded_to.as_ref().map_or(false, |awarded_to| {
+                    local_qualified_identities
+                        .iter()
+                        .any(|identity| identity.identity.id() == awarded_to)
+                })
+            })
+            .collect();
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Collect the identifiers from local qualified identities
let local_identifiers: Vec<_> = local_qualified_identities
.iter()
.map(|identity| identity.identity.id())
.collect();
// Filter the contested names by checking if they were awarded to any local identifier
let owned_contested_names: Vec<ContestedName> = all_contested_names
.into_iter()
.filter(|contested_name| {
if let Some(awarded_to) = &contested_name.awarded_to {
local_identifiers.contains(awarded_to)
} else {
false
}
})
.collect();
let owned_contested_names = all_contested_names
.into_iter()
.filter(|contested_name| {
contested_name.awarded_to.as_ref().map_or(false, |awarded_to| {
local_qualified_identities
.iter()
.any(|identity| identity.identity.id() == awarded_to)
})
})
.collect();


Ok(owned_contested_names)
}

/// Updates the `start_root_screen` in the settings table
pub fn update_settings(&self, root_screen_type: RootScreenType) -> Result<()> {
self.db
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn main() -> eframe::Result<()> {
..Default::default()
};
eframe::run_native(
"Identity Manager",
"Dash Evo Tool",
native_options,
Box::new(|_cc| Ok(Box::new(app::AppState::new()))),
)
Expand Down
54 changes: 54 additions & 0 deletions src/ui/components/dpns_subscreen_chooser_panel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use crate::context::AppContext;
use crate::{app::AppAction, ui::RootScreenType};
use egui::{Context, Frame, Margin, SidePanel};
use std::sync::Arc;

pub fn add_dpns_subscreen_chooser_panel(ctx: &Context, app_context: &Arc<AppContext>) -> AppAction {
let mut action = AppAction::None;

let subscreens = vec!["Active contests", "Past contests", "My usernames"];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider using an enum for subscreen names.

Using string literals for screen names makes the code more prone to typos and harder to maintain. Consider defining an enum for the subscreens.

enum DPNSSubscreenType {
    ActiveContests,
    PastContests,
    MyUsernames,
}

impl DPNSSubscreenType {
    fn display_name(&self) -> &'static str {
        match self {
            Self::ActiveContests => "Active contests",
            Self::PastContests => "Past contests",
            Self::MyUsernames => "My usernames",
        }
    }
}

// Then use it like:
let subscreens = vec![
    DPNSSubscreenType::ActiveContests,
    DPNSSubscreenType::PastContests,
    DPNSSubscreenType::MyUsernames,
];


SidePanel::left("dpns_subscreen_chooser_panel")
.default_width(250.0)
.frame(
Frame::none()
.fill(ctx.style().visuals.panel_fill)
.inner_margin(Margin::same(10.0)),
)
Comment on lines +14 to +20

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider adding accessibility attributes to the panel.

While the panel setup is well-structured, consider adding accessibility attributes such as ARIA labels to improve screen reader support.

 SidePanel::left("dpns_subscreen_chooser_panel")
     .default_width(250.0)
+    .id(egui::Id::new("dpns_navigation_panel"))
     .frame(
         Frame::none()
             .fill(ctx.style().visuals.panel_fill)
             .inner_margin(Margin::same(10.0)),
     )
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
SidePanel::left("dpns_subscreen_chooser_panel")
.default_width(250.0)
.frame(
Frame::none()
.fill(ctx.style().visuals.panel_fill)
.inner_margin(Margin::same(10.0)),
)
SidePanel::left("dpns_subscreen_chooser_panel")
.default_width(250.0)
.id(egui::Id::new("dpns_navigation_panel"))
.frame(
Frame::none()
.fill(ctx.style().visuals.panel_fill)
.inner_margin(Margin::same(10.0)),
)

.show(ctx, |ui| {
// Display subscreen names
ui.vertical(|ui| {
ui.label("DPNS Subscreens");
ui.add_space(10.0);

for subscreen in subscreens {
// Show the subscreen name as a clickable option
if ui.button(subscreen).clicked() {
// Handle navigation based on which subscreen is selected
match subscreen {
"Active contests" => {
action = AppAction::SetMainScreen(
RootScreenType::RootScreenDPNSActiveContests,
)
}
"Past contests" => {
action = AppAction::SetMainScreen(
RootScreenType::RootScreenDPNSPastContests,
)
}
"My usernames" => {
action = AppAction::SetMainScreen(
RootScreenType::RootScreenDPNSOwnedNames,
)
}
_ => {}
}
}

ui.add_space(5.0);
}
Comment on lines +27 to +52

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance user experience with tooltips and keyboard navigation.

Consider adding tooltips and improving keyboard navigation for better user experience.

 for subscreen in subscreens {
-    if ui.button(subscreen.display_name()).clicked() {
+    let button = egui::Button::new(subscreen.display_name())
+        .shortcut_text(format!("Alt+{}", (subscreen as u8 + 1)))
+        .wrap(false);
+    if ui.add(button)
+        .on_hover_text(format!("View {}", subscreen.display_name().to_lowercase()))
+        .clicked() {
         match subscreen {
             // ... existing match arms ...
         }
     }

Committable suggestion was skipped due to low confidence.

});
});
Comment on lines +19 to +54

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Several improvements for the UI implementation.

  1. The app_context parameter is unused and could be removed.
  2. Consider adding tooltips or aria-labels for better accessibility.
  3. The match statement could be simplified using a more direct mapping.

Here's how you could improve the implementation:

-pub fn add_dpns_subscreen_chooser_panel(ctx: &Context, app_context: &Arc<AppContext>) -> AppAction {
+pub fn add_dpns_subscreen_chooser_panel(ctx: &Context) -> AppAction {
     let mut action = AppAction::None;
-    let subscreens = vec!["Active contests", "Past contests", "My usernames"];
+    let subscreens = vec![
+        ("Active contests", RootScreenType::RootScreenDPNSActiveContests),
+        ("Past contests", RootScreenType::RootScreenDPNSPastContests),
+        ("My usernames", RootScreenType::RootScreenDPNSOwnedNames),
+    ];

     // ... panel setup ...

     ui.vertical(|ui| {
         ui.label("DPNS Subscreens");
         ui.add_space(10.0);

-        for subscreen in subscreens {
+        for (label, screen_type) in subscreens {
             // Show the subscreen name as a clickable option
-            if ui.button(subscreen).clicked() {
-                match subscreen {
-                    "Active contests" => {
-                        action = AppAction::SetMainScreen(
-                            RootScreenType::RootScreenDPNSActiveContests,
-                        )
-                    }
-                    "Past contests" => {
-                        action = AppAction::SetMainScreen(
-                            RootScreenType::RootScreenDPNSPastContests,
-                        )
-                    }
-                    "My usernames" => {
-                        action = AppAction::SetMainScreen(
-                            RootScreenType::RootScreenDPNSOwnedNames,
-                        )
-                    }
-                    _ => {}
-                }
+            let button = egui::Button::new(label)
+                .wrap(false)
+                .sense(egui::Sense::click());
+            if ui.add_enabled(true, button)
+                .on_hover_text(format!("View {}", label.to_lowercase()))
+                .clicked() {
+                action = AppAction::SetMainScreen(screen_type);
             }
             ui.add_space(5.0);
         }
     });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Display subscreen names
ui.vertical(|ui| {
ui.label("DPNS Subscreens");
ui.add_space(10.0);
for subscreen in subscreens {
// Show the subscreen name as a clickable option
if ui.button(subscreen).clicked() {
// Handle navigation based on which subscreen is selected
match subscreen {
"Active contests" => {
action = AppAction::SetMainScreen(
RootScreenType::RootScreenDPNSActiveContests,
)
}
"Past contests" => {
action = AppAction::SetMainScreen(
RootScreenType::RootScreenDPNSPastContests,
)
}
"My usernames" => {
action = AppAction::SetMainScreen(
RootScreenType::RootScreenDPNSOwnedNames,
)
}
_ => {}
}
}
ui.add_space(5.0);
}
});
});
// Display subscreen names
ui.vertical(|ui| {
ui.label("DPNS Subscreens");
ui.add_space(10.0);
for (label, screen_type) in subscreens {
// Show the subscreen name as a clickable option
let button = egui::Button::new(label)
.wrap(false)
.sense(egui::Sense::click());
if ui.add_enabled(true, button)
.on_hover_text(format!("View {}", label.to_lowercase()))
.clicked() {
action = AppAction::SetMainScreen(screen_type);
}
ui.add_space(5.0);
}
});
});


action
}
2 changes: 1 addition & 1 deletion src/ui/components/left_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn add_left_panel(
("I", RootScreenType::RootScreenIdentities, "identity.png"),
(
"C",
RootScreenType::RootScreenDPNSContestedNames,
RootScreenType::RootScreenDPNSActiveContests,
"voting.png",
),
("Q", RootScreenType::RootScreenDocumentQuery, "doc.png"),
Expand Down
1 change: 1 addition & 0 deletions src/ui/components/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod contract_chooser_panel;
pub mod dpns_subscreen_chooser_panel;
pub mod entropy_grid;
pub mod left_panel;
pub mod top_panel;
Loading