package fi.jyu.ln.luontonurkka;

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.NavigationView;
import android.util.AttributeSet;
import android.widget.CompoundButton;
import android.widget.Switch;

import fi.jyu.ln.luontonurkka.tools.SettingsManager;

/**
 * Extension to navigation view that saves the
 * settings toggles in the drawer.
 *
 * Created by Jarno on 22.10.16.
 */

public class CustomNavigationView extends NavigationView {
    public CustomNavigationView(Context context, AttributeSet attrs) {
        super(context, attrs);

        final SettingsManager sm = new SettingsManager((Activity) context);

        final String nightSettingsString = context.getString(R.string.setting_night_theme);
        Switch nightSwitch = (Switch)getHeaderView(0).findViewById(R.id.switch_night);
        boolean nightTheme = sm.getBool(nightSettingsString);
        if(nightTheme) {
            nightSwitch.toggle();
        }
        nightSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                sm.setBool(nightSettingsString, isChecked);
            }
        });
    }
}

@@ -174,16 +174,42 @@
 uteSet;%0A
+import android.view.View;%0A
 import a
@@ -238,16 +238,16 @@
 Button;%0A
-
 import a
@@ -509,16 +509,56 @@
 nView %7B%0A
+%0A    private final SettingsManager sm;%0A%0A
     publ
@@ -660,37 +660,60 @@
         
-final SettingsManager
+View headerView = getHeaderView(0);%0A%0A       
  sm = ne
@@ -881,20 +881,17 @@
 (Switch)
-getH
+h
 eaderVie
@@ -891,19 +891,16 @@
 aderView
-(0)
 .findVie
@@ -1327,32 +1327,32 @@
 ;%0A            %7D%0A
-
         %7D);%0A    
@@ -1346,16 +1346,1242 @@
      %7D);
+%0A%0A        final String gpsSettingsString = context.getString(R.string.setting_disable_gps);%0A        Switch gpsSwitch = (Switch)headerView.findViewById(R.id.switch_gps);%0A        boolean gps = sm.getBool(gpsSettingsString);%0A        if(gps) %7B%0A            gpsSwitch.toggle();%0A        %7D%0A        gpsSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() %7B%0A            @Override%0A            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) %7B%0A                sm.setBool(gpsSettingsString, isChecked);%0A            %7D%0A        %7D);%0A%0A        final String mapSettingsString = context.getString(R.string.setting_map_default);%0A        Switch mapSwitch = (Switch)headerView.findViewById(R.id.switch_map);%0A        boolean mapDefault = sm.getBool(mapSettingsString);%0A        if(mapDefault) %7B%0A            mapSwitch.toggle();%0A        %7D%0A        mapSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() %7B%0A            @Override%0A            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) %7B%0A                sm.setBool(mapSettingsString, isChecked);%0A            %7D%0A        %7D);%0A    %7D%0A%0A    public SettingsManager getSettingsManager() %7B%0A        return sm;
 %0A    %7D%0A%7D

Save other settings too