-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManage.xaml.cs
140 lines (122 loc) · 3.65 KB
/
Manage.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System.Collections.ObjectModel;
using System.Data;
using System.Data.Common;
using System.Reflection;
using MongoDB.Driver;
using Microsoft.Maui.Graphics.Text;
using MongoDB.Bson;
using System.Globalization;
using System.Collections.Specialized;
namespace FinanceApp;
public partial class Manage : ContentPage
{
private static Manage _instance;
private static readonly object _lock = new object();
public static Manage Instance
{
get
{
lock (_lock)
{
if (_instance == null)
{
_instance = new Manage();
}
return _instance;
}
}
}
private DatabaseLogic logic;
private const double ExchangeRatePLN = 0.23;
private const double ExchangeRateEUR = 4.30;
public static bool DataHasChanged { get; set; } = false;
/// <summary>
/// This class has two primary functions:
/// a. Manipulates database entries from the connected DatabaseLogic instance.
/// Two collections are created within DatabaseLogic: a user-modifiable one and a copy of it.
/// It compares these collections, generates lists of items for removal and addition, and modifies the database accordingly.
/// b. Exchanges currency based on the selected radio button.
/// A copy of FinanceData (the user-modifiable collection) is created in DatabaseLogic.
/// The due amount of each transaction is converted to the desired currency without altering the original collection.
/// The converted collection is displayed in the ListView instead of the original FinanceData.
/// </summary>
public Manage()
{
InitializeComponent();
InitializeAsync();
this.BindingContext = logic;
}
private async Task InitializeAsync()
{
if (logic == null) logic = await DatabaseLogic.GetInstanceAsync();
}
private void BtnFetch_Clicked(object sender, EventArgs e)
{
if (BusinessType.SelectedItem != null && !string.IsNullOrEmpty(Due.Text) && !string.IsNullOrEmpty(Company.Text))
{
var lv_Company = Company.Text;
var lv_Due = 0;
if (int.TryParse(Due.Text, out lv_Due) && !string.IsNullOrEmpty(Due.Text));
var lv_BusinessType = BusinessType.SelectedItem.ToString();
var lv_Currency = Currency.SelectedItem.ToString();
var lv_Date = DateTime.Now.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
logic.FinanceData.Add(new FinanceModel
{
due = lv_Due,
company = lv_Company,
businessType = lv_BusinessType,
currency = lv_Currency,
date = lv_Date
});
Lv.ItemsSource = null;
Lv.ItemsSource = logic.FinanceData;
Company.Text = "";
Due.Text = "";
DataHasChanged = true;
}
}
private void BtnDelete_Clicked(object sender, EventArgs e)
{
if (Lv.SelectedItem is FinanceModel selection)
{
logic.FinanceData.Remove(selection);
DataHasChanged = true;
}
}
private async void BtnUpdate_Clicked(object sender, EventArgs e)
{
await logic.UpdateDatabase();
}
private void CurrencyExchange(string CurrencyState)
{
logic.CurrencyEx.Clear();
foreach (var item in logic.FinanceData)
{
var newModel = new FinanceModel
{
due = item.due,
company = item.company,
businessType = item.businessType,
currency = item.currency,
date = item.date
};
if (item.currency == "EUR" && CurrencyState == "PLN")
{
newModel.currency = "PLN";
newModel.due = item.due * ExchangeRateEUR;
}
else if (item.currency == "PLN" && CurrencyState == "EUR")
{
newModel.currency = "EUR";
newModel.due = item.due * ExchangeRatePLN;
}
logic.CurrencyEx.Add(newModel);
}
Lv.ItemsSource = null;
Lv.ItemsSource = logic.CurrencyEx;
}
private void PLNtoEUR_CheckedChanged(object sender, CheckedChangedEventArgs e)
{
CurrencyExchange(PLNtoEUR.IsChecked ? "EUR" : "PLN");
}
}