AzureService.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using DevDaysSpeakers.Services;
  2. using DevDaysSpeakers.Model;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.IO;
  7. using Xamarin.Forms;
  8. using Microsoft.WindowsAzure.MobileServices;
  9. using Microsoft.WindowsAzure.MobileServices.Sync;
  10. using System.Threading.Tasks;
  11. using Microsoft.WindowsAzure.MobileServices.SQLiteStore;
  12. using System.Diagnostics;
  13. [assembly: Dependency(typeof(AzureService))]
  14. namespace DevDaysSpeakers.Services
  15. {
  16. public class AzureService
  17. {
  18. public MobileServiceClient Client { get; set; } = null;
  19. IMobileServiceSyncTable<Speaker> table;
  20. public async Task Initialize()
  21. {
  22. if (Client?.SyncContext?.IsInitialized ?? false)
  23. return;
  24. var appUrl = "https://OUR-APP-NAME-HERE.azurewebsites.net";
  25. //Create our client
  26. Client = new MobileServiceClient(appUrl);
  27. //InitialzeDatabase for path
  28. var path = InitializeDatabase();
  29. //setup our local sqlite store and intialize our table
  30. var store = new MobileServiceSQLiteStore(path);
  31. //Define table
  32. store.DefineTable<Speaker>();
  33. //Initialize SyncContext
  34. await Client.SyncContext.InitializeAsync(store, new MobileServiceSyncHandler());
  35. //Get our sync table that will call out to azure
  36. table = Client.GetSyncTable<Speaker>();
  37. }
  38. private string InitializeDatabase()
  39. {
  40. #if __ANDROID__ || __IOS__
  41. Microsoft.WindowsAzure.MobileServices.CurrentPlatform.Init();
  42. #endif
  43. SQLitePCL.Batteries.Init();
  44. var path = "syncstore.db";
  45. #if __ANDROID__
  46. path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), path);
  47. if (!File.Exists(path))
  48. {
  49. File.Create(path).Dispose();
  50. }
  51. #endif
  52. return path;
  53. }
  54. public async Task<IEnumerable<Speaker>> GetSpeakers()
  55. {
  56. return new List<Speaker>();
  57. }
  58. public async Task SyncSpeakers()
  59. {
  60. try
  61. {
  62. }
  63. catch (Exception ex)
  64. {
  65. Debug.WriteLine("Unable to sync speakers, that is alright as we have offline capabilities: " + ex);
  66. }
  67. }
  68. }
  69. }