41 lines
1 KiB
Haskell
41 lines
1 KiB
Haskell
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
module State
|
|
( initialState,
|
|
prepareView
|
|
)
|
|
where
|
|
|
|
import Types (AppState(..), Chapter(chapterTags, chapterTitle), ChapterView(..), EpubEnv, ChapterIndex(..))
|
|
|
|
-- Get an element from a list without crashing
|
|
safeGet :: ChapterIndex -> [a] -> Maybe a
|
|
safeGet (ChapterIndex 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 = ChapterIndex 0,
|
|
cSpine = [],
|
|
bTitle = "Svitak - Loading...",
|
|
zoomlvl = 1.0,
|
|
activeTask = Nothing,
|
|
cAllChapters = [],
|
|
taskVersion = 0
|
|
}
|
|
|
|
-- prepare the data for the UI
|
|
prepareView :: AppState -> ChapterIndex -> Maybe ChapterView
|
|
prepareView st index = do
|
|
chapter <- safeGet index (cAllChapters st)
|
|
pure $ ChapterView
|
|
{ viewTitle = bTitle st <> " - " <> chapterTitle chapter
|
|
, viewHtml = chapterTags chapter
|
|
, viewIdx = index
|
|
}
|