From b0c6c09ceaf5ae4a7271190dd6c0b4614f2096cf Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 20 Sep 2018 23:47:19 -0400 Subject: [PATCH] full sync and cipher modification messages --- src/App/Abstractions/Services/ICipherService.cs | 4 ++-- src/App/Services/CipherService.cs | 17 +++++++++++++---- src/App/Services/SyncService.cs | 10 ++++++---- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/App/Abstractions/Services/ICipherService.cs b/src/App/Abstractions/Services/ICipherService.cs index c9c2343b1..3bbe342f1 100644 --- a/src/App/Abstractions/Services/ICipherService.cs +++ b/src/App/Abstractions/Services/ICipherService.cs @@ -16,9 +16,9 @@ namespace Bit.App.Abstractions Task> GetAllByCollectionAsync(string collectionId); Task, IEnumerable, IEnumerable>> GetAllAsync(string uriString); Task> SaveAsync(Cipher cipher); - Task UpsertDataAsync(CipherData cipher); + Task UpsertDataAsync(CipherData cipher, bool sendMessage); Task DeleteAsync(string id); - Task DeleteDataAsync(string id); + Task DeleteDataAsync(string id, bool sendMessage); Task DownloadAndDecryptAttachmentAsync(string url, string orgId = null); Task> EncryptAndSaveAttachmentAsync(Cipher cipher, byte[] data, string fileName); Task UpsertAttachmentDataAsync(IEnumerable attachments); diff --git a/src/App/Services/CipherService.cs b/src/App/Services/CipherService.cs index a3cc4ce82..5c9f63f60 100644 --- a/src/App/Services/CipherService.cs +++ b/src/App/Services/CipherService.cs @@ -9,6 +9,7 @@ using Bit.App.Models.Data; using System.Net.Http; using Bit.App.Utilities; using System.Text.RegularExpressions; +using Xamarin.Forms; namespace Bit.App.Services { @@ -254,7 +255,7 @@ namespace Bit.App.Services if(response.Succeeded) { var data = new CipherData(response.Result, _authService.UserId); - await UpsertDataAsync(data); + await UpsertDataAsync(data, true); cipher.Id = data.Id; } else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden @@ -266,11 +267,15 @@ namespace Bit.App.Services return response; } - public async Task UpsertDataAsync(CipherData cipher) + public async Task UpsertDataAsync(CipherData cipher, bool sendMessage) { await _cipherRepository.UpsertAsync(cipher); CachedCiphers = null; _appSettingsService.ClearCiphersCache = true; + if(sendMessage) + { + MessagingCenter.Send(Application.Current, "UpsertedCipher", cipher.Id); + } } public async Task DeleteAsync(string id) @@ -278,7 +283,7 @@ namespace Bit.App.Services var response = await _cipherApiRepository.DeleteAsync(id); if(response.Succeeded) { - await DeleteDataAsync(id); + await DeleteDataAsync(id, true); } else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden || response.StatusCode == System.Net.HttpStatusCode.Unauthorized) @@ -289,11 +294,15 @@ namespace Bit.App.Services return response; } - public async Task DeleteDataAsync(string id) + public async Task DeleteDataAsync(string id, bool sendMessage) { await _cipherRepository.DeleteAsync(id); CachedCiphers = null; _appSettingsService.ClearCiphersCache = true; + if(sendMessage) + { + MessagingCenter.Send(Application.Current, "DeletedCipher", id); + } } public async Task DownloadAndDecryptAttachmentAsync(string url, string orgId = null) diff --git a/src/App/Services/SyncService.cs b/src/App/Services/SyncService.cs index d27887da5..00ceb0992 100644 --- a/src/App/Services/SyncService.cs +++ b/src/App/Services/SyncService.cs @@ -84,7 +84,7 @@ namespace Bit.App.Services try { var cipherData = new CipherData(cipher.Result, _authService.UserId); - await _cipherService.UpsertDataAsync(cipherData).ConfigureAwait(false); + await _cipherService.UpsertDataAsync(cipherData, true).ConfigureAwait(false); var localAttachments = (await _attachmentRepository.GetAllByCipherIdAsync(cipherData.Id) .ConfigureAwait(false)); @@ -181,7 +181,7 @@ namespace Bit.App.Services try { - await _cipherService.DeleteDataAsync(id).ConfigureAwait(false); + await _cipherService.DeleteDataAsync(id, true).ConfigureAwait(false); SyncCompleted(true); return true; } @@ -287,11 +287,13 @@ namespace Bit.App.Services domainsTask.Exception != null || profileTask.Exception != null) { SyncCompleted(false); + MessagingCenter.Send(Application.Current, "FullSyncCompleted", false); return false; } _settings.AddOrUpdateValue(Constants.LastSync, now); SyncCompleted(true); + MessagingCenter.Send(Application.Current, "FullSyncCompleted", true); return true; } @@ -442,7 +444,7 @@ namespace Bit.App.Services localCiphers[serverCipher.Value.Id] : null; var data = new CipherData(serverCipher.Value, _authService.UserId); - await _cipherService.UpsertDataAsync(data).ConfigureAwait(false); + await _cipherService.UpsertDataAsync(data, false).ConfigureAwait(false); if(serverCipher.Value.Attachments != null) { @@ -470,7 +472,7 @@ namespace Bit.App.Services { try { - await _cipherService.DeleteDataAsync(cipher.Value.Id).ConfigureAwait(false); + await _cipherService.DeleteDataAsync(cipher.Value.Id, false).ConfigureAwait(false); } catch(SQLite.SQLiteException) { } }