using System; using System.ComponentModel; using System.IO; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media.Imaging; using System.Windows.Threading; using System.Xml.Serialization; using Microsoft.Win32; namespace MyApp { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { ProgramData data = new ProgramData(); string fileName = ""; int currentlyPlaying = 0; bool progresBarDragging = false; EditWindow? editWindow = null; public DispatcherTimer timer = new DispatcherTimer(); public MainWindow() { InitializeComponent(); this.DataContext = this; PlayListView.ItemsSource = data.PlayList; timer.Interval = TimeSpan.FromMinutes(1); timer.Tick += AutoUpdate; } private void AutoUpdate(object? sender, EventArgs e) { Save(); } void Save() { if (fileName == "") { SaveFileDialog dialog = new SaveFileDialog(); dialog.FileName = "Save"; dialog.DefaultExt = "*.xml"; dialog.Filter = "Data|*.xml"; dialog.RestoreDirectory = true; if (dialog.ShowDialog() == true) { fileName = dialog.FileName; } } if (fileName != "") { using (StreamWriter sw = new StreamWriter(fileName)) { XmlSerializer xml = new XmlSerializer(typeof(ProgramData)); xml.Serialize(sw, data); } } } void Load() { if (fileName == "") return; using (StreamReader sr = new StreamReader(fileName)) { XmlSerializer xml = new XmlSerializer(typeof(ProgramData)); if (xml.Deserialize(sr) is ProgramData m) { data = m; PlayListView.ItemsSource = data.PlayList; if(data.AutoSave) timer.Start(); else timer.Stop(); } } } void Window_Closing(object sender, CancelEventArgs e) { if(fileName == "") { MessageBoxResult result = MessageBox.Show("Data was not saved.\n Do you want to save it?", "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning); if (result == MessageBoxResult.Yes) { //e.Cancel = true; Save(); } } else { Save(); } } private void PlayButton_Click(object sender, RoutedEventArgs e) { if (!MediaPlayer.IsPlaying()) { PlayPouseImg.Source = new BitmapImage(new Uri("pack://application:,,,/assets/pause_icon.png")); MediaPlayer.Play(); } else { PlayPouseImg.Source = new BitmapImage(new Uri("pack://application:,,,/assets/play_icon.png")); MediaPlayer.Pause(); } } private void Stop_Click(object sender, RoutedEventArgs e) { MediaPlayer.Stop(); ProgresBar.Value = 0; CurrentTime.Content = TimeSpan.FromSeconds(0).ToString(@"hh\:mm\:ss"); PlayPouseImg.Source = new BitmapImage(new Uri("pack://application:,,,/assets/play_icon.png")); } private void Rewind_Click(object sender, RoutedEventArgs e) { data.PlayList[currentlyPlaying].IsPlaying = false; currentlyPlaying--; if (currentlyPlaying < 0) { currentlyPlaying = 0; } MediaPlayer.ChangeContent(data.PlayList[currentlyPlaying].FilePath); data.PlayList[currentlyPlaying].IsPlaying = true; } private void Forward_Click(object sender, RoutedEventArgs e) { data.PlayList[currentlyPlaying].IsPlaying = false; currentlyPlaying++; if (currentlyPlaying >= data.PlayList.Count) { currentlyPlaying = data.PlayList.Count - 1; } MediaPlayer.ChangeContent(data.PlayList[currentlyPlaying].FilePath); data.PlayList[currentlyPlaying].IsPlaying = true; } private void ProgresBar_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e) { progresBarDragging = false; MediaPlayer.ChangeTime(ProgresBar.Value); } private void ProgresBar_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e) { progresBarDragging = true; } private void PlayListView_MouseDoubleClick(object sender, MouseButtonEventArgs e) { if (PlayListView.SelectedItem is Media m) { data.PlayList[currentlyPlaying].IsPlaying = false; m.IsPlaying = true; MediaPlayer.ChangeContent(m.FilePath); MediaPlayer.Play(); PlayPouseImg.Source = new BitmapImage(new Uri("pack://application:,,,/assets/pause_icon.png")); currentlyPlaying = PlayListView.SelectedIndex; } } private void PlayListView_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (PlayListView.SelectedItem is Media m) { data.SelectedItem = m; if (editWindow != null) { if (data.SelectedItem == null) { editWindow.Close(); } else { editWindow.DataContext = data.SelectedItem; } } } } private void MenuItem_Exit_Click(object sender, RoutedEventArgs e) { Application.Current.Shutdown(); } private void MenuItem_Add_Click(object sender, RoutedEventArgs e) { var dialog = new OpenFileDialog(); //dialog.FileName = "Document"; // Default file name dialog.DefaultExt = "*.*"; // Default file extension dialog.Filter = "All files|*.*|Video media|*.mkv;*.wmv;*.mp4"; // Filter files by extension dialog.Multiselect = true; bool? result = dialog.ShowDialog(); if (result == true) { string[] files = dialog.FileNames; foreach(string file in files) { string filename = file.Split("\\").Last(); data.PlayList.Add(new Media(filename, file)); } } } private void MenuItem_Remove_Click(object sender, RoutedEventArgs e) { if(PlayListView.SelectedItem is Media m) { if(editWindow != null) { editWindow.Close(); } data.PlayList.Remove(m); } else { MessageBox.Show("Nothing was selected"); } } private void MenuItem_Settings_Click(object sender, RoutedEventArgs e) { SettingsWindow? settingsWindow = new SettingsWindow(data); settingsWindow.Owner = this; settingsWindow.ShowDialog(); } private void MenuItem_Save_Click(object sender, RoutedEventArgs e) { Save(); } private void MenuItem_Edit_Click(object sender, RoutedEventArgs e) { if(!(PlayListView.SelectedItem is Media)) return; if(editWindow == null) { editWindow = new EditWindow(data); editWindow.Owner = this; editWindow.Show(); } if (!editWindow.IsLoaded) { editWindow = new EditWindow(data); editWindow.Owner = this; editWindow.Show(); } } private void MenuItem_Load_Click(object sender, RoutedEventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); dialog.FileName = "Save"; // Default file name dialog.DefaultExt = "*.xml"; // Default file extension dialog.Filter = "Data|*.xml"; // Filter files by extension bool? result = dialog.ShowDialog(); if (result == true) { fileName = dialog.FileName; Load(); } } private void MediaPlayer_OnTimeChage(object sender, double time) { if (!progresBarDragging) { ProgresBar.Value = time; CurrentTime.Content = TimeSpan.FromSeconds(time).ToString(@"hh\:mm\:ss"); } } private void MediaPlayer_OnMediaLoad(object sender, bool loded, double totalTimeInMilS) { if (loded) { ProgresBar.Minimum = 0; ProgresBar.Maximum = totalTimeInMilS; TotalTime.Content = TimeSpan.FromSeconds(totalTimeInMilS).ToString(@"hh\:mm\:ss"); } else { PlayPouseImg.Source = new BitmapImage(new Uri("pack://application:,,,/assets/play_icon.png")); } } } }