Skip to content

Commit 81482d5

Browse files
authored
Fix nullability warnings in SDMetaUI (#490)
1 parent 6515a8e commit 81482d5

21 files changed

+100
-87
lines changed

SDMeta/Cache/IImageFileDataSource.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,9 @@ public interface IImageFileDataSource : IDisposable
1818
void Initialize();
1919
}
2020

21-
public class QueryParams(string filter, ModelFilter modelFilter, QuerySortBy querySort)
22-
{
23-
public string Filter { get; } = filter;
24-
public ModelFilter ModelFilter { get; } = modelFilter;
25-
public QuerySortBy QuerySortBy { get; } = querySort;
26-
}
21+
public record QueryParams(string? Filter, ModelFilter? ModelFilter, QuerySortBy QuerySortBy);
2722

28-
public class ModelFilter(string model, string modelHash)
29-
{
30-
public string Model { get; } = model;
31-
public string ModelHash { get; } = modelHash;
32-
}
23+
public record class ModelFilter(string? Model, string? ModelHash);
3324

3425
public enum QuerySortBy
3526
{

SDMeta/CachedImageFileLoader.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class CachedImageFileLoader(
88
IImageFileLoader inner,
99
IImageFileDataSource imageFileDataSource) : IImageFileLoader
1010
{
11-
public async Task<ImageFile> GetImageFile(IFileInfo fileInfo)
11+
public async Task<ImageFile?> GetImageFile(IFileInfo fileInfo)
1212
{
1313
var imageFile = imageFileDataSource.ReadImageFile(fileInfo.FullName);
1414
if (imageFile != null && imageFile.LastUpdated == fileInfo.LastWriteTime && imageFile.Exists)
@@ -18,8 +18,11 @@ public async Task<ImageFile> GetImageFile(IFileInfo fileInfo)
1818
else
1919
{
2020
imageFile = await inner.GetImageFile(fileInfo);
21-
imageFile.Exists = true;
22-
imageFileDataSource.WriteImageFile(imageFile);
21+
if (imageFile != null)
22+
{
23+
imageFile.Exists = true;
24+
imageFileDataSource.WriteImageFile(imageFile);
25+
}
2326
return imageFile;
2427
}
2528
}

SDMeta/IImageFileLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ namespace SDMeta
55
{
66
public interface IImageFileLoader
77
{
8-
Task<ImageFile> GetImageFile(IFileInfo fileInfo);
8+
Task<ImageFile?> GetImageFile(IFileInfo fileInfo);
99
}
1010
}

SDMeta/ImageFileLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace SDMeta
99
{
1010
public class ImageFileLoader(ILogger<ImageFileLoader> logger) : IImageFileLoader
1111
{
12-
public async Task<ImageFile> GetImageFile(IFileInfo fileInfo)
12+
public async Task<ImageFile?> GetImageFile(IFileInfo fileInfo)
1313
{
1414
logger.LogInformation("Indexing: {filename}", fileInfo.FullName);
1515

SDMetaUI/Models/FilteredList.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Action postFilteringAction
1313

1414
public bool FilterError { get; private set; }
1515

16-
private ModelSummaryViewModel modelFilter;
16+
private ModelSummaryViewModel modelFilter = ModelSummaryViewModel.AllModels;
1717
public ModelSummaryViewModel ModelFilter
1818
{
1919
get
@@ -30,9 +30,9 @@ public ModelSummaryViewModel ModelFilter
3030
}
3131
}
3232

33-
private string filter;
33+
private string? filter;
3434

35-
public string Filter
35+
public string? Filter
3636
{
3737
get
3838
{
@@ -80,7 +80,7 @@ public void RunFilter()
8080
{
8181
var queryParams = new QueryParams(
8282
this.filter,
83-
this.modelFilter == null ? null : new ModelFilter(this.modelFilter.Model,this.modelFilter.ModelHash),
83+
this.modelFilter == ModelSummaryViewModel.AllModels ? null : new ModelFilter(this.modelFilter.Model, this.modelFilter.ModelHash),
8484
sortBy
8585
);
8686
try

SDMetaUI/Models/GalleryViewModel.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public ModelSummaryViewModel ModelFilter
4444
set => filteredList.ModelFilter = value;
4545
}
4646

47-
public string Filter
47+
public string? Filter
4848
{
4949
get => filteredList.Filter;
5050
set => filteredList.Filter = value;
@@ -109,8 +109,11 @@ public void RemoveFile()
109109
this.thumbnailService.Delete(filename);
110110
File.Delete(filename);
111111
var original = imageFileDataSource.ReadImageFile(filename);
112-
original.Exists = false;
113-
imageFileDataSource.WriteImageFile(original);
112+
if (original != null)
113+
{
114+
original.Exists = false;
115+
imageFileDataSource.WriteImageFile(original);
116+
}
114117

115118
var next = this.groupList.GetNext(this.SelectedFile);
116119
if (next == this.SelectedFile) next = null;
@@ -122,15 +125,21 @@ public void RemoveFile()
122125

123126
public void MovePrevious()
124127
{
125-
this.SelectedFile = this.groupList.GetPrevious(this.SelectedFile);
128+
if (this.SelectedFile != null)
129+
{
130+
this.SelectedFile = this.groupList.GetPrevious(this.SelectedFile);
131+
}
126132
}
127133

128134
public void MoveNext()
129135
{
130-
this.SelectedFile = this.groupList.GetNext(this.SelectedFile);
136+
if (this.SelectedFile != null)
137+
{
138+
this.SelectedFile = this.groupList.GetNext(this.SelectedFile);
139+
}
131140
}
132141

133-
public IList<GalleryRow> Rows { get; private set; }
142+
public IList<GalleryRow> Rows { get; private set; } = new List<GalleryRow>();
134143

135144
public void ToggleExpandedState(ImageFileViewModel model)
136145
{
@@ -140,7 +149,7 @@ public void ToggleExpandedState(ImageFileViewModel model)
140149
public IList<ModelSummaryViewModel> GetModelsList()
141150
{
142151
var modelsList = this.imageFileDataSource.GetModelSummaryList().Select((p, i) => new ModelSummaryViewModel(p, i + 1)).ToList();
143-
modelsList.Insert(0, new ModelSummaryViewModel());
152+
modelsList.Insert(0, ModelSummaryViewModel.AllModels);
144153
return modelsList;
145154
}
146155
}

SDMetaUI/Models/GroupedByPromptList.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
{
33
public class GroupedByPromptList : IGroupList, IExpandable
44
{
5-
private IList<ImageFileViewModel>? groupedFiles = null;
6-
private IDictionary<string, List<ImageFileViewModel>>? promptGroups = null;
5+
private IList<ImageFileViewModel> groupedFiles = new List<ImageFileViewModel>();
6+
private IDictionary<string, List<ImageFileViewModel>> promptGroups = new Dictionary<string, List<ImageFileViewModel>>();
77
private readonly FilteredList filteredList;
88
private readonly Action postGroupingAction;
99

SDMetaUI/Models/ImageFileViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public ImageFileViewModel(string fileName, string fullPromptHash, Func<string, s
1515
public string ThumbnailUrl => $"/images/thumb/{EncodedFileName.Value}";
1616
public string ImageUrl => $"/images/full/{EncodedFileName.Value}/{func(this.FileName)}";
1717
public string FullPromptHash { get; }
18-
public IList<ImageFileViewModel> SubItems { get; set; }
18+
public IList<ImageFileViewModel>? SubItems { get; set; }
1919

2020
private readonly Lazy<string> EncodedFileName;
2121

SDMetaUI/Models/ImageFileViewModelBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class ImageFileViewModelBuilder(IFileSystem fileSystem)
77
{
88
public ImageFileViewModel BuildModel(ImageFileSummary p)
99
{
10-
return new ImageFileViewModel(p.FileName, p.FullPromptHash, fileSystem.Path.GetFileName);
10+
return new ImageFileViewModel(p.FileName, p.FullPromptHash ?? "", fileSystem.Path.GetFileName);
1111
}
1212
}
1313
}

SDMetaUI/Models/ModelSummaryViewModel.cs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,29 @@
22

33
namespace SDMetaUI.Models
44
{
5-
public class ModelSummaryViewModel
6-
{
7-
public ModelSummaryViewModel() {
8-
Id = 0;
9-
Text = "-- all models --";
10-
}
5+
public class ModelSummaryViewModel
6+
{
7+
public static readonly ModelSummaryViewModel AllModels = new(0, "-- all models --");
118

12-
public ModelSummaryViewModel(ModelSummary p, int id)
13-
{
14-
Id = id;
15-
Count = p.Count;
16-
Model = p.Model;
17-
ModelHash = p.ModelHash;
18-
Text = (p.ModelHash ?? "<empty>") + " (" + (p.Model ?? "<no name>") + ") [" + p.Count + "]";
19-
}
9+
private ModelSummaryViewModel(int id, string text)
10+
{
11+
Id = id;
12+
Text = text;
13+
}
2014

21-
public int Id { get; }
22-
public string? Model { get; }
23-
public string? ModelHash { get; }
24-
public int Count { get; }
25-
public string Text { get; }
26-
}
15+
public ModelSummaryViewModel(ModelSummary p, int id)
16+
{
17+
Id = id;
18+
Count = p.Count;
19+
Model = p.Model;
20+
ModelHash = p.ModelHash;
21+
Text = (p.ModelHash ?? "<empty>") + " (" + (p.Model ?? "<no name>") + ") [" + p.Count + "]";
22+
}
23+
24+
public int Id { get; }
25+
public string? Model { get; }
26+
public string? ModelHash { get; }
27+
public int Count { get; }
28+
public string Text { get; }
29+
}
2730
}

0 commit comments

Comments
 (0)