-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
95 lines (75 loc) · 2.56 KB
/
MainWindow.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
using Newtonsoft.Json;
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using TimeCollect.Models;
using TimeCollect.ViewModels;
namespace TimeCollect
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private readonly MainViewModel viewModel;
private readonly string employeeFilePath;
public MainWindow()
{
viewModel = new MainViewModel();
DataContext = viewModel;
viewModel.LoadCredentials();
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
employeeFilePath = Path.Combine(appDataPath, "TimeCollect", "employees.json");
LoadEmployeeData();
InitializeComponent();
Closing += Window_Closing;
}
private void LoadEmployeeData()
{
if (File.Exists(employeeFilePath))
{
try
{
string jsonData = File.ReadAllText(employeeFilePath);
var employees = JsonConvert.DeserializeObject<ObservableCollection<Employee>>(jsonData);
viewModel.Employees.Clear();
foreach (var employee in employees)
{
viewModel.Employees.Add(employee);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error loading employee data: {ex.Message}");
}
}
}
private void SaveEmployeeData()
{
try
{
string jsonData = JsonConvert.SerializeObject(viewModel.Employees, Formatting.Indented);
Directory.CreateDirectory(Path.GetDirectoryName(employeeFilePath));
File.WriteAllText(employeeFilePath, jsonData);
}
catch (Exception ex)
{
Console.WriteLine($"Error saving employee data: {ex.Message}");
}
}
private void ClearLogButton_Click(object sender, RoutedEventArgs e)
{
viewModel.LogMessages = "";
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
SaveEmployeeData();
}
private void LogTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
logTextBox.ScrollToEnd();
}
}
}