package com.sjn.stamp.ui.fragment.media_list;

import android.support.annotation.NonNull;
import android.support.v4.media.MediaBrowserCompat;

import com.sjn.stamp.media.QueueManager;
import com.sjn.stamp.media.provider.ProviderType;
import com.sjn.stamp.ui.activity.DrawerMenu;
import com.sjn.stamp.ui.item.QueueTitleItem;
import com.sjn.stamp.ui.item.SongItem;
import com.sjn.stamp.utils.LogHelper;
import com.sjn.stamp.utils.MediaIDHelper;

import java.util.List;

import eu.davidea.flexibleadapter.items.AbstractFlexibleItem;

public class QueueListFragment extends SongListFragment {

    private static final String TAG = LogHelper.makeLogTag(QueueListFragment.class);

    public QueueListFragment() {
        setMediaId(DrawerMenu.QUEUE.getMediaId());
    }

    @Override
    public void onMediaBrowserChildrenLoaded(@NonNull String parentId,
                                             @NonNull List<MediaBrowserCompat.MediaItem> children) {
        try {
            LogHelper.d(TAG, "fragment onChildrenLoaded, parentId=" + parentId +
                    "  count=" + children.size());
            mItemList.clear();
            mAdapter.clear();
            String baseMediaId = "";
            for (MediaBrowserCompat.MediaItem item : children) {
                AbstractFlexibleItem songItem = new SongItem(item);
                mItemList.add(songItem);
                mAdapter.addItem(songItem);
                if (item.getDescription().getExtras() != null) {
                    baseMediaId = item.getDescription().getExtras().getString(QueueManager.META_DATA_KEY_BASE_MEDIA_ID);
                }
            }
            mAdapter.notifyDataSetChanged();
            ProviderType providerType = MediaIDHelper.getProviderType(baseMediaId);
            if (providerType != null && baseMediaId != null) {
                mAdapter.showLayoutInfo(
                        new QueueTitleItem(providerType, MediaIDHelper.extractBrowseCategoryValueFromMediaID(baseMediaId)), true);
            }
        } catch (Throwable t) {
            LogHelper.e(TAG, "Error on childrenloaded", t);
        }
    }

}
@@ -321,47 +321,8 @@
 em;%0A
-import com.sjn.stamp.ui.item.SongItem;%0A
 impo
@@ -426,71 +426,8 @@
 t;%0A%0A
-import eu.davidea.flexibleadapter.items.AbstractFlexibleItem;%0A%0A
 publ
@@ -1006,415 +1006,107 @@
     
-mItemList.clear();%0A            mAdapter.clear();%0A            String baseMediaId = %22%22;%0A            for (MediaBrowserCompat.MediaItem item : children) %7B%0A                AbstractFlexibleItem songItem = new SongItem(item);%0A                mItemList.add(songItem);%0A                mAdapter.addItem(songItem);%0A                if (item.getDescription().getExtras() != null) %7B%0A                    baseMediaId = item
+if (children != null && !children.isEmpty()) %7B%0A                String baseMediaId = children.get(0)
 .get
@@ -1192,81 +1192,8 @@
     
-            %7D%0A            %7D%0A            mAdapter.notifyDataSetChanged();%0A
     
@@ -1280,24 +1280,28 @@
             
+  
+  
 if (provider
@@ -1355,16 +1355,20 @@
         
+    
 mAdapter
@@ -1384,16 +1384,20 @@
 utInfo(%0A
+    
         
@@ -1527,25 +1527,97 @@
             
-%7D
+    %7D%0A            %7D%0A            mSongList = children;%0A            draw();
 %0A        %7D c

fix bug of duplicated queue item