Implemeted playlist update system 47/264547/1
authoraman.jeph <aman.jeph@samsung.com>
Fri, 24 Sep 2021 08:28:50 +0000 (13:58 +0530)
committeraman.jeph <aman.jeph@samsung.com>
Fri, 24 Sep 2021 08:28:50 +0000 (13:58 +0530)
Change-Id: Ifcdf30c646525b152d1e0b68ad4807e78a0c5529
Signed-off-by: aman.jeph <aman.jeph@samsung.com>
music-player/Core/PlaylistManager.cs
music-player/ViewModels/PlaylistViewModel.cs
packaging/org.tizen.MusicPlayer-1.0.0.tpk

index 7a2686dd207e14b7229b962563f9a730f19febeb..0757917ce3f54ee4f265fe7260e0d421a8bf4dc6 100755 (executable)
@@ -1,6 +1,6 @@
 using System;
 using System.Collections.Generic;
-using System.Text;
+using Tizen.NUI;
 using Tizen.Content.MediaContent;
 using MusicPlayer.Common;
 using MusicPlayer.Media;
@@ -8,12 +8,27 @@ using MusicPlayer.Media;
 
 namespace MusicPlayer.Core
 {
-    class PlaylistManager
+    class PlaylistManager : IDisposable
     {
-        public event EventHandler<EventArgs> PlaylistDataChanged;
         private static PlaylistManager instance = new PlaylistManager();
+
+        /// <summary>
+        /// MediaDatabase doesn't provide update event when playlist is added , removed or changed
+        /// so we need to manage the update here because we need to update the view whenever playlist data is changed
+        /// every playlist change should use the PlaylistManager to update the playlist data in media database
+        /// otherwise they won't receive the update event.
+        /// Every api that changes the playlist data needs to call <seealso cref="PostUpdate"/>
+        /// </summary>
+        public event EventHandler<EventArgs> PlaylistDataChanged;
+
+        private Timer updateTimer;
+        private bool disposedValue;
+
         private PlaylistManager()
         {
+            updateTimer = new Timer(100);
+            updateTimer.Tick += OnPlayDataUpdate;
+            disposedValue = false;
             Contents.MusicDBUpdate += OnMusicDBUpdate;
             Contents.MusicItemUpdate += OnMusicItemDBUpdate;
         }
@@ -40,12 +55,22 @@ namespace MusicPlayer.Core
 
         public Playlist AddPlaylist(string name)
         {
-            return Contents.CreatePlaylist(name);
+            Playlist createdPlaylist = Contents.CreatePlaylist(name);
+            if(createdPlaylist != null)
+            {
+                PostUpdate();
+            }
+            return createdPlaylist;
         }
 
         public Playlist AddPlaylist(string name, string thumbnailPath)
         {
-            return Contents.CreatePlaylist(name, thumbnailPath);
+            Playlist createdPlaylist = Contents.CreatePlaylist(name, thumbnailPath);
+            if(createdPlaylist != null)
+            {
+                PostUpdate();
+            }
+            return createdPlaylist;
         }
 
         public int PlaylistTrackCount(int playlistId)
@@ -53,9 +78,20 @@ namespace MusicPlayer.Core
             return Contents.GetMemberCount(playlistId);
         }
 
+        /// <summary>
+        /// Delete the Playlist, it is not efficient for deleting multiple playlist as it will call update for each playlist.
+        /// use <seealso cref="DeletePlaylists"/>
+        /// </summary>
+        /// <param name="playlistId">Id of playlist</param>
+        /// <returns></returns>
         public bool Delete(int playlistId)
         {
-            return Contents.DeletePlaylist(playlistId);
+            bool isDeleted = Contents.DeletePlaylist(playlistId);
+            if(isDeleted)
+            {
+                PostUpdate();
+            }
+            return isDeleted;
         }
 
         // TODO untill we refactor the SelectorView this is requiered
@@ -69,6 +105,10 @@ namespace MusicPlayer.Core
                     count++;
                 }
             }
+            if(count > 0)
+            {
+                PostUpdate();
+            }
             return count;
         }
 
@@ -82,6 +122,10 @@ namespace MusicPlayer.Core
                     count++;
                 }
             }
+            if (count > 0)
+            {
+                PostUpdate();
+            }
             return count;
         }
 
@@ -92,38 +136,123 @@ namespace MusicPlayer.Core
 
         public bool AddTracks(int playlistId, List<string> trackList)
         {
-            return Contents.AddMediaList(playlistId, trackList);
+            if(trackList.Count <= 0)
+            {
+                Tizen.Log.Error(AppConstants.LogTag, "Track list empty , can't add anything to playlist");
+                return false;
+            }
+            bool isTracksAdded = Contents.AddMediaList(playlistId, trackList);
+            if(isTracksAdded)
+            {
+                PostUpdate();
+            }
+            return isTracksAdded;
         }
 
+        /// <summary>
+        /// Add track to playlist. it is not efficent for adding multiple track as it will call update for each track added
+        /// use <seealso cref="AddTracks(int, List{string})"/>
+        /// </summary>
+        /// <param name="playlistId">playlist id</param>
+        /// <param name="trackId">track Id</param>
+        /// <returns></returns>
         public bool AddTrack(int playlistId, string trackId)
         {
-            return Contents.AddMedia(playlistId, trackId);
+            bool isTrackAdded = Contents.AddMedia(playlistId, trackId);
+            if(isTrackAdded)
+            {
+                PostUpdate();
+            }
+            return isTrackAdded;
         }
 
         public bool UpdateValues(int playlistId, PlaylistUpdateValues updatedValues)
         {
-            return Contents.UpdatePlaylistValues(playlistId, updatedValues);
+            bool isPlaylistUpdated = Contents.UpdatePlaylistValues(playlistId, updatedValues);
+            if(isPlaylistUpdated)
+            {
+                PostUpdate();
+            }
+            return isPlaylistUpdated;
         }
 
+        /// <summary>
+        /// Remove the track from playlist, it is not efficent for removing multiple track as it will call update for each track removed
+        /// use <seealso cref="DeleteTrackList(int, List{int})"/>
+        /// </summary>
+        /// <param name="playlistId"></param>
+        /// <param name="memberId"></param>
+        /// <returns></returns>
         public bool DeleteTrack(int playlistId, int memberId)
         {
-            return Contents.DeletePlaylistMember(playlistId, memberId);
+            bool isTrackDeleted = Contents.DeletePlaylistMember(playlistId, memberId);
+            if(isTrackDeleted)
+            {
+                PostUpdate();
+            }
+            return isTrackDeleted;
         }
 
         public bool DeleteTrackList(int playlistId, List<int> memberList)
         {
-            return Contents.DeletePlaylistMembers(playlistId, memberList);
+            if (memberList.Count <= 0)
+            {
+                Tizen.Log.Error(AppConstants.LogTag, "Track list empty , can't remove anything from playlist");
+                return false;
+            }
+            bool isTracksDeleted = Contents.DeletePlaylistMembers(playlistId, memberList);
+            if(isTracksDeleted)
+            {
+                PostUpdate();
+            }
+            return isTracksDeleted;
         }
 
-        private void OnMusicItemDBUpdate(object sender, MusicItemUpdateEventArgs e)
+        public void Dispose()
         {
-            PlaylistDataChanged?.Invoke(this, new EventArgs());
+            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
+            Dispose(disposing: true);
+            GC.SuppressFinalize(this);
         }
 
-        private void OnMusicDBUpdate(object sender, MusicDBUpdateEventArgs e)
+        protected virtual void Dispose(bool disposing)
+        {
+            if (!disposedValue)
+            {
+                if (disposing)
+                {
+                    // TODO: dispose managed state (managed objects)
+                    updateTimer.Dispose();
+                    foreach(Delegate func in PlaylistDataChanged?.GetInvocationList())
+                    {
+                        PlaylistDataChanged -= (EventHandler<EventArgs>)func;
+                    }
+                }
+
+                // TODO: free unmanaged resources (unmanaged objects) and override finalizer
+                // TODO: set large fields to null
+                disposedValue = true;
+            }
+        }
+
+        private void PostUpdate()
+        {
+            updateTimer.Start();
+        }
+
+        private bool OnPlayDataUpdate(object source, Timer.TickEventArgs e)
         {
             PlaylistDataChanged?.Invoke(this, new EventArgs());
+            return false;
         }
 
+        private void OnMusicItemDBUpdate(object sender, MusicItemUpdateEventArgs e)
+        {
+            PostUpdate();
+        }
+        private void OnMusicDBUpdate(object sender, MusicDBUpdateEventArgs e)
+        {
+            PostUpdate();
+        }
     }
 }
index 7f0775b7010933d1abe02480455eed3f6cc50385..ecfefbe3241573bc1719a2b0b7896f5d604f8480 100755 (executable)
@@ -71,18 +71,19 @@ namespace MusicPlayer.ViewModels
         private Tuple<int, int> AddMemberToDefaultList(Playlist favourite, Playlist recentlyAdded)
         {
             List<AudioInfo> trackList = Contents.GetTrackList();
-            int recentAddedCount = trackList.Count;
-            int favouriteCount = 0;
+            List<string> recentTrackList = new List<string>();
+            List<string> favouriteTrackList = new List<string>();
             foreach(AudioInfo track in trackList)
             {
-                PlaylistManager.Instance.AddTrack(recentlyAdded.Id, track.Id);
+                recentTrackList.Add(track.Id);
                 if(track.IsFavorite)
                 {
-                    PlaylistManager.Instance.AddTrack(favourite.Id, track.Id);
-                    favouriteCount++;
+                    favouriteTrackList.Add(track.Id);
                 }
             }
-            return new Tuple<int, int>(recentAddedCount, favouriteCount);
+            PlaylistManager.Instance.AddTracks(recentlyAdded.Id, recentTrackList);
+            PlaylistManager.Instance.AddTracks(favourite.Id, favouriteTrackList);
+            return new Tuple<int, int>(recentTrackList.Count, favouriteTrackList.Count);
         }
 
         private string GetTrackCountForPlaylist(int playlistId)
index 98fca5cf4217ae11741ec464aba366a0218ea698..ceb41d649e315eefe69e8c1375401399e791187a 100755 (executable)
Binary files a/packaging/org.tizen.MusicPlayer-1.0.0.tpk and b/packaging/org.tizen.MusicPlayer-1.0.0.tpk differ