Example #1
0
        public IObservable <Unit> Connect()
        {
            return(Observable.Create <Unit>(o =>
            {
                InAppBillingServiceConnection.OnConnectedDelegate connectedDelegate = () =>
                {
                    o.OnNext(Unit.Default);
                    o.OnCompleted();
                };

                this.serviceConnection.OnConnected += connectedDelegate;

                this.serviceConnection.Connect();

                return () => this.serviceConnection.OnConnected -= connectedDelegate;
            }));
        }
Example #2
0
        /// <summary>
        /// Connects to the billing service.
        ///
        /// Call this method before making any other requests.
        /// </summary>
        /// <exception cref="InAppBillingException">
        /// (asynchronous) The connection to the billing service couldn't be established.
        /// </exception>
        public Task Connect()
        {
            return(Observable.Create <Unit>(o =>
            {
                if (this.serviceConnection.Connected)
                {
                    o.OnNext(Unit.Default);
                    o.OnCompleted();
                    return () => { };
                }

                InAppBillingServiceConnection.OnConnectedDelegate connectedDelegate = () =>
                {
                    o.OnNext(Unit.Default);
                    o.OnCompleted();
                };

                InAppBillingServiceConnection.OnInAppBillingErrorDelegate errorDelegate = (error, message) =>
                {
                    string realMessage = $"{message}: {error}";

                    o.OnError(new InAppBillingException(realMessage));
                };

                this.serviceConnection.OnConnected += connectedDelegate;
                this.serviceConnection.OnInAppBillingError += errorDelegate;

                this.serviceConnection.Connect();

                return () =>
                {
                    this.serviceConnection.OnConnected -= connectedDelegate;
                    this.serviceConnection.OnInAppBillingError -= errorDelegate;
                };
            }).ToTask());
        }