From 41278e81a9f6aaa750662cde91ebe0127722c6ef Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Thu, 12 Sep 2024 20:57:51 -0400 Subject: [PATCH] simplified application state --- src/Password/App/Draw.hs | 4 +-- src/Password/App/Event.hs | 9 +++--- src/Password/App/Types.hs | 66 ++++++++++----------------------------- 3 files changed, 21 insertions(+), 58 deletions(-) diff --git a/src/Password/App/Draw.hs b/src/Password/App/Draw.hs index 1cc6ff2..999d9cd 100644 --- a/src/Password/App/Draw.hs +++ b/src/Password/App/Draw.hs @@ -35,9 +35,7 @@ import Password.App.Types -- | Renders the application view drawFunc :: AppState -> [Widget ResName] -drawFunc s = case s^.appMode of - InitMode is -> drawPassForm $ is^.setPassForm - _ -> [emptyWidget] +drawFunc s = maybe [emptyWidget] drawPassForm $ s^.passForm drawPassForm :: Form (Text, Text) () ResName -> [Widget ResName] drawPassForm f = diff --git a/src/Password/App/Event.hs b/src/Password/App/Event.hs index 4f43486..5675dd5 100644 --- a/src/Password/App/Event.hs +++ b/src/Password/App/Event.hs @@ -43,7 +43,7 @@ import Control.Monad.IO.Class (liftIO) import Control.Monad.State.Class (gets, put) import Data.Aeson (decodeFileStrict) import Graphics.Vty.Input.Events (Event (EvKey)) -import Lens.Micro ((^.)) +import Lens.Micro (each, (^.)) import Lens.Micro.Mtl (zoom) import System.EasyFile ( createDirectoryIfMissing @@ -77,10 +77,9 @@ loadDatabase = zoom database $ liftIO ) >>= mapM_ put fallbackHandler :: BrickEvent ResName () -> EventM ResName AppState () -fallbackHandler e = gets (^.appMode) >>= \case - InitMode _ -> zoom (appMode.initState.setPassForm) $ - handleFormEvent e - _ -> return () +fallbackHandler e = gets (^.passForm) >>= \case + Just _ -> zoom (passForm.each) $ handleFormEvent e + Nothing -> return () getKeyDispatcher :: AppState diff --git a/src/Password/App/Types.hs b/src/Password/App/Types.hs index 63ce055..e98f799 100644 --- a/src/Password/App/Types.hs +++ b/src/Password/App/Types.hs @@ -27,22 +27,13 @@ License along with this program. If not, see module Password.App.Types ( -- * Types AppState (..), - AppMode (..), - InitState (..), - RunState (..), ResName (..), -- * Lenses -- ** AppState randGen, database, - appMode, - -- ** AppMode - initState, - runState, - -- ** InitState - setPassForm, - -- ** RunState mainPass, + passForm, -- * Constructors mkInitialState, ) where @@ -63,61 +54,36 @@ data AppState = AppState -- ^ The random number generator , _database :: PWDatabase -- ^ The password database - , _appMode :: AppMode - -- ^ The current operating mode - } - --- | The applicaiton's mode -data AppMode - = InitMode - { _initState :: InitState - -- ^ Initialization state - } -- ^ initialization mode (master password form) - | RunMode - { _runState :: RunState - -- ^ State of initialized app - } -- ^ Running mode - --- | Application initialization state -newtype InitState = InitState - { _setPassForm :: Form (Text, Text) () ResName - -- ^ password form - } - --- | State of the initialized application -newtype RunState = RunState - { _mainPass :: String + , _mainPass :: String -- ^ The main password + , _passForm :: Maybe PassForm } +-- | A password form (with confirmation) +type PassForm = Form (Text, Text) () ResName + -- | Resource identifier data ResName = PassField | ConfField deriving (Eq, Ord, Show) -concat <$> mapM makeLenses - [ ''AppState - , ''AppMode - , ''InitState - , ''RunState - ] +makeLenses ''AppState -- | Builds an initial state mkInitialState :: MonadIO m => m AppState mkInitialState = AppState <$> initStdGen <*> return newPWDatabase - <*> return (InitMode newInitState) + <*> return "" + <*> return (Just newPassForm) --- | New `InitState` value -newInitState :: InitState -newInitState = InitState - ( newForm - [ (txt "Master password: " <+>) @@= editPasswordField _1 PassField - , (txt "Confirm password: " <+>) @@= editPasswordField _2 ConfField - ] - ("", "") - ) +-- | Constructs a blank password form +newPassForm :: PassForm +newPassForm = newForm + [ (txt "Master password: " <+>) @@= editPasswordField _1 PassField + , (txt "Confirm password: " <+>) @@= editPasswordField _2 ConfField + ] + ("", "") --jl