/// Main UI reducer: Returns a new State from an Action.
Main UI reducer: Returns a new State from an Action.
Main UI reducer : Returns a new State from an Action .
pub fn screen_reducer(state: &State, action: &Action) -> State {
    let s = state.clone();
    match action {
        Action::Home(id) => State {
            prev_screen: Screen::Home,
            screen: Screen::Home,
            id: Some(*id),
            message: None,
            context: s.context,
            error_level: None,
        },
        Action::Create(id) => {
            let (message, error_level) =
                match grin::new_wallet(&s.context.wallet_dir, &s.context.wallet_password) {
                    Ok(seed) => (SeedTemplate { seed: &seed }.render().unwrap(), None),
                    Err(e) => (format!("Error: {}", e), Some(Level::Error)),
                };
            State {
                screen: Screen::Create,
                id: Some(*id),
                message: Some(message),
                error_level,
                ..s
            }
        }
        Action::Send(id, amount, destination) => {
            let (message, error_level) = match grin::send(
                *amount,
                destination.as_str(),
                &s.context.wallet_dir,
                &s.context.owner_endpoint,
                &s.context.http_client,
            ) {
                Ok(msg) => (format!("Success:\n{}", msg), None),
                Err(e) => (format!("Error: {}", e), Some(Level::Info)),
            };
            State {
                screen: Screen::Send,
                id: Some(*id),
                message: Some(message),
                error_level,
                ..s
            }
        }
        Action::Balance(id) => {
            let (message, error_level) = match grin::balance(
                &s.context.wallet_dir,
                &s.context.owner_endpoint,
                &s.context.http_client,
            ) {
                Ok(msg) => (format!("Success:\n{}", msg), None),
                Err(e) => (format!("Error: {}", e), Some(Level::Info)),
            };
            State {
                screen: Screen::Balance,
                id: Some(*id),
                message: Some(message),
                error_level,
                ..s
            }
        }
        Action::Help(id) => {
            let message = Some(HelpTemplate {}.render().unwrap());
            State {
                screen: Screen::Help,
                id: Some(*id),
                message,
                error_level: None,
                ..s
            }
        }
        Action::NoUsername(id) => State {
            id: Some(*id),
            message: Some("You must have a username to use Grin Bot.".into()),
            error_level: Some(Level::Warn),
            ..s
        },
        Action::WrongUsername(id) => State {
            id: Some(*id),
            message: Some(
                "Your username does not match the username in the Grin Bot config.".into(),
            ),
            error_level: Some(Level::Warn),
            ..s
        },
        Action::ModeNotSupported(id) => State {
            id: Some(*id),
            message: Some(
                "For security reasons, inline and group messages are not supported.".into(),
            ),
            error_level: Some(Level::Warn),
            ..s
        },
        Action::Back(id) => State {
            prev_screen: Screen::Home,
            id: Some(*id),
            message: None,
            error_level: None,
            ..s
        },
        Action::CommandError(id, error) => State {
            id: Some(*id),
            message: Some(format!("Error: {}", error)),
            error_level: Some(Level::Error),
            ..s
        },
        Action::Unknown(id) => State {
            id: Some(*id),
            message: None,
            error_level: Some(Level::Error),
            ..s
        },
    }
}
pub fn screen_reducer ( state : & State , action : & Action ) -> State { let s = state . clone ( ) ; match action { Action :: Home ( id ) => State { prev_screen : Screen :: Home , screen : Screen :: Home , id : Some ( * id ) , message : None , context : s . context , error_level : None , } , Action :: Create ( id ) => { let ( message , error_level ) = match grin :: new_wallet ( & s . context . wallet_dir , & s . context . wallet_password ) { Ok ( seed ) => ( SeedTemplate { seed : & seed } . render ( ) . unwrap ( ) , None ) , Err ( e ) => ( format ! ( " " e ) , Some ( Level :: Error ) ) , } ; State { screen : Screen :: Create , id : Some ( * id ) , message : Some ( message ) , error_level , .. s } } Action :: Send ( id , amount , destination ) => { let ( message , error_level ) = match grin :: send ( * amount , destination . as_str ( ) , & s . context . wallet_dir , & s . context . owner_endpoint , & s . context . http_client , ) { Ok ( msg ) => ( format ! ( " \n " msg ) , None ) , Err ( e ) => ( format ! ( " " e ) , Some ( Level :: Info ) ) , } ; State { screen : Screen :: Send , id : Some ( * id ) , message : Some ( message ) , error_level , .. s } } Action :: Balance ( id ) => { let ( message , error_level ) = match grin :: balance ( & s . context . wallet_dir , & s . context . owner_endpoint , & s . context . http_client , ) { Ok ( msg ) => ( format ! ( " \n " msg ) , None ) , Err ( e ) => ( format ! ( " " e ) , Some ( Level :: Info ) ) , } ; State { screen : Screen :: Balance , id : Some ( * id ) , message : Some ( message ) , error_level , .. s } } Action :: Help ( id ) => { let message = Some ( HelpTemplate { } . render ( ) . unwrap ( ) ) ; State { screen : Screen :: Help , id : Some ( * id ) , message , error_level : None , .. s } } Action :: NoUsername ( id ) => State { id : Some ( * id ) , message : Some ( " " . into ( ) ) , error_level : Some ( Level :: Warn ) , .. s } , Action :: WrongUsername ( id ) => State { id : Some ( * id ) , message : Some ( " " . into ( ) , ) , error_level : Some ( Level :: Warn ) , .. s } , Action :: ModeNotSupported ( id ) => State { id : Some ( * id ) , message : Some ( " " . into ( ) , ) , error_level : Some ( Level :: Warn ) , .. s } , Action :: Back ( id ) => State { prev_screen : Screen :: Home , id : Some ( * id ) , message : None , error_level : None , .. s } , Action :: CommandError ( id , error ) => State { id : Some ( * id ) , message : Some ( format ! ( " " error ) ) , error_level : Some ( Level :: Error ) , .. s } , Action :: Unknown ( id ) => State { id : Some ( * id ) , message : None , error_level : Some ( Level :: Error ) , .. s } , } }
Main UI reducer: Returns a new State from an Action.
Main UI reducer : Returns a new State from an Action .
state
&State
action
&Action