{-# OPTIONS -fglasgow-exts #-} module Eg.Music.Browser where import Test.FIT.Fixture import Test.FIT.ActionFixture as AF -- hiding (newActionFixture) import Data.IORef import Data.Dynamic import Eg.Music.Music import Eg.Music.Simulator import Eg.Music.MusicPlayer as Player import Eg.Music.MusicLibrary {- There's a lot of global-mutable-state in the various fixtures: - ActionFixture.actor :: Fixture - Music.status :: String (this should really be MusicPlayer.status) - MusicPlayer.playing :: Maybe Music - MusicPlayer.paused :: Double - MusicLibrary.looking :: Maybe Music - MusicLibrary.library :: [Music] - Simulator.system :: Simulator - Simulator.time :: Integer -} newActionFixture :: BaseFixture -> IO ActionFixture newActionFixture bf = do af <- AF.newActionFixture bf "Eg.Music.Browser" setPlaying af Nothing setLooking af Nothing setStatus af "" setPaused af 0 setSimulator af newSimulator return af setMusicLibrary :: ActionFixture -> [Music] -> IO () setMusicLibrary af lib = setGlobalVar af "MusicLibrary.library" lib getMusicLibrary :: ActionFixture -> IO [Music] getMusicLibrary af = getGlobalVar af "MusicLibrary.library" setLooking :: ActionFixture -> Maybe Music -> IO () setLooking af m = setGlobalVar af "MusicLibrary.looking" m getLooking :: ActionFixture -> IO (Maybe Music) getLooking af = getGlobalVar af "MusicLibrary.looking" setPlaying :: ActionFixture -> Maybe Music -> IO () setPlaying af m = setGlobalVar af "MusicLibrary.playing" m getPlaying :: ActionFixture -> IO (Maybe Music) getPlaying af = getGlobalVar af "MusicLibrary.playing" setPaused :: ActionFixture -> Double -> IO () setPaused af m = setGlobalVar af "MusicLibrary.paused" m getPaused :: ActionFixture -> IO Double getPaused af = getGlobalVar af "MusicLibrary.paused" setStatus :: ActionFixture -> String -> IO () setStatus af m = setGlobalVar af "MusicLibrary.status" m getStatus :: ActionFixture -> IO String getStatus af = getGlobalVar af "MusicLibrary.status" setSimulator:: ActionFixture -> Simulator -> IO () setSimulator af s = setGlobalVar af "Simulator.system" s getSimulator:: ActionFixture -> IO Simulator getSimulator af = getGlobalVar af "Simulator.system" totalSongs af p = do music <- getMusicLibrary af check af p (length music) library af p = do let fileName = bodyToText p text <- readFile fileName -- use tail to ignore first line (headings) let music = map parseMusic (tail (lines text)) setMusicLibrary af music --writeIORef (actionFixtureState af) (Just (toDyn (head music, music))) return p select af p = do lib <- getMusicLibrary af let item :: Int; item = read (bodyToText p) setLooking af (Just (lib !! (item - 1))) return p checkMusicProperty af p prop = do Just current <- getLooking af check af p (prop current) checkMusicTextProperty af p prop = do Just current <- getLooking af checkText af p (prop current) (bodyToText p) title af p = checkMusicTextProperty af p musicTitle artist af p = checkMusicTextProperty af p musicArtist album af p = checkMusicTextProperty af p musicAlbum year af p = checkMusicProperty af p musicYear time af p = checkMusicProperty af p musicTime track af p = checkMusicTextProperty af p musicTrack play af p = do playing <- getPlaying af Just looking <- getLooking af library <- getMusicLibrary af sim <- getSimulator af --paused <- getPaused af let paused = 0 let (status, newpaused, newsim) = Player.play paused playing sim looking putStrLn $ "Set status " ++ status setStatus af status setPaused af newpaused setSimulator af newsim status af p = do status <- getStatus af checkText af p status (bodyToText p) pause af p = do putStrLn "Browser.pause: return parse" return p