Implemented Repeat functionality and started working on the Settings and the color scheme. #23, #35, #14, #5

This commit is contained in:
amazing-username
2019-06-08 08:45:24 -07:00
parent fc03d6e15b
commit 7747a87184
9 changed files with 314 additions and 53 deletions
+3
View File
@@ -48,6 +48,9 @@
<EmbeddedResource Update="Views\RegisterPage.xaml"> <EmbeddedResource Update="Views\RegisterPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator> <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource> </EmbeddedResource>
<EmbeddedResource Update="Views\SettingsPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Update="Views\SongView.xaml"> <EmbeddedResource Update="Views\SongView.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator> <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource> </EmbeddedResource>
+130 -22
View File
@@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaManager; using MediaManager;
@@ -22,6 +23,7 @@ namespace Mear.Playback
{ {
#region Fields #region Fields
private static Song _song; private static Song _song;
private static bool? _initialized = null;
#endregion #endregion
@@ -72,27 +74,16 @@ namespace Mear.Playback
return null; 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<Song> ControlMusic(Song song, PlayControls control) public static async Task<Song> ControlMusic(Song song, PlayControls control)
{ {
_song = song;
switch (control) switch (control)
{ {
case PlayControls.PLAYOFFLINE: case PlayControls.PLAYOFFLINE:
var songPath = song.SongPath; var songPath = song.SongPath;
await CrossMediaManager.Current.Play(songPath); await PlaySong(songPath);
var plyCountRepo = new DBPlayCountRepository();
plyCountRepo.AffectPlayCount(song);
InitializeRepeatMode(); InitializeRepeatMode();
break; break;
case PlayControls.PAUSE: case PlayControls.PAUSE:
@@ -122,23 +113,97 @@ namespace Mear.Playback
return null; return null;
} }
public static async Task<string> 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<double?> 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() public static string RetrieveRepeatString()
{ {
var ctrlRepeatMode = CrossMediaManager.Current.RepeatMode; var ctrlRepo = new DBMusicControlsRepository();
var ctrlRepeatMode = ctrlRepo.IsRepeatOn();
switch (ctrlRepeatMode) switch (ctrlRepeatMode)
{ {
case MediaManager.Playback.RepeatMode.Off: case Repeat.OFF:
return "RepOff"; return "RepOff";
case MediaManager.Playback.RepeatMode.One: case Repeat.ONE:
return "RepOn"; return "RepOn";
case MediaManager.Playback.RepeatMode.All: case Repeat.ALL:
return "RepAll"; return "RepAll";
} }
return string.Empty; 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) private static Song StreamSong(Song song)
{ {
string tmpFile = Path.GetTempPath() + "track.mp3"; string tmpFile = Path.GetTempPath() + "track.mp3";
@@ -196,19 +261,62 @@ namespace Mear.Playback
private static async Task PlaySong(string songPath) private static async Task PlaySong(string songPath)
{ {
await CrossMediaManager.Current.Play(songPath); await CrossMediaManager.Current.Play(songPath);
if (_initialized == null)
{
CrossMediaManager.Current.MediaItemFinished += Current_MediaItemFinished;
//BackgroundWork();
_initialized = true;
} }
}
private static void ToggleRepeat() private static void ToggleRepeat()
{ {
var musicCtrl = new DBMusicControlsRepository(); var musicCtrl = new DBMusicControlsRepository();
musicCtrl.UpdateRepeat(); musicCtrl.UpdateRepeat();
var repeatMode = (Repeat)musicCtrl.IsRepeatOn(); var repeatMode = (Repeat)musicCtrl.IsRepeatOn();
for (var rpt = RepeatUtility.RetrieveRepeatMode(repeatMode);
CrossMediaManager.Current.RepeatMode != rpt;)
{
CrossMediaManager.Current.RepeatMode = RepeatUtility.RetrieveRepeatMode(repeatMode); CrossMediaManager.Current.RepeatMode = RepeatUtility.RetrieveRepeatMode(repeatMode);
} }
#region Background
private static async Task BackgroundWork()
{
try
{
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
} }
} }
+63
View File
@@ -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<SwitchItem> _switchItems;
#endregion
#region Properties
public ObservableCollection<SwitchItem> SwitchItems
{
get => _switchItems;
set => _switchItems = value;
}
#endregion
#region Constructors
public SettingsViewModel()
{
_switchItems = new ObservableCollection<SwitchItem>();
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
}
}
-1
View File
@@ -74,7 +74,6 @@ namespace Mear.Views
AccessToken = loginRes.Token, AccessToken = loginRes.Token,
UserId = loginRes.UserId UserId = loginRes.UserId
}); });
await DisplayAlert("Icarus Login", "Successfully logged in", "Ok");
App.Current.MainPage = new MusicLibrary(); App.Current.MainPage = new MusicLibrary();
} }
else else
+29 -16
View File
@@ -51,6 +51,7 @@ namespace Mear.Views
BackgroundSongElasping(); BackgroundSongElasping();
BackgroundSongCoverUpdate(); BackgroundSongCoverUpdate();
//BackgroundControlInit();
InitializeOptions(); InitializeOptions();
@@ -101,7 +102,6 @@ namespace Mear.Views
var controlRepo = new DBMusicControlsRepository(); var controlRepo = new DBMusicControlsRepository();
var shuffleOn = controlRepo.IsShuffleOn(); var shuffleOn = controlRepo.IsShuffleOn();
var repeatMode = (RepeatMode)controlRepo.IsRepeatOn();
Shuffle.Text = (shuffleOn) ? "ShfOn" : "ShfOff"; Shuffle.Text = (shuffleOn) ? "ShfOn" : "ShfOff";
Repeat.Text = MearPlayer.RetrieveRepeatString(); Repeat.Text = MearPlayer.RetrieveRepeatString();
@@ -118,15 +118,12 @@ namespace Mear.Views
{ {
while (true) while (true)
{ {
Device.BeginInvokeOnMainThread(() => Device.BeginInvokeOnMainThread(async () =>
{ {
var ttlSec = (int)CrossMediaManager.Current.Position.TotalSeconds; var curPos = await MearPlayer.ConvertToTime();
var cnvrt = new TimeFormat();
var curPos = cnvrt.ConvertToSongTime(ttlSec);
StartTime.Text = $"{curPos}"; StartTime.Text = $"{curPos}";
double progVal = ((double)ttlSec) / ((double)_song.Duration); double? progVal = await MearPlayer.ProgressValue();
progVal *= 100; SongProgress.Value = progVal.Value;
SongProgress.Value = progVal;
}); });
await Task.Delay(500); await Task.Delay(500);
@@ -144,7 +141,8 @@ namespace Mear.Views
{ {
new Thread(async () => new Thread(async () =>
{ {
while (true) // Will work on this later so I am setting it to false
while (false)
{ {
Device.BeginInvokeOnMainThread(() => Device.BeginInvokeOnMainThread(() =>
{ {
@@ -164,6 +162,26 @@ namespace Mear.Views
var msg = ex.Message; 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 #endregion
#region Events #region Events
@@ -188,11 +206,7 @@ namespace Mear.Views
} }
private void Repeat_Clicked(object sender, EventArgs e) private void Repeat_Clicked(object sender, EventArgs e)
{ {
Task.Run(async () => MearPlayer.ControlMusic(_song, PlayControls.REPEAT);
{
await MearPlayer.ControlMusic(_song, PlayControls.REPEAT);
}).Wait();
Repeat.Text = MearPlayer.RetrieveRepeatString(); Repeat.Text = MearPlayer.RetrieveRepeatString();
} }
private void Shuffle_Clicked(object sender, EventArgs e) private void Shuffle_Clicked(object sender, EventArgs e)
@@ -208,8 +222,7 @@ namespace Mear.Views
private async void SongProgress_DragCompleted(object sender, EventArgs e) private async void SongProgress_DragCompleted(object sender, EventArgs e)
{ {
var progVal = SongProgress.Value; var progVal = SongProgress.Value;
double newPos = (progVal / 100) * ((double)_song.Duration); await MearPlayer.SeekTo(progVal);
await CrossMediaManager.Current.SeekTo(TimeSpan.FromSeconds(newPos));
} }
private void Download_Clicked(object sender, EventArgs e) private void Download_Clicked(object sender, EventArgs e)
+1
View File
@@ -39,6 +39,7 @@ namespace Mear.Views
} }
private void Settings_Clicked(object sender, EventArgs e) private void Settings_Clicked(object sender, EventArgs e)
{ {
Navigation.PushModalAsync(new NavigationPage(new SettingsPage()));
} }
#endregion #endregion
+45
View File
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="Mear.Views.SettingsPage"
xmlns:vm="clr-namespace:Mear.ViewModels"
Title="Settings" >
<ContentPage.Content>
<StackLayout>
<ListView x:Name="Sliders"
ItemsSource="{Binding SwitchItems}"
HasUnevenRows="False"
RowHeight="75" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid MinimumHeightRequest="80" Padding="5">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackLayout Grid.Row="0" Grid.Column="0"
Orientation="Horizontal">
<Label Text="{Binding Title}"
d:Text="{Binding .}"
LineBreakMode="NoWrap"
FontSize="14" />
<Switch x:Name="{Binding Title}"
IsToggled="{Binding On}"
Toggled="Switch_Toggled" />
</StackLayout>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
+40
View File
@@ -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
}
}
+1 -12
View File
@@ -60,20 +60,9 @@ namespace Mear.Views
else else
{ {
song = await MearPlayer.ControlMusic(song, PlayControls.STREAM); song = await MearPlayer.ControlMusic(song, PlayControls.STREAM);
var plyCountRepo = new DBPlayCountRepository();
plyCountRepo.AffectPlayCount(song);
} }
while (true) Navigation.PushModalAsync(new NavigationPage(new MearPlayerView(song)));
{
var buffering = CrossMediaManager.Current.IsBuffering();
if (!buffering)
{
var yup = 0;
break;
}
}
await Navigation.PushModalAsync(new NavigationPage(new MearPlayerView(song)));
} }
catch (Exception ex) catch (Exception ex)
{ {