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 -- | Renders the application view
drawFunc :: AppState -> [Widget ResName] drawFunc :: AppState -> [Widget ResName]
drawFunc s = case s^.appMode of drawFunc s = maybe [emptyWidget] drawPassForm $ s^.passForm
InitMode is -> drawPassForm $ is^.setPassForm
_ -> [emptyWidget]
drawPassForm :: Form (Text, Text) () ResName -> [Widget ResName] drawPassForm :: Form (Text, Text) () ResName -> [Widget ResName]
drawPassForm f = drawPassForm f =

View File

@ -43,7 +43,7 @@ import Control.Monad.IO.Class (liftIO)
import Control.Monad.State.Class (gets, put) import Control.Monad.State.Class (gets, put)
import Data.Aeson (decodeFileStrict) import Data.Aeson (decodeFileStrict)
import Graphics.Vty.Input.Events (Event (EvKey)) import Graphics.Vty.Input.Events (Event (EvKey))
import Lens.Micro ((^.)) import Lens.Micro (each, (^.))
import Lens.Micro.Mtl (zoom) import Lens.Micro.Mtl (zoom)
import System.EasyFile import System.EasyFile
( createDirectoryIfMissing ( createDirectoryIfMissing
@ -77,10 +77,9 @@ loadDatabase = zoom database $ liftIO
) >>= mapM_ put ) >>= mapM_ put
fallbackHandler :: BrickEvent ResName () -> EventM ResName AppState () fallbackHandler :: BrickEvent ResName () -> EventM ResName AppState ()
fallbackHandler e = gets (^.appMode) >>= \case fallbackHandler e = gets (^.passForm) >>= \case
InitMode _ -> zoom (appMode.initState.setPassForm) $ Just _ -> zoom (passForm.each) $ handleFormEvent e
handleFormEvent e Nothing -> return ()
_ -> return ()
getKeyDispatcher getKeyDispatcher
:: AppState :: AppState

View File

@ -27,22 +27,13 @@ License along with this program. If not, see
module Password.App.Types ( module Password.App.Types (
-- * Types -- * Types
AppState (..), AppState (..),
AppMode (..),
InitState (..),
RunState (..),
ResName (..), ResName (..),
-- * Lenses -- * Lenses
-- ** AppState -- ** AppState
randGen, randGen,
database, database,
appMode,
-- ** AppMode
initState,
runState,
-- ** InitState
setPassForm,
-- ** RunState
mainPass, mainPass,
passForm,
-- * Constructors -- * Constructors
mkInitialState, mkInitialState,
) where ) where
@ -63,61 +54,36 @@ data AppState = AppState
-- ^ The random number generator -- ^ The random number generator
, _database :: PWDatabase , _database :: PWDatabase
-- ^ The password database -- ^ The password database
, _appMode :: AppMode , _mainPass :: String
-- ^ 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
-- ^ The main password -- ^ The main password
, _passForm :: Maybe PassForm
} }
-- | A password form (with confirmation)
type PassForm = Form (Text, Text) () ResName
-- | Resource identifier -- | Resource identifier
data ResName data ResName
= PassField = PassField
| ConfField | ConfField
deriving (Eq, Ord, Show) deriving (Eq, Ord, Show)
concat <$> mapM makeLenses makeLenses ''AppState
[ ''AppState
, ''AppMode
, ''InitState
, ''RunState
]
-- | Builds an initial state -- | Builds an initial state
mkInitialState :: MonadIO m => m AppState mkInitialState :: MonadIO m => m AppState
mkInitialState = AppState mkInitialState = AppState
<$> initStdGen <$> initStdGen
<*> return newPWDatabase <*> return newPWDatabase
<*> return (InitMode newInitState) <*> return ""
<*> return (Just newPassForm)
-- | New `InitState` value -- | Constructs a blank password form
newInitState :: InitState newPassForm :: PassForm
newInitState = InitState newPassForm = newForm
( newForm
[ (txt "Master password: " <+>) @@= editPasswordField _1 PassField [ (txt "Master password: " <+>) @@= editPasswordField _1 PassField
, (txt "Confirm password: " <+>) @@= editPasswordField _2 ConfField , (txt "Confirm password: " <+>) @@= editPasswordField _2 ConfField
] ]
("", "") ("", "")
)
--jl --jl