|
| 1 | +using System; |
| 2 | +using CodeFramework.iOS.ViewControllers; |
| 3 | +using MonoTouch.UIKit; |
| 4 | +using Cirrious.CrossCore; |
| 5 | +using CodeHub.Core.Services; |
| 6 | +using CodeHub.Core.Utils; |
| 7 | +using MonoTouch.Foundation; |
| 8 | +using System.Net; |
| 9 | +using System.Collections.Specialized; |
| 10 | +using System.IO; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using CodeFramework.Core.Services; |
| 13 | + |
| 14 | +namespace CodeHub.iOS.ViewControllers |
| 15 | +{ |
| 16 | + public class MarkdownComposerViewController : Composer |
| 17 | + { |
| 18 | + private readonly UISegmentedControl _viewSegment; |
| 19 | + private UIWebView _previewView; |
| 20 | + |
| 21 | + public MarkdownComposerViewController() |
| 22 | + { |
| 23 | + _viewSegment = new UISegmentedControl(new [] { "Compose", "Preview" }); |
| 24 | + _viewSegment.SelectedSegment = 0; |
| 25 | + NavigationItem.TitleView = _viewSegment; |
| 26 | + _viewSegment.ValueChanged += SegmentValueChanged; |
| 27 | + |
| 28 | + var buttons = new [] |
| 29 | + { |
| 30 | + CreateAccessoryButton("@", () => TextView.InsertText("@")), |
| 31 | + CreateAccessoryButton("#", () => TextView.InsertText("#")), |
| 32 | + CreateAccessoryButton("*", () => TextView.InsertText("*")), |
| 33 | + CreateAccessoryButton("`", () => TextView.InsertText("`")), |
| 34 | + CreateAccessoryButton("Image", () => { |
| 35 | + var range = TextView.SelectedRange; |
| 36 | + TextView.InsertText("![]()"); |
| 37 | + TextView.SelectedRange = new MonoTouch.Foundation.NSRange(range.Location + 4, 0); |
| 38 | + }), |
| 39 | + CreateAccessoryButton("Upload", () => SelectImage()), |
| 40 | + CreateAccessoryButton("Link", () => { |
| 41 | + var range = TextView.SelectedRange; |
| 42 | + TextView.InsertText("[]()"); |
| 43 | + TextView.SelectedRange = new MonoTouch.Foundation.NSRange(range.Location + 1, 0); |
| 44 | + }), |
| 45 | + CreateAccessoryButton("~", () => TextView.InsertText("~")), |
| 46 | + CreateAccessoryButton("=", () => TextView.InsertText("=")), |
| 47 | + CreateAccessoryButton("_", () => TextView.InsertText("_")), |
| 48 | + }; |
| 49 | + |
| 50 | + SetAccesoryButtons(buttons); |
| 51 | + } |
| 52 | + |
| 53 | + private class ImagePickerDelegate : UINavigationControllerDelegate |
| 54 | + { |
| 55 | + public override void WillShowViewController(UINavigationController navigationController, UIViewController viewController, bool animated) |
| 56 | + { |
| 57 | + UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.LightContent; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private class ImgurModel |
| 62 | + { |
| 63 | + public ImgurDataModel Data { get; set; } |
| 64 | + public bool Success { get; set; } |
| 65 | + |
| 66 | + public class ImgurDataModel |
| 67 | + { |
| 68 | + public string Link { get; set; } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private async void UploadImage(UIImage img) |
| 73 | + { |
| 74 | + var hud = new CodeFramework.iOS.Utils.Hud(null); |
| 75 | + hud.Show("Uploading..."); |
| 76 | + |
| 77 | + try |
| 78 | + { |
| 79 | + var returnData = await Task.Run<byte[]>(() => |
| 80 | + { |
| 81 | + using (var w = new WebClient()) |
| 82 | + { |
| 83 | + var data = img.AsJPEG(); |
| 84 | + byte[] dataBytes = new byte[data.Length]; |
| 85 | + System.Runtime.InteropServices.Marshal.Copy(data.Bytes, dataBytes, 0, Convert.ToInt32(data.Length)); |
| 86 | + |
| 87 | + w.Headers.Set("Authorization", "Client-ID aa5d7d0bc1dffa6"); |
| 88 | + |
| 89 | + var values = new NameValueCollection |
| 90 | + { |
| 91 | + { "image", Convert.ToBase64String(dataBytes) } |
| 92 | + }; |
| 93 | + |
| 94 | + return w.UploadValues("https://api.imgur.com/3/image", values); |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + |
| 99 | + var json = Mvx.Resolve<IJsonSerializationService>(); |
| 100 | + var imgurModel = json.Deserialize<ImgurModel>(System.Text.Encoding.UTF8.GetString(returnData)); |
| 101 | + TextView.InsertText(""); |
| 102 | + } |
| 103 | + catch (Exception e) |
| 104 | + { |
| 105 | + MonoTouch.Utilities.ShowAlert("Error", "Unable to upload image: " + e.Message); |
| 106 | + } |
| 107 | + finally |
| 108 | + { |
| 109 | + hud.Hide(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private void SelectImage() |
| 114 | + { |
| 115 | + var imagePicker = new UIImagePickerController(); |
| 116 | + imagePicker.NavigationControllerDelegate = new ImagePickerDelegate(); |
| 117 | + imagePicker.SourceType = UIImagePickerControllerSourceType.PhotoLibrary; |
| 118 | + imagePicker.MediaTypes = UIImagePickerController.AvailableMediaTypes(UIImagePickerControllerSourceType.PhotoLibrary); |
| 119 | + imagePicker.FinishedPickingMedia += (object sender, UIImagePickerMediaPickedEventArgs e) => |
| 120 | + { |
| 121 | + // determine what was selected, video or image |
| 122 | + bool isImage = false; |
| 123 | + switch(e.Info[UIImagePickerController.MediaType].ToString()) { |
| 124 | + case "public.image": |
| 125 | + Console.WriteLine("Image selected"); |
| 126 | + isImage = true; |
| 127 | + break; |
| 128 | + case "public.video": |
| 129 | + Console.WriteLine("Video selected"); |
| 130 | + break; |
| 131 | + } |
| 132 | + |
| 133 | + // get common info (shared between images and video) |
| 134 | + NSUrl referenceURL = e.Info[new NSString("UIImagePickerControllerReferenceUrl")] as NSUrl; |
| 135 | + if (referenceURL != null) |
| 136 | + Console.WriteLine("Url:"+referenceURL.ToString ()); |
| 137 | + |
| 138 | + // if it was an image, get the other image info |
| 139 | + if(isImage) { |
| 140 | + // get the original image |
| 141 | + UIImage originalImage = e.Info[UIImagePickerController.OriginalImage] as UIImage; |
| 142 | + if(originalImage != null) { |
| 143 | + // do something with the image |
| 144 | + try |
| 145 | + { |
| 146 | + UploadImage(originalImage); |
| 147 | + } |
| 148 | + catch (Exception ex) |
| 149 | + { |
| 150 | + Console.WriteLine("Fudge..."); |
| 151 | + } |
| 152 | + Console.WriteLine ("got the original image"); |
| 153 | + //imageView.Image = originalImage; // display |
| 154 | + } |
| 155 | + } else { // if it's a video |
| 156 | + //TODO: Don't support video... |
| 157 | + } |
| 158 | + |
| 159 | + // dismiss the picker |
| 160 | + imagePicker.DismissViewController(true, null); |
| 161 | + UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.LightContent; |
| 162 | + }; |
| 163 | + |
| 164 | + |
| 165 | + imagePicker.Canceled += (object sender, EventArgs e) => |
| 166 | + { |
| 167 | + imagePicker.DismissViewController(true, null); |
| 168 | + UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.LightContent; |
| 169 | + }; |
| 170 | + |
| 171 | + NavigationController.PresentViewController(imagePicker, true, null); |
| 172 | + } |
| 173 | + |
| 174 | + void SegmentValueChanged (object sender, EventArgs e) |
| 175 | + { |
| 176 | + if (_viewSegment.SelectedSegment == 0) |
| 177 | + { |
| 178 | + if (_previewView != null) |
| 179 | + { |
| 180 | + _previewView.RemoveFromSuperview(); |
| 181 | + _previewView.Dispose(); |
| 182 | + _previewView = null; |
| 183 | + } |
| 184 | + |
| 185 | + Add(TextView); |
| 186 | + TextView.BecomeFirstResponder(); |
| 187 | + } |
| 188 | + else |
| 189 | + { |
| 190 | + if (_previewView == null) |
| 191 | + _previewView = new UIWebView(this.View.Bounds); |
| 192 | + |
| 193 | + TextView.RemoveFromSuperview(); |
| 194 | + Add(_previewView); |
| 195 | + |
| 196 | + var markdownService = Mvx.Resolve<IMarkdownService>(); |
| 197 | + var path = MarkdownHtmlGenerator.CreateFile(markdownService.Convert(Text)); |
| 198 | + var uri = Uri.EscapeUriString("file://" + path) + "#" + Environment.TickCount; |
| 199 | + _previewView.LoadRequest(new MonoTouch.Foundation.NSUrlRequest(new MonoTouch.Foundation.NSUrl(uri))); |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | +} |
| 204 | + |
0 commit comments