Browse Source

FirstCommit

jimmy.jiang@vinno.com 8 months ago
commit
226b2cdfaa
1 changed files with 121 additions and 0 deletions
  1. 121 0
      Client/Mobile/Vinno.vCloud.Mobile/ViewModel/QrCodeParameter.cs

+ 121 - 0
Client/Mobile/Vinno.vCloud.Mobile/ViewModel/QrCodeParameter.cs

@@ -0,0 +1,121 @@
+using System.Collections.Generic;
+using System.Linq;
+using Vinno.vCloud.Common.Client.Shared.Common.Utilities;
+
+namespace Vinno.vCloud.Mobile.ViewModel
+{
+    public class QrCodeParameterConstant
+    {
+        //Common
+        public const string Prefix = "Vinno:";
+
+        public const string AndSymbol = "&";
+
+        public const string EqualSymbol = "=";
+
+        public const string VCloundTerminalSplit = "://";
+
+        //Parameter
+
+        public const string AccountId = "AccountId";
+
+        public const string AccountName = "AccountName";
+
+        public const string VCloudTerminal = "VCloudTerminal";
+    }
+
+    public class QrCodeParameter
+    {
+        private readonly Dictionary<string, string> _parameters;
+
+        public QrCodeParameter()
+        {
+            _parameters = new Dictionary<string, string>();
+        }
+
+        /// <summary>
+        /// Serialize parameter to string.
+        /// </summary>
+        /// <returns> Vinno : AccountId = "123" & AccountName = "423"</returns>
+        public string SerializeToString()
+        {
+            var result = string.Empty;
+            if (_parameters.Any())
+            {
+                result = _parameters.Aggregate(QrCodeParameterConstant.Prefix,
+                    (current, parameter) =>
+                        current +
+                        $"{parameter.Key}{QrCodeParameterConstant.EqualSymbol}{parameter.Value}{QrCodeParameterConstant.AndSymbol}");
+
+                result = result.TrimEnd(QrCodeParameterConstant.AndSymbol.ToCharArray());
+            }
+
+            return result;
+        }
+
+        /// <summary>
+        /// Add encrypt item
+        /// </summary>
+        /// <param name="key">parameter key.</param>
+        /// <param name="value">parameter value.</param>
+        public void AddEncryptItem(string key, string value)
+        {
+            AddItem(key, EncryptHelper.Encode(value));
+        }
+
+        /// <summary>
+        /// Get encrypt item
+        /// </summary>
+        /// <param name="key">parameter key</param>
+        /// <returns>Decode value.</returns>
+        public string GetDecodeValue(string key)
+        {
+            if (_parameters.ContainsKey(key))
+            {
+                return EncryptHelper.Decode(_parameters[key]);
+            }
+
+            return string.Empty;
+        }
+
+        /// <summary>
+        /// Add parameter item
+        /// </summary>
+        /// <param name="key">Eg:Account id.</param>
+        /// <param name="value">Value</param>
+        private void AddItem(string key, string value)
+        {
+            if (_parameters.ContainsKey(key))
+            {
+                _parameters.Remove(key);
+            }
+
+            _parameters.Add(key, value);
+        }
+
+        /// <summary>
+        /// Deserialize content to parameter
+        /// </summary>
+        /// <param name="content">content of string.</param>
+        /// <returns>Qr code parameter.</returns>
+        public static QrCodeParameter Deserialize(string content)
+        {
+            var qrCodeParameter = new QrCodeParameter();
+            var prefix = QrCodeParameterConstant.Prefix;
+            if (content.StartsWith(prefix))
+            {
+                content = content.TrimStart(prefix.ToCharArray());
+                var items = content.Split(QrCodeParameterConstant.AndSymbol.ToCharArray());
+                foreach (var item in items)
+                {
+                    var legalResults = item.Split(QrCodeParameterConstant.EqualSymbol.ToCharArray(), 2);
+                    var key = legalResults[0];
+                    var value = legalResults[1];
+                    qrCodeParameter.AddItem(key, value);
+                }
+            }
+
+            return qrCodeParameter;
+        }
+    }
+}