package com.andela.webservice;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView output;
    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
        progressBar.setVisibility(View.INVISIBLE);

        output = (TextView) findViewById(R.id.textView);
        output.setMovementMethod(new ScrollingMovementMethod());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        MyTask task = new MyTask();
        //This makes serial request
        //task.execute("param1", "param2", "param3");
        //For parrallel processing
        task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "param1", "param2", "param3");

        return true;
    }

    private void updateDisplay(String s) {
        output.append(s + "\n");
    }

    private class MyTask extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            updateDisplay("Starting task");
            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        protected String doInBackground(String... params) {
            for(int i = 0; i< params.length; i++) {
                publishProgress("working with " + params[i]);

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            return "Task Complete";
        }

        @Override
        protected void onPostExecute(String result) {
            updateDisplay(result);
            progressBar.setVisibility(View.INVISIBLE);

        }

        @Override
        protected void onProgressUpdate(String... values) {
            updateDisplay(values[0]);
        }
    }


}

@@ -331,16 +331,68 @@
 tView;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 public c
@@ -502,16 +502,40 @@
 ressBar;
+%0A    List%3CMyTask%3E tasks;
 %0A%0A    @O
@@ -802,32 +802,67 @@
 View.INVISIBLE);
+%0A        tasks = new ArrayList%3C%3E();
 %0A%0A        output
@@ -1451,17 +1451,16 @@
 ram3%22);%0A
-%0A
         
@@ -1729,24 +1729,64 @@
 ing task%22);%0A
+            if(tasks.size() == 0) %7B%0A    
             
@@ -1818,32 +1818,75 @@
 (View.VISIBLE);%0A
+            %7D%0A            tasks.add(this);%0A
         %7D%0A%0A     
@@ -2414,16 +2414,88 @@
 esult);%0A
+            tasks.remove(this);%0A            if(tasks.size() == 0) %7B%0A    
         
@@ -2541,16 +2541,29 @@
 SIBLE);%0A
+            %7D
 %0A       

Fix progressBar stoping before the final action completes