This commit is contained in:
@@ -1,12 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
|
||||
using SQLite;
|
||||
|
||||
namespace Mear.Models
|
||||
{
|
||||
[DataContract]
|
||||
[Table("PlayCount")]
|
||||
public class PlayCount
|
||||
{
|
||||
public int? Id { get; set; }
|
||||
[PrimaryKey, Column("Id"), AutoIncrement]
|
||||
public int Id { get; set; }
|
||||
public int? PlayCounter { get; set; }
|
||||
public int? SongId { get; set; }
|
||||
}
|
||||
|
||||
@@ -56,6 +56,30 @@ namespace Mear.Playback
|
||||
|
||||
await CrossMediaManager.Current.Play(tmpFile);
|
||||
song.SongPath = tmpFile;
|
||||
|
||||
for (var atmpt = 0; !CrossMediaManager.Current.IsPlaying(); atmpt++)
|
||||
{
|
||||
if (atmpt == 5)
|
||||
{
|
||||
break;
|
||||
}
|
||||
await Task.Delay(500);
|
||||
}
|
||||
|
||||
if (CrossMediaManager.Current.IsPlaying())
|
||||
{
|
||||
var plyCountRepo = new DBPlayCountRepository();
|
||||
var plyCount = plyCountRepo.RetrievePlayCount(song.Id);
|
||||
|
||||
if (plyCount == null)
|
||||
{
|
||||
plyCountRepo.SavePlayCount(song);
|
||||
}
|
||||
else
|
||||
{
|
||||
plyCountRepo.UpdatePlayCount(song);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return song;
|
||||
@@ -74,6 +98,21 @@ namespace Mear.Playback
|
||||
var songPath = song.SongPath;
|
||||
|
||||
await CrossMediaManager.Current.Play(songPath);
|
||||
|
||||
if (CrossMediaManager.Current.IsPlaying())
|
||||
{
|
||||
var plyCountRepo = new DBPlayCountRepository();
|
||||
var plyCount = plyCountRepo.RetrievePlayCount(song.Id);
|
||||
|
||||
if (plyCount == null)
|
||||
{
|
||||
plyCountRepo.SavePlayCount(song);
|
||||
}
|
||||
else
|
||||
{
|
||||
plyCountRepo.UpdatePlayCount(song);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
using Mear.Models;
|
||||
|
||||
namespace Mear.Repositories.Database
|
||||
{
|
||||
public class DBPlayCountRepository : DBRepository
|
||||
{
|
||||
#region Fields
|
||||
#endregion
|
||||
|
||||
|
||||
#region Properties
|
||||
#endregion
|
||||
|
||||
|
||||
#region Constructors
|
||||
public DBPlayCountRepository()
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region Methods
|
||||
public PlayCount RetrievePlayCount(int songId)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (DoesTableExist("PlayCount"))
|
||||
{
|
||||
var playCount = _Db.Table<PlayCount>()
|
||||
.Where(p => p.SongId==songId).First();
|
||||
|
||||
if (playCount != null)
|
||||
{
|
||||
return playCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var msg = ex.Message;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void SavePlayCount(Song song)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!DoesTableExist("PlayCount"))
|
||||
{
|
||||
_Db.CreateTable<PlayCount>();
|
||||
}
|
||||
var playCount = new PlayCount
|
||||
{
|
||||
PlayCounter = 1,
|
||||
SongId = song.Id
|
||||
};
|
||||
|
||||
_Db.Insert(playCount);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var msg = ex.Message;
|
||||
}
|
||||
}
|
||||
public void UpdatePlayCount(Song song)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (DoesTableExist("PlayCount"))
|
||||
{
|
||||
var songId = song.Id;
|
||||
var plyCount = RetrievePlayCount(songId);
|
||||
if (plyCount != null)
|
||||
{
|
||||
plyCount.PlayCounter += 1;
|
||||
|
||||
_Db.Update(plyCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var msg = ex.Message;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -69,5 +69,6 @@
|
||||
</StackLayout>
|
||||
<ContentPage.ToolbarItems>
|
||||
<ToolbarItem Name="Download" Order="Secondary" Priority="1" Clicked="Download_Clicked" />
|
||||
<ToolbarItem Name="PlayCount" Order="Secondary" Priority="2" Clicked="PlayCount_Clicked" />
|
||||
</ContentPage.ToolbarItems>
|
||||
</ContentPage>
|
||||
@@ -11,6 +11,7 @@ using Xamarin.Forms;
|
||||
using Xamarin.Forms.Xaml;
|
||||
|
||||
using Mear.Models;
|
||||
using Mear.Repositories.Database;
|
||||
using Mear.Repositories.Remote;
|
||||
using Mear.Utilities;
|
||||
using Mear.ViewModels;
|
||||
@@ -163,9 +164,19 @@ namespace Mear.Views
|
||||
var songRepo = new RemoteSongRepository();
|
||||
songRepo.DownloadSong(_song);
|
||||
}
|
||||
#endregion
|
||||
private async void PlayCount_Clicked(object sender, EventArgs e)
|
||||
{
|
||||
var playCountRepo = new DBPlayCountRepository();
|
||||
var plyCount = playCountRepo.RetrievePlayCount(_song.Id);
|
||||
|
||||
if (plyCount == null)
|
||||
{
|
||||
await DisplayAlert("Play Count", $"Song has not been played", "Ok");
|
||||
return;
|
||||
}
|
||||
await DisplayAlert("PlayCount", $"Song has been played {plyCount.PlayCounter} times", "Ok");
|
||||
}
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,7 @@ namespace Mear.Views
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region Methods
|
||||
#region Events
|
||||
private async void SongListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
|
||||
@@ -87,7 +88,6 @@ namespace Mear.Views
|
||||
var songs = songRepo.RetrieveSongs();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user