Skip to content

Commit a7027a0

Browse files
committed
Add delete functionality
1 parent 6e5a9c2 commit a7027a0

6 files changed

Lines changed: 335 additions & 57 deletions

File tree

app/Project Tracker/Project Tracker/EditProgram.xaml.cs

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ public EditProgram() {
5959

6060
Startup();
6161
}
62-
63-
// TODO: You can change values from the function. No need to repeat the code. Optimize NOW
6462
/// <summary>
6563
/// Adds a row to a certain table
6664
/// </summary>
@@ -134,7 +132,7 @@ private void SelectionChange(int oldPos, int newPos, int rowsAdded, Table table,
134132
}
135133
}
136134
}
137-
else {
135+
else if (rowsAdded == 1) {
138136
TableRow selectedRow = table.RowGroups[0].Rows[0];
139137
selectedRow.Background = new SolidColorBrush(selectionColor);
140138

@@ -297,22 +295,56 @@ private void CalculatePercentage() {
297295
}
298296
}
299297

300-
long percent = (totalCheckedItems * 100) / totalItems;
298+
long percent;
299+
try {
300+
percent = (totalCheckedItems * 100) / totalItems;
301+
}
302+
catch (DivideByZeroException) {
303+
percent = 0;
304+
}
301305
percentComplete = percent.ToString();
302306
if (percent <= 9) {
303307
percentComplete = "0" + percent.ToString();
304308
// Would convert 9% to 09%
305309
}
306-
307-
this.Title = projectTitle + " - " + percentComplete + "%";
310+
311+
this.Dispatcher.Invoke(() =>
312+
{
313+
if (Passthrough.Title != "") {
314+
this.Title = Passthrough.Title + " - " + percentComplete + "%";
315+
}
316+
else {
317+
this.Title = projectTitle + " - " + percentComplete + "%";
318+
}
319+
});
308320
Save();
309321
}
310322

311323
private void Read() {
312324
while (Passthrough.IsAdding) {
313325
Thread.Sleep(1000);
326+
327+
if (Passthrough.IsDeleting) {
328+
Passthrough.IsAdding = false;
329+
330+
isStopwatchRunning = false;
331+
332+
// Exit out of all threads forcefully
333+
if (timerThread.IsAlive) {
334+
timerThread.Abort();
335+
}
336+
337+
this.Dispatcher.Invoke(() =>
338+
{
339+
this.Hide();
340+
this.Close();
341+
});
342+
break;
343+
}
344+
345+
346+
CalculatePercentage();
314347
string json = File.ReadAllText(editingFile);
315-
dynamic array = JsonConvert.DeserializeObject(json);
316348
MainTableManifest.Rootobject values = JsonConvert.DeserializeObject<MainTableManifest.Rootobject>(json);
317349

318350
// Set values for saving feature to rewrite
@@ -338,7 +370,6 @@ private void Read() {
338370
}
339371
catch (ArgumentOutOfRangeException) {
340372
// They just added a new value
341-
CalculatePercentage();
342373
SelectionChange(0, 0, errorRowsAdded, errorTable, errorsData);
343374
}
344375
int index = 0;
@@ -357,7 +388,6 @@ private void Read() {
357388
}
358389
catch (ArgumentOutOfRangeException) {
359390
// They just added a new value
360-
CalculatePercentage();
361391
SelectionChange(0, 0, featureRowsAdded, featureTable, featuresData);
362392
}
363393
int index = 0;
@@ -376,7 +406,6 @@ private void Read() {
376406
}
377407
catch (ArgumentOutOfRangeException) {
378408
// They just added a new value
379-
CalculatePercentage();
380409
SelectionChange(0, 0, commentsRowsAdded, commentTable, commentsData);
381410
}
382411
int index = 0;
@@ -404,6 +433,16 @@ private void Save() {
404433
js.WriteStartObject();
405434

406435
js.WritePropertyName("Title");
436+
437+
if (Passthrough.Title != "") {
438+
if (Passthrough.Title != projectTitle) {
439+
projectTitle = Passthrough.Title; // Sync up values after changing title from details window
440+
}
441+
}
442+
else { // Set title for passthrough
443+
Passthrough.Title = projectTitle;
444+
}
445+
407446
js.WriteValue(projectTitle);
408447

409448
js.WritePropertyName("Errors");
@@ -474,8 +513,9 @@ private void Startup() {
474513

475514
editingFile = Passthrough.EditingFile;
476515

516+
Passthrough.Title = "";
517+
477518
string json = File.ReadAllText(editingFile);
478-
dynamic array = JsonConvert.DeserializeObject(json);
479519
MainTableManifest.Rootobject values = JsonConvert.DeserializeObject<MainTableManifest.Rootobject>(json);
480520

481521
// Set values for saving feature to rewrite
@@ -556,7 +596,24 @@ private void Stopwatch(object sender, MouseButtonEventArgs e) {
556596
}
557597

558598
private void Edit(object sender, MouseButtonEventArgs e) {
599+
Passthrough.Title = projectTitle;
600+
Passthrough.Errors = errors.ToArray();
601+
Passthrough.ErrorsData = errorsData.ToArray();
602+
Passthrough.Features = features.ToArray();
603+
Passthrough.FeaturesData = featuresData.ToArray();
604+
Passthrough.Comments = comments.ToArray();
605+
Passthrough.CommentsData = commentsData.ToArray();
606+
Passthrough.Duration = duration;
607+
Passthrough.Percent = percentComplete;
608+
Passthrough.EditingFile = editingFile;
609+
Passthrough.SelectedIndex = switchLabels.SelectedIndex;
610+
611+
Passthrough.IsAdding = true;
612+
readThread = new Thread(Read);
613+
readThread.Start();
559614

615+
EditProgramDetails details = new EditProgramDetails();
616+
details.Show();
560617
}
561618

562619
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<Window x:Class="Project_Tracker.EditProgramDetails"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:Project_Tracker"
7+
mc:Ignorable="d"
8+
Title="Edit" Height="190" Width="500" ResizeMode="NoResize" PreviewKeyDown="KeyPress" WindowStartupLocation="CenterScreen" Icon="logo.png">
9+
<Window.Resources>
10+
<Style TargetType="{x:Type TextBox}">
11+
<Setter Property="SnapsToDevicePixels" Value="True"/>
12+
<Setter Property="OverridesDefaultStyle" Value="True"/>
13+
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
14+
<Setter Property="AllowDrop" Value="true"/>
15+
<Setter Property="Template">
16+
<Setter.Value>
17+
<ControlTemplate TargetType="{x:Type TextBoxBase}">
18+
<Border Name="Border" CornerRadius="5" Padding="2" Background="#FFFFFF" BorderBrush="#888888" BorderThickness="2">
19+
<ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
20+
</Border>
21+
<ControlTemplate.Triggers>
22+
<Trigger Property="IsEnabled" Value="False">
23+
<Setter TargetName="Border" Property="Background" Value="#EEEEEE"/>
24+
<Setter TargetName="Border" Property="BorderBrush" Value="#EEEEEE"/>
25+
<Setter Property="Foreground" Value="#888888"/>
26+
</Trigger>
27+
</ControlTemplate.Triggers>
28+
</ControlTemplate>
29+
</Setter.Value>
30+
</Setter>
31+
</Style>
32+
</Window.Resources>
33+
<Grid>
34+
<TextBox x:Name="inputBox" HorizontalAlignment="Left" Margin="10,54,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="420" FontSize="20" VerticalContentAlignment="Center" MaxLines="1" Height="36" Visibility="Visible" Grid.ColumnSpan="2"/>
35+
<TextBlock x:Name="changeTitleLabel" HorizontalAlignment="Left" Margin="10,24,0,0" TextWrapping="Wrap" Text="Project title" VerticalAlignment="Top" FontFamily="Microsoft Sans Serif" FontSize="22"/>
36+
<TextBlock x:Name="warning" HorizontalAlignment="Left" Margin="10,95,0,0" TextWrapping="Wrap" Text="Please enter a title for this project" VerticalAlignment="Top" FontFamily="Microsoft Sans Serif" FontSize="11" Foreground="#FFF72500" Visibility="Hidden"/>
37+
<Button x:Name="deleteButton" Content="Delete project" HorizontalAlignment="Left" Height="33" Margin="10,0,0,10" VerticalAlignment="Bottom" Width="120" Background="#FFB91313" Foreground="White" Click="deleteButton_Click" FontSize="16" Grid.Column="1"/>
38+
<Button x:Name="finishButton" Content="Save" HorizontalAlignment="Right" Height="33" Margin="0,0,10,10" VerticalAlignment="Bottom" Width="80" Background="#FF1394B9" Foreground="White" Click="finishButton_Click" FontSize="16" Grid.Column="1"/>
39+
<Button x:Name="cancelButton" Content="Cancel" HorizontalAlignment="Right" Height="33" Margin="0,0,100,10" VerticalAlignment="Bottom" Width="80" FontSize="16" Grid.Column="1" Click="cancelButton_Click"/>
40+
</Grid>
41+
</Window>
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.ComponentModel;
4+
using System.IO;
5+
using System.Text;
6+
using System.Threading;
7+
using System.Windows;
8+
using System.Windows.Input;
9+
using System.Windows.Forms;
10+
using System.Diagnostics;
11+
12+
namespace Project_Tracker {
13+
14+
/// <summary>
15+
/// Interaction logic for AddNewItem.xaml
16+
/// </summary>
17+
public partial class EditProgramDetails : Window {
18+
readonly string PROGRAM_PATH = @"C:\Program Files\Project Tracker\Project Tracker.exe";
19+
20+
public EditProgramDetails() {
21+
InitializeComponent();
22+
23+
this.Title = "Edit " + Passthrough.Title;
24+
inputBox.Text = Passthrough.Title;
25+
inputBox.Focus();
26+
inputBox.CaretIndex = inputBox.Text.Length;
27+
}
28+
29+
private void KeyPress(object sender, System.Windows.Input.KeyEventArgs e) {
30+
if (e.Key == Key.Return) {
31+
if (inputBox.Text != "") {
32+
Passthrough.Title = inputBox.Text;
33+
Save();
34+
35+
Passthrough.IsAdding = false;
36+
this.Hide();
37+
}
38+
else {
39+
warning.Visibility = Visibility.Visible;
40+
}
41+
}
42+
}
43+
44+
private void finishButton_Click(object sender, RoutedEventArgs e) {
45+
if (inputBox.Text != "") {
46+
Passthrough.Title = inputBox.Text;
47+
Save();
48+
49+
Passthrough.IsAdding = false;
50+
this.Hide();
51+
}
52+
else {
53+
warning.Visibility = Visibility.Visible;
54+
}
55+
}
56+
57+
private void cancelButton_Click(object sender, RoutedEventArgs e) {
58+
Passthrough.IsAdding = false;
59+
this.Hide();
60+
}
61+
62+
private void Save() {
63+
StringBuilder sb = new StringBuilder();
64+
StringWriter sw = new StringWriter(sb);
65+
66+
// Make 10 attempts to save the file.
67+
for (int i = 0; i < 10; i++) {
68+
try {
69+
using (JsonWriter js = new JsonTextWriter(sw)) {
70+
js.Formatting = Formatting.Indented;
71+
72+
js.WriteStartObject();
73+
74+
js.WritePropertyName("Title");
75+
js.WriteValue(Passthrough.Title);
76+
77+
js.WritePropertyName("Errors");
78+
js.WriteStartArray();
79+
foreach (string error in Passthrough.Errors) {
80+
js.WriteValue(error);
81+
}
82+
js.WriteEnd();
83+
84+
js.WritePropertyName("ErrorsData");
85+
js.WriteStartArray();
86+
foreach (string data in Passthrough.ErrorsData) {
87+
js.WriteValue(data);
88+
}
89+
js.WriteEnd();
90+
91+
js.WritePropertyName("Features");
92+
js.WriteStartArray();
93+
foreach (string feature in Passthrough.Features) {
94+
js.WriteValue(feature);
95+
}
96+
js.WriteEnd();
97+
98+
js.WritePropertyName("FeaturesData");
99+
js.WriteStartArray();
100+
foreach (string data in Passthrough.FeaturesData) {
101+
js.WriteValue(data);
102+
}
103+
js.WriteEnd();
104+
105+
js.WritePropertyName("Comments");
106+
js.WriteStartArray();
107+
foreach (string comment in Passthrough.Comments) {
108+
js.WriteValue(comment);
109+
}
110+
js.WriteEnd();
111+
112+
js.WritePropertyName("CommentsData");
113+
js.WriteStartArray();
114+
foreach (string data in Passthrough.CommentsData) {
115+
js.WriteValue(data);
116+
}
117+
js.WriteEnd();
118+
119+
js.WritePropertyName("Duration");
120+
js.WriteValue(Passthrough.Duration);
121+
122+
js.WritePropertyName("Percent");
123+
js.WriteValue(Passthrough.Percent);
124+
125+
js.WriteEndObject();
126+
}
127+
128+
File.WriteAllText(Passthrough.EditingFile, sw.ToString());
129+
sb.Clear();
130+
sw.Close();
131+
132+
break;
133+
}
134+
catch (ObjectDisposedException) {
135+
136+
}
137+
catch (IOException) {
138+
Thread.Sleep(100);
139+
}
140+
}
141+
}
142+
143+
private void deleteButton_Click(object sender, RoutedEventArgs e) {
144+
var deleteData = System.Windows.Forms.MessageBox.Show("Are you sure you wish to delete " + Passthrough.Title + "?", "Confirm project deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
145+
146+
if (deleteData == System.Windows.Forms.DialogResult.Yes) {
147+
Passthrough.IsDeleting = true;
148+
this.Close();
149+
File.Delete(Passthrough.EditingFile);
150+
151+
ProcessStartInfo start = new ProcessStartInfo {
152+
// Enter the executable to run, including the complete path
153+
FileName = PROGRAM_PATH,
154+
// Do you want to show a console window?
155+
WindowStyle = ProcessWindowStyle.Normal,
156+
CreateNoWindow = true
157+
};
158+
Process.Start(start);
159+
Environment.Exit(0);
160+
}
161+
}
162+
}
163+
}

0 commit comments

Comments
 (0)