package org.foi.androidworkshop.activities;

import org.foi.androidworkshop.R;
import org.foi.androidworkshop.fragments.MemeFragment;
import org.foi.androidworkshop.listeners.FragmentActionListener;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.MenuItem;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MemeActivity extends BaseActivity implements FragmentActionListener {

    public static final String EXTRA_NAME = "EXTRA_NAME";

    @BindView(R.id.et_meme_input)
    EditText etMemeInput;

    @BindView(R.id.meme_container)
    LinearLayout memeContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_meme);
        ButterKnife.bind(this);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        String name = "No name passed";
        if (getIntent().getExtras() != null) {
            name = getIntent().getExtras().getString(EXTRA_NAME);
        }

        Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
    }

    @OnClick(R.id.btn_send_meme)
    public void sendMemeClicked() {
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.meme_fragment);

        if (fragment instanceof MemeFragment) {
            ((MemeFragment) fragment).setTopText(etMemeInput.getText().toString());
        }

        //Remove focus from edit text because Android is cancer
        memeContainer.requestFocus();

        //Remove keyboard manually once the user is done typing
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(etMemeInput.getWindowToken(), 0);
    }

    @Override
    public void textSuccessfullySet() {
        Toast.makeText(this, "Successfully set text", Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case android.R.id.home:
                onBackPressed();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}

@@ -704,16 +704,73 @@
 NAME%22;%0A%0A
+    public static final String MEME_NAME = %22MEME_NAME%22;%0A%0A
     @Bin
@@ -1305,32 +1305,190 @@
 ME);%0A        %7D%0A%0A
+        if (savedInstanceState != null) %7B%0A            etMemeInput.setText(savedInstanceState.getString(MEME_NAME));%0A            sendMemeClicked();%0A        %7D%0A%0A
         Toast.ma
@@ -2696,11 +2696,208 @@
 );%0A    %7D
+%0A%0A    @Override%0A    protected void onSaveInstanceState(Bundle outState) %7B%0A        super.onSaveInstanceState(outState);%0A        outState.putString(MEME_NAME, etMemeInput.getText().toString());%0A    %7D
 %0A%7D%0A

Handle orientation changes