Skip to content

Commit 44e9e00

Browse files
📝 Add docstrings to multiple_topmost
Docstrings generation was requested by @Jack251970. * #3500 (comment) The following files were modified: * `Flow.Launcher.Infrastructure/Storage/JsonStorage.cs` * `Flow.Launcher/Storage/TopMostRecord.cs` * `Flow.Launcher/ViewModel/MainViewModel.cs`
1 parent 4ecef47 commit 44e9e00

File tree

3 files changed

+88
-3
lines changed

3 files changed

+88
-3
lines changed

Flow.Launcher.Infrastructure/Storage/JsonStorage.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Globalization;
33
using System.IO;
44
using System.Text.Json;
@@ -37,6 +37,11 @@ protected JsonStorage()
3737
{
3838
}
3939

40+
/// <summary>
41+
/// Initializes a new instance of the <c>JsonStorage&lt;T&gt;</c> class for the specified file path.
42+
/// </summary>
43+
/// <param name="filePath">The full path to the JSON file used for storage.</param>
44+
/// <exception cref="ArgumentException">Thrown if the provided file path does not contain a valid directory.</exception>
4045
public JsonStorage(string filePath)
4146
{
4247
FilePath = filePath;
@@ -45,11 +50,18 @@ public JsonStorage(string filePath)
4550
FilesFolders.ValidateDirectory(DirectoryPath);
4651
}
4752

53+
/// <summary>
54+
/// Determines whether the main JSON storage file exists on disk.
55+
/// </summary>
56+
/// <returns>True if the storage file exists; otherwise, false.</returns>
4857
public bool Exists()
4958
{
5059
return File.Exists(FilePath);
5160
}
5261

62+
/// <summary>
63+
/// Deletes the main JSON storage file, its backup, and temporary file if they exist.
64+
/// </summary>
5365
public void Delete()
5466
{
5567
foreach (var path in new[] { FilePath, BackupFilePath, TempFilePath })
@@ -61,6 +73,10 @@ public void Delete()
6173
}
6274
}
6375

76+
/// <summary>
77+
/// Asynchronously loads and deserializes the JSON file into <c>Data</c>, falling back to a backup or a default instance if necessary.
78+
/// </summary>
79+
/// <returns>The loaded or newly created instance of <typeparamref name="T"/>.</returns>
6480
public async Task<T> LoadAsync()
6581
{
6682
if (Data != null)

Flow.Launcher/Storage/TopMostRecord.cs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.Linq;
@@ -14,6 +14,12 @@ public class FlowLauncherJsonStorageTopMostRecord
1414
private readonly FlowLauncherJsonStorage<MultipleTopMostRecord> _topMostRecordStorage;
1515
private readonly MultipleTopMostRecord _topMostRecord;
1616

17+
/// <summary>
18+
/// Initializes the top most records storage, handling migration from the old single-record-per-query format to the new multiple-records-per-query format if necessary.
19+
/// </summary>
20+
/// <remarks>
21+
/// If new-format data exists, it loads it and deletes any old-format data. If only old-format data exists, it migrates the data to the new format, deletes the old data, and saves the new structure. If neither exists, it initializes an empty new-format storage.
22+
/// </remarks>
1723
public FlowLauncherJsonStorageTopMostRecord()
1824
{
1925
// Get old data & new data
@@ -63,21 +69,35 @@ public FlowLauncherJsonStorageTopMostRecord()
6369
}
6470
}
6571

72+
/// <summary>
73+
/// Persists the current top most records to storage.
74+
/// </summary>
6675
public void Save()
6776
{
6877
_topMostRecordStorage.Save();
6978
}
7079

80+
/// <summary>
81+
/// Determines whether the specified result is marked as top most in the current records.
82+
/// </summary>
83+
/// <param name="result">The result to check.</param>
84+
/// <returns>True if the result is marked as top most; otherwise, false.</returns>
7185
public bool IsTopMost(Result result)
7286
{
7387
return _topMostRecord.IsTopMost(result);
7488
}
7589

90+
/// <summary>
91+
/// Removes the specified result from the top most records if it exists.
92+
/// </summary>
7693
public void Remove(Result result)
7794
{
7895
_topMostRecord.Remove(result);
7996
}
8097

98+
/// <summary>
99+
/// Adds a result to the top most records or updates it if it already exists.
100+
/// </summary>
81101
public void AddOrUpdate(Result result)
82102
{
83103
_topMostRecord.AddOrUpdate(result);
@@ -92,6 +112,11 @@ internal class TopMostRecord
92112
[JsonInclude]
93113
public ConcurrentDictionary<string, Record> records { get; private set; } = new();
94114

115+
/// <summary>
116+
/// Determines whether the specified result is the top most record for its originating query.
117+
/// </summary>
118+
/// <param name="result">The result to check.</param>
119+
/// <returns>True if the result matches the stored top most record for its query; otherwise, false.</returns>
95120
internal bool IsTopMost(Result result)
96121
{
97122
// origin query is null when user select the context menu item directly of one item from query list
@@ -118,6 +143,10 @@ internal void Remove(Result result)
118143
records.Remove(result.OriginQuery.RawQuery, out _);
119144
}
120145

146+
/// <summary>
147+
/// Adds or updates the top most record for the specified result's query, replacing any existing record for that query.
148+
/// </summary>
149+
/// <param name="result">The result whose information is to be stored as the top most record for its originating query. If <c>OriginQuery</c> is null, no action is taken.</param>
121150
internal void AddOrUpdate(Result result)
122151
{
123152
// origin query is null when user select the context menu item directly of one item from query list
@@ -147,6 +176,10 @@ internal class MultipleTopMostRecord
147176
[JsonConverter(typeof(ConcurrentDictionaryConcurrentBagConverter))]
148177
public ConcurrentDictionary<string, ConcurrentBag<Record>> records { get; private set; } = new();
149178

179+
/// <summary>
180+
/// Migrates all records from an existing <see cref="TopMostRecord"/> into this multiple-records-per-query structure.
181+
/// </summary>
182+
/// <param name="topMostRecord">The old single-record-per-query data structure to migrate from.</param>
150183
internal void Add(TopMostRecord topMostRecord)
151184
{
152185
if (topMostRecord == null || topMostRecord.records.IsEmpty)
@@ -164,6 +197,11 @@ internal void Add(TopMostRecord topMostRecord)
164197
}
165198
}
166199

200+
/// <summary>
201+
/// Determines whether the specified result is marked as top most for its originating query.
202+
/// </summary>
203+
/// <param name="result">The result to check.</param>
204+
/// <returns>True if the result is a top most record for its query; otherwise, false.</returns>
167205
internal bool IsTopMost(Result result)
168206
{
169207
// origin query is null when user select the context menu item directly of one item from query list
@@ -178,6 +216,10 @@ internal bool IsTopMost(Result result)
178216
return value.Any(record => record.Equals(result));
179217
}
180218

219+
/// <summary>
220+
/// Removes a matching record for the given result from the collection of top most records for its query.
221+
/// </summary>
222+
/// <param name="result">The result whose corresponding record should be removed.</param>
181223
internal void Remove(Result result)
182224
{
183225
// origin query is null when user select the context menu item directly of one item from query list
@@ -202,6 +244,10 @@ internal void Remove(Result result)
202244
}
203245
}
204246

247+
/// <summary>
248+
/// Adds a result as a top most record for its originating query, or updates the existing record if it already exists.
249+
/// </summary>
250+
/// <param name="result">The result to add or update as top most for its query. Ignored if the result's OriginQuery is null.</param>
205251
internal void AddOrUpdate(Result result)
206252
{
207253
// origin query is null when user select the context menu item directly of one item from query list
@@ -254,6 +300,13 @@ internal void AddOrUpdate(Result result)
254300
/// </summary>
255301
internal class ConcurrentDictionaryConcurrentBagConverter : JsonConverter<ConcurrentDictionary<string, ConcurrentBag<Record>>>
256302
{
303+
/// <summary>
304+
/// Deserializes JSON into a <see cref="ConcurrentDictionary{TKey, TValue}"/> mapping strings to <see cref="ConcurrentBag{T}"/> of <see cref="Record"/>.
305+
/// </summary>
306+
/// <param name="reader">The JSON reader positioned at the start of the object.</param>
307+
/// <param name="typeToConvert">The type to convert (ignored).</param>
308+
/// <param name="options">Serialization options to use during deserialization.</param>
309+
/// <returns>A concurrent dictionary where each key maps to a concurrent bag of records.</returns>
257310
public override ConcurrentDictionary<string, ConcurrentBag<Record>> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
258311
{
259312
var dictionary = JsonSerializer.Deserialize<Dictionary<string, List<Record>>>(ref reader, options);
@@ -265,6 +318,12 @@ public override ConcurrentDictionary<string, ConcurrentBag<Record>> Read(ref Utf
265318
return concurrentDictionary;
266319
}
267320

321+
/// <summary>
322+
/// Serializes a <see cref="ConcurrentDictionary{TKey, TValue}"/> of <see cref="ConcurrentBag{T}"/> records to JSON by converting each bag to a list.
323+
/// </summary>
324+
/// <param name="writer">The JSON writer to which the data will be serialized.</param>
325+
/// <param name="value">The concurrent dictionary containing bags of records to serialize.</param>
326+
/// <param name="options">Serialization options to use.</param>
268327
public override void Write(Utf8JsonWriter writer, ConcurrentDictionary<string, ConcurrentBag<Record>> value, JsonSerializerOptions options)
269328
{
270329
var dict = new Dictionary<string, List<Record>>();
@@ -283,6 +342,11 @@ internal class Record
283342
public string PluginID { get; init; }
284343
public string RecordKey { get; init; }
285344

345+
/// <summary>
346+
/// Determines whether the current record is equal to the specified result based on key or identifying properties.
347+
/// </summary>
348+
/// <param name="r">The result to compare with this record.</param>
349+
/// <returns>True if the records are considered equal; otherwise, false.</returns>
286350
public bool Equals(Result r)
287351
{
288352
if (string.IsNullOrEmpty(RecordKey) || string.IsNullOrEmpty(r.RecordKey))

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.ComponentModel;
44
using System.Globalization;
@@ -54,6 +54,9 @@ public partial class MainViewModel : BaseModel, ISavable, IDisposable
5454

5555
#region Constructor
5656

57+
/// <summary>
58+
/// Initializes a new instance of the MainViewModel class, setting up application state, loading persistent data, configuring results view models, and registering property change handlers and background update tasks.
59+
/// </summary>
5760
public MainViewModel()
5861
{
5962
_queryTextBeforeLeaveResults = "";
@@ -1605,6 +1608,8 @@ public async void Hide()
16051608

16061609
/// <summary>
16071610
/// Save history, user selected records and top most records
1611+
/// <summary>
1612+
/// Persists the current history, user-selected records, and top-most records to storage.
16081613
/// </summary>
16091614
public void Save()
16101615
{

0 commit comments

Comments
 (0)