simplified application state

This commit is contained in:
Jonathan Lamothe 2024-09-12 20:57:51 -04:00
parent 8bddf35a1a
commit 41278e81a9
3 changed files with 21 additions and 58 deletions

View File

@ -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 =

View File

@ -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

View File

@ -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
-- | Constructs a blank password form
newPassForm :: PassForm
newPassForm = newForm
[ (txt "Master password: " <+>) @@= editPasswordField _1 PassField
, (txt "Confirm password: " <+>) @@= editPasswordField _2 ConfField
]
("", "")
)
--jl