package zero.zd.daily_event_logger;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<Event> mEventList;
    ArrayAdapter<Event> mEventArrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showCreateEventDialog();
            }
        });

        mEventList = new ArrayList<>();

        ListView listView = (ListView) findViewById(R.id.list_event);
        mEventArrayAdapter = new EventArrayAdapter(this, R.layout.item_event, mEventList);
        listView.setAdapter(mEventArrayAdapter);
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_about:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void showCreateEventDialog() {
        ViewGroup dialogRootView = (ViewGroup) findViewById(R.id.root_dialog_create_event);
        final View dialogView = getLayoutInflater()
                .inflate(R.layout.dialog_create_event, dialogRootView);
        final EditText eventEditText = (EditText) dialogView.findViewById(R.id.edit_event);

        AlertDialog createEventDialog = new AlertDialog.Builder(this)
                .setTitle(R.string.title_event)
                .setView(dialogView)
                .setCancelable(false)
                .setNegativeButton(R.string.action_discard, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton(R.string.action_save, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String eventText = eventEditText.getText().toString();
                        eventText = eventText.substring(0, 1).toUpperCase() + eventText.substring(1);
                        addEvent(new Event(eventText, new Date()));
                        dialog.dismiss();
                    }
                })
                .create();
        createEventDialog.show();
    }

    private void addEvent(Event event) {
        mEventList.add(event);
        mEventArrayAdapter.notifyDataSetChanged();
    }

    private static class ViewHolder {
        private TextView eventTextView;
        private TextView dateTextView;
    }

    private class EventArrayAdapter extends ArrayAdapter<Event> {

        private Context mContext;
        private int mResource;
        private List<Event> mEventList;

        private EventArrayAdapter(Context context, int resource, List<Event> eventList) {
            super(context, resource, eventList);
            mContext = context;
            mResource = resource;
            mEventList = eventList;
        }

        @Override
        public int getCount() {
            return mEventList.size();
        }

        @Nullable
        @Override
        public Event getItem(int position) {
            return mEventList.get(position);
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

            ViewHolder viewHolder;

            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater)
                        mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(mResource, parent, false);

                viewHolder = new ViewHolder();
                viewHolder.eventTextView = (TextView) convertView.findViewById(R.id.text_event);
                viewHolder.dateTextView = (TextView) convertView.findViewById(R.id.text_date);

                Log.d("ZD", "yeah");
                convertView.setTag(viewHolder);
            } else viewHolder = (ViewHolder) convertView.getTag();

            Event event = mEventList.get(position);

            viewHolder.eventTextView.setText(event.getEvent());
            viewHolder.dateTextView.setText(event.getDate().toString());

            return convertView;
        }
    }
}

@@ -717,37 +717,8 @@
 iew;
-%0Aimport android.widget.Toast;
 %0A%0Aim
@@ -849,44 +849,8 @@
  %7B%0A%0A
-    private List%3CEvent%3E mEventList;%0A
     
@@ -888,16 +888,52 @@
 Adapter;
+%0A    private List%3CEvent%3E mEventList;
 %0A%0A    @O
@@ -1204,150 +1204,183 @@
     
-FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);%0A        fab.setOnClickListener(new View.OnClickListener() %7B%0A        
+initObjects();%0A    %7D%0A%0A    @Override%0A    public boolean onCreateOptionsMenu(Menu menu) %7B%0A        getMenuInflater().inflate(R.menu.menu_main, menu);%0A        return true;%0A    %7D%0A%0A
     
@@ -1385,32 +1385,24 @@
   @Override%0A
-        
     public v
@@ -1404,101 +1404,249 @@
 lic 
-void onClick(View view) %7B%0A                showCreateEventDialog();%0A            %7D%0A        %7D);%0A
+boolean onOptionsItemSelected(MenuItem item) %7B%0A        switch (item.getItemId()) %7B%0A            case R.id.action_about:%0A                break;%0A        %7D%0A        return super.onOptionsItemSelected(item);%0A    %7D%0A%0A    private void initObjects() %7B
 %0A   
@@ -1897,392 +1897,282 @@
 r);%0A
-    %7D%0A%0A    @Override%0A    public boolean onCreateOptionsMenu(Menu menu) %7B%0A        getMenuInflater().inflate(R.menu.menu_main, menu);%0A        return true;%0A    %7D%0A%0A    @Override%0A    public boolean onOptionsItemSelected(MenuItem item) %7B%0A        switch (item.getItemId()) %7B%0A            case R.id.action_about:%0A                break;%0A        %7D%0A        return super.onOptionsItemSelected(item
+%0A        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);%0A        fab.setOnClickListener(new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View view) %7B%0A                showCreateEventDialog();%0A            %7D%0A        %7D
 );%0A 
@@ -3335,71 +3335,28 @@
 t = 
-eventText.substring(0, 1).toUpperCase() + eventText.substring(1
+formatText(eventText
 );%0A 
@@ -3695,32 +3695,146 @@
 anged();%0A    %7D%0A%0A
+    private String formatText(String s) %7B%0A        return s.substring(0, 1).toUpperCase() + s.substring(1);%0A    %7D%0A%0A
     private stat
@@ -5274,45 +5274,8 @@
 );%0A%0A
-                Log.d(%22ZD%22, %22yeah%22);%0A
     

Clean Code