package com.satsumasoftware.timetable.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.satsumasoftware.timetable.R;
import com.satsumasoftware.timetable.db.util.TimetableUtils;
import com.satsumasoftware.timetable.framework.Timetable;
import com.satsumasoftware.timetable.ui.adapter.TimetablesAdapter;
import com.satsumasoftware.timetable.util.ThemeUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class TimetablesActivity extends AppCompatActivity {

    protected static final int REQUEST_CODE_TIMETABLE_EDIT = 1;

    private ArrayList<Timetable> mTimetables;
    private TimetablesAdapter mAdapter;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        toolbar.setNavigationIcon(ThemeUtils.tintDrawable(this, R.drawable.ic_done_black_24dp));
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                actionDone();
            }
        });

        mTimetables = TimetableUtils.getTimetables(this);
        sortList();

        mAdapter = new TimetablesAdapter(this, mTimetables);
        mAdapter.setOnEntryClickListener(new TimetablesAdapter.OnEntryClickListener() {
            @Override
            public void onEntryClick(View view, int position) {
                Intent intent = new Intent(TimetablesActivity.this, TimetableEditActivity.class);
                intent.putExtra(TimetableEditActivity.EXTRA_TIMETABLE, mTimetables.get(position));
                startActivityForResult(intent, REQUEST_CODE_TIMETABLE_EDIT);
            }
        });

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(mAdapter);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(TimetablesActivity.this, TimetableEditActivity.class);
                startActivityForResult(intent, REQUEST_CODE_TIMETABLE_EDIT);
            }
        });
    }

    private void refreshList() {
        mTimetables.clear();
        mTimetables.addAll(TimetableUtils.getTimetables(this));
        sortList();
        mAdapter.notifyDataSetChanged();
    }

    private void sortList() {
        Collections.sort(mTimetables, new Comparator<Timetable>() {
            @Override
            public int compare(Timetable t1, Timetable t2) {
                return t1.getStartDate().compareTo(t2.getStartDate());
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_CODE_TIMETABLE_EDIT) {
            if (resultCode == Activity.RESULT_OK) {
                refreshList();
            }
        }
    }

    @Override
    public void onBackPressed() {
        actionDone();
        super.onBackPressed();
    }

    private void actionDone() {
        Intent intent = new Intent(TimetablesActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

}

@@ -208,32 +208,83 @@
 ort.
-v7.app.AppCompatActivity
+design.widget.NavigationView;%0Aimport android.support.v4.widget.DrawerLayout
 ;%0Aim
@@ -680,62 +680,8 @@
 ter;
-%0Aimport com.satsumasoftware.timetable.util.ThemeUtils;
 %0A%0Aim
@@ -810,17 +810,12 @@
 nds 
-AppCompat
+Base
 Acti
@@ -1210,16 +1210,16 @@
 olbar);%0A
+
         
@@ -1253,303 +1253,8 @@
 );%0A%0A
-        toolbar.setNavigationIcon(ThemeUtils.tintDrawable(this, R.drawable.ic_done_black_24dp));%0A        toolbar.setNavigationOnClickListener(new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View view) %7B%0A                actionDone();%0A            %7D%0A        %7D);%0A%0A
     
@@ -3469,256 +3469,485 @@
    p
-ublic void onBackPressed() %7B%0A        actionDone(
+rotected Toolbar getSelfToolbar() %7B%0A        return (Toolbar) findViewById(R.id.toolbar
 );%0A    
+%7D%0A%0A
     
-super.onBackPressed();%0A    %7D%0A%0A    private void actionDone() %7B%0A        Intent intent = new Intent(TimetablesActivity.this, MainActivity.class);%0A        startActivity(intent);%0A        finish(
+@Override%0A    protected DrawerLayout getSelfDrawerLayout() %7B%0A        return (DrawerLayout) findViewById(R.id.drawerLayout);%0A    %7D%0A%0A    @Override%0A    protected int getSelfNavDrawerItem() %7B%0A        return NAVDRAWER_ITEM_MANAGE_TIMETABLES;%0A    %7D%0A%0A    @Override%0A    protected NavigationView getSelfNavigationView() %7B%0A        return (NavigationView) findViewById(R.id.navigationView
 );%0A 

Make TimetablesActivity extend BaseActivity