Including usage of the GetAccounts API call using the single service conection in the sample app
This commit is contained in:
parent
aa758a09e0
commit
40222055aa
2 changed files with 75 additions and 9 deletions
|
@ -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<Integer, Integer> 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<Object>() {
|
||||
|
@ -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() {
|
||||
|
||||
|
|
|
@ -5,23 +5,34 @@
|
|||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context="com.luminiasoft.labs.sample.MainActivity">
|
||||
|
||||
<Button
|
||||
android:id="@+id/send_message"
|
||||
android:id="@+id/call_get_block"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Send message"
|
||||
app:layout_constraintBottom_toTopOf="@+id/response"
|
||||
android:text="Get block"
|
||||
app:layout_constraintBottom_toTopOf="@+id/call_get_accounts"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/call_get_accounts"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Get accounts"
|
||||
app:layout_constraintBottom_toTopOf="@+id/response"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/call_get_block" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/response"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="200dp"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/send_message"
|
||||
app:layout_constraintTop_toBottomOf="@id/call_get_block"
|
||||
app:layout_constraintBottom_toTopOf="@+id/next_activity"/>
|
||||
|
||||
<Button
|
||||
|
|
Loading…
Reference in a new issue