From d958dc6bcec92d02c3b8920129223052c4299fd5 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 21 Sep 2018 21:53:04 -0400 Subject: [PATCH] replace vs created on save --- .../Abstractions/Services/ICipherService.cs | 2 +- src/App/Services/CipherService.cs | 9 +++++---- src/App/Services/SyncService.cs | 5 +++-- src/iOS/AppDelegate.cs | 18 +++++++++++++----- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/App/Abstractions/Services/ICipherService.cs b/src/App/Abstractions/Services/ICipherService.cs index 3bbe342f1..501e491e5 100644 --- a/src/App/Abstractions/Services/ICipherService.cs +++ b/src/App/Abstractions/Services/ICipherService.cs @@ -16,7 +16,7 @@ namespace Bit.App.Abstractions Task> GetAllByCollectionAsync(string collectionId); Task, IEnumerable, IEnumerable>> GetAllAsync(string uriString); Task> SaveAsync(Cipher cipher); - Task UpsertDataAsync(CipherData cipher, bool sendMessage); + Task UpsertDataAsync(CipherData cipher, bool sendMessage, bool created); Task DeleteAsync(string id); Task DeleteDataAsync(string id, bool sendMessage); Task DownloadAndDecryptAttachmentAsync(string url, string orgId = null); diff --git a/src/App/Services/CipherService.cs b/src/App/Services/CipherService.cs index 0f39e6cb4..3d533ae42 100644 --- a/src/App/Services/CipherService.cs +++ b/src/App/Services/CipherService.cs @@ -255,7 +255,7 @@ namespace Bit.App.Services if(response.Succeeded) { var data = new CipherData(response.Result, _authService.UserId); - await UpsertDataAsync(data, true); + await UpsertDataAsync(data, true, cipher.Id == null); cipher.Id = data.Id; } else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden @@ -267,14 +267,15 @@ namespace Bit.App.Services return response; } - public async Task UpsertDataAsync(CipherData cipher, bool sendMessage) + public async Task UpsertDataAsync(CipherData cipher, bool sendMessage, bool created) { await _cipherRepository.UpsertAsync(cipher); - CachedCiphers = null; + CachedCiphers = null; _appSettingsService.ClearCiphersCache = true; if(sendMessage && Application.Current != null) { - MessagingCenter.Send(Application.Current, "UpsertedCipher", cipher.Id); + MessagingCenter.Send(Application.Current, "UpsertedCipher", + new Tuple(cipher.Id, created)); } } diff --git a/src/App/Services/SyncService.cs b/src/App/Services/SyncService.cs index 00ceb0992..f44054dac 100644 --- a/src/App/Services/SyncService.cs +++ b/src/App/Services/SyncService.cs @@ -83,8 +83,9 @@ namespace Bit.App.Services try { + var existingCipher = await _cipherService.GetByIdAsync(cipher.Result.Id); var cipherData = new CipherData(cipher.Result, _authService.UserId); - await _cipherService.UpsertDataAsync(cipherData, true).ConfigureAwait(false); + await _cipherService.UpsertDataAsync(cipherData, true, existingCipher == null).ConfigureAwait(false); var localAttachments = (await _attachmentRepository.GetAllByCipherIdAsync(cipherData.Id) .ConfigureAwait(false)); @@ -444,7 +445,7 @@ namespace Bit.App.Services localCiphers[serverCipher.Value.Id] : null; var data = new CipherData(serverCipher.Value, _authService.UserId); - await _cipherService.UpsertDataAsync(data, false).ConfigureAwait(false); + await _cipherService.UpsertDataAsync(data, false, false).ConfigureAwait(false); if(serverCipher.Value.Attachments != null) { diff --git a/src/iOS/AppDelegate.cs b/src/iOS/AppDelegate.cs index ec9470f2f..c8a3b2be5 100644 --- a/src/iOS/AppDelegate.cs +++ b/src/iOS/AppDelegate.cs @@ -155,18 +155,26 @@ namespace Bit.iOS } }); - MessagingCenter.Subscribe( - Xamarin.Forms.Application.Current, "UpsertedCipher", async (sender, id) => + MessagingCenter.Subscribe>( + Xamarin.Forms.Application.Current, "UpsertedCipher", async (sender, data) => { if(await ASHelpers.IdentitiesCanIncremental()) { - var identity = await ASHelpers.GetCipherIdentityAsync(id, _cipherService); + var identity = await ASHelpers.GetCipherIdentityAsync(data.Item1, _cipherService); if(identity == null) { return; } - await ASCredentialIdentityStore.SharedStore.SaveCredentialIdentitiesAsync( - new ASPasswordCredentialIdentity[] { identity }); + if(data.Item2) + { + await ASCredentialIdentityStore.SharedStore.SaveCredentialIdentitiesAsync( + new ASPasswordCredentialIdentity[] { identity }); + } + else + { + await ASCredentialIdentityStore.SharedStore.ReplaceCredentialIdentitiesAsync( + new ASPasswordCredentialIdentity[] { identity }); + } return; } await ASHelpers.ReplaceAllIdentities(_cipherService);