Search in sources :

Example 6 with IZypeApiListener

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

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

the class GalleryViewModel method loadRowPlaylists.

private void loadRowPlaylists(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) {
            PlaylistsResponse playlistsResponse = (PlaylistsResponse) response.data;
            if (response.isSuccessful) {
                List<Playlist> result = new ArrayList<>();
                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));
                    }
                }
                repo.insertPlaylists(result);
                row.nestedPlaylists = repo.getPlaylistsSync(row.playlist.id);
                if (playlistsResponse.pagination.current >= playlistsResponse.pagination.pages) {
                    row.pageToLoad = -1;
                    row.state = UPDATED;
                } else {
                    row.pageToLoad = playlistsResponse.pagination.next;
                    row.state = READY;
                    api.getPlaylists(row.playlist.id, row.pageToLoad, parameters, this);
                    Logger.d("loadRowPlaylists(): page=" + playlistsResponse.pagination.next);
                }
                updateGalleryRows(null);
            } else {
                // TODO: Handle api response error
                if (playlistsResponse != null) {
                // errorMessage.setValue(videosResponse.message);
                } else {
                // errorMessage.setValue(getApplication().getString(R.string.videos_error));
                }
            }
        }
    };
    api.getPlaylists(row.playlist.id, row.pageToLoad, parameters, listener);
    Logger.d("loadRowPlaylists(): page=" + row.pageToLoad);
}
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 8 with IZypeApiListener

use of com.zype.android.zypeapi.IZypeApiListener 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

IZypeApiListener (com.zype.android.zypeapi.IZypeApiListener)8 ZypeApiResponse (com.zype.android.zypeapi.ZypeApiResponse)8 ArrayList (java.util.ArrayList)6 Video (com.zype.android.Db.Entity.Video)5 Playlist (com.zype.android.Db.Entity.Playlist)3 VideosResponse (com.zype.android.zypeapi.model.VideosResponse)3 HashMap (java.util.HashMap)3 FavoriteVideo (com.zype.android.Db.Entity.FavoriteVideo)2 PlaylistsResponse (com.zype.android.zypeapi.model.PlaylistsResponse)2 VideoResponse (com.zype.android.zypeapi.model.VideoResponse)2 Application (android.app.Application)1 TextUtils (android.text.TextUtils)1 LiveData (androidx.lifecycle.LiveData)1 AuthHelper (com.zype.android.Auth.AuthHelper)1 DbHelper (com.zype.android.Db.DbHelper)1 AdSchedule (com.zype.android.Db.Entity.AdSchedule)1 AnalyticBeacon (com.zype.android.Db.Entity.AnalyticBeacon)1 PlaylistVideo (com.zype.android.Db.Entity.PlaylistVideo)1 ZypeDb (com.zype.android.Db.ZypeDb)1 SettingsProvider (com.zype.android.core.settings.SettingsProvider)1