package io.github.imsmobile.fahrplan;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.StringRes;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.widget.AutoCompleteTextView;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import io.github.imsmobile.fahrplan.adapter.StationAdapter;
import io.github.imsmobile.fahrplan.listener.TextWatcherAdapter;

public class SettingActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle(getResources().getString(R.string.settings_title));

        AutoCompleteTextView inputTakeMeHome = (AutoCompleteTextView) findViewById(R.id.input_take_me_home);
        inputTakeMeHome.setAdapter(new StationAdapter(this, R.layout.autocomplete_item));
        inputTakeMeHome.addTextChangedListener(new TextWatcherAdapter() {
            @Override
            public void afterTextChanged(Editable e) {
                saveSettings(getString(R.string.setting_key_take_me_home), e.toString());
            }
        });
        inputTakeMeHome.setText(getSettings(getString(R.string.setting_key_take_me_home), ""), false);
        initSettingSelection(R.id.cb_train, R.string.setting_transportation_train);
        initSettingSelection(R.id.cb_tram, R.string.setting_transportation_tram);
        initSettingSelection(R.id.cb_bus, R.string.setting_transportation_bus);
        initSettingSelection(R.id.cb_boat, R.string.setting_transportation_ship);
        initSettingSelection(R.id.cb_firstclass, R.string.setting_classes_first);
        initSettingSelection(R.id.cb_secondclass, R.string.setting_classes_second);
    }

    private void initSettingSelection(@IdRes int checkboxId , @StringRes int keyResId) {
        CheckBox transportationCheckbox = (CheckBox) findViewById(checkboxId);
        final String key = getString(keyResId);
        transportationCheckbox.setChecked(Boolean.valueOf(getSettings(key, String.valueOf(Boolean.TRUE))));
        transportationCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                saveSettings(key, String.valueOf(isChecked));
            }
        });
    }

    private void saveSettings(String key, String value) {
        SharedPreferences preferences = this.getSharedPreferences(getString(R.string.setting_name), MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(key, value);
        editor.apply();
    }

    private String getSettings(String key, String defaultValue) {
        SharedPreferences preferences = this.getSharedPreferences(getString(R.string.setting_name), MODE_PRIVATE);
        return preferences.getString(key, defaultValue);
    }
}


@@ -546,16 +546,69 @@
 Adapter;
+%0Aimport io.github.imsmobile.fahrplan.setting.Setting;
 %0A%0Apublic
@@ -659,16 +659,46 @@
 vity %7B%0A%0A
+    private Setting setting;%0A%0A
     @Ove
@@ -800,24 +800,66 @@
 anceState);%0A
+        this.setting = new Setting(this);%0A
         setC
@@ -1477,32 +1477,40 @@
                 
+setting.
 saveSettings(get
@@ -1502,34 +1502,24 @@
 aveSettings(
-getString(
 R.string.set
@@ -1539,17 +1539,16 @@
 _me_home
-)
 , e.toSt
@@ -1614,16 +1614,24 @@
 setText(
+setting.
 getSetti
@@ -1634,26 +1634,16 @@
 ettings(
-getString(
 R.string
@@ -1667,17 +1667,16 @@
 _me_home
-)
 , %22%22), f
@@ -2252,24 +2252,30 @@
  @StringRes 
+final 
 int keyResId
@@ -2361,56 +2361,8 @@
 d);%0A
-        final String key = getString(keyResId);%0A
     
@@ -2403,27 +2403,26 @@
 ked(
-Boolean.valueOf(get
+setting.getBoolean
 Sett
@@ -2433,39 +2433,19 @@
 (key
+ResId
 , 
-S
 tr
-ing.valueOf(Boolean.TRUE))
+ue
 ));%0A
@@ -2681,625 +2681,80 @@
    s
-aveS
 etting
-s(key, String.valueOf(isChecked));%0A            %7D%0A        %7D);%0A    %7D%0A%0A    private void saveSettings(String key, String value) %7B%0A        SharedPreferences preferences = this.getSharedPreferences(getString(R.string.setting_name), MODE_PRIVATE);%0A        SharedPreferences.Editor editor = preferences.edit();%0A        editor.putString(key, value);%0A        editor.apply();%0A
+.saveBooleanSettings(keyResId, isChecked);%0A        
     %7D%0A
-%0A
     
-private String getSettings(String key, String defaultValue) %7B%0A        SharedPreferences preferences = this.getSharedPreferences(getString(R.string.setting_name), MODE_PRIVATE);%0A        return preferences.getString(key, defaultValue
+    %7D
 );%0A 

Refactor SettingsActivity Use extract logic to read and persist settings from the Setting class