From 96b757060a802cf906ce56e19a99e2ad10351600 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Tue, 1 Jul 2014 07:22:43 -0700 Subject: Adding support for moving videos between chapters. Also moved the Playlist related tests to their own class. --- src/com/p4square/grow/model/Playlist.java | 42 +++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 11 deletions(-) (limited to 'src/com/p4square/grow/model/Playlist.java') diff --git a/src/com/p4square/grow/model/Playlist.java b/src/com/p4square/grow/model/Playlist.java index 79ce68e..b697e66 100644 --- a/src/com/p4square/grow/model/Playlist.java +++ b/src/com/p4square/grow/model/Playlist.java @@ -48,6 +48,22 @@ public class Playlist { return null; } + /** + * @param videoId The video to search for. + * @return the Chapter containing videoId. + */ + private Chapter findChapter(String videoId) { + for (Chapter chapter : mPlaylist.values()) { + VideoRecord r = chapter.getVideoRecord(videoId); + + if (r != null) { + return chapter; + } + } + + return null; + } + /** * @return The last modified date of the source playlist. */ @@ -141,26 +157,30 @@ public class Playlist { Chapter myChapter = mPlaylist.get(entry.getKey()); if (myChapter == null) { - // Add entire chapter - try { - mPlaylist.put(chapterName, theirChapter.clone()); - } catch (CloneNotSupportedException e) { - throw new RuntimeException(e); // Unexpected... - } + // Add new chapter + myChapter = new Chapter(); + addChapter(chapterName, myChapter); + } - } else { - // Check chapter for missing videos - for (Map.Entry videoEntry : theirChapter.getVideos().entrySet()) { - String videoId = videoEntry.getKey(); - VideoRecord myVideo = myChapter.getVideoRecord(videoId); + // Check chapter for missing videos + for (Map.Entry videoEntry : theirChapter.getVideos().entrySet()) { + String videoId = videoEntry.getKey(); + VideoRecord myVideo = myChapter.getVideoRecord(videoId); + if (myVideo == null) { + myVideo = find(videoId); if (myVideo == null) { + // New Video try { myVideo = videoEntry.getValue().clone(); myChapter.setVideoRecord(videoId, myVideo); } catch (CloneNotSupportedException e) { throw new RuntimeException(e); // Unexpected... } + } else { + // Video moved + findChapter(videoId).removeVideoRecord(videoId); + myChapter.setVideoRecord(videoId, myVideo); } } } -- cgit v1.2.3