diff --git a/Mear/Mear/Mear.csproj b/Mear/Mear/Mear.csproj index f1bbd5e..5b40e62 100644 --- a/Mear/Mear/Mear.csproj +++ b/Mear/Mear/Mear.csproj @@ -52,8 +52,4 @@ MSBuild:UpdateDesignTimeXaml - - - - \ No newline at end of file diff --git a/Mear/Mear/Models/Song.cs b/Mear/Mear/Models/Song.cs index 2b6c2cc..49a6cdf 100644 --- a/Mear/Mear/Models/Song.cs +++ b/Mear/Mear/Models/Song.cs @@ -31,5 +31,7 @@ namespace Mear.Models public string Filename { get; set; } [JsonIgnore] public string SongPath { set; get; } + [JsonIgnore] + public bool Downloaded { get; set; } = false; } } diff --git a/Mear/Mear/Playback/MearPlayer.cs b/Mear/Mear/Playback/MearPlayer.cs index 4bf6a31..3c07f0c 100644 --- a/Mear/Mear/Playback/MearPlayer.cs +++ b/Mear/Mear/Playback/MearPlayer.cs @@ -56,9 +56,9 @@ namespace Mear.Playback await CrossMediaManager.Current.Play(tmpFile); song.SongPath = tmpFile; - - return song; } + + return song; } catch (Exception ex) { @@ -67,6 +67,19 @@ 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; + } + } #endregion } } diff --git a/Mear/Mear/Repositories/Database/DBSongRepository.cs b/Mear/Mear/Repositories/Database/DBSongRepository.cs index 088ce53..3095095 100644 --- a/Mear/Mear/Repositories/Database/DBSongRepository.cs +++ b/Mear/Mear/Repositories/Database/DBSongRepository.cs @@ -29,6 +29,21 @@ namespace Mear.Repositories.Database #region Methods + public List RetrieveSongs() + { + try + { + var songs = _Db.Table().ToList(); + + return songs; + } + catch (Exception ex) + { + var msg = ex.Message; + } + + return null; + } public Song RetrieveSong(int id) { try @@ -68,7 +83,14 @@ namespace Mear.Repositories.Database _Db.CreateTable(); } - _Db.Insert(song); + try + { + _Db.Insert(song); + } + catch (Exception ex) + { + var msg = ex.Message; + } } public void UpdateSong(Song song) { diff --git a/Mear/Mear/Repositories/Local/LocalSongRepository.cs b/Mear/Mear/Repositories/Local/LocalSongRepository.cs new file mode 100644 index 0000000..ef5234c --- /dev/null +++ b/Mear/Mear/Repositories/Local/LocalSongRepository.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Text; + +using Mear.Models; +using Mear.Repositories.Database; + +namespace Mear.Repositories.Local +{ + public class LocalSongRepository + { + #region Fields + #endregion + + + #region Properties + #endregion + + + #region Contructors + #endregion + + + #region Methods + public List RetrieveSongs() + { + try + { + var dbSongRepo = new DBSongRepository(); + + var songs = dbSongRepo.RetrieveSongs(); + + if (songs == null) + { + throw new Exception("Unable to retrieve song metadata from the local database"); + } + + return songs; + } + catch (Exception ex) + { + var msg = ex.Message; + } + return null; + } + #endregion + } +} diff --git a/Mear/Mear/Repositories/Remote/RemoteSongRepository.cs b/Mear/Mear/Repositories/Remote/RemoteSongRepository.cs index 977644b..30982ba 100644 --- a/Mear/Mear/Repositories/Remote/RemoteSongRepository.cs +++ b/Mear/Mear/Repositories/Remote/RemoteSongRepository.cs @@ -74,6 +74,11 @@ namespace Mear.Repositories.Remote var response = client.DownloadData(request); } + song.Downloaded = true; + song.SongPath = path; + + var dbSongRepo = new DBSongRepository(); + dbSongRepo.SaveSong(song); } catch (Exception ex) { diff --git a/Mear/Mear/ViewModels/SongViewModel.cs b/Mear/Mear/ViewModels/SongViewModel.cs index 0d5bc97..bd29fb8 100644 --- a/Mear/Mear/ViewModels/SongViewModel.cs +++ b/Mear/Mear/ViewModels/SongViewModel.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using Xamarin.Forms; using Mear.Models; +using Mear.Repositories.Local; using Mear.Repositories.Remote; namespace Mear.ViewModels @@ -50,7 +51,19 @@ namespace Mear.ViewModels public void PopulateSongs() { var songRepo = new RemoteSongRepository(); - var songs = songRepo.RetrieveSongs().OrderBy(s => s.Title).ToList(); + var localSongRepo = new LocalSongRepository(); + var lclSongs = localSongRepo.RetrieveSongs(); + var rmtSongs = songRepo.RetrieveSongs().OrderBy(s => s.Title).ToList(); + var songs = new List(); + if (lclSongs != null) + { + lclSongs = lclSongs.OrderBy(s => s.Title).ToList(); + songs = MergeSongs(lclSongs, rmtSongs); + } + else + { + songs = rmtSongs; + } foreach (var song in songs) { @@ -58,12 +71,44 @@ namespace Mear.ViewModels } } + private List MergeSongs(List local, List remote) + { + try + { + foreach (Song lclSong in local) + { + Song song = remote.Find(s => (s.Title.Equals(lclSong.Title))); + remote.Remove(song); + remote.Add(lclSong); + } + + return remote; + } + catch (Exception ex) + { + var msg = ex.Message; + } + + return null; + } private async Task PopulateSongsAsync() { try { var songRepo = new RemoteSongRepository(); - var songs = songRepo.RetrieveSongs().OrderBy(s => s.Title).ToList(); + var localSongRepo = new LocalSongRepository(); + var lclSongs = localSongRepo.RetrieveSongs(); + var rmtSongs = songRepo.RetrieveSongs().OrderBy(s => s.Title).ToList(); + var songs = new List(); + if (lclSongs != null) + { + lclSongs = lclSongs.OrderBy(s => s.Title).ToList(); + songs = MergeSongs(lclSongs, rmtSongs); + } + else + { + songs = rmtSongs; + } _songItems.Clear(); diff --git a/Mear/Mear/Views/MearPlayerView.xaml b/Mear/Mear/Views/MearPlayerView.xaml index 6b7490d..895ae12 100644 --- a/Mear/Mear/Views/MearPlayerView.xaml +++ b/Mear/Mear/Views/MearPlayerView.xaml @@ -8,10 +8,6 @@ xmlns:vm="clr-namespace:Mear.ViewModels" Title="{Binding Title}"> - - @@ -31,21 +27,6 @@ - @@ -86,9 +67,6 @@ - diff --git a/Mear/Mear/Views/SongView.xaml.cs b/Mear/Mear/Views/SongView.xaml.cs index 09ab616..74b8d67 100644 --- a/Mear/Mear/Views/SongView.xaml.cs +++ b/Mear/Mear/Views/SongView.xaml.cs @@ -49,7 +49,14 @@ namespace Mear.Views try { var song = (Song)SongListView.SelectedItem; - song = await MearPlayer.StreamSongDemoAsync(song); + if (song.Downloaded) + { + await MearPlayer.PlaySong(song); + } + else + { + song = await MearPlayer.StreamSongDemoAsync(song); + } await Navigation.PushModalAsync(new NavigationPage(new MearPlayerView(song))); } catch (Exception ex)