aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Utilities/Tango.Synchronization.UI/ViewModels/MainViewVM.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Software/Visual_Studio/Utilities/Tango.Synchronization.UI/ViewModels/MainViewVM.cs')
-rw-r--r--Software/Visual_Studio/Utilities/Tango.Synchronization.UI/ViewModels/MainViewVM.cs331
1 files changed, 331 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Utilities/Tango.Synchronization.UI/ViewModels/MainViewVM.cs b/Software/Visual_Studio/Utilities/Tango.Synchronization.UI/ViewModels/MainViewVM.cs
new file mode 100644
index 000000000..d388d761f
--- /dev/null
+++ b/Software/Visual_Studio/Utilities/Tango.Synchronization.UI/ViewModels/MainViewVM.cs
@@ -0,0 +1,331 @@
+using Microsoft.Win32;
+using System;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+using System.Windows;
+using Tango.Logging;
+using Tango.SharedUI;
+using Tango.SharedUI.Commands;
+
+namespace Tango.Synchronization.UI.ViewModels
+{
+ public class MainViewVM : ExtendedObject
+ {
+ private String _masterDBFile;
+ private String _slaveDBFile;
+ private SqlDataBaseComparer _comparer;
+
+ #region Properties
+
+ private bool _isBusy;
+ /// <summary>
+ /// Gets or sets a value indicating whether an operation is in progress.
+ /// </summary>
+ public bool IsBusy
+ {
+ get { return _isBusy; }
+ set { _isBusy = value; RaisePropertyChanged(nameof(IsBusy)); }
+ }
+
+ private String _status;
+ /// <summary>
+ /// Gets or sets the current operation status.
+ /// </summary>
+ public String Status
+ {
+ get { return _status; }
+ set { _status = value; RaisePropertyChanged(nameof(Status)); }
+ }
+
+ private String _log;
+ /// <summary>
+ /// Gets or sets the current application log text.
+ /// </summary>
+ public String Log
+ {
+ get { return _log; }
+ set { _log = value; RaisePropertyChanged(nameof(Log)); }
+ }
+
+ private ObservableCollection<SqlDiff> _differences;
+ /// <summary>
+ /// Gets or sets the differences.
+ /// </summary>
+ public ObservableCollection<SqlDiff> Differences
+ {
+ get { return _differences; }
+ set { _differences = value; RaisePropertyChanged(nameof(Differences)); }
+ }
+
+ private SqlDiff _selectedDifference;
+ /// <summary>
+ /// Gets or sets the selected difference.
+ /// </summary>
+ public SqlDiff SelectedDifference
+ {
+ get { return _selectedDifference; }
+ set { _selectedDifference = value; RaisePropertyChanged(nameof(SelectedDifference)); InvalidateRelayCommands(); }
+ }
+
+ private String _masterDBName;
+ /// <summary>
+ /// Gets or sets the name of the master database.
+ /// </summary>
+ public String MasterDBName
+ {
+ get { return _masterDBName; }
+ set { _masterDBName = value; RaisePropertyChanged(nameof(MasterDBName)); }
+ }
+
+ private String _slaveDBName;
+ /// <summary>
+ /// Gets or sets the name of the slave database.
+ /// </summary>
+ public String SlaveDBName
+ {
+ get { return _slaveDBName; }
+ set { _slaveDBName = value; RaisePropertyChanged(nameof(SlaveDBName)); }
+ }
+
+ #endregion
+
+ #region Commands
+
+ /// <summary>
+ /// Gets or sets the browse master database command.
+ /// </summary>
+ public RelayCommand BrowseMasterDBCommand { get; set; }
+
+ /// <summary>
+ /// Gets or sets the browse slave database command.
+ /// </summary>
+ public RelayCommand BrowseSlaveDBCommand { get; set; }
+
+ /// <summary>
+ /// Gets or sets the compare command.
+ /// </summary>
+ public RelayCommand CompareCommand { get; set; }
+
+ /// <summary>
+ /// Gets or sets the commit command.
+ /// </summary>
+ public RelayCommand CommitCommand { get; set; }
+
+ /// <summary>
+ /// Gets or sets the commit all command.
+ /// </summary>
+ public RelayCommand CommitAllCommand { get; set; }
+
+ #endregion
+
+ #region Constructors
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="MainViewVM"/> class.
+ /// </summary>
+ public MainViewVM()
+ {
+ MainViewLogger logger = new MainViewLogger();
+ logger.NewLog += (output) =>
+ {
+ Log += output + Environment.NewLine;
+ };
+
+ LogManager.RegisterLogger(logger);
+
+ LogManager.Log("DB Synchronizer Started!");
+
+ Differences = new ObservableCollection<SqlDiff>();
+
+ BrowseMasterDBCommand = new RelayCommand(BrowseMasterDB);
+ BrowseSlaveDBCommand = new RelayCommand(BrowseSlaveDB);
+ CompareCommand = new RelayCommand(Compare, (x) => _masterDBFile != null && _slaveDBFile != null);
+ CommitCommand = new RelayCommand(Commit, (x) => SelectedDifference != null);
+ CommitAllCommand = new RelayCommand(CommitAll, (x) => Differences.Count > 0);
+
+ Task.Factory.StartNew(() =>
+ {
+ OpenStatus("Loading components, please wait...");
+ Thread.Sleep(1000);
+ CloseStatus();
+ });
+ }
+
+ #endregion
+
+ #region Private Methods
+
+ private void Compare()
+ {
+ _comparer = new SqlDataBaseComparer(new SqliteDataBase(_masterDBFile), new SqliteDataBase(_slaveDBFile));
+
+ OpenStatus("Comparing DataBase...");
+
+ Task.Factory.StartNew(() =>
+ {
+ try
+ {
+ Thread.Sleep(1500);
+ var diffs = _comparer.Compare();
+ Differences = new ObservableCollection<SqlDiff>(diffs);
+ }
+ catch (Exception ex)
+ {
+ ShowError(ex.Message);
+ }
+ finally
+ {
+ SelectedDifference = null;
+ InvalidateRelayCommands();
+ CloseStatus();
+ }
+ });
+ }
+
+ private void Commit()
+ {
+ OpenStatus("Committing difference...");
+
+ Task.Factory.StartNew(() =>
+ {
+ try
+ {
+ Thread.Sleep(1500);
+ SelectedDifference.Commit();
+
+ InvokeUINow(() => Differences.Remove(SelectedDifference));
+ }
+ catch (Exception ex)
+ {
+ ShowError(ex.Message);
+ }
+ finally
+ {
+ SelectedDifference = null;
+ InvalidateRelayCommands();
+ CloseStatus();
+ }
+ });
+ }
+
+ private void CommitAll()
+ {
+ OpenStatus("Committing all differences...");
+
+ Task.Factory.StartNew(() =>
+ {
+ try
+ {
+ Thread.Sleep(1500);
+
+ for (int i = 0; i < Differences.Count; i++)
+ {
+ var diff = Differences[i];
+ OpenStatus("Committing difference " + (Differences.IndexOf(diff) + 1) + "...");
+ diff.Commit();
+ InvokeUINow(() => Differences.Remove(diff));
+ i--;
+ }
+ }
+ catch (Exception ex)
+ {
+ ShowError(ex.Message);
+ }
+ finally
+ {
+ SelectedDifference = null;
+ InvalidateRelayCommands();
+ CloseStatus();
+ }
+ });
+ }
+
+ private void BrowseSlaveDB()
+ {
+ String file = BrowseForFilePath();
+ if (file != null)
+ {
+ _slaveDBFile = file;
+ SlaveDBName = Path.GetFileName(file);
+ InvalidateRelayCommands();
+ }
+ }
+
+ private void BrowseMasterDB()
+ {
+ String file = BrowseForFilePath();
+ if (file != null)
+ {
+ _masterDBFile = file;
+ MasterDBName = Path.GetFileName(file);
+ InvalidateRelayCommands();
+ }
+ }
+
+ private String BrowseForFilePath()
+ {
+ OpenFileDialog dlg = new OpenFileDialog();
+ dlg.Title = "Select SQLite DataBase File";
+ dlg.Filter = "SQLite DataBase|*.db";
+ if (dlg.ShowDialog().Value)
+ {
+ return dlg.FileName;
+ }
+ return null;
+ }
+
+ private void OpenStatus(String status)
+ {
+ IsBusy = true;
+ Status = status;
+ }
+
+ private void CloseStatus()
+ {
+ IsBusy = false;
+ }
+
+ private void ShowError(String message)
+ {
+ MessageBox.Show(message, "Tango", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.No, MessageBoxOptions.DefaultDesktopOnly);
+ }
+
+ private void ShowInfo(String message)
+ {
+ MessageBox.Show(message, "Tango", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.No, MessageBoxOptions.DefaultDesktopOnly);
+ }
+ #endregion
+
+ #region Custom Logger
+
+ public class MainViewLogger : ILogger
+ {
+ public bool Enabled { get; set; }
+ public bool Immediate { get; set; }
+ public event Action<String> NewLog;
+
+ public MainViewLogger()
+ {
+ Enabled = true;
+ Immediate = true;
+ }
+
+ public void OnError(LogItemBase output)
+ {
+ NewLog?.Invoke(output.TimeStamp.ToTimeString() + ": " + output.GetMessage());
+ }
+
+ public void OnTrace(LogItemBase output)
+ {
+ NewLog?.Invoke(output.TimeStamp.ToTimeString() + ": " + output.GetMessage());
+ }
+ }
+
+ #endregion
+ }
+}