Update coding style.
[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 use file = new StreamWriter (predefinedPPIFilepath)
32 file.Write (JsonConvert.SerializeObject (predefinedPPI, JsonSerializerSettings (Formatting = Formatting.Indented)))
33 with
34 ex ->
35 Logger.Log.Error "Unable to save predefined PPI to file \"%s\": %O" predefinedPPIFilepath ex
36
37 let private saveSensorSizesToFile (sensorSizes : SensorSize list) =
38 try
39 use file = new StreamWriter (sensorSizesFilepath)
40 file.Write (JsonConvert.SerializeObject (sensorSizes, JsonSerializerSettings (Formatting = Formatting.Indented)))
41 with
42 ex ->
43 Logger.Log.Error "Unable to save sensor sizes to file \"%s\": %O" sensorSizesFilepath ex
44
45 let predefinedPPI : PredefinedPPI list =
46 try
47 use file = new StreamReader (predefinedPPIFilepath)
48 JsonConvert.DeserializeObject<PredefinedPPI list> (file.ReadToEnd ())
49 with
50 | ex ->
51 savePredefinedPPIToFile defaultPredefinedPPI
52 defaultPredefinedPPI
53
54 let sensorSizes : SensorSize list =
55 try
56 use file = new StreamReader (sensorSizesFilepath)
57 JsonConvert.DeserializeObject<SensorSize list> (file.ReadToEnd ())
58 with
59 | ex ->
60 saveSensorSizesToFile defaultSensorSizes
61 defaultSensorSizes