Search in sources :

Example 1 with IZypeApiListener

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

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

the class DataRepository method loadVideoFavorites.

// Video favorites
public void loadVideoFavorites(IDataLoading listener) {
    deleteVideoFavorites();
    String accessToken = AuthHelper.getAccessToken();
    String consumerId = SettingsProvider.getInstance().getConsumerId();
    ZypeApi.getInstance().getVideoFavorites(accessToken, consumerId, (IZypeApiListener<VideoFavoritesResponse>) response -> {
        if (response.isSuccessful) {
            for (VideoFavoriteData item : response.data.videoFavorites) {
                Video video = getVideoSync(item.videoId);
                if (video == null) {
                    loadVideo(item.videoId, response1 -> {
                        if (response.isSuccessful) {
                            Video dbVideo = getVideoSync(item.videoId);
                            if (dbVideo != null) {
                                dbVideo.isFavorite = 1;
                                updateVideo(dbVideo);
                                FavoriteVideo favoriteVideo = new FavoriteVideo();
                                favoriteVideo.id = item.id;
                                favoriteVideo.videoId = item.videoId;
                                addVideoFavorite(favoriteVideo);
                            }
                        }
                    });
                } else {
                    video.isFavorite = 1;
                    updateVideo(video);
                    FavoriteVideo favoriteVideo = new FavoriteVideo();
                    favoriteVideo.id = item.id;
                    favoriteVideo.videoId = item.videoId;
                    addVideoFavorite(favoriteVideo);
                }
            }
            if (listener != null) {
                listener.onLoadingCompleted(true);
            }
        } else {
            if (listener != null) {
                listener.onLoadingCompleted(false);
            }
        }
    });
}
Also used : VideoFavoritesResponse(com.zype.android.zypeapi.model.VideoFavoritesResponse) LiveData(androidx.lifecycle.LiveData) AdSchedule(com.zype.android.Db.Entity.AdSchedule) TextUtils(android.text.TextUtils) AuthHelper(com.zype.android.Auth.AuthHelper) ZypeDb(com.zype.android.Db.ZypeDb) SettingsProvider(com.zype.android.core.settings.SettingsProvider) VideoResponse(com.zype.android.zypeapi.model.VideoResponse) IZypeApiListener(com.zype.android.zypeapi.IZypeApiListener) ArrayList(java.util.ArrayList) AnalyticBeacon(com.zype.android.Db.Entity.AnalyticBeacon) List(java.util.List) Application(android.app.Application) VideoFavoriteData(com.zype.android.zypeapi.model.VideoFavoriteData) Playlist(com.zype.android.Db.Entity.Playlist) PlaylistVideo(com.zype.android.Db.Entity.PlaylistVideo) VideoEntitlementsResponse(com.zype.android.zypeapi.model.VideoEntitlementsResponse) DbHelper(com.zype.android.Db.DbHelper) Video(com.zype.android.Db.Entity.Video) VideoEntitlementData(com.zype.android.zypeapi.model.VideoEntitlementData) FavoriteVideo(com.zype.android.Db.Entity.FavoriteVideo) ZypeApiResponse(com.zype.android.zypeapi.ZypeApiResponse) ZypeApi(com.zype.android.zypeapi.ZypeApi) VideoFavoriteData(com.zype.android.zypeapi.model.VideoFavoriteData) VideoFavoritesResponse(com.zype.android.zypeapi.model.VideoFavoritesResponse) PlaylistVideo(com.zype.android.Db.Entity.PlaylistVideo) Video(com.zype.android.Db.Entity.Video) FavoriteVideo(com.zype.android.Db.Entity.FavoriteVideo) FavoriteVideo(com.zype.android.Db.Entity.FavoriteVideo)

Example 3 with IZypeApiListener

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

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

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

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