Korero – Blame information for rev 2

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Linq;
5 using System.Threading;
6 using System.Threading.Tasks;
7 using System.Windows.Forms;
8 using Korero.Communication;
9 using Korero.Properties;
10 using Korero.Utilities;
11 using Serilog;
12  
13 namespace Korero.Economy
14 {
15 public partial class EconomyForm : Form
16 {
17 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
18  
19 private readonly CancellationToken _cancellationToken;
20  
21 private readonly CancellationTokenSource _cancellationTokenSource;
22  
23 private readonly MainForm _mainForm;
24  
25 private readonly MqttCommunication _mqttCommunication;
26  
27 private PaymentForm _paymentForm;
28  
29 #endregion
30  
31 #region Constructors, Destructors and Finalizers
32  
33 public EconomyForm()
34 {
35 InitializeComponent();
36 Utilities.WindowState.FormTracker.Track(this);
37  
38 _cancellationTokenSource = new CancellationTokenSource();
39 _cancellationToken = _cancellationTokenSource.Token;
40 }
41  
42 public EconomyForm(MainForm mainForm, MqttCommunication mqttCommunication) : this()
43 {
44 _mainForm = mainForm;
45 _mqttCommunication = mqttCommunication;
46  
47 _mqttCommunication.NotificationReceived += MqttCommunication_NotificationReceived;
48 }
49  
50 /// <summary>
51 /// Clean up any resources being used.
52 /// </summary>
53 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
54 protected override void Dispose(bool disposing)
55 {
56 if (disposing && components != null)
57 {
58 components.Dispose();
59 }
60  
61 _mqttCommunication.NotificationReceived -= MqttCommunication_NotificationReceived;
62 base.Dispose(disposing);
63 }
64  
65 #endregion
66  
67 #region Event Handlers
2 office 68 private void EconomyForm_Load(object sender, EventArgs e)
69 {
70 Utilities.WindowState.FormTracker.Track(this);
71 }
1 office 72 private void MqttCommunication_NotificationReceived(object sender, MqttNotificationEventArgs e)
73 {
74 switch (e.Notification["notification"])
75 {
76 case "balance":
77 textBox1.InvokeIfRequired(textBox => { textBox.Text = e.Notification["balance"]; });
78 break;
79 }
80 }
81  
82 private void Button1_Click(object sender, EventArgs e)
83 {
84 if (_paymentForm != null)
85 {
86 return;
87 }
88  
89 _paymentForm = new PaymentForm(_mainForm, _mainForm.MqttCommunication);
90 _paymentForm.Closing += PaymentForm_Closing;
91 _paymentForm.Show();
92 }
93  
94 private void PaymentForm_Closing(object sender, CancelEventArgs e)
95 {
96 if (_paymentForm == null)
97 {
98 return;
99 }
100  
101 _paymentForm.Closing -= PaymentForm_Closing;
102 _paymentForm.Dispose();
103 _paymentForm = null;
104 }
105  
106 private async void EconomyForm_Shown(object sender, EventArgs e)
107 {
108 await GetBalance();
109 }
110  
111 private void EconomyForm_FormClosing(object sender, FormClosingEventArgs e)
112 {
113 _cancellationTokenSource.Cancel();
114 }
115  
116 #endregion
117  
118 #region Private Methods
119  
120 private async Task GetBalance()
121 {
122 var data = new Dictionary<string, string>
123 {
124 {"command", "getbalance"},
125 {"group", Settings.Default.Group},
126 {"password", Settings.Default.Password}
127 };
128  
129 var callback = await _mqttCommunication.SendCommand(new Command(data), _cancellationToken);
130  
131 if (callback == null || !callback.Success)
132 {
133 if (callback != null)
134 {
135 Log.Warning("Command {Command} has failed with {Error}.",
136 callback.Command, callback.Error);
137 }
138  
139 return;
140 }
141  
142 textBox1.InvokeIfRequired(textBox =>
143 {
144 var balance = callback.Data.FirstOrDefault();
145  
146 textBox.Text = balance;
147 });
148 }
149  
150 #endregion
2 office 151  
152  
1 office 153 }
154 }