-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRootViewController.cs
187 lines (152 loc) · 5.58 KB
/
RootViewController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Drawing;
using System.Collections.Generic;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage;
using System.IO;
using Microsoft.WindowsAzure.Storage.Blob;
using System.Threading.Tasks;
namespace AzureBlobUploader
{
public partial class RootViewController : UITableViewController
{
DataSource dataSource;
public UIImagePickerController ImagePicker { get; private set; }
public RootViewController (IntPtr handle) : base (handle)
{
Title = NSBundle.MainBundle.LocalizedString ("Master", "Master");
// Custom initialization
}
Task UploadTask {
get;
set;
}
void AddNewItem (object sender, EventArgs args)
{
var picker = new UIImagePickerController ();
this.NavigationController.PresentViewController (picker, true, new NSAction(()=>{
Console.WriteLine("done.");
}));
picker.FinishedPickingMedia += (object s, UIImagePickerMediaPickedEventArgs e) => {
Title = NSBundle.MainBundle.LocalizedString ("Uploading photo...", "Uploading photo...");
picker.DismissViewController (true, null);
var randomInt = Convert.ToUInt64(new Random().NextDouble() * UInt64.MaxValue);
var name = randomInt + ".png";
UploadTask = new TaskFactory<CloudBlockBlob>().StartNew(()=>{
return UploadImage(e.OriginalImage, name);
})
.ContinueWith((blob)=>{
InvokeOnMainThread(()=>{
dataSource.Objects.Insert (0, new Tuple<String, UIImage>(name, e.OriginalImage));
Title = NSBundle.MainBundle.LocalizedString ("Photos", "Photos");
using (var indexPath = NSIndexPath.FromRowSection (0, 0))
TableView.InsertRows (new NSIndexPath[] { indexPath }, UITableViewRowAnimation.Automatic);
});
});
};
picker.Canceled += (s, e) => {
picker.DismissViewController (true, null);
};
}
static CloudBlobClient GetClient ()
{
var creds = new StorageCredentials ("YOUR_AZURE_STORAGE_ACCOUNT_NAME", "YOUR_AZURE_STORAGE_ACCOUNT_KEY");
var client = new CloudStorageAccount (creds, true).CreateCloudBlobClient ();
return client;
}
private CloudBlockBlob UploadImage(UIImage image, String name)
{
var client = GetClient ();
var container = client.GetContainerReference("images");
container.CreateIfNotExists();
var blob = container.GetBlockBlobReference(name);
var pngImage = image.AsPNG ();
var stream = pngImage.AsStream();
blob.UploadFromStream (stream);
return blob;
}
public override void DidReceiveMemoryWarning ()
{
// Releases the view if it doesn't have a superview.
base.DidReceiveMemoryWarning ();
// Release any cached data, images, etc that aren't in use.
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
NavigationItem.LeftBarButtonItem = EditButtonItem;
var addButton = new UIBarButtonItem (UIBarButtonSystemItem.Camera, AddNewItem);
NavigationItem.RightBarButtonItem = addButton;
TableView.Source = dataSource = new DataSource (this);
}
class DataSource : UITableViewSource
{
static readonly NSString CellIdentifier = new NSString ("DataSourceCell");
List<Tuple<String, UIImage>> objects = new List<Tuple<String, UIImage>> ();
RootViewController controller;
public DataSource (RootViewController controller)
{
this.controller = controller;
}
public IList<Tuple<String, UIImage>> Objects {
get { return objects; }
}
// Customize the number of sections in the table view.
public override int NumberOfSections (UITableView tableView)
{
return 1;
}
public override int RowsInSection (UITableView tableview, int section)
{
return objects.Count;
}
// Customize the appearance of table view cells.
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
var cell = (UITableViewCell)tableView.DequeueReusableCell (CellIdentifier, indexPath);
cell.TextLabel.Text = objects [indexPath.Row].Item1;
return cell;
}
public override bool CanEditRow (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
// Return false if you do not want the specified item to be editable.
return true;
}
public override void CommitEditingStyle (UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath)
{
if (editingStyle == UITableViewCellEditingStyle.Delete) {
// Delete the row from the data source.
objects.RemoveAt (indexPath.Row);
controller.TableView.DeleteRows (new NSIndexPath[] { indexPath }, UITableViewRowAnimation.Fade);
} else if (editingStyle == UITableViewCellEditingStyle.Insert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
/*
// Override to support rearranging the table view.
public override void MoveRow (UITableView tableView, NSIndexPath sourceIndexPath, NSIndexPath destinationIndexPath)
{
}
*/
/*
// Override to support conditional rearranging of the table view.
public override bool CanMoveRow (UITableView tableView, NSIndexPath indexPath)
{
// Return false if you do not want the item to be re-orderable.
return true;
}
*/
}
public override void PrepareForSegue (UIStoryboardSegue segue, NSObject sender)
{
if (segue.Identifier == "showDetail") {
var indexPath = TableView.IndexPathForSelectedRow;
var item = dataSource.Objects [indexPath.Row];
((DetailViewController)segue.DestinationViewController).SetDetailItem (item);
}
}
}
}