diff --git a/Mear/Mear/Playback/MearPlayer.cs b/Mear/Mear/Playback/MearPlayer.cs index be40a58..4bf6a31 100644 --- a/Mear/Mear/Playback/MearPlayer.cs +++ b/Mear/Mear/Playback/MearPlayer.cs @@ -30,7 +30,7 @@ namespace Mear.Playback #region Methods - public static async Task StreamSongDemoAsync(Song song) + public static async Task StreamSongDemoAsync(Song song) { string tmpFile = Path.GetTempPath() + "track.mp3"; @@ -55,14 +55,17 @@ namespace Mear.Playback var response = client.DownloadData(request); await CrossMediaManager.Current.Play(tmpFile); - var title = CrossMediaManager.Current.MediaQueue.Current.Title; - var ttl = CrossMediaManager.Current.MediaQueue.Title; + song.SongPath = tmpFile; + + return song; } } 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 6cff226..977644b 100644 --- a/Mear/Mear/Repositories/Remote/RemoteSongRepository.cs +++ b/Mear/Mear/Repositories/Remote/RemoteSongRepository.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Text; using Newtonsoft.Json; @@ -8,6 +9,7 @@ using RestSharp; using Mear.Constants.API; using Mear.Models; using Mear.Repositories.Database; +using Mear.Utilities; namespace Mear.Repositories.Remote { @@ -51,6 +53,33 @@ namespace Mear.Repositories.Remote return null; } + public void DownloadSong(Song song) + { + try + { + var dirMgr = new DirectoryManager(song); + var path = dirMgr.CreateSongPath(song); + using (var writer = File.OpenWrite(path)) + { + var client = new RestClient(API.ApiUrl); + var apiEndpoint = $@"api/{API.APIVersion}/song/data/{song.Id}"; ; + var request = new RestRequest(apiEndpoint, Method.GET); + + DBTokenRepository tkRepo = new DBTokenRepository(); + var token = tkRepo.RetrieveToken(); + request.AddHeader("Authorization", $"Bearer {token.AccessToken}"); + + request.ResponseWriter = (responseStream) => + responseStream.CopyTo(writer); + + var response = client.DownloadData(request); + } + } + catch (Exception ex) + { + var msg = ex.Message; + } + } #endregion } } diff --git a/Mear/Mear/Utilities/DirectoryManager.cs b/Mear/Mear/Utilities/DirectoryManager.cs new file mode 100644 index 0000000..fe48484 --- /dev/null +++ b/Mear/Mear/Utilities/DirectoryManager.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +using Mear.Models; + +namespace Mear.Utilities +{ + public class DirectoryManager + { + #region Fields + private Song _song; + private string _path; + #endregion + + + #region Properties + #endregion + + + #region Constructors + public DirectoryManager(Song song) + { + _song = song; + } + #endregion + + + #region Methods + public string CreateSongPath(Song song) + { + try + { + var artist = song.Artist; + var album = song.Album; + var title = song.Title; + + if (!ArtistDirectoryExist(song)) + { + Directory.CreateDirectory(ArtistPath(song)); + } + if (!AlbumDirectoryExist(song)) + { + Directory.CreateDirectory(AlbumPath(song)); + } + if (SongPathExist(song)) + { + // TODO: Song exists + return SongPath(song); + } + else + { + return SongPath(song); + } + } + catch (Exception ex) + { + var msg = ex.Message; + } + + return string.Empty; + } + + private string RootPath(Song song) + { + var path = $@"{Environment.GetFolderPath(Environment.SpecialFolder.Personal)}/"; + + return path; + } + private string ArtistPath(Song song) + { + var path = RootPath(song); + path += $@"{song.Artist}/"; + + return path; + } + private string AlbumPath(Song song) + { + var path = ArtistPath(song); + path += $@"{song.Album}/"; + + return path; + } + private string SongPath(Song song) + { + var path = AlbumPath(song); + path += $@"{song.Filename}"; + + return path; + } + + private bool ArtistDirectoryExist(Song song) + { + var path = RootPath(song); + path += song.Artist; + + return Directory.Exists(path); + } + private bool AlbumDirectoryExist(Song song) + { + var path = AlbumPath(song); + + return Directory.Exists(path); + } + private bool SongPathExist(Song song) + { + var path = SongPath(song); + + return File.Exists(path); + } + + private void Initialize() + { + _path = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"/"; + } + #endregion + } +} diff --git a/Mear/Mear/Utilities/SongMetadataRetriever.cs b/Mear/Mear/Utilities/SongMetadataRetriever.cs index 3ee9808..b437330 100644 --- a/Mear/Mear/Utilities/SongMetadataRetriever.cs +++ b/Mear/Mear/Utilities/SongMetadataRetriever.cs @@ -1,11 +1,14 @@ using System; using System.Collections.Generic; +using System.IO; using System.Text; using TagLib; using Mear.Models; +using IOFile = System.IO.File; + namespace Mear.Utilities { public class SongMetadataRetriever @@ -35,6 +38,37 @@ namespace Mear.Utilities return null; } + public Stream ExtractCovertArtStream(Song song) + { + var strm = new FileStream(song.SongPath, FileMode.Open); + + try + { + } + catch (Exception ex) + { + var msg = ex.Message; + } + + return strm; + } + public string ExtractCoverArtData(Song song) + { + var imgData = string.Empty; + try + { + var d = IOFile.ReadAllBytes(song.SongPath); + /** + */ + + } + catch (Exception ex) + { + var msg = ex.Message; + } + + return imgData; + } #endregion } } diff --git a/Mear/Mear/Utilities/TimeFormat.cs b/Mear/Mear/Utilities/TimeFormat.cs new file mode 100644 index 0000000..f756893 --- /dev/null +++ b/Mear/Mear/Utilities/TimeFormat.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Mear.Utilities +{ + public class TimeFormat + { + #region Fields + #endregion + + + #region Properties + #endregion + + + #region Constructors + #endregion + + + #region Methods + public string ConvertToSongTime(int seconds) + { + var curTime = string.Empty; + var dur = seconds; + var min = TimeSpan.FromSeconds((double) dur).Minutes; + var remainingSec = dur % 60; + + if (remainingSec < 10) + { + curTime = $"{min}:0{remainingSec}"; + } + else + { + curTime = $"{min}:{remainingSec}"; + } + + return curTime; + } + #endregion + } +} diff --git a/Mear/Mear/ViewModels/MearPlayerViewModel.cs b/Mear/Mear/ViewModels/MearPlayerViewModel.cs new file mode 100644 index 0000000..4165602 --- /dev/null +++ b/Mear/Mear/ViewModels/MearPlayerViewModel.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Xamarin.Forms; + +using Mear.Models; + +namespace Mear.ViewModels +{ + public class MearPlayerViewModel : BaseViewModel + { + #region Fields + private ObservableCollection _song; + #endregion + + + #region Properties + public ObservableCollection SongItem + { + get => _song; + } + #endregion + + + #region Constructors + public MearPlayerViewModel() + { + } + public MearPlayerViewModel(Song song) + { + _song = new ObservableCollection(); + _song.Clear(); + _song.Add(song); + } + #endregion + + + #region Methods + #endregion + } +} diff --git a/Mear/Mear/Views/MearPlayerView.xaml b/Mear/Mear/Views/MearPlayerView.xaml index bcbe6db..6b7490d 100644 --- a/Mear/Mear/Views/MearPlayerView.xaml +++ b/Mear/Mear/Views/MearPlayerView.xaml @@ -4,67 +4,92 @@ 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.MearPlayerView" > + x:Class="Mear.Views.MearPlayerView" + xmlns:vm="clr-namespace:Mear.ViewModels" + Title="{Binding Title}"> + + + + + + + Source="" /> + + + + + + + + + + + +