Skip to content

Commit

Permalink
[Azure] for fun, MonoMac displays the tasks from Azure
Browse files Browse the repository at this point in the history
haven't implemented full editing though, just display
  • Loading branch information
conceptdev committed Sep 4, 2012
1 parent c3d3ab2 commit 5c9bffd
Show file tree
Hide file tree
Showing 14 changed files with 3,557 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Azure/Mac/AppDelegate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Drawing;
using MonoMac.Foundation;
using MonoMac.AppKit;
using MonoMac.ObjCRuntime;
//
// inspired by cool tutorial at
// http://tufnelltech.blogspot.ca/2012/01/hello-os-x-from-c-five-steps.html
//
namespace Tasky
{
public partial class AppDelegate : NSApplicationDelegate
{
MainWindowController mainWindowController;

public AppDelegate ()
{
}

public override void FinishedLaunching (NSObject notification)
{
mainWindowController = new MainWindowController ();
mainWindowController.Window.MakeKeyAndOrderFront (this);
}
}
}

10 changes: 10 additions & 0 deletions Azure/Mac/AppDelegate.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

183 changes: 183 additions & 0 deletions Azure/Mac/AzureWebService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
//using System.Json;
using System.Web.Script.Serialization;

namespace Tasky
{
/// <summary>
/// Loosely based on
/// http://chrisrisner.com/Windows-Azure-Mobile-Services-and-iOS
/// also see good discussion of using Fiddler to discover the API
/// http://blog.tattoocoder.com/2012/08/looking-at-windows-azure-mobile.html
/// </summary>
public static class AzureWebService
{
// i've removed the 'done' filter, because i don't want to hide those tasks for now...
static string subdomain = "xxxxxxx"; // your subdomain
static string MobileServiceAppId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // your application key

static string GetAllUrl = "https://"+subdomain+".azure-mobile.net/tables/TodoItem"; //?$filter=(complete%20eq%20false)
static string GetUrl = "https://"+subdomain+".azure-mobile.net/tables/TodoItem?$filter=(id%20eq%20{0})";
static string AddUrl = "https://"+subdomain+".azure-mobile.net/tables/TodoItem";
static string UpdateUrl = "https://"+subdomain+".azure-mobile.net/tables/TodoItem/{0}";
static string DeleteUrl = "https://"+subdomain+".azure-mobile.net/tables/TodoItem/{0}";

/// <summary>
/// GET /tables/TodoItem
/// </summary>
public static List<Task> LoadTodos(Action<List<Task>> whenDone)
{
var tasks = new List<Task>();
WebClient client = new WebClient();
try {
// make it synchronous
client.Headers.Add (HttpRequestHeader.Accept, "application/json");
client.Headers.Add ("X-ZUMO-APPLICATION", MobileServiceAppId);
var response = client.DownloadData (GetAllUrl); // GET
// ...and wait...
var responseString = System.Text.Encoding.UTF8.GetString(response);
// RETURNS [{"id":1,"text":"Port to iOS and Android","complete":false}]

var deser = new JavaScriptSerializer ();
var obj = deser.DeserializeObject (responseString) as Object[];

if (obj != null)
{
tasks = new List<Task>();
foreach (var t in obj)
{
var task = new Task(t);
tasks.Add (task);
}
whenDone(tasks); // hacky to keep doing this...?
}

Console.WriteLine ("Json response => " + responseString);

} catch (System.Net.WebException e) {
Console.WriteLine ("X-ZUMO-APPLICATION failed" + e.Message);
}
return tasks;
}

/// <summary>
/// GET /tables/TodoItem/{id}
/// </summary>
public static Task GetTodo(int id)
{
Task task = null;
WebClient client = new WebClient();
try {
// make it synchronous
client.Headers.Add (HttpRequestHeader.Accept, "application/json");
client.Headers.Add ("X-ZUMO-APPLICATION", MobileServiceAppId);
var response = client.DownloadData (String.Format (GetUrl, id)); // GET
// ...and wait...
var responseString = System.Text.Encoding.UTF8.GetString(response);
// RETURNS [{"id":1,"text":"Port to iOS and Android","complete":false}]

// var responseJson = JsonValue.Parse (responseString);
//
// if (responseJson != null)
// {
// for (var j = 0; j < responseJson.Count; j++) {
// var t = responseJson[j];// as JsonValue;
// task = new Task(t);
// break; // just one required :)
// }
// }

Console.WriteLine ("Json get response => " + responseString);

} catch (System.Net.WebException e) {
Console.WriteLine ("X-ZUMO-APPLICATION failed" + e.Message);
}
return task;
}

/// <summary>
/// PATCH /tables/TodoItem/{id}
/// {"id":1,"text":"updated task text","complete":false}
/// </summary>
public static void UpdateTodo(Task t)
{
WebClient client = new WebClient();
try {
// make it synchronous
client.Headers.Add (HttpRequestHeader.Accept, "application/json");
client.Headers.Add (HttpRequestHeader.ContentType, "application/json");
client.Headers.Add ("X-ZUMO-APPLICATION", MobileServiceAppId);

var payload = t.ToJson ();
var response = client.UploadString (String.Format (UpdateUrl,t.Id), "PATCH", payload);
// ...and wait...
var responseString = response;

//var responseJson = JsonValue.Parse (responseString);
Console.WriteLine ("Update Json response => " + responseString);

} catch (System.Net.WebException e) {
Console.WriteLine ("X-ZUMO-APPLICATION update failed" + e.Message);
}
}

/// <summary>
/// POST /tables/TodoItem
/// {"text":"new task text","complete":false}
/// </summary>
public static Task AddTodo(Task t)
{
WebClient client = new WebClient();
try {
// make it synchronous
client.Headers.Add (HttpRequestHeader.Accept, "application/json");
client.Headers.Add (HttpRequestHeader.ContentType, "application/json");
client.Headers.Add ("X-ZUMO-APPLICATION", MobileServiceAppId);

var payload = t.ToJson ();
var response = client.UploadString (AddUrl, "POST", payload); // PATCH
// ...and wait...
var responseString = response;
// RETURNS [{"id":1,"text":"Port to iOS and Android","complete":false}]
Console.WriteLine ("Add Json response => " + responseString);

// var responseJson = JsonValue.Parse (responseString);
// return new Task(responseJson);

} catch (System.Net.WebException e) {
Console.WriteLine ("X-ZUMO-APPLICATION add failed" + e.Message);
}
return null;
}

/// <summary>
/// DELETE /tables/TodoItem/{id}
/// </summary>
public static void DeleteTodo(Task t)
{
WebClient client = new WebClient();
try {
// make it synchronous
client.Headers.Add (HttpRequestHeader.Accept, "application/json");
client.Headers.Add (HttpRequestHeader.ContentType, "application/json");
client.Headers.Add ("X-ZUMO-APPLICATION", MobileServiceAppId);

var payload = t.ToJson ();
var response = client.UploadString (String.Format (DeleteUrl,t.Id), "DELETE", payload); // DELETE
// ...and wait...
var responseString = response;

//var responseJson = JsonValue.Parse (responseString); //HACK:
Console.WriteLine ("Delete Json response => " + responseString);

} catch (System.Net.WebException e) {
Console.WriteLine ("X-ZUMO-APPLICATION add failed" + e.Message);
}
}
}
}

22 changes: 22 additions & 0 deletions Azure/Mac/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleIdentifier</key>
<string>com.xamarin.samples.taskypro</string>
<key>CFBundleName</key>
<string>Mac</string>
<key>CFBundleShortVersionString</key>
<string>1</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>
<string>10.6</string>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
</dict>
</plist>
23 changes: 23 additions & 0 deletions Azure/Mac/Mac.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tasky", "Tasky.csproj", "{74D62B81-3EE0-4EDD-9F75-A74379B9ACB3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Release|x86 = Release|x86
AppStore|x86 = AppStore|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{74D62B81-3EE0-4EDD-9F75-A74379B9ACB3}.AppStore|x86.ActiveCfg = AppStore|x86
{74D62B81-3EE0-4EDD-9F75-A74379B9ACB3}.AppStore|x86.Build.0 = AppStore|x86
{74D62B81-3EE0-4EDD-9F75-A74379B9ACB3}.Debug|x86.ActiveCfg = Debug|x86
{74D62B81-3EE0-4EDD-9F75-A74379B9ACB3}.Debug|x86.Build.0 = Debug|x86
{74D62B81-3EE0-4EDD-9F75-A74379B9ACB3}.Release|x86.ActiveCfg = Release|x86
{74D62B81-3EE0-4EDD-9F75-A74379B9ACB3}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = Tasky.csproj
EndGlobalSection
EndGlobal
18 changes: 18 additions & 0 deletions Azure/Mac/Main.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Drawing;
using MonoMac.Foundation;
using MonoMac.AppKit;
using MonoMac.ObjCRuntime;

namespace Tasky
{
class MainClass
{
static void Main (string[] args)
{
NSApplication.Init ();
NSApplication.Main (args);
}
}
}

Loading

0 comments on commit 5c9bffd

Please sign in to comment.