package blockchain.breizhcamp.attendeesdapp;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.sqli.blockchain.android_geth.EthereumService;

import ethereumjava.module.objects.BlockFilter;
import ethereumjava.module.objects.Hash;
import ethereumjava.module.objects.Transaction;
import ethereumjava.solidity.SolidityUtils;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;

public class ReadActivity extends AppCompatActivity implements EthereumService.EthereumServiceInterface{

    ProgressBar progressBar;
    MyApplication application;
    TextView lastMsgView;
    MediaPlayer player;

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

        application = (MyApplication) getApplication();
        application.registerGethReady(this);

        progressBar = (ProgressBar) findViewById(R.id.read_loading);
        progressBar.setIndeterminate(true);
        progressBar.setVisibility(ProgressBar.VISIBLE);

        lastMsgView = (TextView) findViewById(R.id.last_message_view);
        player = MediaPlayer.create(this,R.raw.alert);

    }

    @Override
    protected void onResume() {
        super.onResume();
        progressBar.setVisibility(ProgressBar.INVISIBLE);
    }

    @Override
    public void onEthereumServiceReady() {

            Observable.just(true)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(b -> progressBar.setVisibility(ProgressBar.INVISIBLE));


            new BlockFilter(application.ethereumjava.eth).watch()
                    .flatMap(blockHash -> application.ethereumjava.eth.getBlock(Hash.valueOf(blockHash), Transaction.class))
                    .flatMap(block -> Observable.from(block.transactions))
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(transaction -> {
                        if( transaction.input != null && transaction.input.length() > 2) { // No input is '0x'
                            Log.d(ReadActivity.class.getSimpleName(), "CONTENT : "+transaction.input);
                            lastMsgView.setText(SolidityUtils.hexToAscii(transaction.input));
                            player.start();
                        }
                    });

    }



}

@@ -1807,29 +1807,104 @@
     
-.flatMap(blockHash -%3E
+// Observable%3CBlock%3CHash%3E%3E%0A                    .flatMap(blockHash -%3E%0A                           
  app
@@ -1938,16 +1938,53 @@
 etBlock(
+%0A                                    
 Hash.val
@@ -2017,21 +2017,118 @@
 on.class
-))
 %0A  
+                          )%0A                    )%0A%0A%0A                    // Observable%3CTransaction%3E%0A
         
@@ -2129,32 +2129,34 @@
                 
+  
 .flatMap(block -
@@ -2194,16 +2194,223 @@
 tions))%0A
+%0A%0A                    // Observable%3CTransaction%3E%0A                    .filter(transaction -%3E%0A                        return transaction.input != null && transaction.input.length() %3E 2%0A                    )%0A%0A%0A
         
@@ -2456,32 +2456,34 @@
 s.mainThread())%0A
+%0A%0A
                 
@@ -2518,123 +2518,8 @@
 %3E %7B%0A
-                        if( transaction.input != null && transaction.input.length() %3E 2) %7B // No input is '0x'%0A    
     
@@ -2613,20 +2613,17 @@
 input);%0A
-    
+%0A
         
@@ -2704,19 +2704,17 @@
 nput));%0A
-   
+%0A
         
@@ -2725,25 +2725,24 @@
             
- 
 player.start
@@ -2749,34 +2749,8 @@
 ();%0A
-                        %7D%0A
     

add comments on attendees-dapp