From 7747a87184e90a63177855b38811a923239a37f4 Mon Sep 17 00:00:00 2001 From: amazing-username Date: Sat, 8 Jun 2019 08:45:24 -0700 Subject: [PATCH] Implemented Repeat functionality and started working on the Settings and the color scheme. #23, #35, #14, #5 --- Mear/Mear/Mear.csproj | 3 + Mear/Mear/Playback/MearPlayer.cs | 156 ++++++++++++++++++---- Mear/Mear/ViewModels/SettingsViewModel.cs | 63 +++++++++ Mear/Mear/Views/LoginPage.xaml.cs | 1 - Mear/Mear/Views/MearPlayerView.xaml.cs | 45 ++++--- Mear/Mear/Views/MusicLibrary.xaml.cs | 1 + Mear/Mear/Views/SettingsPage.xaml | 45 +++++++ Mear/Mear/Views/SettingsPage.xaml.cs | 40 ++++++ Mear/Mear/Views/SongView.xaml.cs | 13 +- 9 files changed, 314 insertions(+), 53 deletions(-) create mode 100644 Mear/Mear/ViewModels/SettingsViewModel.cs create mode 100644 Mear/Mear/Views/SettingsPage.xaml create mode 100644 Mear/Mear/Views/SettingsPage.xaml.cs diff --git a/Mear/Mear/Mear.csproj b/Mear/Mear/Mear.csproj index 5b40e62..29bbc79 100644 --- a/Mear/Mear/Mear.csproj +++ b/Mear/Mear/Mear.csproj @@ -48,6 +48,9 @@ MSBuild:UpdateDesignTimeXaml + + MSBuild:UpdateDesignTimeXaml + MSBuild:UpdateDesignTimeXaml diff --git a/Mear/Mear/Playback/MearPlayer.cs b/Mear/Mear/Playback/MearPlayer.cs index a694b3a..f719d2e 100644 --- a/Mear/Mear/Playback/MearPlayer.cs +++ b/Mear/Mear/Playback/MearPlayer.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Text; +using System.Threading; using System.Threading.Tasks; using MediaManager; @@ -22,6 +23,7 @@ namespace Mear.Playback { #region Fields private static Song _song; + private static bool? _initialized = null; #endregion @@ -72,27 +74,16 @@ namespace Mear.Playback return null; } - public static async Task PlaySong(Song song) - { - try - { - var songPath = song.SongPath; - - await CrossMediaManager.Current.Play(songPath); - } - catch (Exception ex) - { - var msg = ex.Message; - } - } public static async Task ControlMusic(Song song, PlayControls control) { - + _song = song; switch (control) { case PlayControls.PLAYOFFLINE: var songPath = song.SongPath; - await CrossMediaManager.Current.Play(songPath); + await PlaySong(songPath); + var plyCountRepo = new DBPlayCountRepository(); + plyCountRepo.AffectPlayCount(song); InitializeRepeatMode(); break; case PlayControls.PAUSE: @@ -122,23 +113,97 @@ namespace Mear.Playback return null; } + public static async Task ConvertToTime() + { + try + { + var ttlSec = (int)CrossMediaManager.Current.Position.TotalSeconds; + var cnvrt = new TimeFormat(); + var curPos = cnvrt.ConvertToSongTime(ttlSec); + + return curPos; + } + catch (Exception ex) + { + var msg = ex.Message; + } + + return string.Empty; + } + + public static async Task ProgressValue() + { + try + { + var totalSeconds = (int)CrossMediaManager.Current.Position.TotalSeconds; + double progVal = ((double)totalSeconds) / ((double)_song.Duration) * 100; + + return progVal; + } + catch (Exception ex) + { + var msg = ex.Message; + } + + return null; + } + + public static async Task PlaySong(Song song) + { + try + { + var songPath = song.SongPath; + + await CrossMediaManager.Current.Play(songPath); + } + catch (Exception ex) + { + var msg = ex.Message; + } + } + public static async Task SeekTo(double songProress) + { + double newPosition = (songProress / 100) * ((double)_song.Duration); + await CrossMediaManager.Current.SeekTo(TimeSpan.FromSeconds(newPosition)); + } + public static string RetrieveRepeatString() { - var ctrlRepeatMode = CrossMediaManager.Current.RepeatMode; + var ctrlRepo = new DBMusicControlsRepository(); + var ctrlRepeatMode = ctrlRepo.IsRepeatOn(); switch (ctrlRepeatMode) { - case MediaManager.Playback.RepeatMode.Off: + case Repeat.OFF: return "RepOff"; - case MediaManager.Playback.RepeatMode.One: + case Repeat.ONE: return "RepOn"; - case MediaManager.Playback.RepeatMode.All: + case Repeat.ALL: return "RepAll"; } return string.Empty; } + public static bool IsPlaying() + { + return CrossMediaManager.Current.IsPlaying(); + } + public static bool RepeatMatchedDatabase() + { + var playerRepeatMode = CrossMediaManager.Current.RepeatMode; + var controlRepo = new DBMusicControlsRepository(); + var repeatMode = controlRepo.IsRepeatOn(); + var repeatModeConverted = RepeatUtility.RetrieveRepeatMode(repeatMode); + + return (playerRepeatMode == repeatModeConverted); + } + + public static void UpdateRepeatControls() + { + InitializeRepeatMode(); + } + private static Song StreamSong(Song song) { string tmpFile = Path.GetTempPath() + "track.mp3"; @@ -196,19 +261,62 @@ namespace Mear.Playback private static async Task PlaySong(string songPath) { await CrossMediaManager.Current.Play(songPath); + if (_initialized == null) + { + CrossMediaManager.Current.MediaItemFinished += Current_MediaItemFinished; + //BackgroundWork(); + _initialized = true; + } } + private static void ToggleRepeat() { var musicCtrl = new DBMusicControlsRepository(); musicCtrl.UpdateRepeat(); var repeatMode = (Repeat)musicCtrl.IsRepeatOn(); - for (var rpt = RepeatUtility.RetrieveRepeatMode(repeatMode); - CrossMediaManager.Current.RepeatMode != rpt;) + CrossMediaManager.Current.RepeatMode = RepeatUtility.RetrieveRepeatMode(repeatMode); + } + + #region Background + private static async Task BackgroundWork() + { + try { - CrossMediaManager.Current.RepeatMode = RepeatUtility.RetrieveRepeatMode(repeatMode); + new Thread(async () => + { + + }); + } + catch (Exception ex) + { + var msg = ex.Message; } } - #endregion - } + #endregion + + #region Events + private static void Current_MediaItemFinished(object sender, MediaManager.Media.MediaItemEventArgs e) + { + var ctrlRepo = new DBMusicControlsRepository(); + var repeatMode = ctrlRepo.IsRepeatOn(); + if (CrossMediaManager.Current.IsPlaying()) + { + //return; + } + switch(repeatMode) + { + case Repeat.ONE: + CrossMediaManager.Current.SeekToStart(); + //PlaySong(_song.SongPath); + break; + case Repeat.ALL: + // Will implment this fully later once Queues are a feature + PlaySong(_song.SongPath); + break; + } + } + #endregion + #endregion + } } diff --git a/Mear/Mear/ViewModels/SettingsViewModel.cs b/Mear/Mear/ViewModels/SettingsViewModel.cs new file mode 100644 index 0000000..bc9ba7e --- /dev/null +++ b/Mear/Mear/ViewModels/SettingsViewModel.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Text; + +using Xamarin.Forms; + +namespace Mear.ViewModels +{ + public class SettingsViewModel : BaseViewModel + { + #region Fields + private ObservableCollection _switchItems; + #endregion + + + #region Properties + public ObservableCollection SwitchItems + { + get => _switchItems; + set => _switchItems = value; + } + #endregion + + + #region Constructors + public SettingsViewModel() + { + _switchItems = new ObservableCollection(); + + PopulateSwitch(); + } + #endregion + + + #region Methods + private SwitchItem RetrieveColorSchemeSwitch() + { + var colorScheme = new SwitchItem + { + Title = "Dark Theme", + IsOn = true + }; + + return colorScheme; + } + private void PopulateSwitch() + { + var colorScheme = RetrieveColorSchemeSwitch(); + _switchItems.Add(colorScheme); + } + #endregion + + + #region Classes + public class SwitchItem + { + public string Title { get; set; } + public bool IsOn { get; set; } + } + #endregion + } +} diff --git a/Mear/Mear/Views/LoginPage.xaml.cs b/Mear/Mear/Views/LoginPage.xaml.cs index d644be9..5421ae9 100644 --- a/Mear/Mear/Views/LoginPage.xaml.cs +++ b/Mear/Mear/Views/LoginPage.xaml.cs @@ -74,7 +74,6 @@ namespace Mear.Views AccessToken = loginRes.Token, UserId = loginRes.UserId }); - await DisplayAlert("Icarus Login", "Successfully logged in", "Ok"); App.Current.MainPage = new MusicLibrary(); } else diff --git a/Mear/Mear/Views/MearPlayerView.xaml.cs b/Mear/Mear/Views/MearPlayerView.xaml.cs index 20d1e4c..e5c38d1 100644 --- a/Mear/Mear/Views/MearPlayerView.xaml.cs +++ b/Mear/Mear/Views/MearPlayerView.xaml.cs @@ -51,6 +51,7 @@ namespace Mear.Views BackgroundSongElasping(); BackgroundSongCoverUpdate(); + //BackgroundControlInit(); InitializeOptions(); @@ -101,7 +102,6 @@ namespace Mear.Views var controlRepo = new DBMusicControlsRepository(); var shuffleOn = controlRepo.IsShuffleOn(); - var repeatMode = (RepeatMode)controlRepo.IsRepeatOn(); Shuffle.Text = (shuffleOn) ? "ShfOn" : "ShfOff"; Repeat.Text = MearPlayer.RetrieveRepeatString(); @@ -118,15 +118,12 @@ namespace Mear.Views { while (true) { - Device.BeginInvokeOnMainThread(() => + Device.BeginInvokeOnMainThread(async () => { - var ttlSec = (int)CrossMediaManager.Current.Position.TotalSeconds; - var cnvrt = new TimeFormat(); - var curPos = cnvrt.ConvertToSongTime(ttlSec); + var curPos = await MearPlayer.ConvertToTime(); StartTime.Text = $"{curPos}"; - double progVal = ((double)ttlSec) / ((double)_song.Duration); - progVal *= 100; - SongProgress.Value = progVal; + double? progVal = await MearPlayer.ProgressValue(); + SongProgress.Value = progVal.Value; }); await Task.Delay(500); @@ -144,7 +141,8 @@ namespace Mear.Views { new Thread(async () => { - while (true) + // Will work on this later so I am setting it to false + while (false) { Device.BeginInvokeOnMainThread(() => { @@ -164,6 +162,26 @@ namespace Mear.Views var msg = ex.Message; } } + // Addresses issue where the Player's controls have not been properly initialized + private async Task BackgroundControlInit() + { + try + { + new Thread(async () => + { + while (!MearPlayer.RepeatMatchedDatabase()) + { + MearPlayer.UpdateRepeatControls(); + Repeat.Text = MearPlayer.RetrieveRepeatString(); + await Task.Delay(750); + } + }).Start(); + } + catch (Exception ex) + { + var msg = ex.Message; + } + } #endregion #region Events @@ -188,11 +206,7 @@ namespace Mear.Views } private void Repeat_Clicked(object sender, EventArgs e) { - Task.Run(async () => - { - await MearPlayer.ControlMusic(_song, PlayControls.REPEAT); - }).Wait(); - + MearPlayer.ControlMusic(_song, PlayControls.REPEAT); Repeat.Text = MearPlayer.RetrieveRepeatString(); } private void Shuffle_Clicked(object sender, EventArgs e) @@ -208,8 +222,7 @@ namespace Mear.Views private async void SongProgress_DragCompleted(object sender, EventArgs e) { var progVal = SongProgress.Value; - double newPos = (progVal / 100) * ((double)_song.Duration); - await CrossMediaManager.Current.SeekTo(TimeSpan.FromSeconds(newPos)); + await MearPlayer.SeekTo(progVal); } private void Download_Clicked(object sender, EventArgs e) diff --git a/Mear/Mear/Views/MusicLibrary.xaml.cs b/Mear/Mear/Views/MusicLibrary.xaml.cs index 4e409ca..3ff2190 100644 --- a/Mear/Mear/Views/MusicLibrary.xaml.cs +++ b/Mear/Mear/Views/MusicLibrary.xaml.cs @@ -39,6 +39,7 @@ namespace Mear.Views } private void Settings_Clicked(object sender, EventArgs e) { + Navigation.PushModalAsync(new NavigationPage(new SettingsPage())); } #endregion diff --git a/Mear/Mear/Views/SettingsPage.xaml b/Mear/Mear/Views/SettingsPage.xaml new file mode 100644 index 0000000..0046896 --- /dev/null +++ b/Mear/Mear/Views/SettingsPage.xaml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Mear/Mear/Views/SettingsPage.xaml.cs b/Mear/Mear/Views/SettingsPage.xaml.cs new file mode 100644 index 0000000..f1c69a5 --- /dev/null +++ b/Mear/Mear/Views/SettingsPage.xaml.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Xamarin.Forms; +using Xamarin.Forms.Xaml; + +using Mear.ViewModels; + +namespace Mear.Views +{ + [XamlCompilation(XamlCompilationOptions.Compile)] + public partial class SettingsPage : ContentPage + { + #region Fields + private SettingsViewModel _viewModel; + #endregion + + + #region Constructors + public SettingsPage() + { + InitializeComponent(); + + BindingContext = _viewModel = new SettingsViewModel(); + } + #endregion + + + #region Methods + #region Events + private void Switch_Toggled(object sender, ToggledEventArgs e) + { + } + #endregion + #endregion + } +} \ No newline at end of file diff --git a/Mear/Mear/Views/SongView.xaml.cs b/Mear/Mear/Views/SongView.xaml.cs index c3df4f4..9634105 100644 --- a/Mear/Mear/Views/SongView.xaml.cs +++ b/Mear/Mear/Views/SongView.xaml.cs @@ -60,20 +60,9 @@ namespace Mear.Views else { song = await MearPlayer.ControlMusic(song, PlayControls.STREAM); - var plyCountRepo = new DBPlayCountRepository(); - plyCountRepo.AffectPlayCount(song); } - while (true) - { - var buffering = CrossMediaManager.Current.IsBuffering(); - if (!buffering) - { - var yup = 0; - break; - } - } - await Navigation.PushModalAsync(new NavigationPage(new MearPlayerView(song))); + Navigation.PushModalAsync(new NavigationPage(new MearPlayerView(song))); } catch (Exception ex) {