PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/monotouch/PubnubMessagingExample/Pubnub_MessagingMain.cs

https://github.com/Sfyler/c-sharp
C# | 206 lines | 165 code | 21 blank | 20 comment | 10 complexity | 73f5f1ba5ae9ca0383eb7970069502e5 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MonoTouch.Foundation;
  5. using MonoTouch.UIKit;
  6. using MonoTouch.Dialog;
  7. using PubNubMessaging.Core;
  8. using System.Drawing;
  9. namespace PubnubMessagingExample
  10. {
  11. public partial class Pubnub_MessagingMain : DialogViewController
  12. {
  13. PubnubProxy proxy = null;
  14. Pubnub pubnub = null;
  15. public override void ViewDidAppear (bool animated)
  16. {
  17. AppDelegate.navigation.ToolbarHidden = true;
  18. base.ViewDidAppear (animated);
  19. }
  20. public Pubnub_MessagingMain () : base (UITableViewStyle.Grouped, null)
  21. {
  22. UIView labelView = new UIView(new RectangleF (0, 0, this.View.Bounds.Width, 24));
  23. int left = 20;
  24. string hardwareVer = DeviceHardware.Version.ToString ().ToLower ();
  25. if (hardwareVer.IndexOf ("ipad") >= 0) {
  26. left = 55;
  27. }
  28. labelView.AddSubview(new UILabel (new RectangleF (left, 10, this.View.Bounds.Width - left, 24)){
  29. Font = UIFont.BoldSystemFontOfSize(16),
  30. BackgroundColor = UIColor.Clear,
  31. TextColor = UIColor.FromRGB(76, 86, 108),
  32. Text = "Basic Settings"
  33. });
  34. var headerMultipleChannels = new UILabel (new RectangleF (0, 0, this.View.Bounds.Width, 24)){
  35. Font = UIFont.SystemFontOfSize(12),
  36. TextColor = UIColor.Brown,
  37. BackgroundColor = UIColor.Clear,
  38. TextAlignment = UITextAlignment.Center
  39. };
  40. headerMultipleChannels.Text = "Enter multiple channel names separated by comma";
  41. EntryElement entryChannelName = new EntryElement("Channel(s)", "Enter Channel Name", "");
  42. entryChannelName.AutocapitalizationType = UITextAutocapitalizationType.None;
  43. entryChannelName.AutocorrectionType = UITextAutocorrectionType.No;
  44. EntryElement entryCipher = new EntryElement("Cipher", "Enter Cipher", "");
  45. entryCipher.AutocapitalizationType = UITextAutocapitalizationType.None;
  46. entryCipher.AutocorrectionType = UITextAutocorrectionType.No;
  47. EntryElement entryProxyServer = new EntryElement("Server", "Enter Server", "");
  48. entryProxyServer.AutocapitalizationType = UITextAutocapitalizationType.None;
  49. entryProxyServer.AutocorrectionType = UITextAutocorrectionType.No;
  50. EntryElement entryProxyPort = new EntryElement("Port", "Enter Port", "");
  51. EntryElement entryProxyUser = new EntryElement("Username", "Enter Username", "");
  52. entryProxyUser.AutocapitalizationType = UITextAutocapitalizationType.None;
  53. entryProxyUser.AutocorrectionType = UITextAutocorrectionType.No;
  54. EntryElement entryProxyPassword = new EntryElement("Password", "Enter Password", "", true);
  55. EntryElement entryCustonUuid = new EntryElement("CustomUuid", "Enter Custom UUID", "");
  56. entryCustonUuid.AutocapitalizationType = UITextAutocapitalizationType.None;
  57. entryCustonUuid.AutocorrectionType = UITextAutocorrectionType.No;
  58. BooleanElement proxyEnabled = new BooleanElement ("Proxy", false);
  59. BooleanElement sslEnabled = new BooleanElement ("Enable SSL", false);
  60. Root = new RootElement ("Pubnub Messaging") {
  61. new Section(labelView)
  62. {
  63. },
  64. new Section(headerMultipleChannels)
  65. {
  66. },
  67. new Section ()
  68. {
  69. entryChannelName,
  70. sslEnabled
  71. },
  72. new Section ("Enter cipher key for encryption. Leave blank for unencrypted transfer.")
  73. {
  74. entryCipher
  75. },
  76. new Section ("Enter custom UUID or leave blank to use the default UUID")
  77. {
  78. entryCustonUuid
  79. },
  80. new Section()
  81. {
  82. new RootElement ("Proxy Settings", 0, 0){
  83. new Section (){
  84. proxyEnabled
  85. },
  86. new Section ("Configuration"){
  87. entryProxyServer,
  88. entryProxyPort,
  89. entryProxyUser,
  90. entryProxyPassword
  91. },
  92. }
  93. },
  94. new Section()
  95. {
  96. new StyledStringElement ("Launch Example", () => {
  97. bool errorFree = true;
  98. errorFree = ValidateAndInitPubnub(entryChannelName.Value, entryCipher.Value, sslEnabled.Value,
  99. entryCustonUuid.Value, proxyEnabled.Value, entryProxyPort.Value,
  100. entryProxyUser.Value, entryProxyServer.Value, entryProxyPassword.Value
  101. );
  102. if(errorFree)
  103. {
  104. new Pubnub_MessagingSub(entryChannelName.Value, entryCipher.Value, sslEnabled.Value, pubnub);
  105. }
  106. })
  107. {
  108. BackgroundColor = UIColor.Blue,
  109. TextColor = UIColor.White,
  110. Alignment = UITextAlignment.Center
  111. },
  112. },
  113. /*new Section()
  114. {
  115. new StyledStringElement ("Launch Speed Test", () => {
  116. bool errorFree = true;
  117. errorFree = ValidateAndInitPubnub(entryChannelName.Value, entryCipher.Value, sslEnabled.Value,
  118. entryCustonUuid.Value, proxyEnabled.Value, entryProxyPort.Value,
  119. entryProxyUser.Value, entryProxyServer.Value, entryProxyPassword.Value
  120. );
  121. if(errorFree)
  122. {
  123. new Pubnub_MessagingSpeedTest(entryChannelName.Value, entryCipher.Value, sslEnabled.Value, pubnub);
  124. }
  125. })
  126. {
  127. BackgroundColor = UIColor.Blue,
  128. TextColor = UIColor.White,
  129. Alignment = UITextAlignment.Center
  130. },
  131. }*/
  132. };
  133. }
  134. bool ValidateAndInitPubnub (string channelName, string cipher, bool ssl,
  135. string customUuid, bool proxyEnabled, string proxyPort,
  136. string proxyUser, string proxyServer, string proxyPass
  137. )
  138. {
  139. bool errorFree = true;
  140. if(String.IsNullOrWhiteSpace (channelName))
  141. {
  142. errorFree = false;
  143. new UIAlertView ("Error!", "Please enter a channel name", null, "OK").Show ();
  144. }
  145. if(errorFree)
  146. {
  147. pubnub = new Pubnub ("demo-36", "demo-36", "demo-36", cipher, ssl);
  148. if(!String.IsNullOrWhiteSpace (customUuid.Trim()))
  149. {
  150. pubnub.SessionUUID = customUuid.Trim();
  151. }
  152. }
  153. if ((errorFree) && (proxyEnabled))
  154. {
  155. int port;
  156. if(Int32.TryParse(proxyPort, out port) && ((port >= 1) && (port <= 65535)))
  157. {
  158. proxy = new PubnubProxy();
  159. proxy.ProxyServer = proxyServer;
  160. proxy.ProxyPort = port;
  161. proxy.ProxyUserName = proxyUser;
  162. proxy.ProxyPassword = proxyPass;
  163. try
  164. {
  165. pubnub.Proxy = proxy;
  166. }
  167. catch (MissingFieldException mse)
  168. {
  169. errorFree = false;
  170. proxyEnabled = false;
  171. Console.WriteLine(mse.Message);
  172. new UIAlertView ("Error!", "Proxy settings invalid, please re-enter the details.", null, "OK").Show ();
  173. }
  174. }
  175. else
  176. {
  177. errorFree = false;
  178. new UIAlertView ("Error!", "Proxy port must be a valid integer between 1 to 65535", null, "OK").Show ();
  179. }
  180. }
  181. return errorFree;
  182. }
  183. }
  184. }