package com.sqli.blockchain.ethereum_android_sample.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.dd.processbutton.iml.ActionProcessButton;
import com.sqli.blockchain.ethereum_android_sample.R;

import ethereumjava.module.objects.NodeInfo;
import rx.Subscriber;

/**
 * Created by root on 14/11/16.
 */

public class NodeInfoFragment extends SampleFragment implements View.OnClickListener {

    ActionProcessButton nodeInfoButton;
    TextView textView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.nodeinfo,container,false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        nodeInfoButton = (ActionProcessButton) view.findViewById(R.id.nodeinfo_button);
        nodeInfoButton.setOnClickListener(this);

        textView = (TextView) view.findViewById(R.id.nodeinfo_content);
        textView.setText("");
    }

    @Override
    public void onClick(View v) {
        if( v == nodeInfoButton ){
            ethereumJava.admin.getNodeInfo()
                    .subscribe(new Subscriber<NodeInfo>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {
                            Log.e("GETH",e.getMessage());
                            onNodeInfoError();
                        }

                        @Override
                        public void onNext(NodeInfo nodeInfo) {
                            if( nodeInfo != null ) {
                                onNodeInfoSuccess(nodeInfo);
                            }
                        }
                    });
            nodeInfoButton.setMode(ActionProcessButton.Mode.ENDLESS);
        }
    }

    private void onNodeInfoError(){
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                nodeInfoButton.setProgress(-1);
            }
        });
    }

    private void onNodeInfoSuccess(final NodeInfo nodeInfo){
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                nodeInfoButton.setProgress(100);
                textView.setText(nodeInfo.enode);
            }
        });
    }
}

@@ -79,24 +79,67 @@
 .os.Bundle;%0A
+import android.support.annotation.NonNull;%0A
 import andro
@@ -495,16 +495,93 @@
 scriber;
+%0Aimport rx.android.schedulers.AndroidSchedulers;%0Aimport rx.functions.Action1;
 %0A%0A/**%0A *
@@ -1509,574 +1509,78 @@
     
-    .subscribe(new Subscriber%3CNodeInfo%3E() %7B%0A                        @Override%0A                        public void onCompleted() %7B%0A%0A                        %7D%0A%0A                        @Override%0A                        public void onError(Throwable e) %7B%0A                            Log.e(%22GETH%22,e.getMessage());%0A                            onNodeInfoError();%0A                        %7D%0A%0A                        @Override%0A                        public void onNext(NodeInfo nodeInfo) %7B%0A                            if( nodeInfo != null ) %7B%0A                                
+.observeOn(AndroidSchedulers.mainThread())%0A                .subscribe(
 onNo
@@ -1597,96 +1597,28 @@
 ess(
-n
+), onN
 odeInfo
-);%0A                            %7D%0A                        %7D%0A                    %7D
+Error()
 );%0A 
@@ -1703,36 +1703,63 @@
 %0A    %7D%0A%0A    
-private void
+@NonNull%0A    private Action1%3CThrowable%3E
  onNodeInfoE
@@ -1764,16 +1764,17 @@
 oError()
+ 
 %7B%0A      
@@ -1779,48 +1779,37 @@
     
-getActivity().runOnUiThread(new Runn
+return new Action1%3CThrow
 able
+%3E
 () %7B
@@ -1847,36 +1847,56 @@
     public void 
-run(
+call(Throwable throwable
 ) %7B%0A            
@@ -1946,33 +1946,32 @@
      %7D%0A        %7D
-)
 ;%0A    %7D%0A%0A    pri
@@ -1971,63 +1971,67 @@
     
-private void onNodeInfoSuccess(final NodeInfo nodeInfo)
+@NonNull%0A    private Action1%3CNodeInfo%3E onNodeInfoSuccess() 
 %7B%0A  
@@ -2040,48 +2040,36 @@
     
-getActivity().runOnUiThread(new Runnable
+return new Action1%3CNodeInfo%3E
 () %7B
@@ -2119,12 +2119,30 @@
 oid 
-run(
+call(NodeInfo nodeInfo
 ) %7B%0A
@@ -2263,17 +2263,16 @@
        %7D
-)
 ;%0A    %7D%0A

update app according to changes in ethereum-java-core API