8635bcde813703bfc6e17ce2d48bdaab4aeba28e
[master-thesis.git] / Parasitemia / ParasitemiaUI / Utils.fs
1 module ParasitemiaUI.Utils
2
3 open System.IO
4
5 open Newtonsoft.Json
6 open Newtonsoft.Json.Converters
7
8 open Types
9
10 let inline listAsStr (s : 'a seq) =
11 s |> Seq.fold (fun acc obj -> acc + (if acc = "" then "" else ", ") + string obj) ""
12
13 let percentText (nbTotal : int, nb : int) : string =
14 if nbTotal = 0 then
15 ""
16 else
17 let percent = 100. * (float nb) / (float nbTotal)
18 sprintf "%.1f %% (%d / %d)" percent nb nbTotal
19
20 let roamingDir =
21 Path.Combine (System.Environment.GetFolderPath (System.Environment.SpecialFolder.ApplicationData), "Parasitemia")
22
23 let predefinedPPIFilename = "predefined-ppi.json"
24 let predefinedPPIFilepath = Path.Combine (roamingDir, predefinedPPIFilename)
25
26 let sensorSizesFilename = "sensor-sizes.json"
27 let sensorSizesFilepath = Path.Combine (roamingDir, sensorSizesFilename)
28
29 let private savePredefinedPPIToFile (predefinedPPI : PredefinedPPI list) =
30 try
31 Directory.CreateDirectory roamingDir |> ignore
32 use file = new StreamWriter (predefinedPPIFilepath)
33 file.Write (JsonConvert.SerializeObject (predefinedPPI, JsonSerializerSettings (Formatting = Formatting.Indented)))
34 with
35 ex ->
36 Logger.Log.Error "Unable to save predefined PPI to file \"%s\": %O" predefinedPPIFilepath ex
37
38 let private saveSensorSizesToFile (sensorSizes : SensorSize list) =
39 try
40 Directory.CreateDirectory roamingDir |> ignore
41 use file = new StreamWriter (sensorSizesFilepath)
42 file.Write (JsonConvert.SerializeObject (sensorSizes, JsonSerializerSettings (Formatting = Formatting.Indented)))
43 with
44 ex ->
45 Logger.Log.Error "Unable to save sensor sizes to file \"%s\": %O" sensorSizesFilepath ex
46
47 let predefinedPPI : PredefinedPPI list =
48 try
49 use file = new StreamReader (predefinedPPIFilepath)
50 JsonConvert.DeserializeObject<PredefinedPPI list> (file.ReadToEnd ())
51 with
52 | _ex ->
53 savePredefinedPPIToFile defaultPredefinedPPI
54 defaultPredefinedPPI
55
56 let sensorSizes : SensorSize list =
57 try
58 use file = new StreamReader (sensorSizesFilepath)
59 JsonConvert.DeserializeObject<SensorSize list> (file.ReadToEnd ())
60 with
61 | _ex ->
62 saveSensorSizesToFile defaultSensorSizes
63 defaultSensorSizes