Skip to content

Commit

Permalink
Welcome route
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyGiorgio committed May 21, 2024
1 parent dcb119a commit 02e14eb
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ pub enum CoreUIMsg {
FederationInfo(ClientConfig),
AddFederationSuccess,
FederationListUpdated(Vec<FederationItem>),
NeedsInit,
Locked,
Unlocking,
UnlockSuccess,
UnlockFailed(String),
Expand Down
16 changes: 15 additions & 1 deletion src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use crate::{
};

const PEG_IN_TIMEOUT_YEAR: Duration = Duration::from_secs(86400 * 365);
const HARBOR_FILE_NAME: &str = "harbor.sqlite";

#[derive(Clone)]
struct HarborCore {
Expand Down Expand Up @@ -376,6 +377,19 @@ pub fn run_core() -> Subscription<Message> {
std::fs::create_dir_all(path.clone()).expect("Could not create datadir");
log::info!("Using datadir: {path:?}");

tokio::time::sleep(Duration::from_secs(1)).await;

// Check if the database file exists already, if so tell UI to unlock
if std::fs::metadata(path.join(HARBOR_FILE_NAME)).is_ok() {
tx.send(Message::core_msg(None, CoreUIMsg::Locked))
.await
.expect("should send");
} else {
tx.send(Message::core_msg(None, CoreUIMsg::NeedsInit))
.await
.expect("should send");
}

loop {
let msg = core_handle.recv().await;

Expand All @@ -389,7 +403,7 @@ pub fn run_core() -> Subscription<Message> {
.expect("should send");

// attempting to unlock
let db_path = path.join("harbor.sqlite");
let db_path = path.join(HARBOR_FILE_NAME);
let db =
spawn_blocking(move || setup_db(db_path.to_str().unwrap(), password))
.await
Expand Down
22 changes: 21 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ pub mod routes;
pub fn main() -> iced::Result {
pretty_env_logger::init();
program("Harbor", HarborWallet::update, HarborWallet::view)
// .load(HarborWallet::load)
.font(include_bytes!("../assets/fonts/Inter-Regular.ttf").as_slice())
.font(include_bytes!("../assets/fonts/Inter-Bold.ttf").as_slice())
.theme(HarborWallet::theme)
Expand Down Expand Up @@ -62,6 +61,13 @@ enum ReceiveStatus {
WaitingToReceive,
}

#[derive(Default, Debug, Clone, PartialEq)]
enum WelcomeStatus {
#[default]
Loading,
NeedsInit,
}

#[derive(Default, Debug, Clone, PartialEq)]
enum UnlockStatus {
#[default]
Expand Down Expand Up @@ -124,6 +130,8 @@ pub struct HarborWallet {
balance_sats: u64,
transaction_history: Vec<TransactionItem>,
federation_list: Vec<FederationItem>,
// Welcome screen
init_status: WelcomeStatus,
// Lock screen
password_input_str: String,
unlock_status: UnlockStatus,
Expand Down Expand Up @@ -255,6 +263,7 @@ impl HarborWallet {
self.ui_handle = Some(ui_handle);
println!("Core loaded");

// TODO I don't think this is the best place for it
focus_input_id("password_unlock_input")

// Command::none()
Expand Down Expand Up @@ -563,6 +572,16 @@ impl HarborWallet {
self.receive_address = Some(address);
Command::none()
}
CoreUIMsg::Locked => {
info!("Got locked message");
self.active_route = Route::Unlock;
Command::none()
}
CoreUIMsg::NeedsInit => {
info!("Got init message");
self.init_status = WelcomeStatus::NeedsInit;
Command::none()
}
CoreUIMsg::Unlocking => {
info!("Got unlocking message");
self.unlock_status = UnlockStatus::Unlocking;
Expand Down Expand Up @@ -600,6 +619,7 @@ impl HarborWallet {
Route::History => row![sidebar, crate::routes::history(self)].into(),
Route::Transfer => row![sidebar, crate::routes::transfer(self)].into(),
Route::Settings => row![sidebar, crate::routes::settings(self)].into(),
Route::Welcome => crate::routes::welcome(self),
};

active_route
Expand Down
4 changes: 4 additions & 0 deletions src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ pub use history::*;
pub mod settings;
pub use settings::*;

pub mod welcome;
pub use welcome::*;

#[derive(Default, PartialEq, Debug, Clone, Copy)]
pub enum Route {
#[default]
Welcome,
Unlock,
Home,
Mints,
Expand Down
58 changes: 58 additions & 0 deletions src/routes/welcome.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use crate::{
components::{h_button, the_spinner, SvgIcon},
UnlockStatus, WelcomeStatus,
};
use iced::{
widget::{center, column, container, text, Svg},
Theme,
};
use iced::{Alignment, Element, Length};

use crate::{HarborWallet, Message};

pub fn welcome(harbor: &HarborWallet) -> Element<Message> {
let column = match harbor.init_status {
WelcomeStatus::Loading => {
let harbor_logo = Svg::from_path("assets/harbor_logo.svg")
.width(167)
.height(61);

let welcome_message = text("Welcome, we're glad you are here.").size(24);

let spinner: Element<'static, Message, Theme> = the_spinner();

column![harbor_logo, welcome_message, spinner]
.spacing(32)
.align_items(Alignment::Center)
.width(Length::Fixed(350.))
}
WelcomeStatus::NeedsInit => {
let action = if harbor.unlock_status == UnlockStatus::Unlocking {
None
} else {
Some(Message::Unlock(harbor.password_input_str.clone()))
};

let new_wallet = h_button(
"Create New Wallet",
SvgIcon::Plus,
harbor.unlock_status == UnlockStatus::Unlocking,
)
.on_press_maybe(action.clone())
.width(Length::Fill);

let harbor_logo = Svg::from_path("assets/harbor_logo.svg")
.width(167)
.height(61);

let welcome_message = text("Welcome, we're glad you are here.").size(24);

column![harbor_logo, welcome_message, new_wallet,]
.spacing(32)
.align_items(Alignment::Center)
.width(Length::Fixed(350.))
}
};

container(center(column)).into()
}

0 comments on commit 02e14eb

Please sign in to comment.