Skip to content

Commit 6a9b48f

Browse files
committed
Fix null ref in text recognition
1 parent b7f83c1 commit 6a9b48f

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

Telegram/Controls/Gallery/GalleryContent.xaml.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,14 @@ public async void RecognizeText()
644644
return;
645645
}
646646

647-
var service = TypeResolver.Current.Resolve<ITextRecognitionService>(_window.ViewModel.SessionId);
647+
var viewModel = _window.ViewModel;
648+
if (viewModel == null)
649+
{
650+
return;
651+
}
652+
653+
var fileId = _itemId;
654+
var service = TypeResolver.Current.Resolve<ITextRecognitionService>(viewModel.SessionId);
648655

649656
var status = await service.EnsureReadyAsync();
650657
if (status is TextRecognitionStatusUnavailable unavailable)
@@ -654,11 +661,11 @@ public async void RecognizeText()
654661
WatchDog.TrackEvent("TextRecognizer", new Properties { { "Status", "Unavailable" } });
655662
return;
656663
}
657-
else if (status is TextRecognitionStatusDownloading downloading)
664+
else if (status is TextRecognitionStatusDownloading downloading && fileId == _fileId && IsLoaded)
658665
{
659666
WatchDog.TrackEvent("TextRecognizer", new Properties { { "Status", "Downloading" } });
660667

661-
var confirm = await _window.ViewModel.ShowPopupAsync(new TextRecognitionDownloadPopup(_window.ViewModel.ClientService, _window.ViewModel.Aggregator, downloading.Document), requestedTheme: ElementTheme.Dark);
668+
var confirm = await viewModel.ShowPopupAsync(new TextRecognitionDownloadPopup(viewModel.ClientService, viewModel.Aggregator, downloading.Document), requestedTheme: ElementTheme.Dark);
662669
if (confirm != ContentDialogResult.Primary)
663670
{
664671
return;
@@ -671,7 +678,7 @@ public async void RecognizeText()
671678
WatchDog.TrackEvent("TextRecognizer", new Properties { { "Status", "Available" } });
672679
}
673680

674-
if (status is not TextRecognitionStatusAvailable available)
681+
if (status is not TextRecognitionStatusAvailable available || fileId != _itemId || !IsLoaded)
675682
{
676683
// TODO: Error: not available
677684
return;
@@ -680,9 +687,7 @@ public async void RecognizeText()
680687
IsTextSelectionEnabled = true;
681688
Selection.ShowSkeleton();
682689

683-
var fileId = _itemId;
684-
685-
var bitmap = await GetSoftwareBitmapAsync(_item.File);
690+
var bitmap = await GetSoftwareBitmapAsync(_item?.File);
686691
if (bitmap == null)
687692
{
688693
// TODO: Error: text recognition is not available
@@ -721,6 +726,11 @@ public async void RecognizeText()
721726

722727
public async Task<SoftwareBitmap> GetSoftwareBitmapAsync(File file)
723728
{
729+
if (_window?.ViewModel == null)
730+
{
731+
return null;
732+
}
733+
724734
var storage = await _window.ViewModel.ClientService.GetFileAsync(file);
725735
if (storage == null)
726736
{

Telegram/Services/ClientService.Files.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ public Task<File> GetFileAsync(int fileId)
9999

100100
public async Task<StorageFile> GetFileAsync(File file, bool completed = true)
101101
{
102+
if (file == null)
103+
{
104+
return null;
105+
}
106+
102107
// Extremely important to do this only for completed,
103108
// as this method is being used by RemoteFileStream as well.
104109
if (completed)

0 commit comments

Comments
 (0)