using System;
using System.Collections.Generic;
-using System.Text;
+using Tizen.NUI;
using Tizen.Content.MediaContent;
using MusicPlayer.Common;
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;
}
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)
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
count++;
}
}
+ if(count > 0)
+ {
+ PostUpdate();
+ }
return count;
}
count++;
}
}
+ if (count > 0)
+ {
+ PostUpdate();
+ }
return count;
}
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();
+ }
}
}