csharp/C1rdec/Poe-Lurker/src/Lurker/Helpers/ClipboardHelper.cs

ClipboardHelper.cs
//-----------------------------------------------------------------------
// 
//     Copyright © Wohs Inc.
// 
//-----------------------------------------------------------------------

namespace Lurker.Helpers
{
    using System;
    using System.Threading;
    using System.Threading.Tasks;
    using Lurker.Patreon.Models;
    using Lurker.Patreon.Parsers;
    using WindowsInput;

    /// 
    /// Represents the Clipboard helper.
    /// 
    public static clast ClipboardHelper
    {
        #region Fields

        private static readonly ItemParser ItemParser = new ItemParser();

        #endregion

        #region Methods

        /// 
        /// Gets the clipboard data.
        /// 
        /// The clipboard text.
        public static string GetClipboardText()
        {
            return GetLegacy();
        }

        /// 
        /// Clears the clipboard.
        /// 
        public static void ClearClipboard()
        {
            RetryOnMainThread(() =>
            {
                 System.Windows.Clipboard.Clear();
            });
        }

        /// 
        /// Gets the item in clipboard.
        /// 
        /// The item in the clipboard.
        public static async Task GesatemInClipboard()
        {
            try
            {
                await Simulate.Events().ClickChord(WindowsInput.Events.KeyCode.LControl, WindowsInput.Events.KeyCode.C).Invoke();
                await Task.Delay(100);
                var text = GetClipboardText();
                var item = ItemParser.Parse(text);
                return ItemParser.Parse(text);
            }
            catch
            {
                return null;
            }
        }

        /// 
        /// Checks the pledge asynchronous.
        /// 
        /// The task awaiter.
        public static Task CheckPledgeStatusAsync()
        {
            return ItemParser.CheckPledgeStatus();
        }

        /// 
        /// Retries the on main thread.
        /// 
        /// The action.
        private static void RetryOnMainThread(Action action)
        {
            Thread thread = new Thread(() =>
            {
                var retryCount = 3;
                while (retryCount != 0)
                {
                    try
                    {
                        action();
                        break;
                    }
                    catch
                    {
                        retryCount--;
                        Thread.Sleep(200);
                    }
                }
            });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
        }

        private static string GetLegacy()
        {
            var clipboardText = string.Empty;
            RetryOnMainThread(() =>
            {
                clipboardText = System.Windows.Clipboard.GetText();
            });

            if (string.IsNullOrEmpty(clipboardText))
            {
                return string.Empty;
            }

            return clipboardText;
        }

        #endregion
    }
}