aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Modules/Tango.PPC.BackupRestore/ViewModels/BackupViewVM.cs
blob: ef5b3810c5a9e72cc41ac098d2d8cd8dd3bb8173 (plain)
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.Commands;
using Tango.Core.DI;
using Tango.Explorer;
using Tango.PPC.BackupRestore.Views;
using Tango.PPC.Common;
using Tango.PPC.Common.BackupRestore;
using Tango.PPC.Storage;
using static Tango.PPC.BackupRestore.ViewModels.BackupErrorViewVM;

namespace Tango.PPC.BackupRestore.ViewModels
{
    public class BackupViewVM : PPCViewModel
    {
        private String _backupFileName;

        [TangoInject]
        public IBackupManager BackupManager { get; set; }

        private BackupRestoreProgressEventArgs _currentBackupProgress;
        public BackupRestoreProgressEventArgs CurrentBackupProgress
        {
            get { return _currentBackupProgress; }
            set { _currentBackupProgress = value; RaisePropertyChangedAuto(); }
        }

        private bool _isBackupJobs;
        public bool IsBackupJobs
        {
            get { return _isBackupJobs; }
            set
            {
                if (value)
                {
                    _isBackupJobs = value;
                    RaisePropertyChangedAuto();
                    _isBackupFull = false;
                    RaisePropertyChanged(nameof(IsBackupFull));
                }
                else
                {
                    RaisePropertyChangedAuto();
                }
            }
        }

        private bool _isBackupFull;
        public bool IsBackupFull
        {
            get { return _isBackupFull; }
            set
            {
                if (value)
                {
                    _isBackupFull = value;
                    RaisePropertyChangedAuto();
                    _isBackupJobs = false;
                    RaisePropertyChanged(nameof(IsBackupJobs));
                }
                else
                {
                    RaisePropertyChangedAuto();
                }
            }
        }

        private String _backupLocation;
        public String BackupLocation
        {
            get { return _backupLocation; }
            set { _backupLocation = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        private String _backupName;
        public String BackupName
        {
            get { return _backupName; }
            set { _backupName = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        public RelayCommand BackupCommand { get; set; }

        public RelayCommand BrowseBackupLocationCommand { get; set; }

        public BackupViewVM()
        {
            BrowseBackupLocationCommand = new RelayCommand(BrowseBackupLocation);
            BackupCommand = new RelayCommand(StartBackup, () => !String.IsNullOrWhiteSpace(BackupName) && BackupLocation != null);
            IsBackupJobs = true;
        }

        private async void StartBackup()
        {
            await NavigationManager.NavigateTo<BackupRestoreModule>(nameof(BackupProgressView), false);

            try
            {
                IsFree = false;
                NavigationManager.IsBackEnabled = false;
                await BackupManager.CreateBackup(_backupFileName, BackupName, new BackupSettings()
                {
                    Mode = IsBackupFull ? BackupMode.Full : BackupMode.Jobs,
                });
                await NavigationManager.NavigateTo<BackupRestoreModule>(nameof(BackupCompletedView), false);
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "The backup operation failed.");

                await NavigationManager.NavigateWithObject<BackupRestoreModule, BackupErrorView, BackupErrorNavigationObject>(new BackupErrorNavigationObject()
                {
                    Error = ex.FlattenMessage(),
                }, false);
            }
            finally
            {
                IsFree = true;
                NavigationManager.IsBackEnabled = true;
            }
        }

        public override void OnApplicationStarted()
        {

        }

        public override void OnApplicationReady()
        {
            base.OnApplicationReady();
            BackupManager.Progress += BackupManager_Progress;
        }

        private void BackupManager_Progress(object sender, BackupRestoreProgressEventArgs e)
        {
            CurrentBackupProgress = e;
        }

        private async void BrowseBackupLocation()
        {
            var result = await NavigationManager.
                NavigateForResult<StorageModule,
                Storage.Views.MainView, ExplorerFileItem,
                Storage.Models.StorageNavigationRequest>(
                new Storage.Models.StorageNavigationRequest()
                {
                    Intent = Storage.Models.StorageNavigationIntent.SaveFile,
                    DefaultFileName = $"Tango-Backup-{DateTime.Now.ToFileName()}",
                    Filter = ExplorerFileDefinition.Backup.Extension,
                    Title = "Select Destination Backup File",
                });

            if (result != null)
            {
                _backupFileName = result.Path + ExplorerFileDefinition.Backup.Extension;
                BackupLocation = result.Path + ExplorerFileDefinition.Backup.Extension;
            }
        }
    }
}