From 40222055aa7b47cf2a4b0c204ba9268025fdb73c Mon Sep 17 00:00:00 2001 From: "Nelson R. Perez" Date: Mon, 4 Jun 2018 22:10:55 -0500 Subject: [PATCH] Including usage of the GetAccounts API call using the single service conection in the sample app --- .../luminiasoft/labs/sample/MainActivity.java | 65 +++++++++++++++++-- sample/src/main/res/layout/activity_main.xml | 19 ++++-- 2 files changed, 75 insertions(+), 9 deletions(-) diff --git a/sample/src/main/java/com/luminiasoft/labs/sample/MainActivity.java b/sample/src/main/java/com/luminiasoft/labs/sample/MainActivity.java index 7ccefb0..f4ac073 100644 --- a/sample/src/main/java/com/luminiasoft/labs/sample/MainActivity.java +++ b/sample/src/main/java/com/luminiasoft/labs/sample/MainActivity.java @@ -13,15 +13,20 @@ import android.widget.TextView; import com.google.gson.Gson; +import java.util.HashMap; + import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; +import cy.agorise.graphenej.UserAccount; import cy.agorise.graphenej.api.ConnectionStatusUpdate; import cy.agorise.graphenej.api.android.NetworkService; import cy.agorise.graphenej.api.android.RxBus; +import cy.agorise.graphenej.api.calls.GetAccounts; import cy.agorise.graphenej.api.calls.GetBlock; import cy.agorise.graphenej.models.JsonRpcResponse; import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.Disposable; import io.reactivex.functions.Consumer; public class MainActivity extends AppCompatActivity { @@ -38,13 +43,20 @@ public class MainActivity extends AppCompatActivity { private Gson gson = new Gson(); + private Disposable mDisposable; + + private HashMap responseMap = new HashMap<>(); + + private final int GET_BLOCK_RESPONSE = 0; + private final int GET_ACCOUNTS_RESPONSE = 1; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); - RxBus.getBusInstance() + mDisposable = RxBus.getBusInstance() .asFlowable() .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer() { @@ -58,16 +70,53 @@ public class MainActivity extends AppCompatActivity { Log.d(TAG,"Got connection update. Status: "+((ConnectionStatusUpdate)message).getConnectionStatus()); mConnectionStatus.setText(((ConnectionStatusUpdate) message).getConnectionStatus()); }else if(message instanceof JsonRpcResponse){ - mResponse.setText(mResponse.getText() + gson.toJson(message, JsonRpcResponse.class) + "\n"); + handleJsonRpcResponse((JsonRpcResponse) message); } } }); } - @OnClick(R.id.send_message) - public void onSendMesage(View v){ + /** + * Internal method that will decide what to do with each JSON-RPC response + * + * @param response The JSON-RPC api call response + */ + private void handleJsonRpcResponse(JsonRpcResponse response){ + int id = (int) response.id; + if(responseMap.get(id) != null){ + int responseId = responseMap.get(id); + switch(responseId){ + case GET_BLOCK_RESPONSE: + mResponse.setText(mResponse.getText() + gson.toJson(response, JsonRpcResponse.class) + "\n"); + break; + case GET_ACCOUNTS_RESPONSE: + mResponse.setText(mResponse.getText() + gson.toJson(response, JsonRpcResponse.class) + "\n"); + break; + default: + Log.w(TAG,"Case not handled"); + } + // Remember to remove the used id entry from the map, as it would + // otherwise just increase the app's memory usage + responseMap.remove(id); + }else{ + mResponse.setText(mResponse.getText() + gson.toJson(response, JsonRpcResponse.class) + "\n"); + } + } + + @OnClick(R.id.call_get_block) + public void onGetBlock(View v){ GetBlock getBlock = new GetBlock(1000000); - mService.sendMessage(getBlock, GetBlock.REQUIRED_API); + int id = mService.sendMessage(getBlock, GetBlock.REQUIRED_API); + // Registering the used sequence id + responseMap.put(id, GET_BLOCK_RESPONSE); + } + + @OnClick(R.id.call_get_accounts) + public void onGetAccounts(View v){ + GetAccounts getAccounts = new GetAccounts(new UserAccount("1.2.1000")); + int id = mService.sendMessage(getAccounts, GetBlock.REQUIRED_API); + // Registering the used sequence id + responseMap.put(id, GET_ACCOUNTS_RESPONSE); } @OnClick(R.id.next_activity) @@ -90,6 +139,12 @@ public class MainActivity extends AppCompatActivity { unbindService(mConnection); } + @Override + protected void onDestroy() { + super.onDestroy(); + mDisposable.dispose(); + } + /** Defines callbacks for backend binding, passed to bindService() */ private ServiceConnection mConnection = new ServiceConnection() { diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index 6a55457..b881258 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -5,23 +5,34 @@ android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.luminiasoft.labs.sample.MainActivity"> +