module Anki.Deck (
DeckOptions(..)
, DeckOptionsId
, DeckOptionsLapse(..)
, DeckOptionsRev(..)
, DeckOptionsNew(..)
, Deck(..)
, DeckExtension(..)
, DeckId
) where
import Anki.Common (WeaklyTypedInt, WeaklyTypedBool(..), ModificationTime, AnkiException(..))
import Anki.Common (dropPrefixOptions, getJsonValue, fromDictionary, mkEntry)
import Data.Aeson (Value(..), FromJSON(..), genericParseJSON)
import Data.Aeson.Types ((.:), withObject)
import Database.SQLite.Simple.FromField (FromField(..))
import GHC.Generics (Generic)
type DeckOptionsId = WeaklyTypedInt
data DeckOptions = DeckOptions {
doId :: DeckOptionsId
, doAutoplay :: Value
, doDyn :: Value
, doLapse :: DeckOptionsLapse
, doMaxTaken :: Value
, doMod :: ModificationTime
, doName :: Value
, doNew :: DeckOptionsNew
, doReplayq :: Value
, doRev :: DeckOptionsRev
, doTimer :: Value
, doUsn :: Value
} deriving (Show, Eq, Generic)
instance FromJSON DeckOptions where
parseJSON = genericParseJSON dropPrefixOptions
instance FromField [DeckOptions] where
fromField f = getJsonValue f >>= fromDictionary (mkEntry doId DeckOptionsIdInconsistent) f
data DeckExtension
= NormalDeck {
deckBrowserCollapsed :: Value
, deckConf :: Value
, deckExtendNew :: Value
, deckExtendRev :: Value
}
| DynamicDeck {
deckDelays :: Value
, deckResched :: Value
, deckReturn :: Value
, deckSeparate :: Value
, deckTerms :: Value
} deriving (Show, Eq, Generic)
instance FromJSON DeckExtension where
parseJSON = withObject "DeckExtension" $ \o -> getBool <$> (o .: "dyn") >>= \case
False -> do
deckBrowserCollapsed <- o .: "browserCollapsed"
deckConf <- o .: "conf"
deckExtendNew <- o .: "extendNew"
deckExtendRev <- o .: "extendRev"
return NormalDeck {..}
True -> do
deckDelays <- o .: "delays"
deckResched <- o .: "resched"
deckReturn <- o .: "return"
deckSeparate <- o .: "separate"
deckTerms <- o .: "terms"
return DynamicDeck {..}
data DeckOptionsLapse = DeckOptionsLapse {
dolLeechFails :: Value
, dolMinInt :: Value
, dolDelays :: Value
, dolLeechAction :: Value
, dolMult :: Value
} deriving (Show, Eq, Generic)
instance FromJSON DeckOptionsLapse where
parseJSON = genericParseJSON dropPrefixOptions
data DeckOptionsNew = DeckOptionsNew {
donPerDay :: Value
, donDelays :: Value
, donSeparate :: Value
, donInts :: Value
, donInitialFactor :: Value
, donBury :: Value
, donOrder :: Value
} deriving (Show, Eq, Generic)
instance FromJSON DeckOptionsNew where
parseJSON = genericParseJSON dropPrefixOptions
data DeckOptionsRev = DeckOptionsRev {
dorPerDay :: Value
, dorFuzz :: Value
, dorIvlFct :: Value
, dorMaxIvl :: Value
, dorEase4 :: Value
, dorBury :: Value
, dorMinSpace :: Value
} deriving (Show, Eq, Generic)
instance FromJSON DeckOptionsRev where
parseJSON = genericParseJSON dropPrefixOptions
type DeckId = WeaklyTypedInt
data Deck = Deck {
deckId :: DeckId
, deckName :: Value
, deckCollapsed :: Value
, deckDesc :: Value
, deckMod :: ModificationTime
, deckUsn :: Value
, deckLrnToday :: Value
, deckNewToday :: Value
, deckRevToday :: Value
, deckTimeToday :: Value
, deckExtension :: DeckExtension
} deriving (Show, Eq, Generic)
instance FromJSON Deck where
parseJSON = withObject "Deck" $ \o -> do
deckName <- o .: "name"
deckCollapsed <- o .: "collapsed"
deckDesc <- o .: "desc"
deckId <- o .: "id"
deckMod <- o .: "mod"
deckUsn <- o .: "usn"
deckLrnToday <- o .: "lrnToday"
deckNewToday <- o .: "newToday"
deckRevToday <- o .: "revToday"
deckTimeToday <- o .: "timeToday"
deckExtension <- parseJSON $ Object o
return Deck {..}
instance FromField [Deck] where
fromField f = getJsonValue f >>= fromDictionary (mkEntry deckId DeckIdInconsistent) f