package com.example.habitrack;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;


/**
 *  Handles all the Activities on the Main Page once Logged in
 */

public class MainActivity extends AppCompatActivity {

    // declare components
    Button createTypeButton;
    Button historybutton;
    Button allButton;
    Button logoutButton;
    private ListView displayNames;
    private ArrayList<HabitType> today = new ArrayList<HabitType>();
    private ArrayAdapter<HabitType> adapter;
    // Preliminary Setup
    // 1. Get the controllers
    HabitTypeController htc = new HabitTypeController(this);
    HabitEventController hec = new HabitEventController(this);

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

        createTypeButton = (Button) findViewById(R.id.createHabitButton);
        allButton = (Button) findViewById(R.id.allButton);
        historybutton = (Button) findViewById(R.id.historyButton);
        logoutButton = (Button) findViewById(R.id.button10);
        displayNames = (ListView) findViewById(R.id.listView);
// ------------------
        // Checks if app is in a logged in state. If not, goes to login page (SignupActivity)
        SharedPreferences loggedInPrefs = getApplicationContext().getSharedPreferences("loggedInStatus", MODE_PRIVATE);
        final SharedPreferences.Editor loggedInEditor = loggedInPrefs.edit();
        boolean isLoggedIn = loggedInPrefs.getBoolean("loggedIn", false);
        final Intent toLogIn = new Intent(getApplicationContext(), SignupActivity.class);
        if(!isLoggedIn) {
            startActivity(toLogIn);
        }

        //If logoutButton is clicked, change loggedIn to false and go to SignupAcitivty
        logoutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loggedInEditor.putBoolean("loggedIn", false);
                loggedInEditor.apply();
                startActivity(toLogIn);
            }
        });
// ------------------

        // Handles if user pressed CREATE button , redirects to create a new habit type class
        createTypeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent newType = new Intent(getApplicationContext(), NewHabitTypeActivity.class);
                startActivity(newType);
            }
        });

        // Handles if user pressed HISTORY button , redirects to history class
        historybutton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent newType = new Intent(getApplicationContext(), HabitHistory.class);
                //Intent newType = new Intent(getApplicationContext(), HistoryActivity.class);
                startActivity(newType);
            }
        });

        // Handles if user pressed ALL button, redirects to all habit types
        allButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent viewAll = new Intent(getApplicationContext(), AllHabitTypesActivity.class);
                startActivity(viewAll);
            }
        });

        // Handles the pressing of Habits on the Main Activity to launch a new habit event
        displayNames.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(MainActivity.this, NewHabitEventActivity.class);
                intent.putExtra("habitID", today.get(i).getID());
                startActivity(intent);
            }
        });
    }


    @Override
    protected void onStart() {
        super.onStart();
        // 2. load
        htc.loadHTID();
        hec.loadHEID();
        // 3. Restore all HT and HE if saved
        htc.loadFromFile();
        hec.loadFromFile();
        // 4. Get Recent events and HabitTypes for today
        htc.generateHabitsForToday();
        today = htc.getHabitTypesForToday();
        for (Integer prog = 0; prog < today.size(); prog++) {
            Integer typeID = today.get(prog).getID();
            HabitType ht = new HabitType(typeID);
            ht.incrementMaxCounter();
        }
        hec.updateRecentHabitEvents();

        adapter = new ArrayAdapter<HabitType>(this, R.layout.list_item, today);
        displayNames.setAdapter(adapter);

    }

    @Override
    protected void onResume() {
        super.onResume();
        // 2. load
//        htc.loadHTID();
//        hec.loadHEID();
//        // 3. Restore all HT and HE if saved
//        htc.loadFromFile();
//        hec.loadFromFile();
        // 4. Get Recent events and HabitTypes for today
        htc.generateHabitsForToday();
        today = htc.getHabitTypesForToday();
        hec.updateRecentHabitEvents();

//        adapter = new ArrayAdapter<HabitType>(this, R.layout.list_item, today);
//        displayNames.setAdapter(adapter);

    }

}


@@ -4531,32 +4531,69 @@
 ypesForToday();%0A
+        if (today.size() != 0) %7B%0A    
         for (Int
@@ -4646,24 +4646,28 @@
             
+    
 Integer type
@@ -4704,24 +4704,28 @@
             
+ 
+   
 HabitType ht
@@ -4750,16 +4750,20 @@
 ypeID);%0A
+    
         
@@ -4792,34 +4792,52 @@
 nter();%0A        
+    
 %7D%0A
+        %7D%0A    
         hec.upda
@@ -4860,32 +4860,36 @@
 nts();%0A%0A        
+   
+ 
 adapter = new Ar
@@ -4944,32 +4944,36 @@
 today);%0A        
+    
 displayNames.set
@@ -4991,27 +4991,35 @@
 pter);%0A%0A    
-%7D%0A%0A
+    %7D%0A%0A    
     @Overrid
@@ -5016,32 +5016,36 @@
   @Override%0A    
+ 
+   
 protected void o
@@ -5056,32 +5056,36 @@
 ume() %7B%0A        
+    
 super.onResume()
@@ -5082,24 +5082,28 @@
 onResume();%0A
+    
         // 2
@@ -5268,32 +5268,36 @@
 File();%0A        
+   
+ 
 // 4. Get Recent
@@ -5329,32 +5329,36 @@
 r today%0A        
+    
 htc.generateHabi
@@ -5371,32 +5371,36 @@
 oday();%0A        
+  
+  
 today = htc.getH
@@ -5412,32 +5412,36 @@
 ypesForToday();%0A
+    
         hec.upda
@@ -5595,14 +5595,22 @@
 );%0A%0A    
-%7D%0A%0A
+    %7D%0A%0A    
 %7D%0A%0A

Fix main