package com.marverenic.music.player.browser;

import android.content.Context;
import android.support.v4.media.MediaBrowserCompat.MediaItem;

import com.marverenic.music.R;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.NoSuchElementException;

import javax.inject.Inject;

import rx.Single;

public class MediaBrowserRoot extends MediaBrowserDirectory {

    private static final String ID = "JOCKEY";

    private final List<MediaBrowserDirectory> mChildren;

    @Inject
    public MediaBrowserRoot(Context context) {
        super(ID, R.string.app_name);
        mChildren = Collections.unmodifiableList(Arrays.asList(
                new AllSongsDirectory(context),
                new PlaylistRootDirectory(context)
        ));
    }

    @Override
    protected Single<List<MediaBrowserDirectory>> getSubdirectories() {
        return Single.just(mChildren);
    }

    @Override
    protected Single<List<MediaItem>> getMedia() {
        return Single.just(Collections.emptyList());
    }

    @Override
    protected Single<MediaList> getQueueForMediaItem(String id) {
        return Single.error(new NoSuchElementException(id + " does not exist in the browser root"));
    }

}

@@ -188,17 +188,20 @@
 il.Array
-s
+List
 ;%0Aimport
@@ -318,16 +318,38 @@
 nject;%0A%0A
+import rx.Observable;%0A
 import r
@@ -470,16 +470,77 @@
 CKEY%22;%0A%0A
+    private final List%3CMediaBrowserDirectory%3E mFlatChildren;%0A
     priv
@@ -695,16 +695,20 @@
        m
+Flat
 Children
@@ -743,17 +743,29 @@
 ist(
-Arrays.as
+Collections.singleton
 List
@@ -790,16 +790,20 @@
 new 
-AllSongs
+PlaylistRoot
 Dire
@@ -816,17 +816,16 @@
 context)
-,
 %0A       
@@ -821,32 +821,37 @@
 xt)%0A        
+));%0A%0A
         
 new Playlist
@@ -838,32 +838,112 @@
         
-new PlaylistRoot
+mChildren = Collections.unmodifiableList(Collections.singletonList(%0A                new AllSongs
 Director
@@ -1077,154 +1077,1006 @@
 urn 
-Single.just(mChildren);%0A    %7D%0A%0A    @Override%0A    protected Single%3CList%3CMediaItem%3E%3E getMedia() %7B%0A        return Single.just(Collections.emptyList()
+Observable.from(mFlatChildren)%0A                .flatMap(child -%3E child.getSubdirectories().toObservable())%0A                .reduce(new ArrayList%3CMediaBrowserDirectory%3E(), (aggregate, items) -%3E %7B%0A                    aggregate.addAll(items);%0A                    return aggregate;%0A                %7D)%0A                .map(flatChildren -%3E %7B%0A                    List%3CMediaBrowserDirectory%3E children = new ArrayList%3C%3E(flatChildren);%0A                    children.addAll(mChildren);%0A                    return children;%0A                %7D)%0A                .toSingle();%0A    %7D%0A%0A    @Override%0A    protected Single%3CList%3CMediaItem%3E%3E getMedia() %7B%0A        return Observable.from(mFlatChildren)%0A                .flatMap(mediaBrowserDirectory -%3E mediaBrowserDirectory.getMedia().toObservable())%0A                .reduce((List%3CMediaItem%3E) new ArrayList%3CMediaItem%3E(), (aggregate, items) -%3E %7B%0A                    aggregate.addAll(items);%0A                    return aggregate;%0A                %7D)%0A                .toSingle(
 );%0A 

Move playlists up one level