diff --git a/README.md b/README.md index a157dea..8ba18c8 100644 --- a/README.md +++ b/README.md @@ -43,3 +43,56 @@ Now I'm going to update my iOS and Android projects to reference this PCL projec ![Project references](/images/project-references.png) +### Step 3 - The Great Refactoring + +Now the fun part! Lets pull some shared code into the PCL project. I'll want to take the Mobile Service client, the ToDoItem class, and the code that queries for todo items that are not marked as complete. I could certainly move more over but this should be enough to demonstrate the process. For the sake of simplicity I'll put it all in one file (no I would not do this for a real app): + +
+    public class Common
+    {
+        static Common()
+        {
+            InitializeClient();
+        }
+
+        public static MobileServiceClient MobileService;
+
+        public static void InitializeClient(params DelegatingHandler[] handlers)
+        {
+            MobileService = new MobileServiceClient(
+                @"https://xamarinpcl.azure-mobile.net/",
+                @"your app key goes here",
+                handlers
+            );
+        }
+
+        public static IMobileServiceTableQuery GetIncompleteItems()
+        {
+            var toDoTable = MobileService.GetTable<ToDoItem>();
+            return toDoTable.Where(item => item.Complete == false);
+        }
+    }
+
+    public class ToDoItem
+    {
+        public string Id { get; set; }
+
+        [JsonProperty(PropertyName = "text")]
+        public string Text { get; set; }
+
+        [JsonProperty(PropertyName = "complete")]
+        public bool Complete { get; set; }
+    }
+
+ +I then have a bunch of changes to make to the Android and iOS projects to use the shared code. If you want to see the changes, just take a look at the history of this git repository. + +Lets build and run both projects to make sure we haven't broken anything: + +![Project references](/images/everything-works-great.png) + +Awesome! + +### Contact + +If you have any questions or run into problems feel free to email me at pbatum@microsoft.com or catch me on twitter (@paulbatum). \ No newline at end of file diff --git a/images/everything-works-great.png b/images/everything-works-great.png new file mode 100644 index 0000000..abe9f5d Binary files /dev/null and b/images/everything-works-great.png differ diff --git a/xamarinpcl/xamarinpcl-android/ToDoActivity.cs b/xamarinpcl/xamarinpcl-android/ToDoActivity.cs index a26dd2e..00fdbd0 100755 --- a/xamarinpcl/xamarinpcl-android/ToDoActivity.cs +++ b/xamarinpcl/xamarinpcl-android/ToDoActivity.cs @@ -6,6 +6,7 @@ using System.Net.Http; using System.Threading.Tasks; using Microsoft.WindowsAzure.MobileServices; +using Shared; namespace xamarinpcl @@ -30,8 +31,6 @@ public class ToDoActivity : Activity //Progress spinner to use for table operations private ProgressBar progressBar; - const string applicationURL = @"https://xamarinpcl.azure-mobile.net/"; - protected override async void OnCreate (Bundle bundle) { base.OnCreate (bundle); @@ -56,9 +55,7 @@ protected override async void OnCreate (Bundle bundle) // Create the Mobile Service Client instance, using the provided // Mobile Service URL and key - client = new MobileServiceClient ( - applicationURL, - null, progressHandler); + client = Common.MobileService; // Get the Mobile Service Table instance to use toDoTable = client.GetTable (); @@ -108,7 +105,7 @@ async Task RefreshItemsFromTableAsync () try { // Get the items that weren't marked as completed and add them in the // adapter - var list = await toDoTable.Where (item => item.Complete == false).ToListAsync (); + var list = await Common.GetIncompleteItems().ToListAsync (); adapter.Clear (); diff --git a/xamarinpcl/xamarinpcl-android/ToDoItem.cs b/xamarinpcl/xamarinpcl-android/ToDoItem.cs deleted file mode 100755 index 79c1189..0000000 --- a/xamarinpcl/xamarinpcl-android/ToDoItem.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using Newtonsoft.Json; - -namespace xamarinpcl -{ - public class ToDoItem - { - public string Id { get; set; } - - [JsonProperty(PropertyName = "text")] - public string Text { get; set; } - - [JsonProperty(PropertyName = "complete")] - public bool Complete { get; set; } - } - - public class ToDoItemWrapper : Java.Lang.Object - { - public ToDoItemWrapper (ToDoItem item) - { - ToDoItem = item; - } - - public ToDoItem ToDoItem { get; private set; } - } -} - diff --git a/xamarinpcl/xamarinpcl-android/ToDoItemAdapter.cs b/xamarinpcl/xamarinpcl-android/ToDoItemAdapter.cs index 7f223c6..1caf64d 100755 --- a/xamarinpcl/xamarinpcl-android/ToDoItemAdapter.cs +++ b/xamarinpcl/xamarinpcl-android/ToDoItemAdapter.cs @@ -3,6 +3,7 @@ using Android.Views; using Android.Widget; using System.Collections.Generic; +using Shared; namespace xamarinpcl { diff --git a/xamarinpcl/xamarinpcl-android/ToDoItemWrapper.cs b/xamarinpcl/xamarinpcl-android/ToDoItemWrapper.cs new file mode 100755 index 0000000..94db5b5 --- /dev/null +++ b/xamarinpcl/xamarinpcl-android/ToDoItemWrapper.cs @@ -0,0 +1,18 @@ +using System; +using Newtonsoft.Json; +using Shared; + +namespace xamarinpcl +{ + + public class ToDoItemWrapper : Java.Lang.Object + { + public ToDoItemWrapper (ToDoItem item) + { + ToDoItem = item; + } + + public ToDoItem ToDoItem { get; private set; } + } +} + diff --git a/xamarinpcl/xamarinpcl-android/xamarinpcl-android.csproj b/xamarinpcl/xamarinpcl-android/xamarinpcl-android.csproj index e05d092..4aee6e9 100644 --- a/xamarinpcl/xamarinpcl-android/xamarinpcl-android.csproj +++ b/xamarinpcl/xamarinpcl-android/xamarinpcl-android.csproj @@ -69,8 +69,8 @@ - + diff --git a/xamarinpcl/xamarinpcl-ios/QSTodoListViewController.cs b/xamarinpcl/xamarinpcl-ios/QSTodoListViewController.cs index 71cc4c5..9f25677 100755 --- a/xamarinpcl/xamarinpcl-ios/QSTodoListViewController.cs +++ b/xamarinpcl/xamarinpcl-ios/QSTodoListViewController.cs @@ -3,6 +3,7 @@ using MonoTouch.UIKit; using MonoTouch.Foundation; using System.Threading.Tasks; +using Shared; namespace xamarinpcl { diff --git a/xamarinpcl/xamarinpcl-ios/QSTodoService.cs b/xamarinpcl/xamarinpcl-ios/QSTodoService.cs index 31c72a6..23e89cf 100755 --- a/xamarinpcl/xamarinpcl-ios/QSTodoService.cs +++ b/xamarinpcl/xamarinpcl-ios/QSTodoService.cs @@ -3,13 +3,13 @@ using System.Threading.Tasks; using System.Collections.Generic; using Microsoft.WindowsAzure.MobileServices; +using Shared; namespace xamarinpcl { public class QSTodoService : DelegatingHandler { static QSTodoService instance = new QSTodoService (); - const string applicationURL = @"https://xamarinpcl.azure-mobile.net/"; MobileServiceClient client; IMobileServiceTable todoTable; int busyCount = 0; @@ -21,7 +21,8 @@ public class QSTodoService : DelegatingHandler CurrentPlatform.Init (); // Initialize the Mobile Service client with your URL and key - client = new MobileServiceClient (applicationURL, null, this); + Common.InitializeClient (this); + client = Common.MobileService; // Create an MSTable instance to allow us to work with the TodoItem table todoTable = client.GetTable (); @@ -40,8 +41,7 @@ async public Task> RefreshDataAsync () try { // This code refreshes the entries in the list view by querying the TodoItems table. // The query excludes completed TodoItems - Items = await todoTable - .Where (todoItem => todoItem.Complete == false).ToListAsync (); + Items = await Common.GetIncompleteItems().ToListAsync (); } catch (MobileServiceInvalidOperationException e) { Console.Error.WriteLine (@"ERROR {0}", e.Message); diff --git a/xamarinpcl/xamarinpcl-ios/ToDoItem.cs b/xamarinpcl/xamarinpcl-ios/ToDoItem.cs deleted file mode 100755 index b60d1b4..0000000 --- a/xamarinpcl/xamarinpcl-ios/ToDoItem.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using Newtonsoft.Json; - -namespace xamarinpcl -{ - public class ToDoItem - { - public string Id { get; set; } - - [JsonProperty(PropertyName = "text")] - public string Text { get; set; } - - [JsonProperty(PropertyName = "complete")] - public bool Complete { get; set; } - } -} - diff --git a/xamarinpcl/xamarinpcl-ios/xamarinpcl-ios.csproj b/xamarinpcl/xamarinpcl-ios/xamarinpcl-ios.csproj index 71f9a7c..57dd311 100644 --- a/xamarinpcl/xamarinpcl-ios/xamarinpcl-ios.csproj +++ b/xamarinpcl/xamarinpcl-ios/xamarinpcl-ios.csproj @@ -111,7 +111,6 @@ QSTodoListViewController.cs - diff --git a/xamarinpcl/xamarinpcl-shared/Common.cs b/xamarinpcl/xamarinpcl-shared/Common.cs new file mode 100644 index 0000000..75ad68d --- /dev/null +++ b/xamarinpcl/xamarinpcl-shared/Common.cs @@ -0,0 +1,47 @@ +using Microsoft.WindowsAzure.MobileServices; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; + +namespace Shared +{ + public class Common + { + static Common() + { + InitializeClient(); + } + + public static MobileServiceClient MobileService; + + public static void InitializeClient(params DelegatingHandler[] handlers) + { + MobileService = new MobileServiceClient( + @"https://xamarinpcl.azure-mobile.net/", + null, + handlers + ); + } + + public static IMobileServiceTableQuery GetIncompleteItems() + { + var toDoTable = MobileService.GetTable(); + return toDoTable.Where(item => item.Complete == false); + } + } + + public class ToDoItem + { + public string Id { get; set; } + + [JsonProperty(PropertyName = "text")] + public string Text { get; set; } + + [JsonProperty(PropertyName = "complete")] + public bool Complete { get; set; } + } +} diff --git a/xamarinpcl/xamarinpcl-shared/xamarinpcl-shared.csproj b/xamarinpcl/xamarinpcl-shared/xamarinpcl-shared.csproj index 98053d3..753cd85 100644 --- a/xamarinpcl/xamarinpcl-shared/xamarinpcl-shared.csproj +++ b/xamarinpcl/xamarinpcl-shared/xamarinpcl-shared.csproj @@ -33,6 +33,7 @@ +