-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.xaml.cs
179 lines (160 loc) · 6 KB
/
App.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using CsvHelper.Configuration;
using CsvHelper;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using WpfStroopTestSuite.Models;
using System.Windows.Media;
namespace WpfStroopTestSuite
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
// Global state reference
public static Stages Stage { get; set; } = Stages.First;
// Global subject data reference
public static SubjectData Subject { get; set; } = new SubjectData();
// Global block data reference
public static BlockData BlockData { get; set; } = new BlockData(Stage);
protected override void OnStartup(StartupEventArgs e)
{
MainWindow = new SubjectForm();
//MainWindow = new MainWindow()
//{
// DataContext = new MainController()
//};
;
MainWindow.Show();
base.OnStartup(e);
}
/// <summary>
/// Refreshes <see cref="App.BlockData"/> to a new instance
/// with the current value of <see cref="App.Stage"/>.
/// </summary>
public static void ResetBlockData()
{
BlockData = new BlockData(Stage);
}
#region Data Exports
/// <summary>
/// Save the data about subject into the subjects list.
/// </summary>
public static void SaveSubjectData()
{
string path = ConfigurationManager.AppSettings.Get("RunDataPath") + "SubjectRecords_Stroop.csv";
//var hollow = new { something = string.Empty }; // needed to make extra space in the file
List<RecordSubject> subject = new()
{
new RecordSubject(Subject),
};
StreamWriter writer;
CsvWriter csv;
FileStream? stream;
// if file exist, append current SubjectData to the file
if (File.Exists(path))
{
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
// Don't write the header again.
HasHeaderRecord = false,
};
stream = File.Open(path, FileMode.Append);
writer = new StreamWriter(stream);
csv = new CsvWriter(writer, config);
}
// if file does not exist, create entire file
else
{
stream = null;
writer = new StreamWriter(path);
csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
}
using (stream)
using (writer)
using (csv)
//csv.WriteRecord(hollow);
csv.WriteRecords(subject);
}
/// <summary>
/// Save the data of this run into a .csv file.
/// </summary>
/// <param name="filename">
/// Set the name of the file.
/// If set to default, the filename would consists of subject's credentials.
/// </param>
public static void SaveRunData(string filename = "default")
{
if (filename == "default")
{
filename =
Subject.GetSubjectName("d") +
Subject.GetSubjectId("d") + "_" +
"G-" + Subject.GetGroupId("d");
}
List<RecordData> records = new();
foreach (BlockData block in Subject.Blocks)
{
foreach (TrialData trial in block.Trials)
{
records.Add(new RecordData(Subject, block, trial));
}
}
var path = ConfigurationManager.AppSettings.Get("RunDataPath");
using var writer = new StreamWriter(path + filename + "_Stroop.csv");
using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
csv.WriteRecords(records);
}
#endregion
#region Global Static Parameters
/// <summary>
/// Starting score for each block.
/// </summary>
public static int InitialScore { get; } =
int.Parse(ConfigurationManager.AppSettings.Get("InitialScore"));
/// <summary>
/// Duration for each trial/question in seconds.
/// </summary>
public static int TrialDuration { get; } =
int.Parse(ConfigurationManager.AppSettings.Get("TrialDurationInSeconds"));
/// <summary>
/// Duration for each block in seconds.
/// </summary>
public static int BlockDuration { get; } =
int.Parse(ConfigurationManager.AppSettings.Get("BlockDurationInSeconds"));
/// <summary>
/// Duration for the First block (introductory block) in seconds.
/// </summary>
public static int IntroDuration { get; } =
int.Parse(ConfigurationManager.AppSettings.Get("IntroDurationInSeconds"));
/// <summary>
/// Duration for the visual feedback in ms.
/// </summary>
public static int FeedbackDuration { get; } =
int.Parse(ConfigurationManager.AppSettings.Get("FeedbackDurationInMs"));
/// <summary>
/// Wait between feedback state and the next trial in ms.
/// </summary>
public static int TrialIntervalDuration { get; } =
int.Parse(ConfigurationManager.AppSettings.Get("TrialIntervalDurationInMs"));
#endregion
public static T? FindParentOfType<T>(DependencyObject child) where T : DependencyObject
{
DependencyObject parentDepObj = child;
do
{
parentDepObj = VisualTreeHelper.GetParent(parentDepObj);
if (parentDepObj is T parent) return parent;
}
while (parentDepObj != null);
return null;
}
}
}