diff --git a/Mear/Mear/Models/PlayCount.cs b/Mear/Mear/Models/PlayCount.cs index cac448c..896495d 100644 --- a/Mear/Mear/Models/PlayCount.cs +++ b/Mear/Mear/Models/PlayCount.cs @@ -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; } } diff --git a/Mear/Mear/Playback/MearPlayer.cs b/Mear/Mear/Playback/MearPlayer.cs index 3c07f0c..b14f402 100644 --- a/Mear/Mear/Playback/MearPlayer.cs +++ b/Mear/Mear/Playback/MearPlayer.cs @@ -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) { diff --git a/Mear/Mear/Repositories/Database/DBPlayCountRepository.cs b/Mear/Mear/Repositories/Database/DBPlayCountRepository.cs new file mode 100644 index 0000000..9bca506 --- /dev/null +++ b/Mear/Mear/Repositories/Database/DBPlayCountRepository.cs @@ -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() + .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(); + } + 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 + } +} diff --git a/Mear/Mear/Views/MearPlayerView.xaml b/Mear/Mear/Views/MearPlayerView.xaml index 895ae12..07433cd 100644 --- a/Mear/Mear/Views/MearPlayerView.xaml +++ b/Mear/Mear/Views/MearPlayerView.xaml @@ -69,5 +69,6 @@ - + + \ No newline at end of file diff --git a/Mear/Mear/Views/MearPlayerView.xaml.cs b/Mear/Mear/Views/MearPlayerView.xaml.cs index eb2c026..2bc854a 100644 --- a/Mear/Mear/Views/MearPlayerView.xaml.cs +++ b/Mear/Mear/Views/MearPlayerView.xaml.cs @@ -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); - #endregion - - } + 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 + } } \ No newline at end of file diff --git a/Mear/Mear/Views/SongView.xaml.cs b/Mear/Mear/Views/SongView.xaml.cs index 74b8d67..8bd6502 100644 --- a/Mear/Mear/Views/SongView.xaml.cs +++ b/Mear/Mear/Views/SongView.xaml.cs @@ -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 } } \ No newline at end of file