Search in sources :

Example 1 with VideosResponse

use of com.zype.android.zypeapi.model.VideosResponse 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 2 with VideosResponse

use of com.zype.android.zypeapi.model.VideosResponse 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)

Example 3 with VideosResponse

use of com.zype.android.zypeapi.model.VideosResponse in project zype-android by zype.

the class GalleryViewModel method loadRowVideos.

private void loadRowVideos(GalleryRow row) {
    row.pageToLoad = 1;
    Map<String, String> parameters = new HashMap<>();
    parameters.put(ZypeApi.PER_PAGE, String.valueOf(10));
    final IZypeApiListener listener = new IZypeApiListener() {

        @Override
        public void onCompleted(ZypeApiResponse response) {
            VideosResponse videosResponse = (VideosResponse) response.data;
            if (response.isSuccessful && videosResponse != null) {
                List<Video> result = new ArrayList<>();
                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));
                    }
                }
                repo.insertVideos(result);
                if (videosResponse.pagination.current == 1) {
                    repo.deletePlaylistVideos(row.playlist.id);
                }
                repo.insertPlaylistVideos(result, row.playlist);
                row.videos = repo.getPlaylistVideosSync(row.playlist.id);
                if (videosResponse.pagination.current >= videosResponse.pagination.pages) {
                    row.pageToLoad = -1;
                    row.state = UPDATED;
                } else {
                    row.pageToLoad = videosResponse.pagination.next;
                    row.state = READY;
                    api.getPlaylistVideos(row.playlist.id, row.pageToLoad, parameters, this);
                    Logger.d("loadRowVideos(): page=" + videosResponse.pagination.next);
                }
                updateGalleryRows(null);
            } else {
                // TODO: Handle api response error
                if (videosResponse != null) {
                // errorMessage.setValue(videosResponse.message);
                } else {
                // errorMessage.setValue(getApplication().getString(R.string.videos_error));
                }
            }
        }
    };
    api.getPlaylistVideos(row.playlist.id, row.pageToLoad, parameters, listener);
    Logger.d("loadRowVideos(): page=" + row.pageToLoad);
}
Also used : HashMap(java.util.HashMap) IZypeApiListener(com.zype.android.zypeapi.IZypeApiListener) ArrayList(java.util.ArrayList) ZypeApiResponse(com.zype.android.zypeapi.ZypeApiResponse) Video(com.zype.android.Db.Entity.Video) VideosResponse(com.zype.android.zypeapi.model.VideosResponse)

Aggregations

Video (com.zype.android.Db.Entity.Video)3 IZypeApiListener (com.zype.android.zypeapi.IZypeApiListener)3 ZypeApiResponse (com.zype.android.zypeapi.ZypeApiResponse)3 VideosResponse (com.zype.android.zypeapi.model.VideosResponse)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)1