This repository has been archived by the owner on Jan 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathAzureDataStore.cs
165 lines (136 loc) · 4.69 KB
/
AzureDataStore.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
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.MobileServices;
using Microsoft.WindowsAzure.MobileServices.SQLiteStore;
using Microsoft.WindowsAzure.MobileServices.Sync;
using System.Diagnostics;
using System;
//Comment back in to use azure
using Xamarin.Essentials;
//[assembly: Dependency(typeof(AzureDataStore))]
namespace MyShop
{
public class AzureDataStore : IDataStore
{
public MobileServiceClient MobileService { get; set; }
IMobileServiceSyncTable<Store> storeTable;
IMobileServiceSyncTable<Feedback> feedbackTable;
bool initialized = false;
public AzureDataStore()
{
// This is a sample read-only azure site for demo
// Follow the readme.md in the GitHub repo on how to setup your own.
MobileService = new MobileServiceClient(
"http://myshoppe-demo.azurewebsites.net");
}
public async Task Init()
{
initialized = true;
const string path = "syncstore.db";
var store = new MobileServiceSQLiteStore(path);
store.DefineTable<Store>();
store.DefineTable<Feedback>();
await MobileService.SyncContext.InitializeAsync(store, new MobileServiceSyncHandler());
storeTable = MobileService.GetSyncTable<Store>();
feedbackTable = MobileService.GetSyncTable<Feedback>();
}
public async Task AddFeedbackAsync(Feedback feedback)
{
if (!initialized)
await Init();
await feedbackTable.InsertAsync(feedback);
await SyncFeedbacksAsync();
}
public async Task<IEnumerable<Feedback>> GetFeedbackAsync()
{
if (!initialized)
await Init();
await feedbackTable.PullAsync("allFeedbacks", feedbackTable.CreateQuery());
return await feedbackTable.ToEnumerableAsync();
}
public async Task<bool> RemoveFeedbackAsync(Feedback feedback)
{
if (!initialized)
await Init();
await feedbackTable.DeleteAsync(feedback);
await SyncFeedbacksAsync();
return true;
}
public async Task<Store> AddStoreAsync(Store store)
{
if (!initialized)
await Init();
await storeTable.InsertAsync(store);
await SyncStoresAsync();
await MobileService.SyncContext.PushAsync();
return store;
}
public async Task<bool> RemoveStoreAsync(Store store)
{
if (!initialized)
await Init();
await storeTable.DeleteAsync(store);
await SyncStoresAsync();
await MobileService.SyncContext.PushAsync();
return true;
}
public async Task<Store> UpdateStoreAsync(Store store)
{
if (!initialized)
await Init();
await storeTable.UpdateAsync(store);
await SyncStoresAsync();
await MobileService.SyncContext.PushAsync();
return store;
}
public async Task<IEnumerable<Store>> GetStoresAsync()
{
if (!initialized)
await Init();
await SyncStoresAsync();
return await storeTable.ToEnumerableAsync();
}
public async Task SyncStoresAsync()
{
try
{
if (!IsInternetAvailable || !Settings.NeedsSync)
return;
await storeTable.PullAsync("allStores", storeTable.CreateQuery());
Settings.LastSync = DateTime.Now;
}
catch (Exception ex)
{
Debug.WriteLine("Sync Failed:" + ex.Message);
}
}
public async Task SyncFeedbacksAsync()
{
try
{
Settings.NeedSyncFeedback = true;
if (!IsInternetAvailable)
return;
await MobileService.SyncContext.PushAsync();
Settings.NeedSyncFeedback = false;
}
catch (Exception ex)
{
Debug.WriteLine("Sync Failed:" + ex.Message);
}
}
bool IsInternetAvailable => Connectivity.NetworkAccess == NetworkAccess.Internet;
static readonly AzureDataStore instance = new AzureDataStore();
/// <summary>
/// Gets the instance of the Azure Web Service
/// </summary>
public static AzureDataStore Instance
{
get
{
return instance;
}
}
}
}