csharp/alexdillon/GroupMeClientAvalonia/GroupMeClientAvalonia/ViewModels/Controls/MultiLikeControlViewModel.cs

MultiLikeControlViewModel.cs
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using GroupMeClientApi.Models;

namespace GroupMeClientAvalonia.ViewModels.Controls
{
    /// 
    ///  provides a ViewModel for the  control.
    /// 
    public clast MultiLikeControlViewModel : GalaSoft.MvvmLight.ViewModelBase, IDisposable
    {
        private bool isEnabled;

        /// 
        /// Initializes a new instance of the  clast.
        /// 
        /// The Group to perform MultiLike operations on.
        public MultiLikeControlViewModel(GroupContentsControlViewModel groupContentsControlViewModel)
        {
            this.GroupContentsControlViewModel = groupContentsControlViewModel;

            this.EnableMultiLikeCommand = new RelayCommand(this.EnableMultiLike);
            this.DisableMultiLikeCommand = new RelayCommand(this.DisableMultiLike);
            this.PerformMultiLikeCommand = new RelayCommand(async () => await this.DoMultiLike(), true);
        }

        /// 
        /// Gets or sets the command to be performed to enable MultiLike.
        /// 
        public ICommand EnableMultiLikeCommand { get; set; }

        /// 
        /// Gets or sets the command to be performed to enable MultiLike.
        /// 
        public ICommand DisableMultiLikeCommand { get; set; }

        /// 
        /// Gets or sets the command to be performed to enable MultiLike.
        /// 
        public ICommand PerformMultiLikeCommand { get; set; }

        /// 
        /// Gets or sets a value indicating whether MultiLiking mode is enabled.
        /// 
        public bool IsEnabled
        {
            get => this.isEnabled;

            set
            {
                this.Set(() => this.IsEnabled, ref this.isEnabled, value);
                this.RaisePropertyChanged(nameof(this.IsDisabled));
            }
        }

        /// 
        /// Gets a value indicating whether MultiLiking mode is diabled.
        /// 
        public bool IsDisabled => !this.IsEnabled;

        /// 
        /// Gets a value indicating whether any messages are currently selected for liking.
        /// 
        public bool AnyMessagesSelected
        {
            get
            {
                var range = this.GroupContentsControlViewModel.CurrentlySelectedMessages;
                if (range == null)
                {
                    return false;
                }

                try
                {
                    var itemList = (range as ObservableCollection).Cast().ToList();

                    var oldestId = itemList.Min(m => long.Parse(m.Id));
                    var newestId = itemList.Max(m => long.Parse(m.Id));

                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        private GroupContentsControlViewModel GroupContentsControlViewModel { get; }

        private TimeSpan LikeDelay { get; } = TimeSpan.FromMilliseconds(20);

        /// 
        void IDisposable.Dispose()
        {
        }

        private void EnableMultiLike()
        {
            this.IsEnabled = true;
            this.GroupContentsControlViewModel.IsSelectionAllowed = true;

            this.GroupContentsControlViewModel.PopupManager.PopupDialog = null;
        }

        private void DisableMultiLike()
        {
            this.IsEnabled = false;
            this.GroupContentsControlViewModel.IsSelectionAllowed = false;

            var itemList = this.GroupContentsControlViewModel.CurrentlySelectedMessages as ObservableCollection;
            itemList?.Clear();

            this.GroupContentsControlViewModel.PopupManager.PopupDialog = null;
        }

        private async Task DoMultiLike()
        {
            var range = this.GroupContentsControlViewModel.CurrentlySelectedMessages;
            if (range == null)
            {
                return;
            }

            var itemList = (range as ObservableCollection).Cast().ToList();

            var oldestId = itemList.Min(m => long.Parse(m.Id));
            var newestId = itemList.Max(m => long.Parse(m.Id));

            var loadingControl = new LoadingControlViewModel();
            this.GroupContentsControlViewModel.PopupManager.PopupDialog = loadingControl;

            foreach (var message in this.GroupContentsControlViewModel.SortedMessages)
            {
                var id = long.Parse(message.Id);
                if (id >= oldestId && id  "Multi-Like";

            /// 
            public override string PluginVersion => Thisastembly.SimpleVersion;

            /// 
            public string PluginName => this.PluginDisplayName;

            private GroupContentsControlViewModel GroupContentsControlViewModel { get; }

            private MultiLikeControlViewModel MultiLikeControlViewModel { get; }

            /// 
            public Task Activated(IMessageContainer groupOrChat)
            {
                this.GroupContentsControlViewModel.PopupManager.PopupDialog = this.MultiLikeControlViewModel;
                return Task.CompletedTask;
            }
        }
    }
}