Permalink
Browse files

Betterified all the codes!

  • Loading branch information...
1 parent 6373bb0 commit f712357aef32aea55a20b4d8895560d4c63d8811 @Redth committed Nov 28, 2012
Binary file not shown.
@@ -0,0 +1,39 @@
+using Cirrious.MvvmCross.ExtensionMethods;
+using Cirrious.MvvmCross.Interfaces.ServiceProvider;
+using Cirrious.MvvmCross.ViewModels;
+using System.Collections.Generic;
+using System.Linq;
+using WshLst.Core.Models;
+using WshLst.Core.Interfaces;
+using Xamarin.Contacts;
+
+namespace WshLst.Core
+{
+ public class AddressBookApplicationObject : MvxApplicationObject, IAddressBookSource
+ {
+ public IEnumerable<SelectableContact> GetContactsWithEmailAddresses()
+ {
+#if MONOANDROID
+ var androidGlobals = this.GetService<Cirrious.MvvmCross.Droid.Interfaces.IMvxAndroidGlobals>();
+ var ab = new AddressBook(androidGlobals.ApplicationContext);
+#else
+ var ab = new AddressBook();
+#endif
+
+ var allContacts = ab.ToList();
+
+ return (from c in allContacts
+ orderby c.DisplayName
+ where c.Emails != null && c.Emails.Any()
+ let email = c.Emails.FirstOrDefault()
+ where email != null && !string.IsNullOrEmpty(email.Address)
+ select
+ new SelectableContact
+ {
+ DisplayName = c.DisplayName,
+ Email = email.Address,
+ IsSelected = false
+ }).ToList();
+ }
+ }
+}
View
@@ -8,20 +8,23 @@
namespace WshLst.Core
{
public class App : MvxApplication,
- IMvxServiceProducer<IMvxStartNavigation>,
- IMvxServiceProducer<IErrorReporter>,
- IMvxServiceProducer<IErrorSource>,
- IMvxServiceProducer<ISettingsProvider>,
- IMvxServiceProducer<IGeolocator>,
- IMvxServiceProducer<IBarcodeScanner>,
- IMvxServiceProducer<IMediaFileSource>
+ IMvxServiceProducer<IMvxStartNavigation>,
+ IMvxServiceProducer<IErrorReporter>,
+ IMvxServiceProducer<IErrorSource>,
+ IMvxServiceProducer<ISettingsProvider>,
+ IMvxServiceProducer<IGeolocator>,
+ IMvxServiceProducer<IBarcodeScanner>,
+ IMvxServiceProducer<IMediaFileSource>,
+ IMvxServiceProducer<IAddressBookSource>
{
public static bool IsLaunch = true;
public static MobileServiceClient Azure;
public App()
{
+ Azure = new MobileServiceClient(Config.AZURE_MOBILE_SERVICE_URL, Config.AZURE_MOBILE_SERVICE_APPKEY);
+
var errAppObj = new ErrorApplicationObject();
var settings = new Settings();
settings.Load();
@@ -37,6 +40,7 @@ public App()
this.RegisterServiceInstance<IGeolocator>(geo);
this.RegisterServiceInstance<IBarcodeScanner>(new BarcodeApplicatonObject());
+ this.RegisterServiceInstance<IAddressBookSource>(new AddressBookApplicationObject());
}
public static void Logout()
@@ -0,0 +1,10 @@
+using System.Collections.Generic;
+using WshLst.Core.Models;
+
+namespace WshLst.Core.Interfaces
+{
+ public interface IAddressBookSource
+ {
+ IEnumerable<SelectableContact> GetContactsWithEmailAddresses();
+ }
+}
@@ -3,8 +3,8 @@ namespace WshLst.Core.Interfaces
public interface ISettingsProvider
{
string UserId { get; set; }
- int AuthenticationProvider { get; set; }
-
+ int AuthenticationProvider { get;set; }
+
void Load();
void Save();
}
@@ -0,0 +1,33 @@
+using System.Collections;
+using System.Collections.Generic;
+
+namespace WshLst.Core.Models
+{
+ public class SelectableContactGroup : IEnumerable<SelectableContact>
+ {
+ public string Title { get; set; }
+ public List<SelectableContact> Items { get; set; }
+
+ IEnumerator IEnumerable.GetEnumerator()
+ {
+ return Items.GetEnumerator();
+ }
+
+ IEnumerator<SelectableContact> IEnumerable<SelectableContact>.GetEnumerator()
+ {
+ return Items.GetEnumerator();
+ }
+
+ public IEnumerator<SelectableContact> GetEnumerator()
+ {
+ return Items.GetEnumerator();
+ }
+ }
+
+ public class SelectableContact
+ {
+ public bool IsSelected { get; set; }
+ public string DisplayName { get; set; }
+ public string Email { get; set; }
+ }
+}
@@ -17,7 +17,7 @@ public Settings()
public string UserId { get; set; }
public int AuthenticationProvider { get; set; }
-
+
public void Load()
{
try
@@ -89,6 +89,11 @@ public bool HasImage
public bool UseLocation { get; set; }
+ public ICommand RemovePhotoCommand
+ {
+ get { return new MvxRelayCommand(RemovePhoto); }
+ }
+
public ICommand ScanCommand
{
get { return new MvxRelayCommand(Scan); }
@@ -129,7 +134,7 @@ public void AddPhoto(Stream base64ImageStream)
AddPhoto(strImg);
}
-
+
public void RemovePhoto()
{
_entryImage.ImageBase64 = string.Empty;
@@ -59,33 +59,39 @@ public void Login(LoginPlatform platform)
#if MONOANDROID
var activity = this.GetService<Cirrious.MvvmCross.Droid.Interfaces.IMvxAndroidCurrentTopActivity>().Activity;
- App.Azure.LoginAsync(activity, platform.Provider).ContinueWith((t) =>
+ App.Azure.LoginAsync(activity, platform.Provider).ContinueWith((t) => HandleLoginResult(t, platform));
#elif MONOTOUCH
- App.Azure.LoginAsync(ViewController, platform.Provider).ContinueWith((t) =>
+ App.Azure.LoginAsync(ViewController, platform.Provider).ContinueWith((t) => HandleLoginResult(t, platform));
#else
- App.Azure.LoginAsync(platform.Provider).ContinueWith((t) =>
+ App.Azure.LoginAsync(platform.Provider).ContinueWith((t) => HandleLoginResult(t, platform));
#endif
- {
- IsLoading = false;
-
- if (t.Status == TaskStatus.RanToCompletion && t.Result != null && !string.IsNullOrEmpty(t.Result.UserId))
- {
- //Save our app settings for next launch
- var settings = this.GetService<ISettingsProvider>();
-
- settings.UserId = t.Result.UserId;
- settings.AuthenticationProvider = (int) platform.Provider;
- settings.Save();
-
- //Navigate to the Lists view
- RequestNavigate<WishListsViewModel>();
- }
- else
- {
- //Show Error
- ReportError("Login Failed!");
- }
- });
+
+ }
+
+ void HandleLoginResult(Task<MobileServiceUser> t, LoginPlatform platform = null)
+ {
+ IsLoading = false;
+
+ if (t.Status == TaskStatus.RanToCompletion && t.Result != null && !string.IsNullOrEmpty(t.Result.UserId))
+ {
+ //Save our app settings for next launch
+ var settings = this.GetService<ISettingsProvider>();
+
+ settings.UserId = t.Result.UserId;
+
+ if (platform != null)
+ settings.AuthenticationProvider = (int)platform.Provider;
+
+ settings.Save();
+
+ //Navigate to the Lists view
+ RequestNavigate<WishListsViewModel>();
+ }
+ else
+ {
+ //Show Error
+ ReportError("Login Failed!");
+ }
}
public void CheckLogin()
@@ -95,10 +101,10 @@ public void CheckLogin()
if (settings.AuthenticationProvider < 0) return;
var provider =
- (MobileServiceAuthenticationProvider)
- Enum.Parse(typeof (MobileServiceAuthenticationProvider), settings.AuthenticationProvider.ToString());
+ (MobileServiceAuthenticationProvider)Enum.Parse(typeof (MobileServiceAuthenticationProvider),
+ settings.AuthenticationProvider.ToString());
- Login(new LoginPlatform {Provider = provider, Name = string.Empty});
+ Login(new LoginPlatform {Provider = provider, Name = string.Empty});
}
}
}
@@ -4,6 +4,8 @@
using System.Windows.Input;
using Cirrious.MvvmCross.Commands;
using Cirrious.MvvmCross.ExtensionMethods;
+using WshLst.Core.Models;
+using WshLst.Core.Interfaces;
using Xamarin.Contacts;
namespace WshLst.Core.ViewModels
@@ -33,27 +35,8 @@ public List<SelectableContact> Contacts
public void LoadContacts()
{
-#if MONOANDROID
- var androidGlobals = this.GetService<Cirrious.MvvmCross.Droid.Interfaces.IMvxAndroidGlobals>();
- var ab = new AddressBook(androidGlobals.ApplicationContext);
-#else
- var ab = new AddressBook();
-#endif
-
- var allContacts = ab.ToList();
-
- _contacts = (from c in allContacts
- orderby c.DisplayName
- where c.Emails != null && c.Emails.Any()
- let email = c.Emails.FirstOrDefault()
- where email != null && !string.IsNullOrEmpty(email.Address)
- select
- new SelectableContact
- {
- DisplayName = c.DisplayName,
- Email = email.Address,
- IsSelected = false
- }).ToList();
+ var addressBook = this.GetService<IAddressBookSource>();
+ _contacts = addressBook.GetContactsWithEmailAddresses().ToList();
RaisePropertyChanged(() => Contacts);
}
@@ -99,33 +82,5 @@ public string GetEmailBody()
return "Hi, I'd like to share my wish list with you! You can see my Wish List online at: \r\n\r\n" + url;
}
-
- public class SelectableContact
- {
- public bool IsSelected { get; set; }
- public string DisplayName { get; set; }
- public string Email { get; set; }
- }
-
- public class SelectableContactGroup : IEnumerable<SelectableContact>
- {
- public string Title { get; set; }
- public List<SelectableContact> Items { get; set; }
-
- IEnumerator IEnumerable.GetEnumerator()
- {
- return Items.GetEnumerator();
- }
-
- IEnumerator<SelectableContact> IEnumerable<SelectableContact>.GetEnumerator()
- {
- return Items.GetEnumerator();
- }
-
- public IEnumerator<SelectableContact> GetEnumerator()
- {
- return Items.GetEnumerator();
- }
- }
}
}
@@ -57,7 +57,8 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\References\MonoForAndroid\Cirrious.MvvmCross.Plugins.Visibility.Droid.dll</HintPath>
</Reference>
- <Reference Include="Microsoft.WindowsAzure.MobileServices.Android">
+ <Reference Include="Microsoft.WindowsAzure.MobileServices.Android, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
<HintPath>..\References\MonoForAndroid\Microsoft.WindowsAzure.MobileServices.Android.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
@@ -86,7 +87,9 @@
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
<ItemGroup />
<ItemGroup>
+ <Compile Include="AddressBookApplicationObject.cs" />
<Compile Include="Config.cs" />
+ <Compile Include="Interfaces\IAddressBookSource.cs" />
<Compile Include="Interfaces\IBarcodeScanner.cs" />
<Compile Include="Interfaces\IErrorReporter.cs" />
<Compile Include="Interfaces\IErrorSource.cs" />
@@ -96,6 +99,7 @@
<Compile Include="MediaFileApplicationObject.cs" />
<Compile Include="Models\Entry.cs" />
<Compile Include="Models\EntryImage.cs" />
+ <Compile Include="Models\SelectableContact.cs" />
<Compile Include="Models\WishList.cs" />
<Compile Include="Models\LoginPlatform.cs" />
<Compile Include="Models\ScanditProduct.cs" />
@@ -45,9 +45,6 @@
<Reference Include="Cirrious.MvvmCross.Localization">
<HintPath>..\References\MonoTouch\Cirrious.MvvmCross.Localization.dll</HintPath>
</Reference>
- <Reference Include="MicrosoftAzureZumoiOS">
- <HintPath>..\References\MonoTouch\MicrosoftAzureZumoiOS.dll</HintPath>
- </Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>..\References\MonoTouch\Newtonsoft.Json.dll</HintPath>
</Reference>
@@ -60,6 +57,9 @@
<Reference Include="Xamarin.Mobile">
<HintPath>..\References\MonoTouch\Xamarin.Mobile.dll</HintPath>
</Reference>
+ <Reference Include="MicrosoftAzureZumoiOS">
+ <HintPath>..\References\MonoTouch\MicrosoftAzureZumoiOS.dll</HintPath>
+ </Reference>
</ItemGroup>
<ItemGroup>
<Folder Include="Resources\" />
@@ -97,5 +97,8 @@
<Compile Include="Interfaces\IMediaFileSource.cs" />
<Compile Include="ViewModels\EditWishListViewModel.cs" />
<Compile Include="Config.cs" />
+ <Compile Include="Interfaces\IAddressBookSource.cs" />
+ <Compile Include="Models\SelectableContact.cs" />
+ <Compile Include="AddressBookApplicationObject.cs" />
</ItemGroup>
</Project>
Oops, something went wrong.

0 comments on commit f712357

Please sign in to comment.