load database on start
This commit is contained in:
@@ -24,7 +24,7 @@ License along with this program. If not, see
|
||||
|
||||
{-# LANGUAGE LambdaCase, OverloadedStrings #-}
|
||||
|
||||
module Password.App.Event (eventHandler) where
|
||||
module Password.App.Event (eventHandler, loadDatabase) where
|
||||
|
||||
import Brick (BrickEvent (VtyEvent), EventM, halt)
|
||||
import Brick.Forms (handleFormEvent)
|
||||
@@ -39,13 +39,23 @@ import Brick.Keybindings
|
||||
, onEvent
|
||||
)
|
||||
import Control.Monad (unless)
|
||||
import Control.Monad.State.Class (gets)
|
||||
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.Mtl (zoom)
|
||||
import System.EasyFile
|
||||
( createDirectoryIfMissing
|
||||
, getAppUserDataDirectory
|
||||
, (</>)
|
||||
)
|
||||
|
||||
import Password.App.Types
|
||||
|
||||
dbFile :: String
|
||||
dbFile = "database.json"
|
||||
|
||||
data KEventID = QuitKE deriving (Eq, Ord, Show)
|
||||
|
||||
-- | The main event handler
|
||||
@@ -55,6 +65,14 @@ eventHandler e@(VtyEvent (EvKey k m)) = do
|
||||
handleKey disp k m >>= flip unless (fallbackHandler e)
|
||||
eventHandler e = fallbackHandler e
|
||||
|
||||
loadDatabase :: EventM ResName AppState ()
|
||||
loadDatabase = zoom database $ liftIO
|
||||
( do
|
||||
dir <- mkAppDir
|
||||
let fn = dir </> dbFile
|
||||
decodeFileStrict fn
|
||||
) >>= mapM_ put
|
||||
|
||||
fallbackHandler :: BrickEvent ResName () -> EventM ResName AppState ()
|
||||
fallbackHandler e = gets (^.appMode) >>= \case
|
||||
InitMode _ -> zoom (appMode.initState.setPassForm) $
|
||||
@@ -76,4 +94,10 @@ getKeyDispatcher s = either (error "can't build dispatcher") id $
|
||||
keyBindingsFor :: AppState -> [(KEventID, [Binding])]
|
||||
keyBindingsFor = const [(QuitKE, [ctrl 'c'])]
|
||||
|
||||
mkAppDir :: IO FilePath
|
||||
mkAppDir = do
|
||||
path <- getAppUserDataDirectory "passman"
|
||||
createDirectoryIfMissing True path
|
||||
return path
|
||||
|
||||
--jl
|
||||
|
||||
Reference in New Issue
Block a user