Search in sources :

Example 1 with ZypeApiResponse

use of com.zype.android.zypeapi.ZypeApiResponse in project zype-android by zype.

the class GalleryViewModel method loadRootPlaylists.

private void loadRootPlaylists() {
    Logger.d("loadRootPlaylists(): parentPlaylistId=" + parentPlaylistId);
    final List<Playlist> result = new ArrayList<>();
    Map<String, String> parameters = new HashMap<>();
    parameters.put(ZypeApi.PER_PAGE, String.valueOf(100));
    final IZypeApiListener listener = new IZypeApiListener() {

        @Override
        public void onCompleted(ZypeApiResponse response) {
            PlaylistsResponse playlistsResponse = (PlaylistsResponse) response.data;
            if (playlistsResponse != null && response.isSuccessful) {
                for (com.zype.android.zypeapi.model.PlaylistData item : playlistsResponse.response) {
                    Playlist playlist = repo.getPlaylistSync(item.id);
                    if (playlist != null) {
                        result.add(DbHelper.playlistUpdateEntityByApi(playlist, item));
                    } else {
                        result.add(DbHelper.playlistApiToEntity(item));
                    }
                }
                if (playlistsResponse.pagination.current >= playlistsResponse.pagination.pages) {
                    repo.insertPlaylists(result);
                    createGalleryRows();
                    Logger.d("loadRootPlaylists(): size=" + result.size());
                } else {
                    api.getPlaylists(parentPlaylistId, playlistsResponse.pagination.next, parameters, this);
                    Logger.d("loadRootPlaylists(): page=" + playlistsResponse.pagination.next);
                }
            } else {
                // TODO: Handle api response error
                if (playlistsResponse != null) {
                // errorMessage.setValue(videosResponse.message);
                } else {
                // errorMessage.setValue(getApplication().getString(R.string.videos_error));
                }
            }
        }
    };
    api.getPlaylists(parentPlaylistId, 1, parameters, listener);
    Logger.d("loadRootPlaylists(): page=1");
}
Also used : HashMap(java.util.HashMap) IZypeApiListener(com.zype.android.zypeapi.IZypeApiListener) ArrayList(java.util.ArrayList) PlaylistsResponse(com.zype.android.zypeapi.model.PlaylistsResponse) ZypeApiResponse(com.zype.android.zypeapi.ZypeApiResponse) Playlist(com.zype.android.Db.Entity.Playlist)

Example 2 with ZypeApiResponse

use of com.zype.android.zypeapi.ZypeApiResponse in project zype-android by zype.

the class VideoDetailViewModel method loadVideo.

// Zype API
/**
 * Make API request for video
 *
 * @param videoId Video id
 */
private void loadVideo(String videoId) {
    Logger.d("loadVideo(): videoId=" + videoId);
    final IZypeApiListener listener = new IZypeApiListener() {

        @Override
        public void onCompleted(ZypeApiResponse response) {
            VideoResponse videoResponse = (VideoResponse) response.data;
            if (response.isSuccessful) {
                Video video = repo.getVideoSync(videoId);
                if (video != null) {
                    video = DbHelper.videoUpdateEntityByApi(video, videoResponse.videoData);
                } else {
                    video = DbHelper.videoApiToEntity(videoResponse.videoData);
                }
                repo.updateVideo(video);
            } else {
            // TODO: Add error handling
            }
        }
    };
    api.getVideo(videoId, false, listener);
// VideoParamsBuilder builder = new VideoParamsBuilder()
// .addVideoId(videoId);
// oldApi.executeRequest(WebApiManager.Request.VIDEO, builder.build());
}
Also used : ZypeApiResponse(com.zype.android.zypeapi.ZypeApiResponse) VideoResponse(com.zype.android.zypeapi.model.VideoResponse) IZypeApiListener(com.zype.android.zypeapi.IZypeApiListener) Video(com.zype.android.Db.Entity.Video)

Example 3 with ZypeApiResponse

use of com.zype.android.zypeapi.ZypeApiResponse in project zype-android by zype.

the class VideoActionsHelper method onUnfavorite.

public static void onUnfavorite(Video video, Application application, IVideoActionCallback listener) {
    if (AuthHelper.isLoggedIn()) {
        String accessToken = AuthHelper.getAccessToken();
        String consumerId = SettingsProvider.getInstance().getConsumerId();
        FavoriteVideo favoriteVideo = DataRepository.getInstance(application).getVideoFavoriteByVideoId(video.id);
        if (favoriteVideo != null) {
            ZypeApi.getInstance().removeVideoFavorite(accessToken, consumerId, favoriteVideo.id, new IZypeApiListener() {

                @Override
                public void onCompleted(ZypeApiResponse response) {
                    if (response.isSuccessful) {
                        updateVideoFavorite(application, video, favoriteVideo.id, false, listener);
                    }
                }
            });
        } else {
            updateVideoFavorite(application, video, null, false, listener);
        }
    } else {
        if (ZypeApp.get(application).getAppConfiguration().hideFavoritesActionWhenSignedOut) {
            if (listener != null) {
                listener.onActionCompleted(false);
            }
        // NavigationHelper.getInstance(activity).switchToLoginScreen(activity);
        } else {
            updateVideoFavorite(application, video, null, false, listener);
        }
    }
}
Also used : ZypeApiResponse(com.zype.android.zypeapi.ZypeApiResponse) IZypeApiListener(com.zype.android.zypeapi.IZypeApiListener) FavoriteVideo(com.zype.android.Db.Entity.FavoriteVideo)

Example 4 with ZypeApiResponse

use of com.zype.android.zypeapi.ZypeApiResponse in project zype-android by zype.

the class SearchViewModel method loadSearchResult.

private void loadSearchResult(String query, String playlistId) {
    final List<Video> result = new ArrayList<>();
    final IZypeApiListener listener = new IZypeApiListener() {

        @Override
        public void onCompleted(ZypeApiResponse response) {
            VideosResponse videosResponse = (VideosResponse) response.data;
            if (response.isSuccessful) {
                for (com.zype.android.zypeapi.model.VideoData item : videosResponse.videoData) {
                    Video video = repo.getVideoSync(item.id);
                    if (video != null) {
                        result.add(DbHelper.videoUpdateEntityByApi(video, item));
                    } else {
                        result.add(DbHelper.videoApiToEntity(item));
                    }
                }
                if (videosResponse.pagination.current == videosResponse.pagination.pages || videosResponse.pagination.pages == 0) {
                    repo.insertVideos(result);
                    updateVideos(new StatefulData<>(result, null, DataState.READY));
                } else {
                    api.searchVideos(query, playlistId, videosResponse.pagination.next, this);
                }
            } else {
                if (videosResponse != null) {
                    updateVideos(new StatefulData<>(null, videosResponse.message, DataState.ERROR));
                } else {
                    updateVideos(new StatefulData<>(null, getApplication().getString(R.string.videos_error), DataState.ERROR));
                }
            }
        }
    };
    api.searchVideos(query, playlistId, 1, listener);
}
Also used : ZypeApiResponse(com.zype.android.zypeapi.ZypeApiResponse) IZypeApiListener(com.zype.android.zypeapi.IZypeApiListener) Video(com.zype.android.Db.Entity.Video) ArrayList(java.util.ArrayList) VideosResponse(com.zype.android.zypeapi.model.VideosResponse)

Example 5 with ZypeApiResponse

use of com.zype.android.zypeapi.ZypeApiResponse in project zype-android by zype.

the class PlaylistVideosViewModel method loadVideos.

private void loadVideos(final String playlistId) {
    updateVideos(new StatefulData<>(null, null, DataState.LOADING));
    final List<Video> result = new ArrayList<>();
    final IZypeApiListener listener = new IZypeApiListener() {

        @Override
        public void onCompleted(ZypeApiResponse response) {
            VideosResponse videosResponse = (VideosResponse) response.data;
            if (response.isSuccessful) {
                for (com.zype.android.zypeapi.model.VideoData item : videosResponse.videoData) {
                    Video video = repo.getVideoSync(item.id);
                    if (video != null) {
                        result.add(DbHelper.videoUpdateEntityByApi(video, item));
                    } else {
                        result.add(DbHelper.videoApiToEntity(item));
                    }
                }
                if (videosResponse.pagination.current == videosResponse.pagination.pages) {
                    repo.insertVideos(result);
                    repo.deletePlaylistVideos(playlistId);
                    repo.insertPlaylistVideos(DbHelper.videosToPlaylistVideos(result, playlistId));
                    updateVideos(new StatefulData<>(repo.getPlaylistVideosSync(playlistId), null, DataState.READY));
                } else {
                    api.getPlaylistVideos(playlistId, videosResponse.pagination.next, this);
                }
            } else {
                if (videosResponse != null) {
                    updateVideos(new StatefulData<>(null, videosResponse.message, DataState.ERROR));
                // errorMessage.setValue(videosResponse.message);
                } else {
                    updateVideos(new StatefulData<>(null, getApplication().getString(R.string.videos_error), DataState.ERROR));
                // errorMessage.setValue(getApplication().getString(R.string.videos_error));
                }
            }
        }
    };
    api.getPlaylistVideos(playlistId, 1, listener);
}
Also used : ZypeApiResponse(com.zype.android.zypeapi.ZypeApiResponse) IZypeApiListener(com.zype.android.zypeapi.IZypeApiListener) Video(com.zype.android.Db.Entity.Video) ArrayList(java.util.ArrayList) VideosResponse(com.zype.android.zypeapi.model.VideosResponse)

Aggregations

IZypeApiListener (com.zype.android.zypeapi.IZypeApiListener)7 ZypeApiResponse (com.zype.android.zypeapi.ZypeApiResponse)7 ArrayList (java.util.ArrayList)5 Video (com.zype.android.Db.Entity.Video)4 VideosResponse (com.zype.android.zypeapi.model.VideosResponse)3 HashMap (java.util.HashMap)3 Playlist (com.zype.android.Db.Entity.Playlist)2 PlaylistsResponse (com.zype.android.zypeapi.model.PlaylistsResponse)2 FavoriteVideo (com.zype.android.Db.Entity.FavoriteVideo)1 VideoResponse (com.zype.android.zypeapi.model.VideoResponse)1