Start Using the Clover SDK

Add Imports:

import android.accounts.Account;
import android.os.AsyncTask;

import com.clover.sdk.util.CloverAccount;
import com.clover.sdk.v1.BindingException;
import com.clover.sdk.v1.ClientException;
import com.clover.sdk.v1.ServiceException;
import com.clover.sdk.v1.merchant.Merchant;
import com.clover.sdk.v1.merchant.MerchantConnector;
import com.clover.sdk.v3.inventory.InventoryConnector;
import com.clover.sdk.v3.inventory.Item;

import java.util.List;

Add Variables:

    private Account mAccount;
    private InventoryConnector mInventoryConnector;
    private MerchantConnector mMerchantConnector;
    private TextView mMerchantTextView;
    private TextView mInventoryTextView;

We want to add our text views to activity's onResume:

 @Override
    protected void onResume() {
        super.onResume();

        mMerchantTextView = (TextView) findViewById(R.id.merchantName);
        mInventoryTextView = (TextView) findViewById(R.id.inventoryItems);
    }

We need to bring in the Clover account so we can tie in the connectors:

  //Retrieve Clover account
        if (mAccount == null) {
            mAccount = CloverAccount.getAccount(this);

            if (mAccount == null) {
                return;
            }
        }

We will create functions to make the connectors for merchant and inventory:

 private void connectMerchant() {
        disconnectMerchant();

        if (mAccount != null) {
            mMerchantConnector = new MerchantConnector(this, mAccount, null);
            mMerchantConnector.connect();
        }
    }

    private void disconnectMerchant() {
        if (mMerchantConnector != null) {
            mMerchantConnector.disconnect();
            mMerchantConnector = null;
        }
    }

    private void connectInventory() {
        disconnectInventory();

        if (mAccount != null ) {
            mInventoryConnector = new InventoryConnector(this, mAccount, null);
            mInventoryConnector.connect();
        }
    }

    private void disconnectInventory() {
        if (mInventoryConnector != null) {
            mInventoryConnector.disconnect();
            mInventoryConnector = null;
        }
    }

Now we add those connecting functions to the onResume:

       connectInventory();
       connectMerchant();

And making sure to disconnect onDestroy:

  @Override
    protected void onDestroy() {
        super.onDestroy();
        disconnectInventory();
        disconnectMerchant();
    }

Now we will collect the information we are looking for in background tasks and display on the screen:

 private class InventoryAsyncTask extends AsyncTask<Void, Void, List<Item>> {

        @Override
        protected List<Item> doInBackground(Void... voids) {
            try {
                return mInventoryConnector.getItems();
            } catch (RemoteException | ClientException | ServiceException| BindingException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected final void onPostExecute(Item item) {
            super.onPostExecute(items);

            StringBuilder inventoryText = new StringBuilder("Items: ");
            inventoryText.append("\n");

            for (int i = 0; i < items.size(); i++) {
                inventoryText.append(items.get(i).getName() + "\n");
            }

            mInventoryTextView.setText(inventoryText.toString());
        }

    }

    private class MerchantAsyncTask extends AsyncTask<Void, Void, Merchant> {

        @Override
        protected Merchant doInBackground(Void... voids) {
            try {
                return mMerchantConnector.getMerchant();
            } catch (ServiceException | BindingException | ClientException | RemoteException e) {

            }

            return null;
        }

        @Override
        protected void onPostExecute(Merchant merchant) {
            super.onPostExecute(merchant);
            mMerchantTextView.setText("Merchant Name: " + merchant.getName());
        }
    }

Now we add the tasks to OnResume:

        new MerchantAsyncTask().execute();
        new InventoryAsyncTask().execute();

Here is what your final result should look like:

package com.jamescha.clovercodelab;

import android.accounts.Account;
import android.os.AsyncTask;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.clover.sdk.util.CloverAccount;
import com.clover.sdk.v1.BindingException;
import com.clover.sdk.v1.ClientException;
import com.clover.sdk.v1.ServiceException;
import com.clover.sdk.v1.merchant.Merchant;
import com.clover.sdk.v1.merchant.MerchantConnector;
import com.clover.sdk.v3.inventory.InventoryConnector;
import com.clover.sdk.v3.inventory.Item;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Account mAccount;
    private InventoryConnector mInventoryConnector;
    private MerchantConnector mMerchantConnector;
    private TextView mMerchantTextView;
    private TextView mInventoryTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onResume() {
        super.onResume();

        mMerchantTextView = (TextView) findViewById(R.id.merchantName);
        mInventoryTextView = (TextView) findViewById(R.id.inventoryItems);

        //Retrieve Clover account
        if (mAccount == null) {
            mAccount = CloverAccount.getAccount(this);

            if (mAccount == null) {
                return;
            }
        }

        connectInventory();
        connectMerchant();

        new MerchantAsyncTask().execute();
        new InventoryAsyncTask().execute();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        disconnectInventory();
        disconnectMerchant();
    }

    private void connectMerchant() {
        disconnectMerchant();

        if (mAccount != null) {
            mMerchantConnector = new MerchantConnector(this, mAccount, null);
            mMerchantConnector.connect();
        }
    }

    private void disconnectMerchant() {
        if (mMerchantConnector != null) {
            mMerchantConnector.disconnect();
            mMerchantConnector = null;
        }
    }

    private void connectInventory() {
        disconnectInventory();

        if (mAccount != null ) {
            mInventoryConnector = new InventoryConnector(this, mAccount, null);
            mInventoryConnector.connect();
        }
    }

    private void disconnectInventory() {
        if (mInventoryConnector != null) {
            mInventoryConnector.disconnect();
            mInventoryConnector = null;
        }
    }

    private class InventoryAsyncTask extends AsyncTask<Void, Void, List<Item>> {

        @Override
        protected List<Item> doInBackground(Void... voids) {
            try {
                return mInventoryConnector.getItems();
            } catch (RemoteException | ClientException | ServiceException| BindingException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected final void onPostExecute(List<Item> items) {
            super.onPostExecute(items);

            StringBuilder inventoryText = new StringBuilder("Items: ");
            inventoryText.append("\n");

            for (int i = 0; i < items.size(); i++) {
                inventoryText.append(items.get(i).getName() + "\n");
            }

            mInventoryTextView.setText(inventoryText.toString());
        }
    }

    private class MerchantAsyncTask extends AsyncTask<Void, Void, Merchant> {

        @Override
        protected Merchant doInBackground(Void... voids) {
            try {
                return mMerchantConnector.getMerchant();
            } catch (ServiceException | BindingException | ClientException | RemoteException e) {

            }

            return null;
        }

        @Override
        protected void onPostExecute(Merchant merchant) {
            super.onPostExecute(merchant);
            mMerchantTextView.setText("Merchant Name: " + merchant.getName());
        }
    }
}

Now we run what we have, and we should see (assuming no items were added):

In the next section, we will add items to our inventory to display.

results matching ""

    No results matching ""