namespace MusicPlayer.ViewModels
{
- class PlaylistSelectorViewModel
+ class PlaylistSelectorViewModel : PropertyNotifier
{
- public PlaylistSelectorViewModel()
+ private List<string> addingTrackList;
+ public PlaylistSelectorViewModel(List<string> trackList)
{
- PlaylistManager.Instance.PlaylistDataChanged += OnPlaylistDataChanged;
+ addingTrackList = trackList;
List<PlaylistData> dataList = GeneratePlaylistData();
listViewModel = new ListViewModel<PlaylistModel>();
if (dataList.Count > 0)
length = UpdateLength(length, count);
newName = newName.Substring(0, length);
newName += count.ToString();
- if (CheckPlayListName(newName) == true)
+ if (IsPlaylistExists(newName) == false)
+ {
NewPlayListName = newName;
+ }
}
}
get => listViewModel;
}
+ public string NewPlayListName { get; set; }
+
+ private object playlistSelectedItem;
+
+ public object PlaylistSelectedItem
+ {
+ get => playlistSelectedItem;
+ set => SetProperty(ref playlistSelectedItem, value);
+ }
+
+ public bool CreatePlaylist(string playlistName)
+ {
+ Playlist playlist = PlaylistManager.Instance.GetPlaylist(playlistName);
+ if (playlist == null)
+ {
+ Playlist createdPlaylist = PlaylistManager.Instance.AddPlaylist(playlistName);
+ if(createdPlaylist != null)
+ {
+ PlaylistModel playlistModel = new PlaylistModel(new PlaylistData(createdPlaylist.Id, createdPlaylist.Name, GetTrackCountForPlaylist(createdPlaylist.Id), createdPlaylist.ThumbnailPath));
+ listViewModel.Add(playlistModel);
+ PlaylistSelectedItem = playlistModel;
+ }
+ return true;
+ }
+ return false;
+ }
+
+ public bool AddTracksToPlaylist(object selectedItemData)
+ {
+ PlaylistModel playlistModel = (PlaylistModel)selectedItemData;
+ if(addingTrackList == null || addingTrackList.Count <= 0)
+ {
+ return false;
+ }
+ return PlaylistManager.Instance.AddTracks(playlistModel.PlaylistId, addingTrackList);
+ }
+
+ public bool IsPlaylistExists(string playlistName)
+ {
+ Playlist playlist = PlaylistManager.Instance.GetPlaylist(playlistName);
+ return playlist == null ? false : true;
+ }
+
private List<PlaylistData> GeneratePlaylistData()
{
List<PlaylistData> dataList = new List<PlaylistData>();
{
continue;
}
- Tizen.Log.Debug(AppConstants.LogTag, playlist.Id + ": " + playlist.Name);
dataList.Add(new PlaylistData(playlist.Id, playlist.Name, GetTrackCountForPlaylist(playlist.Id), playlist.ThumbnailPath));
}
return dataList;
}
- private void UpdatePlaylistData()
+ private string GetTrackCountForPlaylist(int playlistId)
{
- listViewModel.Clear();
- List<PlaylistData> dataList = GeneratePlaylistData();
- listViewModel.CreateData(dataList);
+ int trackCount = PlaylistManager.Instance.PlaylistTrackCount(playlistId);
+ return trackCount > 1 ? trackCount.ToString() + " tracks" : trackCount.ToString() + " track";
}
private int UpdateLength(int length, int count)
}
return length;
}
-
- private void OnPlaylistDataChanged(object sender, System.EventArgs e)
- {
- UpdatePlaylistData();
- }
-
- public string GetTrackCountForPlaylist(int playlistId)
- {
- int trackCount = PlaylistManager.Instance.PlaylistTrackCount(playlistId);
- return trackCount > 1 ? trackCount.ToString() + " tracks" : trackCount.ToString() + " track";
- }
-
- public string NewPlayListName { get; set; }
-
- public bool CheckPlayListName(string playlistName)
- {
- Playlist playlist = PlaylistManager.Instance.GetPlaylist(playlistName);
- if (playlist == null)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
}
}
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
using Tizen.Content.MediaContent;
using MusicPlayer.ViewModels;
using Tizen.NUI.Components;
namespace MusicPlayer.Views
{
+ class PlaylistMemberAddEventArgs: EventArgs
+ {
+ public PlaylistMemberAddEventArgs(PlaylistMemberAddStatus currentAddStatus)
+ {
+ PlaylistAddStatus = currentAddStatus;
+ }
+
+ public PlaylistMemberAddStatus PlaylistAddStatus { get; private set; }
+ }
+
class PlaylistSelectorView : View
{
private View selectPlaylistContentArea;
private Button crossButton;
private PlaylistSelectorViewModel viewModel;
+ public event EventHandler<PlaylistMemberAddEventArgs> PlaylistMemberAdd;
+
public PlaylistSelectorView(PlaylistSelectorViewModel viewModel)
{
this.viewModel = viewModel;
WidthSpecification = LayoutParamPolicies.MatchParent,
HeightSpecification = 1,
BackgroundColor = UIColors.ItemSeperator,
- Margin = new Extents(44, 44, 0, 0),
+ Margin = new Extents(64, 64, 0, 0),
};
selectPlaylistContentArea.Add(itemSeperator);
createNewPlaylistButton.Clicked += CreateNewPlaylistButtonClicked;
collectionView.ItemTemplate = new DataTemplate(() =>
{
DefaultLinearItem layout = new DefaultLinearItem();
+ layout.WidthSpecification = LayoutParamPolicies.MatchParent;
+ layout.HeightSpecification = 108;
+ layout.Seperator.Padding = new Extents(0, 0, 0, 0);
+ layout.Seperator.Margin = new Extents(0, 0, 0, 0);
+ layout.Seperator.WidthSpecification = LayoutParamPolicies.MatchParent;
layout.Label.SetBinding(TextLabel.TextProperty, "PlaylistName");
layout.Padding = new Extents(0, 0, 0, 0);
layout.Margin = new Extents(0, 0, 0, 0);
return layout;
});
collectionView.ItemsSource = viewModel.ListViewModel;
+ collectionView.BindingContext = viewModel;
+ collectionView.SetBinding(CollectionView.SelectedItemProperty, "PlaylistSelectedItem");
+ collectionView.SelectionChanged += OnSelectionChange;
+ }
+
+ private void OnSelectionChange(object sender, SelectionChangedEventArgs e)
+ {
+ CollectionView collectionView = (CollectionView)sender;
+ object item = collectionView.SelectedItem;
+ if(item == null)
+ {
+ Tizen.Log.Error(AppConstants.LogTag, "Selected item is null");
+ return;
+ }
+ bool isAdded = viewModel.AddTracksToPlaylist(item);
+ if(isAdded == false)
+ {
+ Tizen.Log.Error(AppConstants.LogTag, "Failed to add track to playlist");
+ }
+ if(createPlaylistDialog != null && createPlaylistDialog.IsOnWindow)
+ {
+ RemoveTheCreatePopup();
+ }
+ PlaylistMemberAdd?.Invoke(this, new PlaylistMemberAddEventArgs(PlaylistMemberAddStatus.Added));
+ RemoveTheSelectPopup();
+ DeleteSelectorView();
}
private void AddNoListText()
private void SelectPlaylistCancelButtonClicked(object sender, ClickedEventArgs e)
{
+ PlaylistMemberAdd?.Invoke(this, new PlaylistMemberAddEventArgs(PlaylistMemberAddStatus.Cancelled));
RemoveTheSelectPopup();
+ DeleteSelectorView();
}
private void AddCreatePlaylistContentArea()
ThemeChangeSensitive = true,
Position2D = new Position2D(976, 6),
};
- crossButton.Clicked += CrossButtonClicked;
+ crossButton.Clicked += (object o, ClickedEventArgs e) =>
+ {
+ textField.Text = string.Empty;
+ };
inputArea.Add(textField);
inputArea.Add(crossButton);
}
if (searchText.Length > 64)
{
- Notification.MakeToast("Maximum number of characters reached.", Notification.ToastCenter).Post(Notification.ToastShort);
+ ShowInfoMessage("Maximum number of characters reached");
underText.Text = "Can't enter more than 64 characters.";
textField.Text = searchText.Substring(0, 63);
}
}
- private void CrossButtonClicked(object sender, ClickedEventArgs e)
- {
- textField.Text = "";
- }
-
private void AddCreatePlaylistButtons()
{
createPlaylistCancelButton = new Button("CancelButton")
private void CreatePlaylistCancelButtonClicked(object sender, ClickedEventArgs e)
{
RemoveTheCreatePopup();
+ DeleteTheCreatePopup();
+ ShowTheSelectPopup();
+ }
+
+ private void ShowInfoMessage(string message)
+ {
+ Notification.MakeToast(message, Notification.ToastCenter).Post(Notification.ToastShort);
}
private void CreatePlaylistCreateButtonClicked(object sender, ClickedEventArgs e)
{
- if (viewModel.CheckPlayListName(textField.Text))
+ if(string.IsNullOrEmpty(textField.Text) || string.IsNullOrWhiteSpace(textField.Text))
{
- RemoveTheCreatePopup();
- Window.Instance.Add(selectPlaylistDialog);
- Playlist playlist = PlaylistManager.Instance.AddPlaylist(textField.Text);
- PlaylistModel playlistModel = new PlaylistModel(new PlaylistData(playlist.Id, playlist.Name, viewModel.GetTrackCountForPlaylist(playlist.Id), playlist.ThumbnailPath));
- collectionView.SelectedItem = playlistModel;
+ ShowInfoMessage("Can't create playlist with empty or whitespace only characters");
+ return;
}
- else
+ if (viewModel.CreatePlaylist(textField.Text) == false)
{
- Notification.MakeToast("Playlist name already in use.", Notification.ToastCenter).Post(Notification.ToastShort);
+ ShowInfoMessage("Playlist name already in use.");
underText.Text = "Playlist name already in use.";
}
}
+ private void DeleteTheCreatePopup()
+ {
+ inputArea?.Remove(textField);
+ textField?.Dispose();
+ textField = null;
+ inputArea?.Remove(crossButton);
+ crossButton?.Dispose();
+ crossButton = null;
+
+ if (createPlaylistContentArea != null)
+ {
+ List<View> children = createPlaylistContentArea.Children;
+ while (children.Count > 0)
+ {
+ View child = children[0];
+ createPlaylistContentArea.Remove(child);
+ child?.Dispose();
+ }
+ }
+ inputArea = null;
+ underText = null;
+ createPlaylistDialog?.Dispose();
+ createPlaylistDialog = null;
+ createPlaylistContentArea = null;
+ createPlaylistCancelButton?.Dispose();
+ createPlaylistCancelButton = null;
+ createPlaylistCreateButton?.Dispose();
+ createPlaylistCreateButton = null;
+ }
+
protected override void Dispose(DisposeTypes type)
{
if (Disposed)
}
if (type == DisposeTypes.Explicit)
{
- inputArea?.Remove(textField);
- textField?.Dispose();
- textField = null;
- inputArea?.Remove(crossButton);
- crossButton?.Dispose();
- crossButton = null;
-
+ DeleteTheCreatePopup();
if (selectPlaylistContentArea != null)
{
List<View> children = selectPlaylistContentArea.Children;
child?.Dispose();
}
}
- if (createPlaylistContentArea != null)
- {
- List<View> children = createPlaylistContentArea.Children;
- while (children.Count > 0)
- {
- View child = children[0];
- createPlaylistContentArea.Remove(child);
- child?.Dispose();
- }
- }
+
createNewPlaylistButton = null;
collectionView = null;
noListText = null;
- inputArea = null;
- underText = null;
selectPlaylistDialog?.Dispose();
selectPlaylistDialog = null;
- createPlaylistDialog?.Dispose();
- createPlaylistDialog = null;
selectPlaylistContentArea = null;
- createPlaylistContentArea = null;
selectPlaylistCancelButton?.Dispose();
selectPlaylistCancelButton = null;
- createPlaylistCancelButton?.Dispose();
- createPlaylistCancelButton = null;
- createPlaylistCreateButton?.Dispose();
- createPlaylistCreateButton = null;
}
base.Dispose(type);
}
- public void DeleteSelectorView()
+ private void DeleteSelectorView()
{
Dispose(DisposeTypes.Explicit);
}
- public CollectionView GetCollectionView()
+
+ private void RemoveTheSelectPopup()
{
- return collectionView;
+ Window.Instance.Remove(selectPlaylistDialog);
}
- public void RemoveTheSelectPopup()
+ private void ShowTheSelectPopup()
{
- Window.Instance.Remove(selectPlaylistDialog);
+ if(selectPlaylistDialog != null)
+ {
+ Window.Instance.Add(selectPlaylistDialog);
+ }
}
- public void RemoveTheCreatePopup()
+ private void RemoveTheCreatePopup()
{
Window.Instance.Remove(createPlaylistDialog);
}