Skip to content

Commit

Permalink
Added functionality to Settings screen #40
Browse files Browse the repository at this point in the history
  • Loading branch information
Schmitt-Florian committed Jun 6, 2017
1 parent 69672b1 commit 9a1d14d
Show file tree
Hide file tree
Showing 7 changed files with 419 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;

Expand Down Expand Up @@ -103,10 +104,12 @@ static GregorianCalendar getDateFromMandatoryEditText(View view, int id) throws
* @param view the view the {@link TextView} is in
* @param id Resource ID of the {@link TextView}
* @param text The text to set to the {@link TextView}
* @return the updated {@link TextView}
*/
static void setTextToTextView(View view, int id, String text) {
static TextView setTextToTextView(View view, int id, String text) {
TextView textView = (TextView) view.findViewById(id);
textView.setText(text);
return textView;
}

/**
Expand All @@ -115,10 +118,12 @@ static void setTextToTextView(View view, int id, String text) {
* @param view the view the {@link Button} is in
* @param id Resource ID of the {@link Button}
* @param colorId Resource ID of the color
* @return the updated {@link Button}
*/
static void setColorToButton(View view, int id, int colorId) {
static Button setColorToButton(View view, int id, int colorId) {
Button b = (Button) view.findViewById(id);
b.setBackgroundResource(colorId);
return b;
}

/**
Expand All @@ -128,10 +133,12 @@ static void setColorToButton(View view, int id, int colorId) {
* @param id Resource ID of the {@link View}
* @param visibility The visibility to set to the {@link View},
* must be one of {@link View#VISIBLE} , {@link View#INVISIBLE} , {@link View#GONE}
* @return the updated {@link View}
*/
static void setVisibility(View view, int id, int visibility) {
static View setVisibility(View view, int id, int visibility) {
View v = view.findViewById(id);
v.setVisibility(visibility);
return v;
}

/**
Expand All @@ -140,12 +147,14 @@ static void setVisibility(View view, int id, int visibility) {
* @param view the view the {@link ListView} is in
* @param id Resource ID of the {@link ListView}
* @param content The content to fill the {@link ListView} with as string array
* @return the updated {@link ListView}
*/
static void fillListViewFromArray(View view, int id, String[] content) {
static ListView fillListViewFromArray(View view, int id, String[] content) {
ListView listView = (ListView) view.findViewById(id);
ArrayAdapter<String> adapter = new ArrayAdapter<>(view.getContext(), android.R.layout.simple_list_item_1, content);

listView.setAdapter(adapter);
return listView;
}

/**
Expand All @@ -157,12 +166,14 @@ static void fillListViewFromArray(View view, int id, String[] content) {
* Fills the grid from left to right, so if you have a {@link GridView}
* with the {@link GridView#getNumColumns()} == 2 the content[] indices 0 & 1
* will form the first row in the grid, 2 & 3 the second row and so on.
* @return the updated {@link GridView}
*/
static void fillGridViewFromArray(View view, int id, String[] content) {
static GridView fillGridViewFromArray(View view, int id, String[] content) {
GridView gridView = (GridView) view.findViewById(id);
ArrayAdapter<String> adapter = new ArrayAdapter<>(view.getContext(), android.R.layout.simple_list_item_1, content);

gridView.setAdapter(adapter);
return gridView;
}

/**
Expand All @@ -171,13 +182,15 @@ static void fillGridViewFromArray(View view, int id, String[] content) {
* @param view the view the {@link Spinner} is in
* @param id Resource ID of the {@link Spinner}
* @param content The content to fill the {@link Spinner} with as string array
* @return the updated {@link Spinner}
*/
static void fillSpinnerFromArray(View view, int id, String[] content) {
static Spinner fillSpinnerFromArray(View view, int id, String[] content) {
Spinner spinner = (Spinner) view.findViewById(id);
ArrayAdapter<String> adapter = new ArrayAdapter<>(view.getContext(), android.R.layout.simple_spinner_item, content);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinner.setAdapter(adapter);
return spinner;
}

//region GUI string
Expand Down Expand Up @@ -261,10 +274,12 @@ static String extractGuiString(Teacher teacher, Context context) {
* @param view the view the {@link Button} is in
* @param id Resource ID of the {@link Button}
* @param onClickListener the {@link View.OnClickListener} to set the {@link Button} to
* @return the updated {@link Button}
*/
static void defineButtonOnClickListener(View view, int id, View.OnClickListener onClickListener) {
static Button defineButtonOnClickListener(View view, int id, View.OnClickListener onClickListener) {
Button b = (Button) view.findViewById(id);
b.setOnClickListener(onClickListener);
return b;
}

/**
Expand All @@ -273,10 +288,26 @@ static void defineButtonOnClickListener(View view, int id, View.OnClickListener
* @param view the view the {@link FloatingActionButton} is in
* @param id Resource ID of the {@link FloatingActionButton}
* @param onClickListener the {@link View.OnClickListener} to set the {@link FloatingActionButton} to
* @return the updated {@link FloatingActionButton}
*/
static void defineFloatingActionButtonOnClickListener(View view, int id, View.OnClickListener onClickListener) {
static FloatingActionButton defineFloatingActionButtonOnClickListener(View view, int id, View.OnClickListener onClickListener) {
FloatingActionButton b = (FloatingActionButton) view.findViewById(id);
b.setOnClickListener(onClickListener);
return b;
}

/**
* method to set the {@link SeekBar.OnSeekBarChangeListener} of a {@link SeekBar} at a given id
*
* @param view the view the {@link SeekBar} is in
* @param id Resource ID of the {@link SeekBar}
* @param onSeekBarChangeListener The {@link SeekBar.OnSeekBarChangeListener} to set to the {@link SeekBar}
* @return the updated {@link SeekBar}
*/
static SeekBar defineSeekBarOnChangeListener(View view, int id, SeekBar.OnSeekBarChangeListener onSeekBarChangeListener) {
SeekBar seekBar = (SeekBar) view.findViewById(id);
seekBar.setOnSeekBarChangeListener(onSeekBarChangeListener);
return seekBar;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,73 +8,40 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.Toast;

import schmitt_florian.schoolplanner.R;
import schmitt_florian.schoolplanner.logic.Settings;

/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link SettingsFragment.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link SettingsFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class SettingsFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

public class SettingsFragment extends Fragment implements View.OnClickListener {
@SuppressWarnings({"FieldNever", "unused"})
private OnFragmentInteractionListener mListener;
private Settings settings;
private View view;

public SettingsFragment() {
// Required empty public constructor
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment SettingsFragment.
*/
// TODO: Rename and change types and number of parameters
public static SettingsFragment newInstance(String param1, String param2) {
SettingsFragment fragment = new SettingsFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_settings, container, false);
}
view = inflater.inflate(R.layout.fragment_settings, container, false);

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
settings = Settings.getInstance(view.getContext());
initGui();
return view;
}

@Override
Expand All @@ -94,6 +61,22 @@ public void onDetach() {
mListener = null;
}

/**
* Called when a view has been clicked.
*
* @param v The view that was clicked.
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.settings_buttonSave:
readGui();
settings.saveSettings();
Toast.makeText(getContext(), R.string.string_settings_saved, Toast.LENGTH_SHORT).show();
}
}


/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
Expand All @@ -105,7 +88,73 @@ public void onDetach() {
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
@SuppressWarnings({"FieldNever", "unused"})
void onFragmentInteraction(Uri uri);
}

//region private methods

/**
* method to initialise components of the GUI
*/
private void initGui() {
initSeekBar();
initDateFormatSpinner();
GuiHelper.defineButtonOnClickListener(view, R.id.settings_buttonSave, this);

}

/**
* initialises the {@link SeekBar} which displays the {@link Settings#periodsAtDay}
*/
private void initSeekBar() {
GuiHelper.defineSeekBarOnChangeListener(view, R.id.settings_seekbarPeriods,
new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
GuiHelper.setTextToTextView(view, R.id.settings_textviewSeekbarPeriodsPos, String.valueOf(progress));
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
//ignore
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//ignore
}
}
).setProgress(settings.getPeriodsAtDay());
}

/**
* initialises the {@link Settings#DATE_FORMAT} {@link Spinner}
*/
private void initDateFormatSpinner() {
Spinner spinner = GuiHelper.fillSpinnerFromArray(view, R.id.settings_spinnerDate,
new String[]{Settings.DATE_FORMAT_DDMMYYYY, Settings.DATE_FORMAT_MMDDYYYY, Settings.DATE_FORMAT_YYYYMMDD});
switch (settings.getActiveDateFormat()) {
case Settings.DATE_FORMAT_DDMMYYYY:
spinner.setSelection(0);
break;
case Settings.DATE_FORMAT_MMDDYYYY:
spinner.setSelection(1);
break;
case Settings.DATE_FORMAT_YYYYMMDD:
spinner.setSelection(2);
}
}

/**
* updates {@link SettingsFragment#settings} with values in GUI
*/
private void readGui() {
SeekBar seekBar = (SeekBar) view.findViewById(R.id.settings_seekbarPeriods);
settings.setPeriodsAtDay(seekBar.getProgress());

Spinner spinner = (Spinner) view.findViewById(R.id.settings_spinnerDate);
settings.setActiveDateFormat((String) spinner.getSelectedItem());
}
//endregion
}
Loading

0 comments on commit 9a1d14d

Please sign in to comment.