module Anki.Collection (
Collection(..)
, GlobalOptions(..)
, Tag(..)
) where
import Anki.Common (ModificationTime, AnkiException(..), throwErr)
import Anki.Common (dropPrefixOptions, getTextValue, getJsonValue, fromDictionary)
import Anki.Deck (DeckId, Deck, DeckOptions)
import Anki.Model (ModelId, Model)
import Data.Aeson (Value(..), decode, FromJSON(..), genericParseJSON)
import Data.Text (Text)
import Database.SQLite.Simple (FromRow(..), field)
import Database.SQLite.Simple.FromField (FromField(..))
import Database.SQLite.Simple.Internal (Field(..))
import Database.SQLite.Simple.Ok (Ok(..))
import GHC.Generics (Generic)
import qualified Data.Text as T
data Collection = Collection {
collectionId :: Int
, collectionCrt :: Int
, collectionMod :: ModificationTime
, collectionScm :: Int
, collectionVer :: Int
, collectionDty :: Int
, collectionUsn :: Int
, collectionLs :: Int
, collectionGlobalOptions :: GlobalOptions
, collectionModels :: [Model]
, collectionDecks :: [Deck]
, collectionDeckOptions :: [DeckOptions]
, collectionTags :: [Tag]
} deriving (Show, Eq, Generic)
instance FromRow Collection where
fromRow = Collection
<$> field
<*> field
<*> field
<*> field
<*> field
<*> field
<*> field
<*> field
<*> field
<*> field
<*> field
<*> field
<*> field
data GlobalOptions = GlobalOptions {
goNextPos :: Value
, goEstTimes :: Value
, goSortBackwards :: Value
, goSortType :: Value
, goTimeLim :: Value
, goActiveDecks :: [DeckId]
, goAddToCur :: Value
, goCurDeck :: Value
, goCurModel :: ModelId
, goLastUnburied :: Value
, goCollapseTime :: Value
, goActiveCols :: Value
, goSavedFilters :: Value
, goDueCounts :: Value
, goNewBury :: Value
, goNewSpread :: Value
} deriving (Show, Eq, Generic)
instance FromJSON GlobalOptions where
parseJSON = genericParseJSON dropPrefixOptions
instance FromField GlobalOptions where
fromField f = getTextValue f >>= maybe (throwErr f WrongJsonFormat) return . decode
data Tag = Tag {
tagName :: String
, tagNumber :: Int
} deriving (Show, Eq)
instance FromField [Tag] where
fromField f = getJsonValue f >>= fromDictionary mkTag f where
mkTag :: Field -> (Text, Value) -> Ok Tag
mkTag f' = \case
(name, Number number) -> return $ Tag (T.unpack name) (round number)
_ -> throwErr f' WrongJsonFormat