-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSyncViewModel.cs
More file actions
128 lines (110 loc) · 5.71 KB
/
SyncViewModel.cs
File metadata and controls
128 lines (110 loc) · 5.71 KB
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
using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Input;
using Xamarin.Forms;
using MobileSample.Models;
using MobileSample.Services;
using NETCoreSync;
using System.Text;
namespace MobileSample.ViewModels
{
public class SyncViewModel : BaseViewModel
{
private readonly INavigation navigation;
private readonly DatabaseService databaseService;
private readonly SyncConfiguration syncConfiguration;
private List<string> synchronizationMethods;
public List<string> SynchronizationMethods
{
get { return synchronizationMethods; }
set { SetProperty(ref synchronizationMethods, value); }
}
private string selectedSynchronizationMethod;
public string SelectedSynchronizationMethod
{
get { return selectedSynchronizationMethod; }
set { SetProperty(ref selectedSynchronizationMethod, value); }
}
public SyncViewModel(INavigation navigation, DatabaseService databaseService, SyncConfiguration syncConfiguration)
{
this.navigation = navigation;
this.databaseService = databaseService;
this.syncConfiguration = syncConfiguration;
Title = MainMenuItem.GetMenus().Where(w => w.Id == MenuItemType.Sync).First().Title;
ServerUrl = databaseService.GetServerUrl();
SynchronizationMethods = new List<string>(Enum.GetNames(typeof(SyncClient.SynchronizationMethodEnum)));
SelectedSynchronizationMethod = SyncClient.SynchronizationMethodEnum.PushThenPull.ToString();
}
private string serverUrl;
public string ServerUrl
{
get { return serverUrl; }
set { SetProperty(ref serverUrl, value); }
}
private bool isSynchronizing;
public bool IsSynchronizing
{
get { return isSynchronizing; }
set { SetProperty(ref isSynchronizing, value); }
}
private string log;
public string Log
{
get { return log; }
set { SetProperty(ref log, value); }
}
public ICommand SynchronizeCommand => new Command(async () =>
{
if (IsSynchronizing)
{
await Application.Current.MainPage.DisplayAlert("Sync is Running", "Synchronization is already running, please wait until it finished", "OK");
return;
}
IsSynchronizing = true;
try
{
if (string.IsNullOrEmpty(ServerUrl)) throw new Exception("Please specify Server URL");
databaseService.SetServerUrl(ServerUrl);
string synchronizationId = databaseService.GetSynchronizationId();
if (string.IsNullOrEmpty(synchronizationId)) throw new NullReferenceException(nameof(synchronizationId));
CustomSyncEngine customSyncEngine = new CustomSyncEngine(databaseService, syncConfiguration);
SyncClient syncClient = new SyncClient(synchronizationId, customSyncEngine, ServerUrl);
Log = "";
SyncClient.SynchronizationMethodEnum synchronizationMethod = (SyncClient.SynchronizationMethodEnum)Enum.Parse(typeof(SyncClient.SynchronizationMethodEnum), SelectedSynchronizationMethod);
SyncResult result = await syncClient.SynchronizeAsync(synchronizationMethod);
string tempLog = "";
tempLog += $"Client Log: {Environment.NewLine}";
tempLog += $"Sent Changes Count: {result.ClientLog.SentChanges.Count}{Environment.NewLine}";
tempLog += $"Applied Changes Insert Count: {result.ClientLog.AppliedChanges.Inserts.Count}{Environment.NewLine}";
tempLog += $"Applied Changes Updates Count: {result.ClientLog.AppliedChanges.Updates.Count}{Environment.NewLine}";
tempLog += $"Applied Changes Deletes Count: {result.ClientLog.AppliedChanges.Deletes.Count}{Environment.NewLine}";
tempLog += $"Applied Changes Conflicts Count: {result.ClientLog.AppliedChanges.Conflicts.Count}{Environment.NewLine}";
tempLog += $"{Environment.NewLine}";
tempLog += $"Server Log: {Environment.NewLine}";
tempLog += $"Sent Changes Count: {result.ServerLog.SentChanges.Count}{Environment.NewLine}";
tempLog += $"Applied Changes Insert Count: {result.ServerLog.AppliedChanges.Inserts.Count}{Environment.NewLine}";
tempLog += $"Applied Changes Updates Count: {result.ServerLog.AppliedChanges.Updates.Count}{Environment.NewLine}";
tempLog += $"Applied Changes Deletes Count: {result.ServerLog.AppliedChanges.Deletes.Count}{Environment.NewLine}";
tempLog += $"Applied Changes Conflicts Count: {result.ServerLog.AppliedChanges.Conflicts.Count}{Environment.NewLine}";
tempLog += $"{Environment.NewLine}";
tempLog += $"Detail Log: {Environment.NewLine}";
for (int i = 0; i < result.Log.Count; i++)
{
tempLog += $"{result.Log[i]}{Environment.NewLine}";
}
Log = tempLog;
if (!string.IsNullOrEmpty(result.ErrorMessage)) throw new Exception($"Synchronization Error: {result.ErrorMessage}");
await Application.Current.MainPage.DisplayAlert("Finished", "Synchronization is finished", "OK");
}
catch (Exception e)
{
await Application.Current.MainPage.DisplayAlert("Error", e.Message, "OK");
}
finally
{
IsSynchronizing = false;
}
});
}
}