package ru.dyatel.tsuschedule;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import hirondelle.date4j.DateTime;
import ru.dyatel.tsuschedule.data.DataFragment;
import ru.dyatel.tsuschedule.parsing.Parity;
import ru.dyatel.tsuschedule.parsing.util.DateUtil;

import java.util.TimeZone;

public class NavigationDrawerFragment extends Fragment {

    private static final String DRAWER_LEARNED_KEY = "drawer_learned";

    private ActionBarDrawerToggle toggle;

    private TextView groupIndexText;
    private Spinner subgroupSpinner;

    public NavigationDrawerFragment() {
    }

    public void initialize(
            DrawerLayout drawerLayout, Toolbar toolbar, final DataFragment dataFragment,
            String savedGroupIndex, int savedSubgroup
    ) {
        dataFragment.setGroup(savedGroupIndex);
        dataFragment.setSubgroup(savedSubgroup);

        groupIndexText.setText(savedGroupIndex);
        subgroupSpinner.setSelection(savedSubgroup - 1); // subgroup 1 has (1 - 1) position in string array, etc.

        Activity activity = getActivity();

        toggle = new ActionBarDrawerToggle(
                activity,
                drawerLayout,
                toolbar,
                R.string.navigation_drawer_open,
                R.string.navigation_drawer_close
        ) {
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                //Activity activity = getActivity();

                // Hide the keyboard
                /*InputMethodManager imm =
                        (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(groupIndex.getWindowToken(), 0);*/
                groupIndexText.onEditorAction(EditorInfo.IME_ACTION_DONE);

                String group = groupIndexText.getText().toString();
                int subgroup = subgroupSpinner.getSelectedItemPosition() + 1;

                // Save new group and subgroup in DataFragment
                dataFragment.setGroup(group);
                if (dataFragment.getSubgroup() != subgroup) {
                    dataFragment.setSubgroup(subgroup);
                    dataFragment.broadcastDataUpdate();
                }
            }
        };
        drawerLayout.setDrawerListener(toggle);

        // Open drawer if user had never seen it
        SharedPreferences preferences =
                activity.getSharedPreferences(MainActivity.PREFERENCES_FILE, Context.MODE_PRIVATE);
        boolean seenDrawer = preferences.getBoolean(DRAWER_LEARNED_KEY, false);
        if (!seenDrawer) {
            drawerLayout.openDrawer(drawerLayout.findViewById(R.id.drawer_content));
            preferences.edit()
                    .putBoolean(DRAWER_LEARNED_KEY, true)
                    .apply();
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        toggle.syncState();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.navigation_drawer_layout, container, false);

        int parityStringResource =
                DateUtil.getWeekParity(DateTime.now(TimeZone.getDefault()))
                        .equals(Parity.EVEN) ? R.string.even_week : R.string.odd_week;
        ((TextView) root.findViewById(R.id.current_parity)).setText(getString(parityStringResource));

        groupIndexText = (TextView) root.findViewById(R.id.group_index);

        subgroupSpinner = (Spinner) root.findViewById(R.id.subgroup);
        ArrayAdapter<CharSequence> subgroupAdapter = ArrayAdapter.createFromResource(
                getActivity(),
                R.array.subgroups,
                android.R.layout.simple_spinner_item
        );
        subgroupAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        subgroupSpinner.setAdapter(subgroupAdapter);

        return root;
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        toggle.onConfigurationChanged(newConfig);
    }

}

@@ -536,24 +536,56 @@
 rayAdapter;%0A
+import android.widget.EditText;%0A
 import andro
@@ -1029,24 +1029,24 @@
 private 
+Edit
 Text
-View
  groupIn
@@ -4121,24 +4121,24 @@
 Text = (
+Edit
 Text
-View
 ) root.f

Change TextView to EditText for group index input