Skip to content

Commit 314cc2c

Browse files
committed
Fix more nullable reference warnings
1 parent 3d52634 commit 314cc2c

File tree

11 files changed

+79
-39
lines changed

11 files changed

+79
-39
lines changed

src/Bible.Alarm.Shared/Models/Media/AudioSource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ public class AudioSource
44
{
55
public int Id { get; set; }
66

7-
public string Url { get; set; }
7+
public string Url { get; set; } = string.Empty;
88

9-
public string LookUpPath { get; set; }
9+
public string LookUpPath { get; set; } = string.Empty;
1010
}
1111
}

src/Bible.Alarm.Shared/Models/Media/Bible/BibleBook.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ public class BibleBook : IComparable
77
{
88
public int Id { get; set; }
99

10-
public string Name { get; set; }
10+
public string Name { get; set; } = string.Empty;
1111
public int Number { get; set; }
1212

1313
public int BibleTranslationId { get; set; }
14-
public BibleTranslation BibleTranslation { get; set; }
14+
public BibleTranslation? BibleTranslation { get; set; }
1515

1616
public List<BibleChapter> Chapters { get; set; } = new List<BibleChapter>();
1717

src/Bible.Alarm.Shared/Models/Media/Bible/BibleChapter.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ public class BibleChapter : IComparable
1010

1111
public string Title => $"Chapter {Number}";
1212

13-
public AudioSource Source { get; set; }
13+
public AudioSource? Source { get; set; }
1414

1515
public int BibleBookId { get; set; }
16-
public BibleBook Book { get; set; }
16+
public BibleBook? Book { get; set; }
1717

18-
public int CompareTo(object obj)
18+
public int CompareTo(object? obj)
1919
{
20-
return Number.CompareTo((obj as BibleChapter).Number);
20+
if (obj is not BibleChapter other) return 1;
21+
return Number.CompareTo(other.Number);
2122
}
2223
}
2324
}

src/Bible.Alarm.Shared/Models/Media/Music/MusicTrack.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ public class MusicTrack : IComparable
77
public int Id { get; set; }
88

99
public int Number { get; set; }
10-
public string Title { get; set; }
10+
public string Title { get; set; } = string.Empty;
1111

12-
public AudioSource Source { get; set; }
12+
public AudioSource? Source { get; set; }
1313

14-
public int CompareTo(object obj)
14+
public int CompareTo(object? obj)
1515
{
16-
return Number.CompareTo((obj as MusicTrack).Number);
16+
if (obj is not MusicTrack other) return 1;
17+
return Number.CompareTo(other.Number);
1718
}
1819
}
1920
}

src/Bible.Alarm/Common/Extensions/CloneExtensions.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,21 @@ public static class CloneExtensions
99
{
1010
public static T DeepClone<T>(this T obj)
1111
{
12+
if (obj == null)
13+
throw new ArgumentNullException(nameof(obj));
14+
1215
var settings = new JsonSerializerSettings
1316
{
1417
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
1518
PreserveReferencesHandling = PreserveReferencesHandling.None,
1619
ContractResolver = new IgnoreNonSerializableContractResolver()
1720
};
1821
var json = JsonConvert.SerializeObject(obj, settings);
19-
return JsonConvert.DeserializeObject<T>(json, settings);
22+
var result = JsonConvert.DeserializeObject<T>(json, settings);
23+
if (result == null)
24+
throw new InvalidOperationException("Deserialization returned null");
25+
26+
return result;
2027
}
2128

2229
private class IgnoreNonSerializableContractResolver : DefaultContractResolver

src/Bible.Alarm/Models/Schedule/AlarmMusic.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ public class AlarmMusic
88
public int Id { get; set; }
99

1010
public MusicType MusicType { get; set; }
11-
public string PublicationCode { get; set; }
11+
public string PublicationCode { get; set; } = string.Empty;
1212

13-
public string LanguageCode { get; set; }
13+
public string? LanguageCode { get; set; }
1414
public int TrackNumber { get; set; }
1515

1616
//Always play current track.
1717
public bool Repeat { get; set; }
1818

19-
public virtual AlarmSchedule AlarmSchedule { get; set; }
19+
public virtual AlarmSchedule? AlarmSchedule { get; set; }
2020
public int AlarmScheduleId { get; set; }
2121
}

src/Bible.Alarm/Models/Schedule/AlarmSchedule.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class AlarmSchedule : IComparable
1010
{
1111
public int Id { get; set; }
1212

13-
public string Name { get; set; }
13+
public string Name { get; set; } = string.Empty;
1414
public bool IsEnabled { get; set; }
1515

1616
//24 hour based
@@ -33,9 +33,9 @@ public class AlarmSchedule : IComparable
3333

3434
public bool NotificationEnabled { get; set; }
3535
public bool MusicEnabled { get; set; }
36-
public virtual AlarmMusic Music { get; set; }
36+
public virtual AlarmMusic? Music { get; set; }
3737

38-
public virtual BibleReadingSchedule BibleReadingSchedule { get; set; }
38+
public virtual BibleReadingSchedule? BibleReadingSchedule { get; set; }
3939

4040
public int SnoozeMinutes { get; set; } = 5;
4141

@@ -92,9 +92,10 @@ private static void ValidateNextFire(CronExpression expression)
9292
if (nextFire == null) throw new Exception("Invalid alarm time.");
9393
}
9494

95-
public int CompareTo(object obj)
95+
public int CompareTo(object? obj)
9696
{
97-
return Id.CompareTo(((AlarmSchedule)obj).Id);
97+
if (obj is not AlarmSchedule other) return 1;
98+
return Id.CompareTo(other.Id);
9899
}
99100

100101
public static async Task<AlarmSchedule> GetSampleSchedule(bool isNew, MediaDbContext mediaDbContext)
@@ -130,17 +131,28 @@ public static async Task<AlarmSchedule> GetSampleSchedule(bool isNew, MediaDbCon
130131
== sample.BibleReadingSchedule.LanguageCode)
131132
.FirstOrDefaultAsync();
132133

134+
if (bible == null)
135+
throw new InvalidOperationException("Bible translation not found for sample schedule");
136+
133137
var rnd = new Random();
134138
var book = bible.Books[rnd.Next() % bible.Books.Count];
139+
if (sample.BibleReadingSchedule == null)
140+
throw new InvalidOperationException("BibleReadingSchedule is null in sample schedule");
141+
135142
sample.BibleReadingSchedule.BookNumber = book.Number;
136143

144+
if (sample.Music == null)
145+
throw new InvalidOperationException("Music is null in sample schedule");
146+
137147
var music = await mediaDbContext
138148
.MelodyMusic
139149
.Include(x => x.Tracks)
140150
.Where(x => x.Code
141151
== sample.Music.PublicationCode)
142152
.FirstOrDefaultAsync();
143153

154+
if (music == null)
155+
throw new InvalidOperationException("Melody music not found for sample schedule");
144156

145157
var track = music.Tracks[rnd.Next() % music.Tracks.Count];
146158
sample.Music.TrackNumber = track.Number;

src/Bible.Alarm/Models/Schedule/BibleReadingSchedule.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ public class BibleReadingSchedule
55
{
66
public int Id { get; set; }
77

8-
public string LanguageCode { get; set; }
9-
public string PublicationCode { get; set; }
8+
public string LanguageCode { get; set; } = string.Empty;
9+
public string PublicationCode { get; set; } = string.Empty;
1010

1111
public int BookNumber { get; set; }
1212
public int ChapterNumber { get; set; }
1313
public TimeSpan FinishedDuration { get; set; }
1414

15-
public virtual AlarmSchedule AlarmSchedule { get; set; }
15+
public virtual AlarmSchedule? AlarmSchedule { get; set; }
1616
public int AlarmScheduleId { get; set; }
1717
}

src/Bible.Alarm/Services/Media/PlaylistService.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ public async Task MarkTrackAsFinished(NotificationDetail trackDetail)
135135
var next = await GetNextBibleChapter(trackDetail.LanguageCode, trackDetail.PublicationCode,
136136
trackDetail.BookNumber, trackDetail.ChapterNumber);
137137

138+
if (next.Key == null || next.Value == null)
139+
throw new InvalidOperationException($"Next chapter Key or Value is null");
140+
138141
bibleReadingSchedule.BookNumber = next.Key.Number;
139142
bibleReadingSchedule.ChapterNumber = next.Value.Number;
140143
bibleReadingSchedule.FinishedDuration = TimeSpan.Zero;
@@ -227,6 +230,8 @@ public async Task<List<PlayItem>> NextTracks(long scheduleId)
227230
}
228231

229232
var chapterDetail = chapters[chapter];
233+
if (chapterDetail.Source == null)
234+
throw new InvalidOperationException($"Chapter {chapter} Source is null in book {bookNumber}");
230235

231236
var publicationCode = bibleReadingSchedule.PublicationCode;
232237
var languageCode = bibleReadingSchedule.LanguageCode;
@@ -267,6 +272,11 @@ public async Task<List<PlayItem>> NextTracks(long scheduleId)
267272
bibleReadingSchedule.PublicationCode,
268273
bookNumber, chapter);
269274

275+
if (next.Key == null || next.Value == null)
276+
throw new InvalidOperationException($"Next chapter Key or Value is null");
277+
if (next.Value.Source == null)
278+
throw new InvalidOperationException($"Next chapter Source is null");
279+
270280
bookNumber = next.Key.Number;
271281
chapter = next.Value.Number;
272282
url = next.Value.Source.Url;
@@ -323,8 +333,11 @@ public async Task MoveToPreviousBibleChapter(long scheduleId)
323333

324334
var previous = await GetPreviousBibleChapter(languageCode, publicationCode, bookNumber, chapter);
325335

326-
bibleReadingSchedule.BookNumber = previous.Key.Number;
327-
bibleReadingSchedule.ChapterNumber = previous.Value.Number;
336+
if (previous.Key == null || previous.Value == null)
337+
throw new InvalidOperationException($"Previous chapter Key or Value is null");
338+
339+
bibleReadingSchedule.BookNumber = previous.Key.Number;
340+
bibleReadingSchedule.ChapterNumber = previous.Value.Number;
328341
bibleReadingSchedule.FinishedDuration = TimeSpan.Zero;
329342

330343
await scheduleDbContext.SaveChangesAsync();
@@ -432,6 +445,9 @@ private async Task<PlayItem> NextMusicUrlToPlay(AlarmSchedule schedule, bool nex
432445
throw new InvalidOperationException($"Invalid track index {melodyTrackIndex} for {melodyTracks.Count} tracks");
433446

434447
var melodyTrack = melodyTracks[melodyTrackIndex];
448+
if (melodyTrack.Source == null)
449+
throw new InvalidOperationException($"Melody track {melodyTrackIndex} Source is null");
450+
435451
return new PlayItem(new NotificationDetail
436452
{
437453
ScheduleId = schedule.Id,
@@ -452,6 +468,9 @@ private async Task<PlayItem> NextMusicUrlToPlay(AlarmSchedule schedule, bool nex
452468
throw new InvalidOperationException($"Invalid track index {vocalTrackIndex} for {vocalTracks.Count} tracks");
453469

454470
var vocalTrack = vocalTracks[vocalTrackIndex];
471+
if (vocalTrack.Source == null)
472+
throw new InvalidOperationException($"Vocal track {vocalTrackIndex} Source is null");
473+
455474
return new PlayItem(new NotificationDetail
456475
{
457476
ScheduleId = schedule.Id,

src/Bible.Alarm/Stores/State.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,26 @@ public class ApplicationState
99
{
1010
public ObservableHashSet<AlarmSchedule> Schedules { get; set; }
1111

12-
public AlarmSchedule CurrentSchedule { get; set; }
12+
public AlarmSchedule? CurrentSchedule { get; set; }
1313

14-
public AlarmMusic CurrentMusic { get; set; }
15-
public AlarmMusic TentativeMusic { get; set; }
14+
public AlarmMusic? CurrentMusic { get; set; }
15+
public AlarmMusic? TentativeMusic { get; set; }
1616

17-
public BibleReadingSchedule CurrentBibleReadingSchedule { get; set; }
18-
public BibleReadingSchedule TentativeBibleReadingSchedule { get; set; }
17+
public BibleReadingSchedule? CurrentBibleReadingSchedule { get; set; }
18+
public BibleReadingSchedule? TentativeBibleReadingSchedule { get; set; }
1919

2020
public ApplicationState()
2121
{
2222
Schedules = new ObservableHashSet<AlarmSchedule>();
2323
}
2424

2525
public ApplicationState(
26-
ObservableHashSet<AlarmSchedule> schedules,
27-
AlarmSchedule currentSchedule,
28-
AlarmMusic currentMusic,
29-
AlarmMusic tentativeMusic,
30-
BibleReadingSchedule currentBibleReadingSchedule,
31-
BibleReadingSchedule tentativeBibleReadingSchedule)
26+
ObservableHashSet<AlarmSchedule>? schedules,
27+
AlarmSchedule? currentSchedule,
28+
AlarmMusic? currentMusic,
29+
AlarmMusic? tentativeMusic,
30+
BibleReadingSchedule? currentBibleReadingSchedule,
31+
BibleReadingSchedule? tentativeBibleReadingSchedule)
3232
{
3333
Schedules = schedules ?? new ObservableHashSet<AlarmSchedule>();
3434
CurrentSchedule = currentSchedule;

0 commit comments

Comments
 (0)