-- | Module    : Hum.Rebuild
-- Copyright   : (c) Itai Y. Efrat 2020-2021
-- License     : GPLv2-or-later (see LICENSE)
-- Maintainer  : Itai Y. Efrat <itai3397@gmail.com>
--
-- Functions that rebuild the state after changes that require it,
-- Or that have to be here to avoid dependency loops.

module Hum.Rebuild where

import           Hum.Types
import           Control.Lens
import           Data.Foldable
import           Brick.Widgets.List
import           Network.MPD                    ( withMPD )
import qualified Network.MPD                   as MPD
import qualified Data.Vector                   as V

-- | All songs of a given artist
songsOfArtist :: MPD.Value -> IO (V.Vector MPD.Song)
songsOfArtist :: Value -> IO (Vector Song)
songsOfArtist Value
artist' =
  ([Song] -> Vector Song
forall a. [a] -> Vector a
V.fromList ([Song] -> Vector Song)
-> (Either MPDError [Song] -> [Song])
-> Either MPDError [Song]
-> Vector Song
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Song] -> Either MPDError [Song] -> [Song]
forall b a. b -> Either a b -> b
fromRight [] (Either MPDError [Song] -> Vector Song)
-> IO (Either MPDError [Song]) -> IO (Vector Song)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>)
  (IO (Either MPDError [Song]) -> IO (Vector Song))
-> (Query -> IO (Either MPDError [Song]))
-> Query
-> IO (Vector Song)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MPD [Song] -> IO (Either MPDError [Song])
forall a. MPD a -> IO (Response a)
withMPD
  (MPD [Song] -> IO (Either MPDError [Song]))
-> (Query -> MPD [Song]) -> Query -> IO (Either MPDError [Song])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Query -> MPD [Song]
forall (m :: * -> *). MonadMPD m => Query -> m [Song]
MPD.find (Query -> IO (Vector Song)) -> Query -> IO (Vector Song)
forall a b. (a -> b) -> a -> b
$ (Metadata
MPD.AlbumArtist Metadata -> Value -> Query
MPD.=? Value
artist')

-- | All songs in a given album
songsOfAlbum ::MPD.Value -> IO (V.Vector MPD.Song)
songsOfAlbum :: Value -> IO (Vector Song)
songsOfAlbum Value
album' =
   ([Song] -> Vector Song
forall a. [a] -> Vector a
V.fromList ([Song] -> Vector Song)
-> (Either MPDError [Song] -> [Song])
-> Either MPDError [Song]
-> Vector Song
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Song] -> Either MPDError [Song] -> [Song]
forall b a. b -> Either a b -> b
fromRight [] (Either MPDError [Song] -> Vector Song)
-> IO (Either MPDError [Song]) -> IO (Vector Song)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>)
  (IO (Either MPDError [Song]) -> IO (Vector Song))
-> (Query -> IO (Either MPDError [Song]))
-> Query
-> IO (Vector Song)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MPD [Song] -> IO (Either MPDError [Song])
forall a. MPD a -> IO (Response a)
withMPD
  (MPD [Song] -> IO (Either MPDError [Song]))
-> (Query -> MPD [Song]) -> Query -> IO (Either MPDError [Song])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Query -> MPD [Song]
forall (m :: * -> *). MonadMPD m => Query -> m [Song]
MPD.find (Query -> IO (Vector Song)) -> Query -> IO (Vector Song)
forall a b. (a -> b) -> a -> b
$ (Metadata
MPD.Album Metadata -> Value -> Query
MPD.=? Value
album')

-- | All albums of a given artist
albumsOfArtist :: MPD.Value -> IO (V.Vector MPD.Value)
albumsOfArtist :: Value -> IO (Vector Value)
albumsOfArtist Value
artist' =
  [Value] -> Vector Value
forall a. [a] -> Vector a
V.fromList ([Value] -> Vector Value)
-> (Either MPDError [Value] -> [Value])
-> Either MPDError [Value]
-> Vector Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Value] -> Either MPDError [Value] -> [Value]
forall b a. b -> Either a b -> b
fromRight [] (Either MPDError [Value] -> Vector Value)
-> IO (Either MPDError [Value]) -> IO (Vector Value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MPD [Value] -> IO (Either MPDError [Value])
forall a. MPD a -> IO (Response a)
withMPD (Metadata -> Query -> MPD [Value]
forall (m :: * -> *). MonadMPD m => Metadata -> Query -> m [Value]
MPD.list Metadata
MPD.Album (Metadata
MPD.AlbumArtist Metadata -> Value -> Query
MPD.=? Value
artist'))

-- | All year-album pairs of a given artist
yalbumsOfArtist ::  Bool -> MPD.Value -> IO (V.Vector (MPD.Value,MPD.Value))
yalbumsOfArtist :: Bool -> Value -> IO (Vector (Value, Value))
yalbumsOfArtist Bool
bl Value
artist' = let srt :: (a, a) -> a
srt = (if Bool
bl then (a, a) -> a
forall a b. (a, b) -> a
fst else (a, a) -> a
forall a b. (a, b) -> b
snd)
  in do
  [Value]
albums' <- [Value] -> Either MPDError [Value] -> [Value]
forall b a. b -> Either a b -> b
fromRight [] (Either MPDError [Value] -> [Value])
-> IO (Either MPDError [Value]) -> IO [Value]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MPD [Value] -> IO (Either MPDError [Value])
forall a. MPD a -> IO (Response a)
withMPD (Metadata -> Query -> MPD [Value]
forall (m :: * -> *). MonadMPD m => Metadata -> Query -> m [Value]
MPD.list Metadata
MPD.Album (Metadata
MPD.AlbumArtist Metadata -> Value -> Query
MPD.=? Value
artist'))
  [(Value, Value)]
yalbums' <- IO [(Value, Value)] -> IO [(Value, Value)]
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO [(Value, Value)] -> IO [(Value, Value)])
-> ([IO (Value, Value)] -> IO [(Value, Value)])
-> [IO (Value, Value)]
-> IO [(Value, Value)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [IO (Value, Value)] -> IO [(Value, Value)]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence ([IO (Value, Value)] -> IO [(Value, Value)])
-> [IO (Value, Value)] -> IO [(Value, Value)]
forall a b. (a -> b) -> a -> b
$ (\Value
x -> (,Value
x) (Value -> (Value, Value)) -> IO Value -> IO (Value, Value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> IO Value
yearOfAlbum Value
x) (Value -> IO (Value, Value)) -> [Value] -> [IO (Value, Value)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Value]
albums'
  Vector (Value, Value) -> IO (Vector (Value, Value))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Vector (Value, Value) -> IO (Vector (Value, Value)))
-> Vector (Value, Value) -> IO (Vector (Value, Value))
forall a b. (a -> b) -> a -> b
$ [(Value, Value)] -> Vector (Value, Value)
forall a. [a] -> Vector a
V.fromList (((Value, Value) -> (Value, Value) -> Ordering)
-> [(Value, Value)] -> [(Value, Value)]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (\(Value, Value)
x (Value, Value)
y -> Value -> Value -> Ordering
forall a. Ord a => a -> a -> Ordering
compare ((Value, Value) -> Value
forall a. (a, a) -> a
srt (Value, Value)
x) ((Value, Value) -> Value
forall a. (a, a) -> a
srt (Value, Value)
y)) [(Value, Value)]
yalbums')

-- | Earliest year of any song in the given album
yearOfAlbum :: MPD.Value -> IO MPD.Value
yearOfAlbum :: Value -> IO Value
yearOfAlbum Value
album' = Value -> Either MPDError Value -> Value
forall b a. b -> Either a b -> b
fromRight Value
"????" (Either MPDError Value -> Value)
-> IO (Either MPDError Value) -> IO Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([Value] -> Value
minYear ([Value] -> Value)
-> IO (Either MPDError [Value]) -> IO (Either MPDError Value)
forall (f :: * -> *) (g :: * -> *) a b.
(Functor f, Functor g) =>
(a -> b) -> f (g a) -> f (g b)
<<$>> MPD [Value] -> IO (Either MPDError [Value])
forall a. MPD a -> IO (Response a)
withMPD (Metadata -> Query -> MPD [Value]
forall (m :: * -> *). MonadMPD m => Metadata -> Query -> m [Value]
MPD.list Metadata
MPD.Date (Metadata
MPD.Album Metadata -> Value -> Query
MPD.=? Value
album')))
  where minYear :: [MPD.Value] -> MPD.Value
        minYear :: [Value] -> Value
minYear [] = Value
"????"
        minYear [Value]
vals = [Value] -> Value
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum [Value]
vals

-- | Rebuild entire library state, keeping the index of the left column if possible.
rebuildLib :: MonadIO m => HumState -> m HumState
rebuildLib :: HumState -> m HumState
rebuildLib HumState
s = do
    let mi :: Maybe Int
mi = HumState
s HumState
-> Getting (List Name Value) HumState (List Name Value)
-> List Name Value
forall s a. s -> Getting a s a -> a
^. (LibraryState -> Const (List Name Value) LibraryState)
-> HumState -> Const (List Name Value) HumState
Lens' HumState LibraryState
libraryL ((LibraryState -> Const (List Name Value) LibraryState)
 -> HumState -> Const (List Name Value) HumState)
-> ((List Name Value -> Const (List Name Value) (List Name Value))
    -> LibraryState -> Const (List Name Value) LibraryState)
-> Getting (List Name Value) HumState (List Name Value)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (List Name Value -> Const (List Name Value) (List Name Value))
-> LibraryState -> Const (List Name Value) LibraryState
Lens' LibraryState (List Name Value)
artistsL List Name Value -> (List Name Value -> Maybe Int) -> Maybe Int
forall a b. a -> (a -> b) -> b
& List Name Value -> Maybe Int
forall n (t :: * -> *) e. GenericList n t e -> Maybe Int
listSelected
    Vector Value
artistsVec <- IO (Vector Value) -> m (Vector Value)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO ([Value] -> Vector Value
forall a. [a] -> Vector a
V.fromList ([Value] -> Vector Value)
-> (Either MPDError [Value] -> [Value])
-> Either MPDError [Value]
-> Vector Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Value] -> Either MPDError [Value] -> [Value]
forall b a. b -> Either a b -> b
fromRight [] (Either MPDError [Value] -> Vector Value)
-> IO (Either MPDError [Value]) -> IO (Vector Value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MPD [Value] -> IO (Either MPDError [Value])
forall a. MPD a -> IO (Response a)
withMPD
      (Metadata -> Query -> MPD [Value]
forall (m :: * -> *). MonadMPD m => Metadata -> Query -> m [Value]
MPD.list Metadata
MPD.AlbumArtist Query
forall a. Monoid a => a
mempty))
    let artists' :: List Name Value
artists' = (List Name Value -> List Name Value)
-> (Int -> List Name Value -> List Name Value)
-> Maybe Int
-> List Name Value
-> List Name Value
forall b a. b -> (a -> b) -> Maybe a -> b
maybe List Name Value -> List Name Value
forall a. a -> a
id Int -> List Name Value -> List Name Value
forall (t :: * -> *) n e.
(Foldable t, Splittable t) =>
Int -> GenericList n t e -> GenericList n t e
listMoveTo Maybe Int
mi (List Name Value -> List Name Value)
-> List Name Value -> List Name Value
forall a b. (a -> b) -> a -> b
$ Name -> Vector Value -> Int -> List Name Value
forall (t :: * -> *) n e.
Foldable t =>
n -> t e -> Int -> GenericList n t e
list Name
ArtistsList Vector Value
artistsVec Int
1
    Vector Value
albumsVec   <- IO (Vector Value) -> m (Vector Value)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Vector Value) -> m (Vector Value))
-> IO (Vector Value) -> m (Vector Value)
forall a b. (a -> b) -> a -> b
$ IO (Vector Value)
-> (Value -> IO (Vector Value)) -> Maybe Value -> IO (Vector Value)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Vector Value -> IO (Vector Value)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Vector Value
forall (f :: * -> *) a. Alternative f => f a
empty) Value -> IO (Vector Value)
albumsOfArtist ((Int, Value) -> Value
forall a b. (a, b) -> b
snd ((Int, Value) -> Value) -> Maybe (Int, Value) -> Maybe Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> List Name Value -> Maybe (Int, Value)
forall (t :: * -> *) n e.
(Splittable t, Foldable t) =>
GenericList n t e -> Maybe (Int, e)
listSelectedElement List Name Value
artists')
    let albums' :: List Name Value
albums'  = Name -> Vector Value -> Int -> List Name Value
forall (t :: * -> *) n e.
Foldable t =>
n -> t e -> Int -> GenericList n t e
list Name
AlbumsList Vector Value
albumsVec Int
1
    Vector (Value, Value)
yalbumsVec   <- IO (Vector (Value, Value)) -> m (Vector (Value, Value))
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Vector (Value, Value)) -> m (Vector (Value, Value)))
-> IO (Vector (Value, Value)) -> m (Vector (Value, Value))
forall a b. (a -> b) -> a -> b
$ IO (Vector (Value, Value))
-> (Value -> IO (Vector (Value, Value)))
-> Maybe Value
-> IO (Vector (Value, Value))
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Vector (Value, Value) -> IO (Vector (Value, Value))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Vector (Value, Value)
forall (f :: * -> *) a. Alternative f => f a
empty) (Bool -> Value -> IO (Vector (Value, Value))
yalbumsOfArtist (HumState
s HumState -> Getting Bool HumState Bool -> Bool
forall s a. s -> Getting a s a -> a
^. (LibraryState -> Const Bool LibraryState)
-> HumState -> Const Bool HumState
Lens' HumState LibraryState
libraryL ((LibraryState -> Const Bool LibraryState)
 -> HumState -> Const Bool HumState)
-> ((Bool -> Const Bool Bool)
    -> LibraryState -> Const Bool LibraryState)
-> Getting Bool HumState Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Bool -> Const Bool Bool)
-> LibraryState -> Const Bool LibraryState
Lens' LibraryState Bool
yalbumSortL)) ((Int, Value) -> Value
forall a b. (a, b) -> b
snd ((Int, Value) -> Value) -> Maybe (Int, Value) -> Maybe Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> List Name Value -> Maybe (Int, Value)
forall (t :: * -> *) n e.
(Splittable t, Foldable t) =>
GenericList n t e -> Maybe (Int, e)
listSelectedElement List Name Value
artists')
    let yalbums' :: GenericList Name Vector (Value, Value)
yalbums'    = Name
-> Vector (Value, Value)
-> Int
-> GenericList Name Vector (Value, Value)
forall (t :: * -> *) n e.
Foldable t =>
n -> t e -> Int -> GenericList n t e
list Name
YalbumsList Vector (Value, Value)
yalbumsVec Int
1
    Vector Song
songsVec     <- IO (Vector Song) -> m (Vector Song)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Vector Song) -> m (Vector Song))
-> IO (Vector Song) -> m (Vector Song)
forall a b. (a -> b) -> a -> b
$ IO (Vector Song)
-> (Value -> IO (Vector Song)) -> Maybe Value -> IO (Vector Song)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Vector Song -> IO (Vector Song)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Vector Song
forall (f :: * -> *) a. Alternative f => f a
empty) Value -> IO (Vector Song)
songsOfAlbum ((Int, Value) -> Value
forall a b. (a, b) -> b
snd ((Int, Value) -> Value) -> Maybe (Int, Value) -> Maybe Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> List Name Value -> Maybe (Int, Value)
forall (t :: * -> *) n e.
(Splittable t, Foldable t) =>
GenericList n t e -> Maybe (Int, e)
listSelectedElement List Name Value
albums')
    let songs' :: GenericList Name Vector Song
songs'   = Name -> Vector Song -> Int -> GenericList Name Vector Song
forall (t :: * -> *) n e.
Foldable t =>
n -> t e -> Int -> GenericList n t e
list Name
SongsList Vector Song
songsVec Int
1
    HumState -> m HumState
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HumState -> m HumState) -> HumState -> m HumState
forall a b. (a -> b) -> a -> b
$ HumState
s HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
&  (LibraryState -> Identity LibraryState)
-> HumState -> Identity HumState
Lens' HumState LibraryState
libraryL ((LibraryState -> Identity LibraryState)
 -> HumState -> Identity HumState)
-> ((List Name Value -> Identity (List Name Value))
    -> LibraryState -> Identity LibraryState)
-> (List Name Value -> Identity (List Name Value))
-> HumState
-> Identity HumState
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (List Name Value -> Identity (List Name Value))
-> LibraryState -> Identity LibraryState
Lens' LibraryState (List Name Value)
artistsL ((List Name Value -> Identity (List Name Value))
 -> HumState -> Identity HumState)
-> List Name Value -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ List Name Value
artists'
             HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
&  (LibraryState -> Identity LibraryState)
-> HumState -> Identity HumState
Lens' HumState LibraryState
libraryL ((LibraryState -> Identity LibraryState)
 -> HumState -> Identity HumState)
-> ((GenericList Name Vector (Value, Value)
     -> Identity (GenericList Name Vector (Value, Value)))
    -> LibraryState -> Identity LibraryState)
-> (GenericList Name Vector (Value, Value)
    -> Identity (GenericList Name Vector (Value, Value)))
-> HumState
-> Identity HumState
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GenericList Name Vector (Value, Value)
 -> Identity (GenericList Name Vector (Value, Value)))
-> LibraryState -> Identity LibraryState
Lens' LibraryState (GenericList Name Vector (Value, Value))
yalbumsL ((GenericList Name Vector (Value, Value)
  -> Identity (GenericList Name Vector (Value, Value)))
 -> HumState -> Identity HumState)
-> GenericList Name Vector (Value, Value) -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ GenericList Name Vector (Value, Value)
yalbums'
             HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
&  (LibraryState -> Identity LibraryState)
-> HumState -> Identity HumState
Lens' HumState LibraryState
libraryL ((LibraryState -> Identity LibraryState)
 -> HumState -> Identity HumState)
-> ((GenericList Name Vector Song
     -> Identity (GenericList Name Vector Song))
    -> LibraryState -> Identity LibraryState)
-> (GenericList Name Vector Song
    -> Identity (GenericList Name Vector Song))
-> HumState
-> Identity HumState
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GenericList Name Vector Song
 -> Identity (GenericList Name Vector Song))
-> LibraryState -> Identity LibraryState
Lens' LibraryState (GenericList Name Vector Song)
songsL ((GenericList Name Vector Song
  -> Identity (GenericList Name Vector Song))
 -> HumState -> Identity HumState)
-> GenericList Name Vector Song -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ GenericList Name Vector Song
songs'

-- | Rebuild library state from selected artist.
rebuildLibArtists :: MonadIO m => HumState -> m HumState
rebuildLibArtists :: HumState -> m HumState
rebuildLibArtists HumState
s = do
    let artists' :: List Name Value
artists' = HumState
s HumState
-> Getting (List Name Value) HumState (List Name Value)
-> List Name Value
forall s a. s -> Getting a s a -> a
^. (LibraryState -> Const (List Name Value) LibraryState)
-> HumState -> Const (List Name Value) HumState
Lens' HumState LibraryState
libraryL ((LibraryState -> Const (List Name Value) LibraryState)
 -> HumState -> Const (List Name Value) HumState)
-> ((List Name Value -> Const (List Name Value) (List Name Value))
    -> LibraryState -> Const (List Name Value) LibraryState)
-> Getting (List Name Value) HumState (List Name Value)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (List Name Value -> Const (List Name Value) (List Name Value))
-> LibraryState -> Const (List Name Value) LibraryState
Lens' LibraryState (List Name Value)
artistsL
    Vector (Value, Value)
yalbumsVec   <- IO (Vector (Value, Value)) -> m (Vector (Value, Value))
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Vector (Value, Value)) -> m (Vector (Value, Value)))
-> IO (Vector (Value, Value)) -> m (Vector (Value, Value))
forall a b. (a -> b) -> a -> b
$ IO (Vector (Value, Value))
-> (Value -> IO (Vector (Value, Value)))
-> Maybe Value
-> IO (Vector (Value, Value))
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Vector (Value, Value) -> IO (Vector (Value, Value))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Vector (Value, Value)
forall (f :: * -> *) a. Alternative f => f a
empty) (Bool -> Value -> IO (Vector (Value, Value))
yalbumsOfArtist (HumState
s HumState -> Getting Bool HumState Bool -> Bool
forall s a. s -> Getting a s a -> a
^. (LibraryState -> Const Bool LibraryState)
-> HumState -> Const Bool HumState
Lens' HumState LibraryState
libraryL ((LibraryState -> Const Bool LibraryState)
 -> HumState -> Const Bool HumState)
-> ((Bool -> Const Bool Bool)
    -> LibraryState -> Const Bool LibraryState)
-> Getting Bool HumState Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Bool -> Const Bool Bool)
-> LibraryState -> Const Bool LibraryState
Lens' LibraryState Bool
yalbumSortL)) ((Int, Value) -> Value
forall a b. (a, b) -> b
snd ((Int, Value) -> Value) -> Maybe (Int, Value) -> Maybe Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> List Name Value -> Maybe (Int, Value)
forall (t :: * -> *) n e.
(Splittable t, Foldable t) =>
GenericList n t e -> Maybe (Int, e)
listSelectedElement List Name Value
artists')
    let yalbums' :: GenericList Name Vector (Value, Value)
yalbums'    = Name
-> Vector (Value, Value)
-> Int
-> GenericList Name Vector (Value, Value)
forall (t :: * -> *) n e.
Foldable t =>
n -> t e -> Int -> GenericList n t e
list Name
YalbumsList Vector (Value, Value)
yalbumsVec Int
1
    Vector Song
songsVec    <- IO (Vector Song) -> m (Vector Song)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Vector Song) -> m (Vector Song))
-> IO (Vector Song) -> m (Vector Song)
forall a b. (a -> b) -> a -> b
$ IO (Vector Song)
-> (Value -> IO (Vector Song)) -> Maybe Value -> IO (Vector Song)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Vector Song -> IO (Vector Song)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Vector Song
forall (f :: * -> *) a. Alternative f => f a
empty) Value -> IO (Vector Song)
songsOfAlbum ((Value, Value) -> Value
forall a b. (a, b) -> b
snd ((Value, Value) -> Value)
-> ((Int, (Value, Value)) -> (Value, Value))
-> (Int, (Value, Value))
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int, (Value, Value)) -> (Value, Value)
forall a b. (a, b) -> b
snd ((Int, (Value, Value)) -> Value)
-> Maybe (Int, (Value, Value)) -> Maybe Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GenericList Name Vector (Value, Value)
-> Maybe (Int, (Value, Value))
forall (t :: * -> *) n e.
(Splittable t, Foldable t) =>
GenericList n t e -> Maybe (Int, e)
listSelectedElement GenericList Name Vector (Value, Value)
yalbums')
    let songs' :: GenericList Name Vector Song
songs'   = Name -> Vector Song -> Int -> GenericList Name Vector Song
forall (t :: * -> *) n e.
Foldable t =>
n -> t e -> Int -> GenericList n t e
list Name
SongsList Vector Song
songsVec Int
1
    HumState -> m HumState
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HumState -> m HumState) -> HumState -> m HumState
forall a b. (a -> b) -> a -> b
$ HumState
s HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
&  (LibraryState -> Identity LibraryState)
-> HumState -> Identity HumState
Lens' HumState LibraryState
libraryL ((LibraryState -> Identity LibraryState)
 -> HumState -> Identity HumState)
-> ((GenericList Name Vector (Value, Value)
     -> Identity (GenericList Name Vector (Value, Value)))
    -> LibraryState -> Identity LibraryState)
-> (GenericList Name Vector (Value, Value)
    -> Identity (GenericList Name Vector (Value, Value)))
-> HumState
-> Identity HumState
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GenericList Name Vector (Value, Value)
 -> Identity (GenericList Name Vector (Value, Value)))
-> LibraryState -> Identity LibraryState
Lens' LibraryState (GenericList Name Vector (Value, Value))
yalbumsL ((GenericList Name Vector (Value, Value)
  -> Identity (GenericList Name Vector (Value, Value)))
 -> HumState -> Identity HumState)
-> GenericList Name Vector (Value, Value) -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ GenericList Name Vector (Value, Value)
yalbums'
             HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
&  (LibraryState -> Identity LibraryState)
-> HumState -> Identity HumState
Lens' HumState LibraryState
libraryL ((LibraryState -> Identity LibraryState)
 -> HumState -> Identity HumState)
-> ((GenericList Name Vector Song
     -> Identity (GenericList Name Vector Song))
    -> LibraryState -> Identity LibraryState)
-> (GenericList Name Vector Song
    -> Identity (GenericList Name Vector Song))
-> HumState
-> Identity HumState
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GenericList Name Vector Song
 -> Identity (GenericList Name Vector Song))
-> LibraryState -> Identity LibraryState
Lens' LibraryState (GenericList Name Vector Song)
songsL ((GenericList Name Vector Song
  -> Identity (GenericList Name Vector Song))
 -> HumState -> Identity HumState)
-> GenericList Name Vector Song -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ GenericList Name Vector Song
songs'

-- | Rebuild library state from selected artist.
reloadLibArtists :: MonadIO m => HumState -> m HumState
reloadLibArtists :: HumState -> m HumState
reloadLibArtists HumState
s = do
    let mi :: Maybe Int
mi = HumState
s HumState
-> Getting
     (GenericList Name Vector (Value, Value))
     HumState
     (GenericList Name Vector (Value, Value))
-> GenericList Name Vector (Value, Value)
forall s a. s -> Getting a s a -> a
^. (LibraryState
 -> Const (GenericList Name Vector (Value, Value)) LibraryState)
-> HumState
-> Const (GenericList Name Vector (Value, Value)) HumState
Lens' HumState LibraryState
libraryL ((LibraryState
  -> Const (GenericList Name Vector (Value, Value)) LibraryState)
 -> HumState
 -> Const (GenericList Name Vector (Value, Value)) HumState)
-> ((GenericList Name Vector (Value, Value)
     -> Const
          (GenericList Name Vector (Value, Value))
          (GenericList Name Vector (Value, Value)))
    -> LibraryState
    -> Const (GenericList Name Vector (Value, Value)) LibraryState)
-> Getting
     (GenericList Name Vector (Value, Value))
     HumState
     (GenericList Name Vector (Value, Value))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GenericList Name Vector (Value, Value)
 -> Const
      (GenericList Name Vector (Value, Value))
      (GenericList Name Vector (Value, Value)))
-> LibraryState
-> Const (GenericList Name Vector (Value, Value)) LibraryState
Lens' LibraryState (GenericList Name Vector (Value, Value))
yalbumsL GenericList Name Vector (Value, Value)
-> (GenericList Name Vector (Value, Value) -> Maybe Int)
-> Maybe Int
forall a b. a -> (a -> b) -> b
& GenericList Name Vector (Value, Value) -> Maybe Int
forall n (t :: * -> *) e. GenericList n t e -> Maybe Int
listSelected
    let mj :: Maybe Int
mj = HumState
s HumState
-> Getting
     (GenericList Name Vector Song)
     HumState
     (GenericList Name Vector Song)
-> GenericList Name Vector Song
forall s a. s -> Getting a s a -> a
^. (LibraryState -> Const (GenericList Name Vector Song) LibraryState)
-> HumState -> Const (GenericList Name Vector Song) HumState
Lens' HumState LibraryState
libraryL ((LibraryState
  -> Const (GenericList Name Vector Song) LibraryState)
 -> HumState -> Const (GenericList Name Vector Song) HumState)
-> ((GenericList Name Vector Song
     -> Const
          (GenericList Name Vector Song) (GenericList Name Vector Song))
    -> LibraryState
    -> Const (GenericList Name Vector Song) LibraryState)
-> Getting
     (GenericList Name Vector Song)
     HumState
     (GenericList Name Vector Song)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GenericList Name Vector Song
 -> Const
      (GenericList Name Vector Song) (GenericList Name Vector Song))
-> LibraryState
-> Const (GenericList Name Vector Song) LibraryState
Lens' LibraryState (GenericList Name Vector Song)
songsL GenericList Name Vector Song
-> (GenericList Name Vector Song -> Maybe Int) -> Maybe Int
forall a b. a -> (a -> b) -> b
& GenericList Name Vector Song -> Maybe Int
forall n (t :: * -> *) e. GenericList n t e -> Maybe Int
listSelected
    let artists' :: List Name Value
artists' = HumState
s HumState
-> Getting (List Name Value) HumState (List Name Value)
-> List Name Value
forall s a. s -> Getting a s a -> a
^. (LibraryState -> Const (List Name Value) LibraryState)
-> HumState -> Const (List Name Value) HumState
Lens' HumState LibraryState
libraryL ((LibraryState -> Const (List Name Value) LibraryState)
 -> HumState -> Const (List Name Value) HumState)
-> ((List Name Value -> Const (List Name Value) (List Name Value))
    -> LibraryState -> Const (List Name Value) LibraryState)
-> Getting (List Name Value) HumState (List Name Value)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (List Name Value -> Const (List Name Value) (List Name Value))
-> LibraryState -> Const (List Name Value) LibraryState
Lens' LibraryState (List Name Value)
artistsL
    Vector (Value, Value)
yalbumsVec   <- IO (Vector (Value, Value)) -> m (Vector (Value, Value))
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Vector (Value, Value)) -> m (Vector (Value, Value)))
-> IO (Vector (Value, Value)) -> m (Vector (Value, Value))
forall a b. (a -> b) -> a -> b
$ IO (Vector (Value, Value))
-> (Value -> IO (Vector (Value, Value)))
-> Maybe Value
-> IO (Vector (Value, Value))
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Vector (Value, Value) -> IO (Vector (Value, Value))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Vector (Value, Value)
forall (f :: * -> *) a. Alternative f => f a
empty) (Bool -> Value -> IO (Vector (Value, Value))
yalbumsOfArtist (HumState
s HumState -> Getting Bool HumState Bool -> Bool
forall s a. s -> Getting a s a -> a
^. (LibraryState -> Const Bool LibraryState)
-> HumState -> Const Bool HumState
Lens' HumState LibraryState
libraryL ((LibraryState -> Const Bool LibraryState)
 -> HumState -> Const Bool HumState)
-> ((Bool -> Const Bool Bool)
    -> LibraryState -> Const Bool LibraryState)
-> Getting Bool HumState Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Bool -> Const Bool Bool)
-> LibraryState -> Const Bool LibraryState
Lens' LibraryState Bool
yalbumSortL)) ((Int, Value) -> Value
forall a b. (a, b) -> b
snd ((Int, Value) -> Value) -> Maybe (Int, Value) -> Maybe Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> List Name Value -> Maybe (Int, Value)
forall (t :: * -> *) n e.
(Splittable t, Foldable t) =>
GenericList n t e -> Maybe (Int, e)
listSelectedElement List Name Value
artists')
    let yalbums' :: GenericList Name Vector (Value, Value)
yalbums'    = (GenericList Name Vector (Value, Value)
 -> GenericList Name Vector (Value, Value))
-> (Int
    -> GenericList Name Vector (Value, Value)
    -> GenericList Name Vector (Value, Value))
-> Maybe Int
-> GenericList Name Vector (Value, Value)
-> GenericList Name Vector (Value, Value)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe GenericList Name Vector (Value, Value)
-> GenericList Name Vector (Value, Value)
forall a. a -> a
id Int
-> GenericList Name Vector (Value, Value)
-> GenericList Name Vector (Value, Value)
forall (t :: * -> *) n e.
(Foldable t, Splittable t) =>
Int -> GenericList n t e -> GenericList n t e
listMoveTo Maybe Int
mi (GenericList Name Vector (Value, Value)
 -> GenericList Name Vector (Value, Value))
-> GenericList Name Vector (Value, Value)
-> GenericList Name Vector (Value, Value)
forall a b. (a -> b) -> a -> b
$ Name
-> Vector (Value, Value)
-> Int
-> GenericList Name Vector (Value, Value)
forall (t :: * -> *) n e.
Foldable t =>
n -> t e -> Int -> GenericList n t e
list Name
YalbumsList Vector (Value, Value)
yalbumsVec Int
1
    Vector Song
songsVec    <- IO (Vector Song) -> m (Vector Song)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Vector Song) -> m (Vector Song))
-> IO (Vector Song) -> m (Vector Song)
forall a b. (a -> b) -> a -> b
$ IO (Vector Song)
-> (Value -> IO (Vector Song)) -> Maybe Value -> IO (Vector Song)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Vector Song -> IO (Vector Song)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Vector Song
forall (f :: * -> *) a. Alternative f => f a
empty) Value -> IO (Vector Song)
songsOfAlbum ((Value, Value) -> Value
forall a b. (a, b) -> b
snd ((Value, Value) -> Value)
-> ((Int, (Value, Value)) -> (Value, Value))
-> (Int, (Value, Value))
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int, (Value, Value)) -> (Value, Value)
forall a b. (a, b) -> b
snd ((Int, (Value, Value)) -> Value)
-> Maybe (Int, (Value, Value)) -> Maybe Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GenericList Name Vector (Value, Value)
-> Maybe (Int, (Value, Value))
forall (t :: * -> *) n e.
(Splittable t, Foldable t) =>
GenericList n t e -> Maybe (Int, e)
listSelectedElement GenericList Name Vector (Value, Value)
yalbums')
    let songs' :: GenericList Name Vector Song
songs'   = (GenericList Name Vector Song -> GenericList Name Vector Song)
-> (Int
    -> GenericList Name Vector Song -> GenericList Name Vector Song)
-> Maybe Int
-> GenericList Name Vector Song
-> GenericList Name Vector Song
forall b a. b -> (a -> b) -> Maybe a -> b
maybe GenericList Name Vector Song -> GenericList Name Vector Song
forall a. a -> a
id Int -> GenericList Name Vector Song -> GenericList Name Vector Song
forall (t :: * -> *) n e.
(Foldable t, Splittable t) =>
Int -> GenericList n t e -> GenericList n t e
listMoveTo Maybe Int
mj (GenericList Name Vector Song -> GenericList Name Vector Song)
-> GenericList Name Vector Song -> GenericList Name Vector Song
forall a b. (a -> b) -> a -> b
$ Name -> Vector Song -> Int -> GenericList Name Vector Song
forall (t :: * -> *) n e.
Foldable t =>
n -> t e -> Int -> GenericList n t e
list Name
SongsList Vector Song
songsVec Int
1
    HumState -> m HumState
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HumState -> m HumState) -> HumState -> m HumState
forall a b. (a -> b) -> a -> b
$ HumState
s HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
&  (LibraryState -> Identity LibraryState)
-> HumState -> Identity HumState
Lens' HumState LibraryState
libraryL ((LibraryState -> Identity LibraryState)
 -> HumState -> Identity HumState)
-> ((GenericList Name Vector (Value, Value)
     -> Identity (GenericList Name Vector (Value, Value)))
    -> LibraryState -> Identity LibraryState)
-> (GenericList Name Vector (Value, Value)
    -> Identity (GenericList Name Vector (Value, Value)))
-> HumState
-> Identity HumState
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GenericList Name Vector (Value, Value)
 -> Identity (GenericList Name Vector (Value, Value)))
-> LibraryState -> Identity LibraryState
Lens' LibraryState (GenericList Name Vector (Value, Value))
yalbumsL ((GenericList Name Vector (Value, Value)
  -> Identity (GenericList Name Vector (Value, Value)))
 -> HumState -> Identity HumState)
-> GenericList Name Vector (Value, Value) -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ GenericList Name Vector (Value, Value)
yalbums'
             HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
&  (LibraryState -> Identity LibraryState)
-> HumState -> Identity HumState
Lens' HumState LibraryState
libraryL ((LibraryState -> Identity LibraryState)
 -> HumState -> Identity HumState)
-> ((GenericList Name Vector Song
     -> Identity (GenericList Name Vector Song))
    -> LibraryState -> Identity LibraryState)
-> (GenericList Name Vector Song
    -> Identity (GenericList Name Vector Song))
-> HumState
-> Identity HumState
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GenericList Name Vector Song
 -> Identity (GenericList Name Vector Song))
-> LibraryState -> Identity LibraryState
Lens' LibraryState (GenericList Name Vector Song)
songsL ((GenericList Name Vector Song
  -> Identity (GenericList Name Vector Song))
 -> HumState -> Identity HumState)
-> GenericList Name Vector Song -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ GenericList Name Vector Song
songs'


-- | Rebuild library state from selected album.
rebuildLibAlbums :: MonadIO m => HumState -> m HumState
rebuildLibAlbums :: HumState -> m HumState
rebuildLibAlbums HumState
s = do
    let yalbums' :: GenericList Name Vector (Value, Value)
yalbums' = HumState
s HumState
-> Getting
     (GenericList Name Vector (Value, Value))
     HumState
     (GenericList Name Vector (Value, Value))
-> GenericList Name Vector (Value, Value)
forall s a. s -> Getting a s a -> a
^. (LibraryState
 -> Const (GenericList Name Vector (Value, Value)) LibraryState)
-> HumState
-> Const (GenericList Name Vector (Value, Value)) HumState
Lens' HumState LibraryState
libraryL ((LibraryState
  -> Const (GenericList Name Vector (Value, Value)) LibraryState)
 -> HumState
 -> Const (GenericList Name Vector (Value, Value)) HumState)
-> ((GenericList Name Vector (Value, Value)
     -> Const
          (GenericList Name Vector (Value, Value))
          (GenericList Name Vector (Value, Value)))
    -> LibraryState
    -> Const (GenericList Name Vector (Value, Value)) LibraryState)
-> Getting
     (GenericList Name Vector (Value, Value))
     HumState
     (GenericList Name Vector (Value, Value))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GenericList Name Vector (Value, Value)
 -> Const
      (GenericList Name Vector (Value, Value))
      (GenericList Name Vector (Value, Value)))
-> LibraryState
-> Const (GenericList Name Vector (Value, Value)) LibraryState
Lens' LibraryState (GenericList Name Vector (Value, Value))
yalbumsL
    Vector Song
songsVec   <- IO (Vector Song) -> m (Vector Song)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Vector Song) -> m (Vector Song))
-> IO (Vector Song) -> m (Vector Song)
forall a b. (a -> b) -> a -> b
$ IO (Vector Song)
-> (Value -> IO (Vector Song)) -> Maybe Value -> IO (Vector Song)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Vector Song -> IO (Vector Song)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Vector Song
forall (f :: * -> *) a. Alternative f => f a
empty) Value -> IO (Vector Song)
songsOfAlbum ((Value, Value) -> Value
forall a b. (a, b) -> b
snd ((Value, Value) -> Value)
-> ((Int, (Value, Value)) -> (Value, Value))
-> (Int, (Value, Value))
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int, (Value, Value)) -> (Value, Value)
forall a b. (a, b) -> b
snd ((Int, (Value, Value)) -> Value)
-> Maybe (Int, (Value, Value)) -> Maybe Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GenericList Name Vector (Value, Value)
-> Maybe (Int, (Value, Value))
forall (t :: * -> *) n e.
(Splittable t, Foldable t) =>
GenericList n t e -> Maybe (Int, e)
listSelectedElement GenericList Name Vector (Value, Value)
yalbums')
    let songs' :: GenericList Name Vector Song
songs'  = Name -> Vector Song -> Int -> GenericList Name Vector Song
forall (t :: * -> *) n e.
Foldable t =>
n -> t e -> Int -> GenericList n t e
list Name
SongsList Vector Song
songsVec Int
1
    HumState -> m HumState
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HumState -> m HumState) -> HumState -> m HumState
forall a b. (a -> b) -> a -> b
$ HumState
s HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
& (LibraryState -> Identity LibraryState)
-> HumState -> Identity HumState
Lens' HumState LibraryState
libraryL ((LibraryState -> Identity LibraryState)
 -> HumState -> Identity HumState)
-> ((GenericList Name Vector Song
     -> Identity (GenericList Name Vector Song))
    -> LibraryState -> Identity LibraryState)
-> (GenericList Name Vector Song
    -> Identity (GenericList Name Vector Song))
-> HumState
-> Identity HumState
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GenericList Name Vector Song
 -> Identity (GenericList Name Vector Song))
-> LibraryState -> Identity LibraryState
Lens' LibraryState (GenericList Name Vector Song)
songsL ((GenericList Name Vector Song
  -> Identity (GenericList Name Vector Song))
 -> HumState -> Identity HumState)
-> GenericList Name Vector Song -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ GenericList Name Vector Song
songs'

-- | Rebuild library state from selected album, keeping the selected song index if possible.
reloadLibAlbums :: MonadIO m => HumState -> m HumState
reloadLibAlbums :: HumState -> m HumState
reloadLibAlbums HumState
s = do
    let mi :: Maybe Int
mi = HumState
s HumState
-> Getting
     (GenericList Name Vector Song)
     HumState
     (GenericList Name Vector Song)
-> GenericList Name Vector Song
forall s a. s -> Getting a s a -> a
^. (LibraryState -> Const (GenericList Name Vector Song) LibraryState)
-> HumState -> Const (GenericList Name Vector Song) HumState
Lens' HumState LibraryState
libraryL ((LibraryState
  -> Const (GenericList Name Vector Song) LibraryState)
 -> HumState -> Const (GenericList Name Vector Song) HumState)
-> ((GenericList Name Vector Song
     -> Const
          (GenericList Name Vector Song) (GenericList Name Vector Song))
    -> LibraryState
    -> Const (GenericList Name Vector Song) LibraryState)
-> Getting
     (GenericList Name Vector Song)
     HumState
     (GenericList Name Vector Song)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GenericList Name Vector Song
 -> Const
      (GenericList Name Vector Song) (GenericList Name Vector Song))
-> LibraryState
-> Const (GenericList Name Vector Song) LibraryState
Lens' LibraryState (GenericList Name Vector Song)
songsL GenericList Name Vector Song
-> (GenericList Name Vector Song -> Maybe Int) -> Maybe Int
forall a b. a -> (a -> b) -> b
& GenericList Name Vector Song -> Maybe Int
forall n (t :: * -> *) e. GenericList n t e -> Maybe Int
listSelected
    let yalbums' :: GenericList Name Vector (Value, Value)
yalbums' = HumState
s HumState
-> Getting
     (GenericList Name Vector (Value, Value))
     HumState
     (GenericList Name Vector (Value, Value))
-> GenericList Name Vector (Value, Value)
forall s a. s -> Getting a s a -> a
^. (LibraryState
 -> Const (GenericList Name Vector (Value, Value)) LibraryState)
-> HumState
-> Const (GenericList Name Vector (Value, Value)) HumState
Lens' HumState LibraryState
libraryL ((LibraryState
  -> Const (GenericList Name Vector (Value, Value)) LibraryState)
 -> HumState
 -> Const (GenericList Name Vector (Value, Value)) HumState)
-> ((GenericList Name Vector (Value, Value)
     -> Const
          (GenericList Name Vector (Value, Value))
          (GenericList Name Vector (Value, Value)))
    -> LibraryState
    -> Const (GenericList Name Vector (Value, Value)) LibraryState)
-> Getting
     (GenericList Name Vector (Value, Value))
     HumState
     (GenericList Name Vector (Value, Value))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GenericList Name Vector (Value, Value)
 -> Const
      (GenericList Name Vector (Value, Value))
      (GenericList Name Vector (Value, Value)))
-> LibraryState
-> Const (GenericList Name Vector (Value, Value)) LibraryState
Lens' LibraryState (GenericList Name Vector (Value, Value))
yalbumsL
    Vector Song
songsVec   <- IO (Vector Song) -> m (Vector Song)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Vector Song) -> m (Vector Song))
-> IO (Vector Song) -> m (Vector Song)
forall a b. (a -> b) -> a -> b
$ IO (Vector Song)
-> (Value -> IO (Vector Song)) -> Maybe Value -> IO (Vector Song)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Vector Song -> IO (Vector Song)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Vector Song
forall (f :: * -> *) a. Alternative f => f a
empty) Value -> IO (Vector Song)
songsOfAlbum ((Value, Value) -> Value
forall a b. (a, b) -> b
snd ((Value, Value) -> Value)
-> ((Int, (Value, Value)) -> (Value, Value))
-> (Int, (Value, Value))
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int, (Value, Value)) -> (Value, Value)
forall a b. (a, b) -> b
snd ((Int, (Value, Value)) -> Value)
-> Maybe (Int, (Value, Value)) -> Maybe Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GenericList Name Vector (Value, Value)
-> Maybe (Int, (Value, Value))
forall (t :: * -> *) n e.
(Splittable t, Foldable t) =>
GenericList n t e -> Maybe (Int, e)
listSelectedElement GenericList Name Vector (Value, Value)
yalbums')
    let songs' :: GenericList Name Vector Song
songs'  = (GenericList Name Vector Song -> GenericList Name Vector Song)
-> (Int
    -> GenericList Name Vector Song -> GenericList Name Vector Song)
-> Maybe Int
-> GenericList Name Vector Song
-> GenericList Name Vector Song
forall b a. b -> (a -> b) -> Maybe a -> b
maybe GenericList Name Vector Song -> GenericList Name Vector Song
forall a. a -> a
id Int -> GenericList Name Vector Song -> GenericList Name Vector Song
forall (t :: * -> *) n e.
(Foldable t, Splittable t) =>
Int -> GenericList n t e -> GenericList n t e
listMoveTo Maybe Int
mi (GenericList Name Vector Song -> GenericList Name Vector Song)
-> GenericList Name Vector Song -> GenericList Name Vector Song
forall a b. (a -> b) -> a -> b
$ Name -> Vector Song -> Int -> GenericList Name Vector Song
forall (t :: * -> *) n e.
Foldable t =>
n -> t e -> Int -> GenericList n t e
list Name
SongsList Vector Song
songsVec Int
1
    HumState -> m HumState
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HumState -> m HumState) -> HumState -> m HumState
forall a b. (a -> b) -> a -> b
$ HumState
s HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
& (LibraryState -> Identity LibraryState)
-> HumState -> Identity HumState
Lens' HumState LibraryState
libraryL ((LibraryState -> Identity LibraryState)
 -> HumState -> Identity HumState)
-> ((GenericList Name Vector Song
     -> Identity (GenericList Name Vector Song))
    -> LibraryState -> Identity LibraryState)
-> (GenericList Name Vector Song
    -> Identity (GenericList Name Vector Song))
-> HumState
-> Identity HumState
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GenericList Name Vector Song
 -> Identity (GenericList Name Vector Song))
-> LibraryState -> Identity LibraryState
Lens' LibraryState (GenericList Name Vector Song)
songsL ((GenericList Name Vector Song
  -> Identity (GenericList Name Vector Song))
 -> HumState -> Identity HumState)
-> GenericList Name Vector Song -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ GenericList Name Vector Song
songs'


-- | Rebuild entire stored playlists state, keeping the index of the left column if possible.
rebuildPl :: MonadIO m => HumState -> m HumState
rebuildPl :: HumState -> m HumState
rebuildPl HumState
s = do
  let mi :: Maybe Int
mi = HumState
s HumState
-> Getting
     (List Name PlaylistName) HumState (List Name PlaylistName)
-> List Name PlaylistName
forall s a. s -> Getting a s a -> a
^. (PlaylistsState -> Const (List Name PlaylistName) PlaylistsState)
-> HumState -> Const (List Name PlaylistName) HumState
Lens' HumState PlaylistsState
playlistsL ((PlaylistsState -> Const (List Name PlaylistName) PlaylistsState)
 -> HumState -> Const (List Name PlaylistName) HumState)
-> ((List Name PlaylistName
     -> Const (List Name PlaylistName) (List Name PlaylistName))
    -> PlaylistsState -> Const (List Name PlaylistName) PlaylistsState)
-> Getting
     (List Name PlaylistName) HumState (List Name PlaylistName)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (List Name PlaylistName
 -> Const (List Name PlaylistName) (List Name PlaylistName))
-> PlaylistsState -> Const (List Name PlaylistName) PlaylistsState
Lens' PlaylistsState (List Name PlaylistName)
plListL List Name PlaylistName
-> (List Name PlaylistName -> Maybe Int) -> Maybe Int
forall a b. a -> (a -> b) -> b
& List Name PlaylistName -> Maybe Int
forall n (t :: * -> *) e. GenericList n t e -> Maybe Int
listSelected
  Vector PlaylistName
plListVec  <- IO (Vector PlaylistName) -> m (Vector PlaylistName)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Vector PlaylistName) -> m (Vector PlaylistName))
-> IO (Vector PlaylistName) -> m (Vector PlaylistName)
forall a b. (a -> b) -> a -> b
$  [PlaylistName] -> Vector PlaylistName
forall a. [a] -> Vector a
V.fromList ([PlaylistName] -> Vector PlaylistName)
-> (Either MPDError [PlaylistName] -> [PlaylistName])
-> Either MPDError [PlaylistName]
-> Vector PlaylistName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [PlaylistName] -> [PlaylistName]
forall a. Ord a => [a] -> [a]
sort ([PlaylistName] -> [PlaylistName])
-> (Either MPDError [PlaylistName] -> [PlaylistName])
-> Either MPDError [PlaylistName]
-> [PlaylistName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [PlaylistName] -> Either MPDError [PlaylistName] -> [PlaylistName]
forall b a. b -> Either a b -> b
fromRight [] (Either MPDError [PlaylistName] -> Vector PlaylistName)
-> IO (Either MPDError [PlaylistName]) -> IO (Vector PlaylistName)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MPD [PlaylistName] -> IO (Either MPDError [PlaylistName])
forall a. MPD a -> IO (Response a)
withMPD MPD [PlaylistName]
forall (m :: * -> *). MonadMPD m => m [PlaylistName]
MPD.listPlaylists
  let plList' :: List Name PlaylistName
plList' = (List Name PlaylistName -> List Name PlaylistName)
-> (Int -> List Name PlaylistName -> List Name PlaylistName)
-> Maybe Int
-> List Name PlaylistName
-> List Name PlaylistName
forall b a. b -> (a -> b) -> Maybe a -> b
maybe List Name PlaylistName -> List Name PlaylistName
forall a. a -> a
id Int -> List Name PlaylistName -> List Name PlaylistName
forall (t :: * -> *) n e.
(Foldable t, Splittable t) =>
Int -> GenericList n t e -> GenericList n t e
listMoveTo Maybe Int
mi (List Name PlaylistName -> List Name PlaylistName)
-> List Name PlaylistName -> List Name PlaylistName
forall a b. (a -> b) -> a -> b
$ Name -> Vector PlaylistName -> Int -> List Name PlaylistName
forall (t :: * -> *) n e.
Foldable t =>
n -> t e -> Int -> GenericList n t e
list Name
PlaylistList Vector PlaylistName
plListVec Int
1
  Vector Song
plSongsVec <- IO (Vector Song) -> m (Vector Song)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Vector Song) -> m (Vector Song))
-> IO (Vector Song) -> m (Vector Song)
forall a b. (a -> b) -> a -> b
$ [Song] -> Vector Song
forall a. [a] -> Vector a
V.fromList ([Song] -> Vector Song)
-> (Either MPDError [Song] -> [Song])
-> Either MPDError [Song]
-> Vector Song
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Song] -> Either MPDError [Song] -> [Song]
forall b a. b -> Either a b -> b
fromRight [] (Either MPDError [Song] -> Vector Song)
-> IO (Either MPDError [Song]) -> IO (Vector Song)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MPD [Song] -> IO (Either MPDError [Song])
forall a. MPD a -> IO (Response a)
withMPD
           (PlaylistName -> MPD [Song]
forall (m :: * -> *). MonadMPD m => PlaylistName -> m [Song]
MPD.listPlaylistInfo
               (PlaylistName
-> ((Int, PlaylistName) -> PlaylistName)
-> Maybe (Int, PlaylistName)
-> PlaylistName
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PlaylistName
"<no playlists>" (Int, PlaylistName) -> PlaylistName
forall a b. (a, b) -> b
snd (List Name PlaylistName -> Maybe (Int, PlaylistName)
forall (t :: * -> *) n e.
(Splittable t, Foldable t) =>
GenericList n t e -> Maybe (Int, e)
listSelectedElement List Name PlaylistName
plList'))
           )
  let plSongs' :: GenericList Name Vector (Song, Bool)
plSongs' = (, Bool
False) (Song -> (Song, Bool))
-> GenericList Name Vector Song
-> GenericList Name Vector (Song, Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Name -> Vector Song -> Int -> GenericList Name Vector Song
forall (t :: * -> *) n e.
Foldable t =>
n -> t e -> Int -> GenericList n t e
list Name
PlaylistSongs Vector Song
plSongsVec Int
1
  HumState -> m HumState
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HumState -> m HumState) -> HumState -> m HumState
forall a b. (a -> b) -> a -> b
$ HumState
s HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
& (PlaylistsState -> Identity PlaylistsState)
-> HumState -> Identity HumState
Lens' HumState PlaylistsState
playlistsL ((PlaylistsState -> Identity PlaylistsState)
 -> HumState -> Identity HumState)
-> ((List Name PlaylistName -> Identity (List Name PlaylistName))
    -> PlaylistsState -> Identity PlaylistsState)
-> (List Name PlaylistName -> Identity (List Name PlaylistName))
-> HumState
-> Identity HumState
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (List Name PlaylistName -> Identity (List Name PlaylistName))
-> PlaylistsState -> Identity PlaylistsState
Lens' PlaylistsState (List Name PlaylistName)
plListL  ((List Name PlaylistName -> Identity (List Name PlaylistName))
 -> HumState -> Identity HumState)
-> List Name PlaylistName -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ List Name PlaylistName
plList'
           HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
& (PlaylistsState -> Identity PlaylistsState)
-> HumState -> Identity HumState
Lens' HumState PlaylistsState
playlistsL ((PlaylistsState -> Identity PlaylistsState)
 -> HumState -> Identity HumState)
-> ((GenericList Name Vector (Song, Bool)
     -> Identity (GenericList Name Vector (Song, Bool)))
    -> PlaylistsState -> Identity PlaylistsState)
-> (GenericList Name Vector (Song, Bool)
    -> Identity (GenericList Name Vector (Song, Bool)))
-> HumState
-> Identity HumState
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GenericList Name Vector (Song, Bool)
 -> Identity (GenericList Name Vector (Song, Bool)))
-> PlaylistsState -> Identity PlaylistsState
Lens' PlaylistsState (GenericList Name Vector (Song, Bool))
plSongsL ((GenericList Name Vector (Song, Bool)
  -> Identity (GenericList Name Vector (Song, Bool)))
 -> HumState -> Identity HumState)
-> GenericList Name Vector (Song, Bool) -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ GenericList Name Vector (Song, Bool)
plSongs'

-- | Rebuild stored playlists state from selected playlist.
rebuildPlList :: MonadIO m => HumState -> m HumState
rebuildPlList :: HumState -> m HumState
rebuildPlList HumState
s = do
    let plList' :: List Name PlaylistName
plList' = HumState
s HumState
-> Getting
     (List Name PlaylistName) HumState (List Name PlaylistName)
-> List Name PlaylistName
forall s a. s -> Getting a s a -> a
^. (PlaylistsState -> Const (List Name PlaylistName) PlaylistsState)
-> HumState -> Const (List Name PlaylistName) HumState
Lens' HumState PlaylistsState
playlistsL ((PlaylistsState -> Const (List Name PlaylistName) PlaylistsState)
 -> HumState -> Const (List Name PlaylistName) HumState)
-> ((List Name PlaylistName
     -> Const (List Name PlaylistName) (List Name PlaylistName))
    -> PlaylistsState -> Const (List Name PlaylistName) PlaylistsState)
-> Getting
     (List Name PlaylistName) HumState (List Name PlaylistName)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (List Name PlaylistName
 -> Const (List Name PlaylistName) (List Name PlaylistName))
-> PlaylistsState -> Const (List Name PlaylistName) PlaylistsState
Lens' PlaylistsState (List Name PlaylistName)
plListL
    Vector Song
plSongsVec <- IO (Vector Song) -> m (Vector Song)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
        ([Song] -> Vector Song
forall a. [a] -> Vector a
V.fromList ([Song] -> Vector Song)
-> (Either MPDError [Song] -> [Song])
-> Either MPDError [Song]
-> Vector Song
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Song] -> Either MPDError [Song] -> [Song]
forall b a. b -> Either a b -> b
fromRight [] (Either MPDError [Song] -> Vector Song)
-> IO (Either MPDError [Song]) -> IO (Vector Song)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MPD [Song] -> IO (Either MPDError [Song])
forall a. MPD a -> IO (Response a)
withMPD
            (PlaylistName -> MPD [Song]
forall (m :: * -> *). MonadMPD m => PlaylistName -> m [Song]
MPD.listPlaylistInfo
                (PlaylistName
-> ((Int, PlaylistName) -> PlaylistName)
-> Maybe (Int, PlaylistName)
-> PlaylistName
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PlaylistName
"<no playlists>" (Int, PlaylistName) -> PlaylistName
forall a b. (a, b) -> b
snd (List Name PlaylistName -> Maybe (Int, PlaylistName)
forall (t :: * -> *) n e.
(Splittable t, Foldable t) =>
GenericList n t e -> Maybe (Int, e)
listSelectedElement List Name PlaylistName
plList'))
            )
        )
    let plSongs' :: GenericList Name Vector (Song, Bool)
plSongs' = (, Bool
False) (Song -> (Song, Bool))
-> GenericList Name Vector Song
-> GenericList Name Vector (Song, Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Name -> Vector Song -> Int -> GenericList Name Vector Song
forall (t :: * -> *) n e.
Foldable t =>
n -> t e -> Int -> GenericList n t e
list Name
PlaylistSongs Vector Song
plSongsVec Int
1
    HumState -> m HumState
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HumState -> m HumState) -> HumState -> m HumState
forall a b. (a -> b) -> a -> b
$ HumState
s HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
& (PlaylistsState -> Identity PlaylistsState)
-> HumState -> Identity HumState
Lens' HumState PlaylistsState
playlistsL ((PlaylistsState -> Identity PlaylistsState)
 -> HumState -> Identity HumState)
-> ((GenericList Name Vector (Song, Bool)
     -> Identity (GenericList Name Vector (Song, Bool)))
    -> PlaylistsState -> Identity PlaylistsState)
-> (GenericList Name Vector (Song, Bool)
    -> Identity (GenericList Name Vector (Song, Bool)))
-> HumState
-> Identity HumState
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GenericList Name Vector (Song, Bool)
 -> Identity (GenericList Name Vector (Song, Bool)))
-> PlaylistsState -> Identity PlaylistsState
Lens' PlaylistsState (GenericList Name Vector (Song, Bool))
plSongsL ((GenericList Name Vector (Song, Bool)
  -> Identity (GenericList Name Vector (Song, Bool)))
 -> HumState -> Identity HumState)
-> GenericList Name Vector (Song, Bool) -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ GenericList Name Vector (Song, Bool)
plSongs'

-- | Rebuild stored playlists state from selected playlist, keeping the selected song index if possible.
reloadPlList :: MonadIO m => HumState -> m HumState
reloadPlList :: HumState -> m HumState
reloadPlList HumState
s = do
    let mi :: Maybe Int
mi = HumState
s HumState
-> Getting
     (GenericList Name Vector (Song, Bool))
     HumState
     (GenericList Name Vector (Song, Bool))
-> GenericList Name Vector (Song, Bool)
forall s a. s -> Getting a s a -> a
^. (PlaylistsState
 -> Const (GenericList Name Vector (Song, Bool)) PlaylistsState)
-> HumState
-> Const (GenericList Name Vector (Song, Bool)) HumState
Lens' HumState PlaylistsState
playlistsL ((PlaylistsState
  -> Const (GenericList Name Vector (Song, Bool)) PlaylistsState)
 -> HumState
 -> Const (GenericList Name Vector (Song, Bool)) HumState)
-> ((GenericList Name Vector (Song, Bool)
     -> Const
          (GenericList Name Vector (Song, Bool))
          (GenericList Name Vector (Song, Bool)))
    -> PlaylistsState
    -> Const (GenericList Name Vector (Song, Bool)) PlaylistsState)
-> Getting
     (GenericList Name Vector (Song, Bool))
     HumState
     (GenericList Name Vector (Song, Bool))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GenericList Name Vector (Song, Bool)
 -> Const
      (GenericList Name Vector (Song, Bool))
      (GenericList Name Vector (Song, Bool)))
-> PlaylistsState
-> Const (GenericList Name Vector (Song, Bool)) PlaylistsState
Lens' PlaylistsState (GenericList Name Vector (Song, Bool))
plSongsL GenericList Name Vector (Song, Bool)
-> (GenericList Name Vector (Song, Bool) -> Maybe Int) -> Maybe Int
forall a b. a -> (a -> b) -> b
& GenericList Name Vector (Song, Bool) -> Maybe Int
forall n (t :: * -> *) e. GenericList n t e -> Maybe Int
listSelected
    let plList' :: List Name PlaylistName
plList' = HumState
s HumState
-> Getting
     (List Name PlaylistName) HumState (List Name PlaylistName)
-> List Name PlaylistName
forall s a. s -> Getting a s a -> a
^. (PlaylistsState -> Const (List Name PlaylistName) PlaylistsState)
-> HumState -> Const (List Name PlaylistName) HumState
Lens' HumState PlaylistsState
playlistsL ((PlaylistsState -> Const (List Name PlaylistName) PlaylistsState)
 -> HumState -> Const (List Name PlaylistName) HumState)
-> ((List Name PlaylistName
     -> Const (List Name PlaylistName) (List Name PlaylistName))
    -> PlaylistsState -> Const (List Name PlaylistName) PlaylistsState)
-> Getting
     (List Name PlaylistName) HumState (List Name PlaylistName)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (List Name PlaylistName
 -> Const (List Name PlaylistName) (List Name PlaylistName))
-> PlaylistsState -> Const (List Name PlaylistName) PlaylistsState
Lens' PlaylistsState (List Name PlaylistName)
plListL
    Vector Song
plSongsVec <- IO (Vector Song) -> m (Vector Song)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
        ([Song] -> Vector Song
forall a. [a] -> Vector a
V.fromList ([Song] -> Vector Song)
-> (Either MPDError [Song] -> [Song])
-> Either MPDError [Song]
-> Vector Song
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Song] -> Either MPDError [Song] -> [Song]
forall b a. b -> Either a b -> b
fromRight [] (Either MPDError [Song] -> Vector Song)
-> IO (Either MPDError [Song]) -> IO (Vector Song)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MPD [Song] -> IO (Either MPDError [Song])
forall a. MPD a -> IO (Response a)
withMPD
            (PlaylistName -> MPD [Song]
forall (m :: * -> *). MonadMPD m => PlaylistName -> m [Song]
MPD.listPlaylistInfo
                (PlaylistName
-> ((Int, PlaylistName) -> PlaylistName)
-> Maybe (Int, PlaylistName)
-> PlaylistName
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PlaylistName
"<no playlists>" (Int, PlaylistName) -> PlaylistName
forall a b. (a, b) -> b
snd (List Name PlaylistName -> Maybe (Int, PlaylistName)
forall (t :: * -> *) n e.
(Splittable t, Foldable t) =>
GenericList n t e -> Maybe (Int, e)
listSelectedElement List Name PlaylistName
plList'))
            )
        )
    let plSongs' :: GenericList Name Vector (Song, Bool)
plSongs' = (GenericList Name Vector (Song, Bool)
 -> GenericList Name Vector (Song, Bool))
-> (Int
    -> GenericList Name Vector (Song, Bool)
    -> GenericList Name Vector (Song, Bool))
-> Maybe Int
-> GenericList Name Vector (Song, Bool)
-> GenericList Name Vector (Song, Bool)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe GenericList Name Vector (Song, Bool)
-> GenericList Name Vector (Song, Bool)
forall a. a -> a
id Int
-> GenericList Name Vector (Song, Bool)
-> GenericList Name Vector (Song, Bool)
forall (t :: * -> *) n e.
(Foldable t, Splittable t) =>
Int -> GenericList n t e -> GenericList n t e
listMoveTo Maybe Int
mi (GenericList Name Vector (Song, Bool)
 -> GenericList Name Vector (Song, Bool))
-> GenericList Name Vector (Song, Bool)
-> GenericList Name Vector (Song, Bool)
forall a b. (a -> b) -> a -> b
$ (, Bool
False) (Song -> (Song, Bool))
-> GenericList Name Vector Song
-> GenericList Name Vector (Song, Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Name -> Vector Song -> Int -> GenericList Name Vector Song
forall (t :: * -> *) n e.
Foldable t =>
n -> t e -> Int -> GenericList n t e
list Name
PlaylistSongs Vector Song
plSongsVec Int
1
    HumState -> m HumState
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HumState -> m HumState) -> HumState -> m HumState
forall a b. (a -> b) -> a -> b
$ HumState
s HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
& (PlaylistsState -> Identity PlaylistsState)
-> HumState -> Identity HumState
Lens' HumState PlaylistsState
playlistsL ((PlaylistsState -> Identity PlaylistsState)
 -> HumState -> Identity HumState)
-> ((GenericList Name Vector (Song, Bool)
     -> Identity (GenericList Name Vector (Song, Bool)))
    -> PlaylistsState -> Identity PlaylistsState)
-> (GenericList Name Vector (Song, Bool)
    -> Identity (GenericList Name Vector (Song, Bool)))
-> HumState
-> Identity HumState
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GenericList Name Vector (Song, Bool)
 -> Identity (GenericList Name Vector (Song, Bool)))
-> PlaylistsState -> Identity PlaylistsState
Lens' PlaylistsState (GenericList Name Vector (Song, Bool))
plSongsL ((GenericList Name Vector (Song, Bool)
  -> Identity (GenericList Name Vector (Song, Bool)))
 -> HumState -> Identity HumState)
-> GenericList Name Vector (Song, Bool) -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ GenericList Name Vector (Song, Bool)
plSongs'


-- | Rebuild queue state, keeping the index of the left column if possible.
rebuildQueue :: MonadIO m => HumState -> m HumState
rebuildQueue :: HumState -> m HumState
rebuildQueue HumState
s = do
  let mi :: Maybe Int
mi = HumState
s HumState
-> Getting
     (GenericList Name Vector (Song, Bool))
     HumState
     (GenericList Name Vector (Song, Bool))
-> GenericList Name Vector (Song, Bool)
forall s a. s -> Getting a s a -> a
^. Getting
  (GenericList Name Vector (Song, Bool))
  HumState
  (GenericList Name Vector (Song, Bool))
Lens' HumState (GenericList Name Vector (Song, Bool))
queueL GenericList Name Vector (Song, Bool)
-> (GenericList Name Vector (Song, Bool) -> Maybe Int) -> Maybe Int
forall a b. a -> (a -> b) -> b
& GenericList Name Vector (Song, Bool) -> Maybe Int
forall n (t :: * -> *) e. GenericList n t e -> Maybe Int
listSelected
  Vector Song
queueVec  <- IO (Vector Song) -> m (Vector Song)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Vector Song) -> m (Vector Song))
-> IO (Vector Song) -> m (Vector Song)
forall a b. (a -> b) -> a -> b
$ [Song] -> Vector Song
forall a. [a] -> Vector a
V.fromList ([Song] -> Vector Song)
-> (Either MPDError [Song] -> [Song])
-> Either MPDError [Song]
-> Vector Song
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Song] -> Either MPDError [Song] -> [Song]
forall b a. b -> Either a b -> b
fromRight [] (Either MPDError [Song] -> Vector Song)
-> IO (Either MPDError [Song]) -> IO (Vector Song)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MPD [Song] -> IO (Either MPDError [Song])
forall a. MPD a -> IO (Response a)
withMPD (Maybe Int -> MPD [Song]
forall (m :: * -> *). MonadMPD m => Maybe Int -> m [Song]
MPD.playlistInfo Maybe Int
forall a. Maybe a
Nothing)
  let queue' :: GenericList Name Vector (Song, Bool)
queue' = (GenericList Name Vector (Song, Bool)
 -> GenericList Name Vector (Song, Bool))
-> (Int
    -> GenericList Name Vector (Song, Bool)
    -> GenericList Name Vector (Song, Bool))
-> Maybe Int
-> GenericList Name Vector (Song, Bool)
-> GenericList Name Vector (Song, Bool)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe GenericList Name Vector (Song, Bool)
-> GenericList Name Vector (Song, Bool)
forall a. a -> a
id Int
-> GenericList Name Vector (Song, Bool)
-> GenericList Name Vector (Song, Bool)
forall (t :: * -> *) n e.
(Foldable t, Splittable t) =>
Int -> GenericList n t e -> GenericList n t e
listMoveTo Maybe Int
mi (GenericList Name Vector (Song, Bool)
 -> GenericList Name Vector (Song, Bool))
-> GenericList Name Vector (Song, Bool)
-> GenericList Name Vector (Song, Bool)
forall a b. (a -> b) -> a -> b
$ (, Bool
False) (Song -> (Song, Bool))
-> GenericList Name Vector Song
-> GenericList Name Vector (Song, Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Name -> Vector Song -> Int -> GenericList Name Vector Song
forall (t :: * -> *) n e.
Foldable t =>
n -> t e -> Int -> GenericList n t e
list Name
QueueList Vector Song
queueVec Int
1
  HumState -> m HumState
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HumState -> m HumState) -> HumState -> m HumState
forall a b. (a -> b) -> a -> b
$ HumState
s HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
& (GenericList Name Vector (Song, Bool)
 -> Identity (GenericList Name Vector (Song, Bool)))
-> HumState -> Identity HumState
Lens' HumState (GenericList Name Vector (Song, Bool))
queueL ((GenericList Name Vector (Song, Bool)
  -> Identity (GenericList Name Vector (Song, Bool)))
 -> HumState -> Identity HumState)
-> GenericList Name Vector (Song, Bool) -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ GenericList Name Vector (Song, Bool)
queue'

-- | Rebuild status and current song state.
rebuildStatus :: MonadIO m => HumState -> m HumState
rebuildStatus :: HumState -> m HumState
rebuildStatus HumState
s = do
  Maybe Song
currentSong' <- IO (Maybe Song) -> m (Maybe Song)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Maybe Song -> Either MPDError (Maybe Song) -> Maybe Song
forall b a. b -> Either a b -> b
fromRight Maybe Song
forall a. Maybe a
Nothing (Either MPDError (Maybe Song) -> Maybe Song)
-> IO (Either MPDError (Maybe Song)) -> IO (Maybe Song)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MPD (Maybe Song) -> IO (Either MPDError (Maybe Song))
forall a. MPD a -> IO (Response a)
withMPD MPD (Maybe Song)
forall (m :: * -> *). MonadMPD m => m (Maybe Song)
MPD.currentSong)
  Maybe Status
status'      <- IO (Maybe Status) -> m (Maybe Status)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Maybe Status -> Either MPDError (Maybe Status) -> Maybe Status
forall b a. b -> Either a b -> b
fromRight Maybe Status
forall a. Maybe a
Nothing (Either MPDError (Maybe Status) -> Maybe Status)
-> IO (Either MPDError (Maybe Status)) -> IO (Maybe Status)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Status -> Maybe Status
forall a. a -> Maybe a
Just (Status -> Maybe Status)
-> IO (Either MPDError Status)
-> IO (Either MPDError (Maybe Status))
forall (f :: * -> *) (g :: * -> *) a b.
(Functor f, Functor g) =>
(a -> b) -> f (g a) -> f (g b)
<<$>> MPD Status -> IO (Either MPDError Status)
forall a. MPD a -> IO (Response a)
withMPD MPD Status
forall (m :: * -> *). MonadMPD m => m Status
MPD.status))
  HumState -> m HumState
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HumState -> m HumState) -> HumState -> m HumState
forall a b. (a -> b) -> a -> b
$ HumState
s HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
& (Maybe Song -> Identity (Maybe Song))
-> HumState -> Identity HumState
Lens' HumState (Maybe Song)
currentSongL ((Maybe Song -> Identity (Maybe Song))
 -> HumState -> Identity HumState)
-> Maybe Song -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Maybe Song
currentSong'
           HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
& (Maybe Status -> Identity (Maybe Status))
-> HumState -> Identity HumState
Lens' HumState (Maybe Status)
statusL ((Maybe Status -> Identity (Maybe Status))
 -> HumState -> Identity HumState)
-> Maybe Status -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Maybe Status
status'