Esempio n. 1
0
        public void ExportAccounts()
        {
            try
            {
                if (AccountViewModels.Any(vm => vm.HasChanges))
                {
                    DialogService.Notify("You have unsaved changes. Please save changes before exporting accounts.");

                    return;
                }

                using (var dialog = new FolderBrowserDialog())
                {
                    if (dialog.ShowDialog() != DialogResult.OK)
                    {
                        return;
                    }

                    var filePath = $@"{dialog.SelectedPath}\exported-accounts-{DateTime.Now.Ticks}.json";

                    string encrypt(string text) => CryptoService.Encrypt(StorageService.GetSecretKey(Password), text);

                    var data = AccountViewModels.Select(vm => vm.ViewModelToModel(encrypt));

                    File.WriteAllText(filePath, JsonConvert.SerializeObject(data));

                    Status = "Accounts exported...";
                }
            }
            catch (Exception e)
            {
                DialogService.Exception(e);
            }
        }
Esempio n. 2
0
        public override void CanClose(Action <bool> callback)
        {
            if (AccountViewModels.Any(vm => vm.HasChanges))
            {
                callback(DialogService.Confirm("You have unsaved changes. Are you sure you want to exit?"));

                return;
            }

            callback(true);
        }
Esempio n. 3
0
        public void ImportAccounts()
        {
            try
            {
                if (AccountViewModels.Any(vm => vm.HasChanges))
                {
                    DialogService.Notify("You have unsaved changes. Please save changes before importing accounts.");

                    return;
                }

                using (var dialog = new OpenFileDialog
                {
                    Filter = "JSON files (*.json)|*.json"
                })
                {
                    if (dialog.ShowDialog() != DialogResult.OK)
                    {
                        return;
                    }

                    var json = File.ReadAllText(dialog.FileName);

                    var data = JsonConvert.DeserializeObject <Dtos.AccountDto[]>(json);

                    string dencrypt(string text) => CryptoService.Decrypt(StorageService.GetSecretKey(Password), text);

                    foreach (var item in data)
                    {
                        var found = AccountViewModels.Any(vm => vm.AccountName == item.AccountName);

                        item.AccountName = found ? $"{item.AccountName} - duplicate" : item.AccountName;

                        AccountViewModels.Add(new AccountViewModel(item, dencrypt, true));
                    }

                    Account = AccountViewModels.FirstOrDefault();

                    Status = "Accounts imported...";
                }
            }
            catch (Exception e)
            {
                DialogService.Exception(e);
            }
        }