41 lines
963 B
Haskell
41 lines
963 B
Haskell
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
module State
|
|
( initialState,
|
|
prepareView
|
|
)
|
|
where
|
|
|
|
import Types (AppState(..), Chapter(chapterTags, chapterTitle), ChapterView(..), EpubEnv)
|
|
|
|
-- Get an element from a list without crashing
|
|
safeGet :: Int -> [a] -> Maybe a
|
|
safeGet n xs
|
|
| n < 0 = Nothing
|
|
| otherwise = case drop n xs of
|
|
(x : _) -> Just x
|
|
[] -> Nothing
|
|
|
|
-- loading template
|
|
initialState :: EpubEnv -> AppState
|
|
initialState env =
|
|
AppState
|
|
{ cEnv = env,
|
|
cIdx = 0,
|
|
cSpine = [],
|
|
bTitle = "Svitak - Loading...",
|
|
zoomlvl = 1.0,
|
|
activeTask = Nothing,
|
|
cAllChapters = [],
|
|
taskVersion = 0
|
|
}
|
|
|
|
-- prepare the data for the UI
|
|
prepareView :: AppState -> Int -> Maybe ChapterView
|
|
prepareView st index = do
|
|
chapter <- safeGet index (cAllChapters st)
|
|
return $ ChapterView
|
|
{ viewTitle = bTitle st <> " - " <> chapterTitle chapter
|
|
, viewHtml = chapterTags chapter
|
|
, viewIdx = index
|
|
}
|