<|start_filename|>PayCardRecognizerSample.Android/Services/PayCardRecognizerService.cs<|end_filename|>
﻿using System;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.Runtime;
using Cards.Pay.Paycardsrecognizer.Sdk;
using PayCardRecognizerSample.Models;
using PayCardRecognizerSample.Services;

namespace PayCardRecognizerSample.Droid.Services
{
    public class PayCardRecognizerService : IPayCardRecognizerService
    {
        static TaskCompletionSource<PayCard> _cardTcs;
        static int _requestCodeScanCard = 1734;
        Activity _activity;

        public static void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            if(requestCode == _requestCodeScanCard)
            {
                if(resultCode == Result.Ok)
                {
                    Card card = data.GetParcelableExtra(ScanCardIntent.ResultPaycardsCard).JavaCast<Card>();

                    _cardTcs.TrySetResult(new PayCard()
                    {
                        HolderName = card.CardHolderName,
                        CardNumber = card.CardNumber,
                        ExpirationDate = card.ExpirationDate
                    });
                }
                else
                {
                    _cardTcs.TrySetCanceled();
                }
            }
        }

        public PayCardRecognizerService(Activity activity,int requestCode = 1734)
        {
            _requestCodeScanCard = requestCode;
            _activity = activity;


        }

        public async Task<PayCard> ScanAsync()
        {
            _cardTcs = new TaskCompletionSource<PayCard>();
            Intent intent = new ScanCardIntent.Builder(_activity).Build();
            _activity.StartActivityForResult(intent, _requestCodeScanCard);
            return await _cardTcs.Task;
        }
    }
}


<|start_filename|>PayCardRecognizerSample/App.xaml.cs<|end_filename|>
﻿using System;
using PayCardRecognizerSample.Services;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace PayCardRecognizerSample
{
    public partial class App : Application
    {
        public App(IPayCardRecognizerService payCardRecognizerService)
        {
            InitializeComponent();

            MainPage = new MainPage(payCardRecognizerService);
        }

        protected override void OnStart()
        {
        }

        protected override void OnSleep()
        {
        }

        protected override void OnResume()
        {
        }
    }
}


<|start_filename|>PayCardRecognizerSample/Services/IPayCardRecognizerService.cs<|end_filename|>
﻿using System;
using System.Threading.Tasks;
using PayCardRecognizerSample.Models;

namespace PayCardRecognizerSample.Services
{
    public interface IPayCardRecognizerService
    {
        Task<PayCard> ScanAsync();
    }
}


<|start_filename|>PayCardRecognizerSample/ViewModels/MainViewModel.cs<|end_filename|>
﻿using System;
using PayCardRecognizerSample.Services;
using System.Windows.Input;
using Xamarin.Forms;

namespace PayCardRecognizerSample.ViewModels
{
    public class MainViewModel
    {
        IPayCardRecognizerService _payCardRecognizerService;

        public ICommand ScanCommand
        {
            get
            {
                return new Command(async () =>
                {
                    try
                    {
                        var card = await _payCardRecognizerService.ScanAsync();
                        await App.Current.MainPage.DisplayAlert("Result", $"{card.HolderName}\n{card.CardNumber}\n{card.ExpirationDate}","Ok");
                    }
                    catch(Exception ex)
                    {

                    }
                   
                });
            }
        }

        public MainViewModel(IPayCardRecognizerService payCardRecognizerService)
        {
            _payCardRecognizerService = payCardRecognizerService;
        }

    }
}


<|start_filename|>PayCardRecognizerSample/Models/PayCard.cs<|end_filename|>
﻿using System;
namespace PayCardRecognizerSample.Models
{
    public class PayCard
    {
        public string HolderName { get; set; }

        public string CardNumber { get; set; }

        public string ExpirationDate { get; set; }
    }
}


<|start_filename|>PayCardRecognizerSample.iOS/RecognizerViewController.cs<|end_filename|>
﻿using System;
using System.Linq;
using CoreGraphics;
using PayCards;
using UIKit;

namespace PayCardRecognizerSample.iOS
{
    public partial class RecognizerViewController : UIViewController
    {
        IPayCardsRecognizerPlatformDelegate _recognizerDelegate;
        PayCardsRecognizer _recognizer;
        UIButton _closeButton;

        public RecognizerViewController(IPayCardsRecognizerPlatformDelegate recognizerDelegate) : base()
        {
            _recognizerDelegate = recognizerDelegate;
        }


        public RecognizerViewController(IntPtr handle) : base(handle)
        {
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            // Perform any additional setup after loading the view, typically from a nib.
            _recognizer = new PayCardsRecognizer(_recognizerDelegate,PayCardsRecognizerResultMode.Async, container: View, UIColor.Green);

            _closeButton = new UIButton(UIButtonType.Close);

            _closeButton.TouchUpInside += Close;


        }

        private void Close(object sender, EventArgs e)
        {
           DismissViewController(true, null);
        }

        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);
           
            _recognizer.StartCamera();
            var sb = View.Subviews.FirstOrDefault();

            if(_closeButton.Superview == null)
            {
                _closeButton.SetTitle("Close", UIControlState.Normal);
               
                sb.AddSubview(_closeButton);
                _closeButton.Frame = new CGRect(sb.Bounds.Width - 100, 0, 100, 80);
            }
           
        }

        public override void ViewDidDisappear(bool animated)
        {
            base.ViewDidDisappear(animated);

           
            _recognizer.StopCamera();
        }

        public override void DidReceiveMemoryWarning()
        {
            base.DidReceiveMemoryWarning();
            // Release any cached data, images, etc that aren't in use.
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            _closeButton.TouchUpInside -= Close;
        }
    }
}



<|start_filename|>PayCardRecognizerSample.iOS/Services/PayCardRecognizerService.cs<|end_filename|>
﻿using System;
using System.Threading.Tasks;
using Foundation;
using PayCardRecognizerSample.Models;
using PayCardRecognizerSample.Services;
using PayCards;
using UIKit;

namespace PayCardRecognizerSample.iOS.Services
{
    public class PayCardRecognizerService : NSObject, IPayCardRecognizerService, IPayCardsRecognizerPlatformDelegate
    {
        TaskCompletionSource<PayCard> _cardTcs;
        RecognizerViewController _recognizerViewController;

        public PayCardRecognizerService()
        {

        }

        [Export("payCardsRecognizer:didRecognize:")]
        public void DidRecognize(PayCardsRecognizer payCardsRecognizer, PayCardsRecognizerResult result)
        {
            if (result.IsCompleted)
            {
                _cardTcs.TrySetResult(new PayCard()
                {
                    HolderName = result.RecognizedHolderName,
                    CardNumber = result.RecognizedNumber,
                    ExpirationDate = $"{result.RecognizedExpireDateMonth}/{result.RecognizedExpireDateYear}"
                });

                _recognizerViewController.DismissViewController(true, null);
            }
           
        }

        public async Task<PayCard> ScanAsync()
        {
            _cardTcs = new TaskCompletionSource<PayCard>();
            var window = UIApplication.SharedApplication.KeyWindow;
            var _viewController = window.RootViewController;
            while (_viewController.PresentedViewController != null)
                _viewController = _viewController.PresentedViewController;


             _recognizerViewController = new RecognizerViewController(this);
            
             _viewController?.PresentViewController(_recognizerViewController, true, null);

            return await _cardTcs.Task;

        }

    }
}
