Search in sources :

Example 1 with PlaylistData

use of com.zype.fire.api.Model.PlaylistData in project zype-firebuilder by zype.

the class ContentLoader method loadPlayList.

public Observable<ContentContainer> loadPlayList(ContentContainer root, String playListId) {
    return Observable.just(playListId).subscribeOn(Schedulers.io()).observeOn(Schedulers.io()).flatMap(id -> {
        PlaylistData playlistData = ZypeDataDownloaderHelper.loadPlayList(playListId);
        if (playlistData != null) {
            // need to convert this to a content container
            playlistData.parentId = playListId;
            HashMap map = new HashMap();
            map.put("categories", Arrays.asList(playlistData));
            GsonBuilder builder = new GsonBuilder();
            Gson gson = builder.create();
            String feed = gson.toJson(map);
            String[] params = new String[] { playListId };
            Recipe recipeDynamicParserContainer = Recipe.newInstance(mContext, "recipes/ZypeCategoriesRecipe.json");
            return mDynamicParser.cookRecipeObservable(recipeDynamicParserContainer, feed, null, params).flatMap(o -> {
                ContentContainer contentContainer = (ContentContainer) o;
                return Observable.just(contentContainer);
            });
        }
        return Observable.error(new Exception("unable to load the playlist"));
    });
}
Also used : PlaylistData(com.zype.fire.api.Model.PlaylistData) HashMap(java.util.HashMap) GsonBuilder(com.google.gson.GsonBuilder) Recipe(com.amazon.android.recipe.Recipe) Gson(com.google.gson.Gson) ContentContainer(com.amazon.android.model.content.ContentContainer)

Example 2 with PlaylistData

use of com.zype.fire.api.Model.PlaylistData in project zype-firebuilder by zype.

the class ZypeDataDownloader method fetchData.

/**
 * Fetches the {@link Data} for this data downloader.
 *
 * @param dataLoadRecipe The data load recipe.
 * @return The downloaded {@link Data}.
 * @throws Exception if there was an error while fetching the data.
 */
@Override
protected Data fetchData(Recipe dataLoadRecipe) throws Exception {
    Log.d(TAG, "fetchData(): Started");
    AppData appData = loadAppConfiguration();
    Log.d(TAG, "fetchData(): App configuration loaded");
    ZypeConfiguration.update(appData, mContext);
    loadZobjectContents();
    List<PlaylistData> playlists = loadPlaylists();
    Log.d(TAG, "fetchData(): Playlists loaded");
    addFavoritesPlaylist(playlists);
    if (ZypeSettings.LIBRARY_ENABLED) {
        addMyLibraryPlaylists(playlists);
    }
    // Result data
    JSONArray jsonCategories = new JSONArray();
    JSONArray jsonContents = new JSONArray();
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();
    for (PlaylistData playlistData : playlists) {
        if (TextUtils.isEmpty(playlistData.description)) {
            playlistData.description = " ";
        }
        // Skip playlist that are not direct child of the root playlist
        if (TextUtils.isEmpty(playlistData.parentId) || !playlistData.parentId.equals(ZypeConfiguration.getRootPlaylistId(mContext))) {
            continue;
        }
        if (playlistData.playlistItemCount > 0) {
            Log.d(TAG, "fetchData(): Loading videos for " + playlistData.title);
            VideosResponse videosResponse = ZypeDataDownloaderHelper.loadPlaylistVideos(playlistData.id, 1);
            if (videosResponse != null) {
                for (VideoData videoData : videosResponse.videoData) {
                    jsonContents.put(new JSONObject(gson.toJson(videoData)));
                }
            }
        }
    }
    Log.d(TAG, "fetchData(): Videos loaded");
    Collections.sort(playlists, (a, b) -> {
        Integer valA;
        Integer valB;
        try {
            valA = a.priority;
            valB = b.priority;
        } catch (Exception e) {
            return 0;
        }
        return valA.compareTo(valB);
    });
    for (PlaylistData playlistData : playlists) {
        String playlistId = playlistData.id;
        if (playlistId.equals(ZypeConfiguration.getRootPlaylistId(mContext)) || TextUtils.isEmpty(playlistData.parentId)) {
            continue;
        }
        jsonCategories.put(new JSONObject(gson.toJson(playlistData)));
    }
    JSONObject jsonResult = new JSONObject();
    jsonResult.put("categories", jsonCategories);
    jsonResult.put("contents", jsonContents);
    Log.d(TAG, "fetchData(): finished");
    return Data.createDataForPayload(jsonResult.toString());
}
Also used : JSONObject(org.json.JSONObject) AppData(com.zype.fire.api.Model.AppData) PlaylistData(com.zype.fire.api.Model.PlaylistData) GsonBuilder(com.google.gson.GsonBuilder) JSONArray(org.json.JSONArray) VideoData(com.zype.fire.api.Model.VideoData) Gson(com.google.gson.Gson) VideosResponse(com.zype.fire.api.Model.VideosResponse)

Example 3 with PlaylistData

use of com.zype.fire.api.Model.PlaylistData in project zype-firebuilder by zype.

the class ZypeDataDownloader method addMyLibraryPlaylists.

private void addMyLibraryPlaylists(List<PlaylistData> playlists) {
    PlaylistData item = new PlaylistData();
    item.id = ZypeSettings.ROOT_MY_LIBRARY_PLAYLIST_ID;
    item.description = " ";
    item.parentId = ZypeConfiguration.getRootPlaylistId(mContext);
    item.thumbnailLayout = "landscape";
    item.title = ZypeSettings.ROOT_MY_LIBRARY_PLAYLIST_ID;
    playlists.add(item);
    item = new PlaylistData();
    item.id = ZypeSettings.MY_LIBRARY_PLAYLIST_ID;
    item.description = " ";
    item.parentId = ZypeSettings.ROOT_MY_LIBRARY_PLAYLIST_ID;
    item.playlistItemCount = 1;
    item.thumbnailLayout = "landscape";
    item.title = "Library";
    playlists.add(item);
}
Also used : PlaylistData(com.zype.fire.api.Model.PlaylistData)

Example 4 with PlaylistData

use of com.zype.fire.api.Model.PlaylistData in project zype-firebuilder by zype.

the class ZypeDataDownloader method loadPlaylists.

private List<PlaylistData> loadPlaylists() {
    List<PlaylistData> result = new ArrayList<>();
    int page = 1;
    PlaylistsResponse playlistsResponse = ZypeApi.getInstance().getPlaylists(page);
    if (playlistsResponse != null && playlistsResponse.response != null) {
        result.addAll(playlistsResponse.response);
        if (playlistsResponse.pagination != null && playlistsResponse.pagination.pages > 1) {
            for (page = playlistsResponse.pagination.next; page <= playlistsResponse.pagination.pages; page++) {
                playlistsResponse = ZypeApi.getInstance().getPlaylists(page);
                if (playlistsResponse != null && playlistsResponse.response != null) {
                    result.addAll(playlistsResponse.response);
                }
            }
        }
    }
    return result;
}
Also used : PlaylistData(com.zype.fire.api.Model.PlaylistData) ArrayList(java.util.ArrayList) PlaylistsResponse(com.zype.fire.api.Model.PlaylistsResponse)

Example 5 with PlaylistData

use of com.zype.fire.api.Model.PlaylistData in project zype-firebuilder by zype.

the class ZypeDataDownloader method addFavoritesPlaylist.

private void addFavoritesPlaylist(List<PlaylistData> playlists) {
    PlaylistData item = new PlaylistData();
    item.id = ZypeSettings.ROOT_FAVORITES_PLAYLIST_ID;
    item.description = " ";
    item.parentId = ZypeConfiguration.getRootPlaylistId(mContext);
    item.thumbnailLayout = "landscape";
    item.title = ZypeSettings.ROOT_FAVORITES_PLAYLIST_ID;
    playlists.add(item);
    item = new PlaylistData();
    item.id = ZypeSettings.FAVORITES_PLAYLIST_ID;
    // TODO: Use string resources for description and title instead of hardcoded strings
    item.description = "Favorites";
    item.parentId = ZypeSettings.ROOT_FAVORITES_PLAYLIST_ID;
    item.thumbnailLayout = "landscape";
    item.title = ZypeSettings.FAVORITES_PLAYLIST_ID;
    playlists.add(item);
}
Also used : PlaylistData(com.zype.fire.api.Model.PlaylistData)

Aggregations

PlaylistData (com.zype.fire.api.Model.PlaylistData)5 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 ContentContainer (com.amazon.android.model.content.ContentContainer)1 Recipe (com.amazon.android.recipe.Recipe)1 AppData (com.zype.fire.api.Model.AppData)1 PlaylistsResponse (com.zype.fire.api.Model.PlaylistsResponse)1 VideoData (com.zype.fire.api.Model.VideoData)1 VideosResponse (com.zype.fire.api.Model.VideosResponse)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1