Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.IO;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using Lama.Forms.Source.UI.CustomControls.AudioViews;
- using Lama.Shared.Source.Audio;
- using Lama.Shared.Source.Audio.Metadata.ID3.V2;
- using Lama.Shared.Source.Audio.Metadata.Metadatas;
- using Lama.Shared.Source.Audio.PCM;
- using Lama.Shared.Source.Various;
- using SharpLibrary.Standard.Source.MultiThreading;
- using SharpLibrary.Standard.Source.UI.Font;
- using Xamarin.Forms;
- namespace Lama.Forms.Source.UI.Pages.MainPages
- {
- public class AudioViewCellViewModel : INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged = delegate { };
- private AudioFile _audioFile;
- public AudioFile Audiofile
- {
- get => _audioFile;
- set
- {
- Debug.WriteLine("Setting Audiofile");
- _audioFile = value;
- PropertyChanged(this, new PropertyChangedEventArgs(nameof(Audiofile)));
- PropertyChanged(this, new PropertyChangedEventArgs(nameof(Filename)));
- }
- }
- private readonly Color ColorError = Color.FromRgba(0.7f, 0, 0, 1f);
- private readonly Color ColorNotTagged = Color.Transparent;
- private readonly Color ColorOK = Color.FromRgba(0, 0.7f, 0, 1f);
- private TagState _state = TagState.NotTagged;
- public TagState State
- {
- get => _state;
- set
- {
- _state = value;
- PropertyChanged(this, new PropertyChangedEventArgs(nameof(State)));
- PropertyChanged(this, new PropertyChangedEventArgs(nameof(TagStateColor)));
- }
- }
- public Color TagStateColor
- {
- get
- {
- switch (State)
- {
- case TagState.NotTagged:
- return ColorNotTagged;
- case TagState.Error:
- return ColorError;
- case TagState.OK:
- return ColorOK;
- default:
- return ColorNotTagged;
- }
- }
- }
- public string Filename => Audiofile?.Filename;
- public string Artist => Audiofile?.GetMetadataValue(FrameID.ARTIST);
- public string Title => Audiofile?.GetMetadataValue(FrameID.TITLE);
- public ImageSource Cover
- {
- get
- {
- var pic = (Picture) Audiofile?.GetMetadata(FrameID.PICTURE);
- return pic == null ? null : ImageSource.FromStream(() => new MemoryStream(pic.GetValue()));
- }
- }
- public bool IsExpanded = false;
- public const double HeightExpanded = 50;
- public const double HeightCollapsed = 0;
- public string ExpandCollapeButtonText => (IsExpanded) ? "-" : "+";
- public string PlayButtonIcon => BaseFontAwesome.FAPlay;
- public string StopButtonIcon => BaseFontAwesome.FAStop;
- #region Commands
- public Command ExpandCollapseCommand;
- #endregion
- public StackLayout SlCollapsableLayout;
- public AudioWaveView AwvWaveView;
- public AudioViewCellViewModel(StackLayout sl, AudioWaveView waveView)
- {
- ExpandCollapseCommand = new Command(ExpandCollape);
- SlCollapsableLayout = sl;
- AwvWaveView = waveView;
- }
- private void ExpandCollape()
- {
- void Callback(double input) => SlCollapsableLayout.HeightRequest = input;
- if (IsExpanded == true)
- {
- SlCollapsableLayout.Animate("HideCollapse", Callback, HeightExpanded, HeightCollapsed, 8, 200, Easing.CubicOut);
- IsExpanded = false;
- }
- else
- {
- SlCollapsableLayout.Animate("HideCollapse", Callback, HeightCollapsed, HeightExpanded, 8, 200, Easing.CubicOut);
- IsExpanded = true;
- }
- PropertyChanged(this, new PropertyChangedEventArgs(nameof(ExpandCollapeButtonText)));
- }
- private bool loaded = false;
- //private CancellationToken CancelToken = new CancellationToken(false);
- private CancellationTokenSource TokenSource = new CancellationTokenSource();
- private async Task LoadWaveBytesAsync()
- {
- try
- {
- if (Audiofile == null) return;
- byte[] byteArray = Audiofile.GetAsByteArray();
- ScheduleAction<byte[], PCMData, CancellationToken> a = (Audiofile).DecodeToPCM;
- // Add the method to the the TaskScheduler
- PCMData pcm = await TaskSchedule.RunAsync(a, byteArray, TokenSource.Token);
- // If the pcm could not be loaded, show this to the user
- if (pcm == null)
- {
- AwvWaveView.State = AudioWaveView.LoadingState.Error;
- return;
- }
- // Set the Bitdepth so the WaveView knows the Bounds
- AwvWaveView.BitDepth = pcm.BitDepth;
- // Loading the wave
- AwvWaveView.LoadWaveView(pcm.AudioBytes);
- AwvWaveView.WaveColor = Color.FromRgb(51, 204, 255);
- AwvWaveView.State = AudioWaveView.LoadingState.Success;
- // Free the Memeory
- pcm = null;
- GC.Collect();
- }
- catch (Exception ex)
- {
- Debug.WriteLine(ex.ToString());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement