From d58293bef5fcb296b56804e9566ddc04307dab5c Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Thu, 19 Sep 2019 07:17:42 -0400 Subject: [PATCH] load database on start --- package.yaml | 1 + src/Mtlstats.hs | 20 ++++++++++++++++++-- src/Mtlstats/Config.hs | 8 ++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/package.yaml b/package.yaml index 895bf92..7d92da2 100644 --- a/package.yaml +++ b/package.yaml @@ -22,6 +22,7 @@ description: Please see the README on GitHub at = 4.7 && < 5 - aeson >= 1.4.4.0 && < 1.5 +- easy-file >= 0.2.2 && < 0.3 - extra >= 1.6.17 && < 1.7 - microlens-th >= 0.4.2.3 && < 0.5 - ncurses >= 0.2.16 && < 0.3 diff --git a/src/Mtlstats.hs b/src/Mtlstats.hs index a077f28..aab6f25 100644 --- a/src/Mtlstats.hs +++ b/src/Mtlstats.hs @@ -19,15 +19,23 @@ along with this program. If not, see . -} +{-# LANGUAGE ScopedTypeVariables #-} + module Mtlstats (initState, mainLoop) where +import Control.Exception (IOException, catch) import Control.Monad (void) import Control.Monad.Extra (whenM) +import Control.Monad.IO.Class (liftIO) import Control.Monad.Trans.Class (lift) import Control.Monad.Trans.State (get, gets) -import Data.Maybe (fromJust) +import Data.Aeson (decodeFileStrict) +import Data.Maybe (fromJust, fromMaybe) +import Lens.Micro ((&), (.~)) +import System.EasyFile (getAppUserDataDirectory, ()) import qualified UI.NCurses as C +import Mtlstats.Config import Mtlstats.Control import Mtlstats.Types @@ -36,7 +44,15 @@ initState :: C.Curses ProgState initState = do C.setEcho False void $ C.setCursorMode C.CursorInvisible - return newProgState + db <- liftIO $ do + dir <- getAppUserDataDirectory appName + let dbFile = dir dbFname + fromMaybe newDatabase <$> catch + (decodeFileStrict dbFile) + (\(_ :: IOException) -> return Nothing) + return + $ newProgState + & database .~ db -- | Main program loop mainLoop :: Action () diff --git a/src/Mtlstats/Config.hs b/src/Mtlstats/Config.hs index 392c17d..22bc105 100644 --- a/src/Mtlstats/Config.hs +++ b/src/Mtlstats/Config.hs @@ -28,3 +28,11 @@ myTeam = "MONTREAL" -- | The maximum number of function keys maxFunKeys :: Int maxFunKeys = 9 + +-- | The application name +appName :: String +appName = "mtlstats" + +-- | The database filename +dbFname :: String +dbFname = "database.json"