Ver código fonte

Remove playlist and songs from playlist

Kenneth van Ewijk 10 anos atrás
pai
commit
292405cf4d

+ 16 - 0
MusicPlayer/MusicPlayer/Playlist.cs

@@ -71,5 +71,21 @@ namespace MusicPlayer
                 stw.Flush();
             }
         }
+
+        public void Delete()
+        {
+            try {
+                System.IO.File.Move(basedir + name + ".txt", basedir + name + ".txt.old");
+            }
+            catch(Exception)
+            {
+                //Already removed.
+            }
+        }
+
+        internal void RemoveSong(Song s)
+        {
+            this.songs.Remove(s);
+        }
     }
 }

+ 17 - 0
MusicPlayer/MusicPlayer/PlaylistHandler.cs

@@ -20,6 +20,8 @@ namespace MusicPlayer
 
         public void Populate()
         {
+            playlists.Clear();
+
             try {
                 Directory.GetFiles(basedir).ToList().ForEach(f =>
                 {
@@ -63,5 +65,20 @@ namespace MusicPlayer
             }
             return currentPlaylists;
         }
+
+        internal void RemovePlaylistByName(string name)
+        {
+            Playlist toRemove = null;
+            playlists.ForEach(p =>
+            {
+                if (p.name == name) { toRemove = p; }
+            });
+
+            if(toRemove != null)
+            {
+                toRemove.Delete();
+                playlists.Remove(toRemove);
+            }
+        }
     }
 }

+ 31 - 2
MusicPlayer/MusicPlayer/PlaylistMaker.Designer.cs

@@ -43,6 +43,8 @@ namespace MusicPlayer
             this.label4 = new System.Windows.Forms.Label();
             this.FilterTextBox = new System.Windows.Forms.TextBox();
             this.label5 = new System.Windows.Forms.Label();
+            this.DeletePlaylistButton = new System.Windows.Forms.Button();
+            this.DeleteSongsButton = new System.Windows.Forms.Button();
             this.SuspendLayout();
             // 
             // PlaylistSelectBox
@@ -87,6 +89,7 @@ namespace MusicPlayer
             this.PlaylistSongContainer.FormattingEnabled = true;
             this.PlaylistSongContainer.Location = new System.Drawing.Point(10, 319);
             this.PlaylistSongContainer.Name = "PlaylistSongContainer";
+            this.PlaylistSongContainer.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
             this.PlaylistSongContainer.Size = new System.Drawing.Size(306, 82);
             this.PlaylistSongContainer.TabIndex = 3;
             // 
@@ -123,7 +126,7 @@ namespace MusicPlayer
             // label2
             // 
             this.label2.AutoSize = true;
-            this.label2.Location = new System.Drawing.Point(10, 80);
+            this.label2.Location = new System.Drawing.Point(8, 75);
             this.label2.Name = "label2";
             this.label2.Size = new System.Drawing.Size(97, 13);
             this.label2.TabIndex = 7;
@@ -133,7 +136,7 @@ namespace MusicPlayer
             // 
             this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
             this.label3.AutoSize = true;
-            this.label3.Location = new System.Drawing.Point(7, 303);
+            this.label3.Location = new System.Drawing.Point(11, 297);
             this.label3.Name = "label3";
             this.label3.Size = new System.Drawing.Size(82, 13);
             this.label3.TabIndex = 8;
@@ -165,11 +168,35 @@ namespace MusicPlayer
             this.label5.TabIndex = 11;
             this.label5.Text = "Filter";
             // 
+            // DeletePlaylistButton
+            // 
+            this.DeletePlaylistButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
+            this.DeletePlaylistButton.Location = new System.Drawing.Point(225, 70);
+            this.DeletePlaylistButton.Name = "DeletePlaylistButton";
+            this.DeletePlaylistButton.Size = new System.Drawing.Size(92, 23);
+            this.DeletePlaylistButton.TabIndex = 12;
+            this.DeletePlaylistButton.Text = "Delete  Playlist";
+            this.DeletePlaylistButton.UseVisualStyleBackColor = true;
+            this.DeletePlaylistButton.Click += new System.EventHandler(this.DeletePlaylistButton_Click);
+            // 
+            // DeleteSongsButton
+            // 
+            this.DeleteSongsButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
+            this.DeleteSongsButton.Location = new System.Drawing.Point(172, 292);
+            this.DeleteSongsButton.Name = "DeleteSongsButton";
+            this.DeleteSongsButton.Size = new System.Drawing.Size(143, 23);
+            this.DeleteSongsButton.TabIndex = 13;
+            this.DeleteSongsButton.Text = "Delete Selected Songs";
+            this.DeleteSongsButton.UseVisualStyleBackColor = true;
+            this.DeleteSongsButton.Click += new System.EventHandler(this.DeleteSongsButton_Click);
+            // 
             // PlaylistMaker
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.ClientSize = new System.Drawing.Size(334, 411);
+            this.Controls.Add(this.DeleteSongsButton);
+            this.Controls.Add(this.DeletePlaylistButton);
             this.Controls.Add(this.label5);
             this.Controls.Add(this.FilterTextBox);
             this.Controls.Add(this.label4);
@@ -206,5 +233,7 @@ namespace MusicPlayer
         private Label label4;
         private TextBox FilterTextBox;
         private Label label5;
+        private Button DeletePlaylistButton;
+        private Button DeleteSongsButton;
     }
 }

+ 39 - 1
MusicPlayer/MusicPlayer/PlaylistMaker.cs

@@ -47,12 +47,17 @@ namespace MusicPlayer
                 PlaylistSelectBox.SelectedIndex = PlaylistSelectBox.Items.Count - 1;
             else
                 PlaylistSelectBox.SelectedIndex = selection;
+            PlaylistSongContainer.Items.Clear();
             if (PlaylistSelectBox.SelectedItem != null)
             {
-                PlaylistSongContainer.Items.Clear();
                 allPlaylistSongs = pl.GetPlaylistByName(PlaylistSelectBox.SelectedItem.ToString()).GetSongs();
                 allPlaylistSongs.ForEach(s => PlaylistSongContainer.Items.Add(s.Name));
             }
+            else
+            {
+                PlaylistSelectBox.SelectedIndex = -1;
+                PlaylistSelectBox.Text = "";
+            }
         }
 
         public void Repopulate()
@@ -145,5 +150,38 @@ namespace MusicPlayer
                 PlaylistNewButton_Click(sender, new EventArgs());
             }
         }
+
+        private void DeletePlaylistButton_Click(object sender, EventArgs e)
+        {
+            if (PlaylistSelectBox.SelectedItem != null)
+                if (MessageBox.Show("Are you sure to delete " + PlaylistSelectBox.SelectedItem.ToString() + "?", "Confirm Delete!", MessageBoxButtons.YesNo) == DialogResult.Yes)
+                {
+                    pl.RemovePlaylistByName(PlaylistSelectBox.SelectedItem.ToString());
+                    Repopulate(true);
+                }
+        }
+
+        private void DeleteSongsButton_Click(object sender, EventArgs e)
+        {
+            if (PlaylistSelectBox.SelectedItem != null)
+            {
+                Playlist currentPlaylist = pl.GetPlaylistByName(PlaylistSelectBox.SelectedItem.ToString());
+                foreach (string song in PlaylistSongSelector.SelectedItems)
+                {
+                    for (int i = currentPlaylist.GetSongs().Count - 1; i > 0; i--)
+                    {
+                        Song s = currentPlaylist.GetSongs()[i];
+                        if (s.Name == song)
+                        {
+                            currentPlaylist.RemoveSong(s);
+                            break;
+                        }
+                    }
+                }
+                currentPlaylist.WriteToFile();
+            }
+            Thread.Sleep(10);
+            Repopulate();
+        }
     }
 }

+ 24 - 0
MusicPlayer/MusicPlayer/Song.cs

@@ -53,5 +53,29 @@ namespace MusicPlayer
         {
             return $"{this.SongID}|{this.Name}|{this.Album}|{this.Artist}|{this.Genre}|{this.Seconds}";
         }
+
+        public override bool Equals(System.Object obj)
+        {
+            // If parameter is null return false.
+            if (obj == null)
+            {
+                return false;
+            }
+
+            // If parameter cannot be cast to Point return false.
+            Song s = obj as Song;
+            if ((System.Object)s == null)
+            {
+                return false;
+            }
+
+            // Return true if the fields match:
+            return (s.Name == Name) && (s.SongID == SongID);
+        }
+
+        public override int GetHashCode()
+        {
+            return int.Parse(this.SongID);
+        }
     }
 }