Skip to content

Commit

Permalink
[Improved + Fixed] Duplicate data on remote database
Browse files Browse the repository at this point in the history
  • Loading branch information
KaustubhPatange committed Nov 25, 2021
1 parent ffe6c88 commit a8a41b8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 18 deletions.
49 changes: 36 additions & 13 deletions XClipper.App/Data/helpers/AppSingleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,43 +173,66 @@ public void InsertContent(TableCopy model, bool pushToDatabase = true)
if (model == null) return;

var list = ClipData;


bool insertToLocalDatabase = true;
foreach (var c in list)
{
if (c.ContentType == model.ContentType)
{
switch (model.ContentType)
{
case ContentType.Text:
if (model.Text == c.Text) return;
if (model.Text == c.Text)
{
insertToLocalDatabase = false;
goto insertActualContent;
}
break;
case ContentType.Image:
if (model.ImagePath == c.ImagePath) return;
if (model.ImagePath == c.ImagePath)
{
insertToLocalDatabase = false;
goto insertActualContent;
}
break;
case ContentType.Files:
if (model.LongText == c.LongText) return;
if (model.LongText == c.LongText)
{
insertToLocalDatabase = false;
goto insertActualContent;
}
break;
}
}
}

// Implementation of setting TotalClipLength
if (list.Count >= TotalClipLength)
insertActualContent:

if (insertToLocalDatabase)
{
var lastItem = list[list.Count - 1];
dataDB.Delete(lastItem);
FirebaseSingletonV2.GetInstance.RemoveImage(Path.GetFileName(lastItem.ImagePath)).RunAsync(); // TODO: See if you really want to delete the clip from online database as well.
}
// Implementation of setting TotalClipLength
if (list.Count >= TotalClipLength)
{
var lastItem = list[list.Count - 1];
dataDB.Delete(lastItem);
FirebaseSingletonV2.GetInstance.RemoveImage(Path.GetFileName(lastItem.ImagePath)).RunAsync(); // TODO: See if you really want to delete the clip from online database as well.
}

dataDB.Insert(model);
dataDB.Insert(model);
}

if (pushToDatabase)
{
if (model.ContentType == ContentType.Text && model.RawText != null) FirebaseHelper.AddContent(model.RawText);
FirebaseSingletonV2.GetInstance.AddImage(model.ContentType == ContentType.Image ? model.ImagePath : null).RunAsync();
InsertDirectFirebase(model);
}
}

public void InsertDirectFirebase(TableCopy model)
{
if (model.ContentType == ContentType.Text && model.RawText != null) FirebaseHelper.AddContent(model.RawText);
FirebaseSingletonV2.GetInstance.AddImage(model.ContentType == ContentType.Image ? model.ImagePath : null).RunAsync();
}

public void InsertTextClipNoUpdate(string UnEncryptedText)
{
InsertContent(CreateTable(UnEncryptedText, ContentTypes.Text), false);
Expand Down
18 changes: 13 additions & 5 deletions XClipper.App/Data/helpers/FirebaseSingletonV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using FireSharp.Core.Interfaces;
using System.Collections.Generic;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using FireSharp.Core.Config;
using FireSharp.Core;
Expand Down Expand Up @@ -889,13 +891,19 @@ public async Task AddClip(string? Text)
// Remove clip if greater than item
if (clips.Count > DatabaseMaxItem)
clips.RemoveAt(0);

// Add data from current [Text]
clips.Add(new Clip { data = Text.EncryptBase64(DatabaseEncryptPassword), time = DateTime.Now.ToFormattedDateTime(false) });


addStack.Insert(0, Text);

// Find if duplicate exists (if yes then skip else add the current text)
var decryptedClips = clips.Select(c => c.data.DecryptBase64(DatabaseEncryptPassword)).ToList();

// Also add data from stack
foreach (var stackText in addStack)
clips.Add(new Clip { data = stackText.EncryptBase64(DatabaseEncryptPassword), time = DateTime.Now.ToFormattedDateTime(false) });
{
bool duplicateExists = decryptedClips.Any(c => c == stackText);
if (!duplicateExists)
clips.Add(new Clip { data = stackText.EncryptBase64(DatabaseEncryptPassword), time = DateTime.Now.ToFormattedDateTime(false) });
}

// Clear the stack after adding them all.
addStack.Clear();
Expand Down
1 change: 1 addition & 0 deletions XClipper.App/UI/ClipWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,7 @@ private void SetCurrentClip()
break;
}
CollectionViewSource.GetDefaultView(_lvClip.ItemsSource).Refresh();
AppSingleton.GetInstance.InsertDirectFirebase(clip);
});
}

Expand Down
3 changes: 3 additions & 0 deletions scripts/changes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<!-- Loaded from scripts\changes.txt -->
<!-- Make sure you update version number in XClipper.sln for this new release -->
- Added: Custom toggle visibility for viewing password in settings.
- Improved: Set current clipboard will push data to remote database.
- Improved: Clean migration when changing end-to-end password & improved more concurrency.
- Fixed: Incorrect setting of end to end password.
- Fixed: Sync service not deinitializing when authentication is failed.
- Fixed: Mobile & Desktop OAuth client Id not changing.
- Fixed: If data exists in database it does push it to remote database.

0 comments on commit a8a41b8

Please sign in to comment.