private async Task onAddManually() { try { if (user.OriginUser == null) { MessagingCenter.Send(this, "emptyOrigin"); } else { var businesscardEntryPage = new BusinesscardEntryPage(); await Navigation.PushAsync(businesscardEntryPage); } } catch (Exception) { Debug.WriteLine("BusinessCardsViewModel - Failed to add card"); } }
// Method which analyses the photo using the ocr service and then redirecting to the entrypage public async Task SendToOCR(string path, int id) { try { network = Connectivity.NetworkAccess; if (network == NetworkAccess.Internet) { // Send a message to the view to start the loading indicator and to disable the buttons MessagingCenter.Send(this, "Sending"); DependencyService.Get <IToast>().LongAlert("Analyzing businesscard.\nThis may take a while."); //Reset the OCR Service ocrService.resetOCR(); // Define a new card before trying to analyze the image Businesscard card; // We want the analyzing to stop if it doesn't complete within a certain timeframe int timeout = 20000; Task <Businesscard> getCardTask = ocrService.getCard(path); using (var timeoutCancellationTokenSource = new CancellationTokenSource()) { // Check which task finishes first: the delay or the scanning if (await Task.WhenAny(getCardTask, Task.Delay(timeout, timeoutCancellationTokenSource.Token)) == getCardTask) { // Task completed within timeout. // Consider that the task may have faulted or been canceled. // We re-await the task so that any exceptions/cancellation is rethrown. card = await getCardTask; } else { // Task didn't complete within timeout. // Throw timeexception to show relevant toast throw new TimeoutException("OCR took to long."); } } // Save the base64 version of the image in the object to later send to the endpoint card.Base64 = imageService.ConvertImageToBase64(path); card.Id = id; var businesscardEntryPage = new BusinesscardEntryPage(card); await Navigation.PushAsync(businesscardEntryPage); MessagingCenter.Send(this, "Done"); } else { Debug.WriteLine("Won't try to scan this image since there is no internet connection"); DependencyService.Get <IToast>().LongAlert("Can't scan the businesscard.\nThere is no internet connection."); } } catch (BadRequestException ex) { Debug.WriteLine("BADREQUESTEXCEPTION for OCR: " + ex.ToString()); // Functionality to display a pop up - sending a message to the view that a bad request was sent MessagingCenter.Send(this, "BadRequest"); MessagingCenter.Send(this, "Done"); } catch (FormRecognizerException ex) { Debug.WriteLine("FORMRECOGNIZEREXCEPTION for OCR: " + ex.ToString()); // Functionality to display a pop up - sending a message to the view that a bad request was sent MessagingCenter.Send(this, "GeneralError"); MessagingCenter.Send(this, "Done"); } catch (TimeoutException ex) { Debug.WriteLine("TIMEOUTEXCEPTION for OCR: " + ex.ToString()); MessagingCenter.Send(this, "TimeoutException"); MessagingCenter.Send(this, "Done"); } catch (Exception ex) { Debug.WriteLine("GENERALEXCEPTION for OCR: " + ex.ToString()); // Functionality to display a pop up - sending a message to the view that a bad request was sent MessagingCenter.Send(this, "GeneralError"); MessagingCenter.Send(this, "Done"); } }