Skip to content

Commit

Permalink
Merge pull request #269 from nlogozzo/transaction-copy
Browse files Browse the repository at this point in the history
Make transaction copy
  • Loading branch information
nlogozzo authored Jan 30, 2023
2 parents 4b02e18 + 0d3806c commit 946f129
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 9 deletions.
2 changes: 1 addition & 1 deletion NickvisionMoney.GNOME/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Program()
_mainWindowController.AppInfo.ShortName = "Denaro";
_mainWindowController.AppInfo.Description = $"{_mainWindowController.Localizer["Description"]}.";
_mainWindowController.AppInfo.Version = "2023.2.0-beta1";
_mainWindowController .AppInfo.Changelog = "<ul><li>Added the ability to sort transactions by amount</li><li>LC_MONETARY and LC_TIME will now be respected</li><li>Added the ability to transfer money between accounts with different currencies by providing a conversion rate in TransferDialog</li><li>Recent accounts are now available to select from the TransferDialog</li><li>Added a \"New Window\" action to the main menu</li></ul>";
_mainWindowController .AppInfo.Changelog = "<ul><li>Added the ability to sort transactions by amount</li><li>LC_MONETARY and LC_TIME will now be respected</li><li>Added the ability to transfer money between accounts with different currencies by providing a conversion rate in TransferDialog</li><li>Added the ability to copy individual transactions</li><li>Recent accounts are now available to select from the TransferDialog</li><li>Added a \"New Window\" action to the main menu</li></ul>";
_mainWindowController.AppInfo.GitHubRepo = new Uri("https://github.com/nlogozzo/NickvisionMoney");
_mainWindowController.AppInfo.IssueTracker = new Uri("https://github.com/nlogozzo/NickvisionMoney/issues/new");
_mainWindowController.AppInfo.SupportUrl = new Uri("https://github.com/nlogozzo/NickvisionMoney/discussions");
Expand Down
37 changes: 37 additions & 0 deletions NickvisionMoney.GNOME/Views/AccountView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,38 @@ private void NewTransaction(Gio.SimpleAction sender, EventArgs e)
};
}

/// <summary>
/// Occurs when creation of transaction copy was requested
/// </summary>
/// <param name="source">Source transaction for copy</param>
private void CopyTransaction(Transaction source)
{
using var transactionController = _controller.CreateTransactionDialogController(source);
var transactionDialog = new TransactionDialog(transactionController, _parentWindow);
transactionDialog.Show();
transactionDialog.OnResponse += async (sender, e) =>
{
if (transactionController.Accepted)
{
//Start Spinner
_statusPageNoTransactions.SetVisible(false);
_scrollTransactions.SetVisible(true);
_overlayMain.SetOpacity(0.0);
_binSpinner.SetVisible(true);
_spinner.Start();
_scrollPane.SetSensitive(false);
//Work
await Task.Run(async () => await _controller.AddTransactionAsync(transactionController.Transaction));
//Stop Spinner
_spinner.Stop();
_binSpinner.SetVisible(false);
_overlayMain.SetOpacity(1.0);
_scrollPane.SetSensitive(true);
}
transactionDialog.Destroy();
};
}

/// <summary>
/// Occurs when the edit transaction item is activated
/// </summary>
Expand All @@ -910,6 +942,11 @@ private void EditTransaction(object? sender, uint id)
{
if (transactionController.Accepted)
{
if (transactionController.CopyRequested)
{
CopyTransaction(transactionController.Transaction);
return;
}
if (_controller.GetIsSourceRepeatTransaction(id) && transactionController.OriginalRepeatInterval != TransactionRepeatInterval.Never)
{
if (transactionController.OriginalRepeatInterval != transactionController.Transaction.RepeatInterval)
Expand Down
11 changes: 9 additions & 2 deletions NickvisionMoney.GNOME/Views/TransactionDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,17 @@ public TransactionDialog(TransactionDialogController controller, Gtk.Window pare
_dialog.SetModal(true);
_dialog.AddResponse("cancel", _controller.Localizer["Cancel"]);
_dialog.SetCloseResponse("cancel");
if (_controller.CanCopy)
{
_dialog.AddResponse("copy", _controller.Localizer["MakeCopy"]);
}
_dialog.AddResponse("ok", _controller.Localizer["OK"]);
_dialog.SetDefaultResponse("ok");
_dialog.SetResponseAppearance("ok", Adw.ResponseAppearance.Suggested);
_dialog.OnResponse += (sender, e) => _controller.Accepted = e.Response == "ok";
_dialog.OnResponse += (sender, e) => {
_controller.Accepted = e.Response != "cancel";
_controller.CopyRequested = e.Response == "copy";
};
//Main Box
_boxMain = Gtk.Box.New(Gtk.Orientation.Vertical, 10);
//Main Preferences Group
Expand Down Expand Up @@ -319,7 +326,7 @@ public TransactionDialog(TransactionDialogController controller, Gtk.Window pare
{
_btnRepeatEndDate.SetLabel(_controller.Localizer["NoEndDate"]);
}
if(_controller.Transaction.GroupId == -1)
if (_controller.Transaction.GroupId == -1)
{
_rowGroup.SetSelected(0);
}
Expand Down
1 change: 1 addition & 0 deletions NickvisionMoney.GNOME/org.nickvision.money.metainfo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<p>- Added the ability to sort transactions by amount</p>
<p>- LC_MONETARY and LC_TIME will now be respected</p>
<p>- Added the ability to transfer money between accounts with different currencies by providing a conversion rate in TransferDialog</p>
<p>- Added the ability to copy individual transactions</p>
<p>- Recent accounts are now available to select from the TransferDialog</p>
<p>- Added a "New Window" action to the main menu</p>
</description>
Expand Down
34 changes: 31 additions & 3 deletions NickvisionMoney.Shared/Controllers/AccountViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ public async Task StartupAsync()
public AccountSettingsDialogController CreateAccountSettingsDialogController() => new AccountSettingsDialogController(_account.Metadata, _account.NeedsAccountSetup, Localizer);

/// <summary>
/// Creates a new TransactionDialogController
/// Creates a new TransactionDialogController for a new transaction
/// </summary>
/// <returns>The new TransactionDialogController</returns>
public TransactionDialogController CreateTransactionDialogController()
Expand All @@ -428,7 +428,7 @@ public TransactionDialogController CreateTransactionDialogController()
}

/// <summary>
/// Creates a new TransactionDialogController
/// Creates a new TransactionDialogController for an existing transaction
/// </summary>
/// <param name="id">The id of the existing transaction</param>
/// <returns>The TransactionDialogController for the existing transaction</returns>
Expand All @@ -439,7 +439,35 @@ public TransactionDialogController CreateTransactionDialogController(uint id)
{
groups.Add(pair.Key, pair.Value.Name);
}
return new TransactionDialogController(_account.Transactions[id], groups, _account.Metadata.DefaultTransactionType, TransactionDefaultColor, CultureForNumberString, CultureForDateString, Localizer);
return new TransactionDialogController(_account.Transactions[id], groups, _account.Metadata.DefaultTransactionType, TransactionDefaultColor, true, CultureForNumberString, CultureForDateString, Localizer);
}

/// <summary>
/// Creates a new TransactionDialogController for a copy transaction
/// </summary>
/// <param name="source">The transaction to copy</param>
/// <returns>The TransactionDialogController for the copied transaction</returns>
public TransactionDialogController CreateTransactionDialogController(Transaction source)
{
var groups = new Dictionary<uint, string>();
foreach (var pair in _account.Groups)
{
groups.Add(pair.Key, pair.Value.Name);
}
var toCopy = new Transaction(_account.NextAvailableTransactionId)
{
Date = source.Date,
Description = $"{source.Description} {Localizer["Copy", "Transaction"]}",
Type = source.Type,
RepeatInterval = source.RepeatInterval,
Amount = source.Amount,
GroupId = source.GroupId,
RGBA = source.RGBA,
Receipt = source.Receipt,
RepeatFrom = source.RepeatFrom,
RepeatEndDate = source.RepeatEndDate
};
return new TransactionDialogController(toCopy, groups, _account.Metadata.DefaultTransactionType, TransactionDefaultColor, false, CultureForNumberString, CultureForDateString, Localizer);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,18 @@ public class TransactionDialogController : IDisposable
/// </summary>
public Dictionary<uint, string> Groups { get; init; }
/// <summary>
/// Whether or not this transaction can be copied
/// </summary>
public bool CanCopy { get; init; }
/// <summary>
/// Whether or not the dialog was accepted (response)
/// </summary>
public bool Accepted { get; set; }
/// <summary>
/// Whether or not there was a request to make a copy of transaction
/// </summary>
public bool CopyRequested { get; set; }
/// <summary>
/// The original repeat interval of a transaction
/// </summary>
public TransactionRepeatInterval OriginalRepeatInterval { get; private set; }
Expand Down Expand Up @@ -83,16 +91,19 @@ public class TransactionDialogController : IDisposable
/// <param name="groups">The list of groups in the account</param>
/// <param name="transactionDefaultType">A default type for the transaction</param>
/// <param name="transactionDefaultColor">A default color for the transaction</param>
/// <param name="canCopy">Whether or not the transaction can be copied</param>
/// <param name="cultureNumber">The CultureInfo to use for the amount string</param>
/// <param name="cultureDate">The CultureInfo to use for the date string</param>
/// <param name="localizer">The Localizer of the app</param>
internal TransactionDialogController(Transaction transaction, Dictionary<uint, string> groups, TransactionType transactionDefaultType, string transactionDefaultColor, CultureInfo cultureNumber, CultureInfo cultureDate, Localizer localizer)
internal TransactionDialogController(Transaction transaction, Dictionary<uint, string> groups, TransactionType transactionDefaultType, string transactionDefaultColor, bool canCopy, CultureInfo cultureNumber, CultureInfo cultureDate, Localizer localizer)
{
_disposed = false;
Localizer = localizer;
Transaction = (Transaction)transaction.Clone();
Groups = groups;
CanCopy = canCopy;
Accepted = false;
CopyRequested = false;
OriginalRepeatInterval = Transaction.RepeatInterval;
CultureForNumberString = cultureNumber;
CultureForDateString = cultureDate;
Expand All @@ -119,7 +130,9 @@ internal TransactionDialogController(uint id, Dictionary<uint, string> groups, T
Localizer = localizer;
Transaction = new Transaction(id);
Groups = groups;
CanCopy = false;
Accepted = false;
CopyRequested = false;
OriginalRepeatInterval = Transaction.RepeatInterval;
CultureForNumberString = cultureNumber;
CultureForDateString = cultureDate;
Expand Down
1 change: 1 addition & 0 deletions NickvisionMoney.Shared/Models/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,7 @@ private async Task<List<uint>> ImportFromCSVAsync(string path)
{
continue;
}
amount = Math.Abs(amount);
//Get RGBA
var rgba = fields[8];
//Get Group Id
Expand Down
8 changes: 8 additions & 0 deletions NickvisionMoney.Shared/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,14 @@
<value>Upload</value>
</data>

<data name="MakeCopy" xml:space="preserve">
<value>Make a Copy</value>
</data>

<data name="Copy.Transaction" xml:space="preserve">
<value>(Copy)</value>
</data>

<!--TransferDialog-->
<data name="Transfer" xml:space="preserve">
<value>Transfer</value>
Expand Down
2 changes: 1 addition & 1 deletion NickvisionMoney.WinUI/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public App()
_mainWindowController.AppInfo.ShortName = "Denaro";
_mainWindowController.AppInfo.Description = $"{_mainWindowController.Localizer["Description"]}.";
_mainWindowController.AppInfo.Version = "2023.2.0-beta1";
_mainWindowController.AppInfo.Changelog = "- Added the ability to sort transactions by amount\n- Added the ability to transfer money between accounts with different currencies\nby providing a conversion rate in TransferDialog\n- Recent accounts are now available to select from the TransferDialog\n- Updated AccountView design with a new collapsible sidebar\n- Fixed an issue where dates in TransactionDialog were not displayed with locale settings";
_mainWindowController.AppInfo.Changelog = "- Added the ability to sort transactions by amount\n- Added the ability to transfer money between accounts with different currencies\nby providing a conversion rate in TransferDialog\n- Added the ability to copy individual transactions\n- Recent accounts are now available to select from the TransferDialog\n- Updated AccountView design with a new collapsible sidebar\n- Fixed an issue where dates in TransactionDialog were not displayed with locale settings";
_mainWindowController.AppInfo.GitHubRepo = new Uri("https://github.com/nlogozzo/NickvisionMoney");
_mainWindowController.AppInfo.IssueTracker = new Uri("https://github.com/nlogozzo/NickvisionMoney/issues/new");
_mainWindowController.AppInfo.SupportUrl = new Uri("https://github.com/nlogozzo/NickvisionMoney/discussions");
Expand Down
28 changes: 28 additions & 0 deletions NickvisionMoney.WinUI/Views/AccountView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,29 @@ private async void NewTransaction(object? sender, RoutedEventArgs e)
}
}

/// <summary>
/// Occurs when creation of transaction copy was requested
/// </summary>
/// <param name="source">Source transaction for copy</param>
private async Task CopyTransactionAsync(Transaction source)
{
using var transactionController = _controller.CreateTransactionDialogController(source);
var transactionDialog = new TransactionDialog(transactionController, _initializeWithWindow)
{
XamlRoot = Content.XamlRoot
};
if (await transactionDialog.ShowAsync())
{
//Start Loading
LoadingCtrl.IsLoading = true;
//Work
await Task.Delay(50);
await _controller.AddTransactionAsync(transactionController.Transaction);
//Done Loading
LoadingCtrl.IsLoading = false;
}
}

/// <summary>
/// Occurs when the edit transaction action is triggered
/// </summary>
Expand All @@ -350,6 +373,11 @@ private async void EditTransaction(object? sender, uint transactionId)
};
if (await transactionDialog.ShowAsync())
{
if(transactionController.CopyRequested)
{
await CopyTransactionAsync(transactionController.Transaction);
return;
}
if(_controller.GetIsSourceRepeatTransaction(transactionId) && transactionController.OriginalRepeatInterval != TransactionRepeatInterval.Never)
{
if (transactionController.OriginalRepeatInterval != transactionController.Transaction.RepeatInterval)
Expand Down
10 changes: 9 additions & 1 deletion NickvisionMoney.WinUI/Views/TransactionDialog.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public TransactionDialog(TransactionDialogController controller, Action<object>
Title = $"{_controller.Localizer["Transaction"]} - {_controller.Transaction.Id}";
CloseButtonText = _controller.Localizer["Cancel"];
PrimaryButtonText = _controller.Localizer["OK"];
if(_controller.CanCopy)
{
SecondaryButtonText = _controller.Localizer["MakeCopy"];
}
TxtDescription.Header = _controller.Localizer["Description", "Field"];
TxtDescription.PlaceholderText = _controller.Localizer["Description", "Placeholder"];
TxtAmount.Header = $"{_controller.Localizer["Amount", "Field"]} - {_controller.CultureForNumberString.NumberFormat.CurrencySymbol} ({_controller.CultureForNumberString.NumberFormat.NaNSymbol})";
Expand Down Expand Up @@ -125,6 +129,10 @@ public Color SelectedColor
_controller.Accepted = false;
return false;
}
if(result == ContentDialogResult.Secondary)
{
_controller.CopyRequested = true;
}
_controller.Accepted = true;
return true;
}
Expand All @@ -134,7 +142,7 @@ public Color SelectedColor
/// </summary>
private void Validate()
{
var checkStatus = _controller.UpdateTransaction(DateOnly.FromDateTime(CalendarDate.Date!.Value.Date), TxtDescription.Text, (TransactionType)CmbType.SelectedIndex, CmbRepeatInterval.SelectedIndex, (string)CmbGroup.SelectedItem, ColorHelpers.ToRGBA(BtnColor.SelectedColor), TxtAmount.Text, _receiptPath, CalendarRepeatEndDate.Date == null ? null : DateOnly.FromDateTime(CalendarRepeatEndDate.Date!.Value.Date));
var checkStatus = _controller.UpdateTransaction(DateOnly.FromDateTime(CalendarDate.Date!.Value.Date), TxtDescription.Text, (TransactionType)CmbType.SelectedIndex, CmbRepeatInterval.SelectedIndex, (string)CmbGroup.SelectedItem, ColorHelpers.ToRGBA(SelectedColor), TxtAmount.Text, _receiptPath, CalendarRepeatEndDate.Date == null ? null : DateOnly.FromDateTime(CalendarRepeatEndDate.Date!.Value.Date));
TxtDescription.Header = _controller.Localizer["Description", "Field"];
TxtAmount.Header = $"{_controller.Localizer["Amount", "Field"]} - {_controller.CultureForNumberString.NumberFormat.CurrencySymbol} {(string.IsNullOrEmpty(_controller.CultureForNumberString.NumberFormat.NaNSymbol) ? "" : $"({_controller.CultureForNumberString.NumberFormat.NaNSymbol})")}";
CalendarRepeatEndDate.Header = _controller.Localizer["TransactionRepeatEndDate", "Field"];
Expand Down

0 comments on commit 946f129

Please sign in to comment.