Search in sources :

Example 1 with GalleryRow

use of com.zype.android.ui.Gallery.Model.GalleryRow in project zype-android by zype.

the class GalleryViewModel method getGalleryRows.

public LiveData<List<GalleryRow>> getGalleryRows(String parentPlaylistId) {
    if (data == null) {
        this.parentPlaylistId = parentPlaylistId;
        data = new MediatorLiveData<>();
        liveDataPlaylists = getPlaylists(parentPlaylistId);
        liveDataVideos = new HashMap<>();
        liveDataNestedPlaylists = new HashMap<>();
        data.addSource(liveDataPlaylists, new Observer<List<Playlist>>() {

            @Override
            public void onChanged(@Nullable List<Playlist> playlists) {
                Logger.d("onChanged(): Playlists, size=" + playlists.size());
                if (!playlists.isEmpty()) {
                    data.removeSource(liveDataPlaylists);
                }
                final List<GalleryRow> galleryRows = new ArrayList<>();
                for (final Playlist playlist : playlists) {
                    final GalleryRow row = new GalleryRow(playlist);
                    // Add video items to the row if playlist contains any video
                    if (playlist.playlistItemCount > 0) {
                        LiveData<List<Video>> playlistVideos = liveDataVideos.get(playlist.id);
                        if (playlistVideos == null) {
                            playlistVideos = getPlaylistVideos(playlist.id);
                            liveDataVideos.put(playlist.id, playlistVideos);
                            data.addSource(playlistVideos, new Observer<List<Video>>() {

                                @Override
                                public void onChanged(@Nullable List<Video> videos) {
                                    Logger.d("onChanged(): Videos (" + playlist.title + "), size=" + videos.size());
                                    row.videos = videos;
                                    if (allDataLoaded(galleryRows)) {
                                        data.setValue(galleryRows);
                                        ZypeApp.needToLoadData = false;
                                    }
                                }
                            });
                        }
                    } else // Otherwise add nested playlists
                    {
                        LiveData<List<Playlist>> nestedPlaylists = liveDataNestedPlaylists.get(playlist.id);
                        if (nestedPlaylists == null) {
                            nestedPlaylists = getPlaylists(playlist.id);
                            liveDataNestedPlaylists.put(playlist.id, nestedPlaylists);
                            data.addSource(nestedPlaylists, new Observer<List<Playlist>>() {

                                @Override
                                public void onChanged(@Nullable List<Playlist> playlists) {
                                    Logger.d("onChanged(): Nested playlists (" + playlist.title + "), size=" + playlists.size());
                                    if (playlists.isEmpty()) {
                                        if (playlistApiRequests.containsKey(playlist.id)) {
                                            if (playlistApiRequests.get(playlist.id)) {
                                                Logger.d("onChanged(): Nested playlists (" + playlist.title + "), wait for API response");
                                            } else {
                                                Logger.d("onChanged(): Nested playlists (" + playlist.title + "), row removed");
                                                galleryRows.remove(row);
                                            }
                                        } else {
                                            Logger.d("onChanged(): Nested playlists (" + playlist.title + "), row removed");
                                            galleryRows.remove(row);
                                        }
                                    } else {
                                        row.nestedPlaylists = playlists;
                                        playlistApiRequests.remove(playlist.id);
                                    }
                                    if (allDataLoaded(galleryRows)) {
                                        data.setValue(galleryRows);
                                        ZypeApp.needToLoadData = false;
                                    }
                                }
                            });
                        }
                    }
                    galleryRows.add(row);
                }
                data.setValue(galleryRows);
            }
        });
    }
    return data;
}
Also used : MutableLiveData(androidx.lifecycle.MutableLiveData) LiveData(androidx.lifecycle.LiveData) MediatorLiveData(androidx.lifecycle.MediatorLiveData) GalleryRow(com.zype.android.ui.Gallery.Model.GalleryRow) Playlist(com.zype.android.Db.Entity.Playlist) Video(com.zype.android.Db.Entity.Video) Observer(androidx.lifecycle.Observer) ArrayList(java.util.ArrayList) List(java.util.List) Nullable(androidx.annotation.Nullable)

Example 2 with GalleryRow

use of com.zype.android.ui.Gallery.Model.GalleryRow in project zype-android by zype.

the class GalleryViewModel method createGalleryRows.

private void createGalleryRows() {
    List<GalleryRow> rows = new ArrayList<>();
    List<Playlist> rootPlaylists = repo.getPlaylistsSync(parentPlaylistId);
    for (Playlist playlist : rootPlaylists) {
        GalleryRow row = new GalleryRow(playlist);
        if (playlist.playlistItemCount > 0) {
            row.videos = repo.getPlaylistVideosSync(playlist.id);
            if (row.videos.isEmpty()) {
                row.state = LOADING;
            } else {
                row.state = READY;
            }
            loadRowVideos(row);
        } else {
            row.nestedPlaylists = repo.getPlaylistsSync(playlist.id);
            if (row.nestedPlaylists.isEmpty()) {
                row.state = LOADING;
            } else {
                row.state = READY;
            }
            loadRowPlaylists(row);
        }
        rows.add(row);
    }
    galleryRowsState = CREATED;
    updateGalleryRows(rows);
}
Also used : Playlist(com.zype.android.Db.Entity.Playlist) ArrayList(java.util.ArrayList) GalleryRow(com.zype.android.ui.Gallery.Model.GalleryRow)

Example 3 with GalleryRow

use of com.zype.android.ui.Gallery.Model.GalleryRow in project zype-android by zype.

the class GalleryViewModel method allDataLoaded.

private boolean allDataLoaded(List<GalleryRow> rows) {
    boolean result = true;
    List<GalleryRow> rowsToDelete = new ArrayList<>();
    if (data.getValue() == null || data.getValue().isEmpty()) {
        result = false;
    } else {
        for (GalleryRow item : rows) {
            boolean videosEmpty = item.videos == null || item.videos.isEmpty();
            if (videosEmpty) {
                boolean nestedPlaylistsEmpty = item.nestedPlaylists == null || item.nestedPlaylists.isEmpty();
                if (nestedPlaylistsEmpty) {
                    if (playlistApiRequests.containsKey(item.playlist.id)) {
                        if (playlistApiRequests.get(item.playlist.id)) {
                            result = false;
                            break;
                        } else {
                            rowsToDelete.add(item);
                        }
                    } else {
                        result = false;
                        break;
                    }
                }
            }
        }
    }
    Logger.d("allDataLoaded(): " + result);
    if (result) {
        for (GalleryRow item : rowsToDelete) {
            Logger.d("allDataLoaded(): (" + item.playlist.title + "), row removed");
            rows.remove(item);
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) GalleryRow(com.zype.android.ui.Gallery.Model.GalleryRow)

Aggregations

GalleryRow (com.zype.android.ui.Gallery.Model.GalleryRow)3 ArrayList (java.util.ArrayList)3 Playlist (com.zype.android.Db.Entity.Playlist)2 Nullable (androidx.annotation.Nullable)1 LiveData (androidx.lifecycle.LiveData)1 MediatorLiveData (androidx.lifecycle.MediatorLiveData)1 MutableLiveData (androidx.lifecycle.MutableLiveData)1 Observer (androidx.lifecycle.Observer)1 Video (com.zype.android.Db.Entity.Video)1 List (java.util.List)1