{-# LANGUAGE OverloadedStrings #-} module State where import Text.HTML.TagSoup (renderTags) 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, cTags = Nothing, 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 = renderTags (chapterTags chapter) , viewIdx = index }