Advertisement
Guest User

Untitled

a guest
Mar 6th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XML 5.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Lama.Forms.Source.UI.CustomControls.AudioViews;
  10. using Lama.Shared.Source.Audio;
  11. using Lama.Shared.Source.Audio.Metadata.ID3.V2;
  12. using Lama.Shared.Source.Audio.Metadata.Metadatas;
  13. using Lama.Shared.Source.Audio.PCM;
  14. using Lama.Shared.Source.Various;
  15. using SharpLibrary.Standard.Source.MultiThreading;
  16. using SharpLibrary.Standard.Source.UI.Font;
  17. using Xamarin.Forms;
  18.  
  19. namespace Lama.Forms.Source.UI.Pages.MainPages
  20. {
  21.     public class AudioViewCellViewModel : INotifyPropertyChanged
  22.     {
  23.         public event PropertyChangedEventHandler PropertyChanged = delegate { };
  24.  
  25.         private AudioFile _audioFile;
  26.         public AudioFile Audiofile
  27.         {
  28.             get => _audioFile;
  29.             set
  30.             {
  31.                 Debug.WriteLine("Setting Audiofile");
  32.                 _audioFile = value;
  33.                 PropertyChanged(this, new PropertyChangedEventArgs(nameof(Audiofile)));
  34.                 PropertyChanged(this, new PropertyChangedEventArgs(nameof(Filename)));
  35.             }
  36.         }
  37.  
  38.         private readonly Color ColorError = Color.FromRgba(0.7f, 0, 0, 1f);
  39.         private readonly Color ColorNotTagged = Color.Transparent;
  40.         private readonly Color ColorOK = Color.FromRgba(0, 0.7f, 0, 1f);
  41.  
  42.         private TagState _state = TagState.NotTagged;
  43.  
  44.         public TagState State
  45.         {
  46.             get => _state;
  47.             set
  48.             {
  49.                 _state = value;
  50.                 PropertyChanged(this, new PropertyChangedEventArgs(nameof(State)));
  51.                 PropertyChanged(this, new PropertyChangedEventArgs(nameof(TagStateColor)));
  52.             }
  53.         }
  54.  
  55.         public Color TagStateColor
  56.         {
  57.             get
  58.             {
  59.                 switch (State)
  60.                 {
  61.                     case TagState.NotTagged:
  62.                         return ColorNotTagged;
  63.                     case TagState.Error:
  64.                         return ColorError;
  65.                     case TagState.OK:
  66.                         return ColorOK;
  67.                     default:
  68.                         return ColorNotTagged;
  69.                 }
  70.             }
  71.         }
  72.  
  73.         public string Filename => Audiofile?.Filename;
  74.  
  75.         public string Artist => Audiofile?.GetMetadataValue(FrameID.ARTIST);
  76.  
  77.         public string Title => Audiofile?.GetMetadataValue(FrameID.TITLE);
  78.  
  79.         public ImageSource Cover
  80.         {
  81.             get
  82.             {
  83.                 var pic = (Picture) Audiofile?.GetMetadata(FrameID.PICTURE);
  84.                 return pic == null ? null : ImageSource.FromStream(() => new MemoryStream(pic.GetValue()));
  85.             }
  86.         }
  87.  
  88.         public bool IsExpanded = false;
  89.  
  90.         public const double HeightExpanded = 50;
  91.         public const double HeightCollapsed = 0;
  92.  
  93.         public string ExpandCollapeButtonText => (IsExpanded) ? "-" : "+";
  94.  
  95.         public string PlayButtonIcon => BaseFontAwesome.FAPlay;
  96.  
  97.         public string StopButtonIcon => BaseFontAwesome.FAStop;
  98.  
  99.         #region Commands
  100.         public Command ExpandCollapseCommand;
  101.         #endregion
  102.  
  103.         public StackLayout SlCollapsableLayout;
  104.  
  105.         public AudioWaveView AwvWaveView;
  106.  
  107.         public AudioViewCellViewModel(StackLayout sl, AudioWaveView waveView)
  108.         {
  109.             ExpandCollapseCommand = new Command(ExpandCollape);
  110.  
  111.             SlCollapsableLayout = sl;
  112.             AwvWaveView = waveView;
  113.         }
  114.  
  115.         private void ExpandCollape()
  116.         {
  117.             void Callback(double input) => SlCollapsableLayout.HeightRequest = input;
  118.  
  119.             if (IsExpanded == true)
  120.             {
  121.                 SlCollapsableLayout.Animate("HideCollapse", Callback, HeightExpanded, HeightCollapsed, 8, 200, Easing.CubicOut);
  122.                 IsExpanded = false;
  123.             }
  124.             else
  125.             {
  126.                 SlCollapsableLayout.Animate("HideCollapse", Callback, HeightCollapsed, HeightExpanded, 8, 200, Easing.CubicOut);
  127.                 IsExpanded = true;
  128.             }
  129.  
  130.             PropertyChanged(this, new PropertyChangedEventArgs(nameof(ExpandCollapeButtonText)));
  131.         }
  132.  
  133.         private bool loaded = false;
  134.  
  135.         //private CancellationToken CancelToken = new CancellationToken(false);
  136.         private CancellationTokenSource TokenSource = new CancellationTokenSource();
  137.  
  138.         private async Task LoadWaveBytesAsync()
  139.         {
  140.             try
  141.             {
  142.                 if (Audiofile == null) return;
  143.  
  144.                 byte[] byteArray = Audiofile.GetAsByteArray();
  145.  
  146.                 ScheduleAction<byte[], PCMData, CancellationToken> a = (Audiofile).DecodeToPCM;
  147.  
  148.                 // Add the method to the the TaskScheduler
  149.                 PCMData pcm = await TaskSchedule.RunAsync(a, byteArray, TokenSource.Token);
  150.  
  151.                 // If the pcm could not be loaded, show this to the user
  152.                 if (pcm == null)
  153.                 {
  154.                     AwvWaveView.State = AudioWaveView.LoadingState.Error;
  155.                     return;
  156.                 }
  157.  
  158.                 // Set the Bitdepth so the WaveView knows the Bounds
  159.                 AwvWaveView.BitDepth = pcm.BitDepth;
  160.  
  161.                 // Loading the wave
  162.                 AwvWaveView.LoadWaveView(pcm.AudioBytes);
  163.  
  164.                 AwvWaveView.WaveColor = Color.FromRgb(51, 204, 255);
  165.                 AwvWaveView.State = AudioWaveView.LoadingState.Success;
  166.  
  167.                 // Free the Memeory
  168.                 pcm = null;
  169.                 GC.Collect();
  170.             }
  171.             catch (Exception ex)
  172.             {
  173.                 Debug.WriteLine(ex.ToString());
  174.             }
  175.         }
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement