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.view.View;
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 {

    private final SettingsManager sm;

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

        View headerView = getHeaderView(0);

        sm = new SettingsManager((Activity) context);

        final String nightSettingsString = context.getString(R.string.setting_night_theme);
        Switch nightSwitch = (Switch)headerView.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);
            }
        });

        final String gpsSettingsString = context.getString(R.string.setting_disable_gps);
        Switch gpsSwitch = (Switch)headerView.findViewById(R.id.switch_gps);
        boolean gps = sm.getBool(gpsSettingsString);
        if(gps) {
            gpsSwitch.toggle();
        }
        gpsSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                sm.setBool(gpsSettingsString, isChecked);
            }
        });

        final String mapSettingsString = context.getString(R.string.setting_map_default);
        Switch mapSwitch = (Switch)headerView.findViewById(R.id.switch_map);
        boolean mapDefault = sm.getBool(mapSettingsString);
        if(mapDefault) {
            mapSwitch.toggle();
        }
        mapSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                sm.setBool(mapSettingsString, isChecked);
            }
        });
    }

    public SettingsManager getSettingsManager() {
        return sm;
    }
}

@@ -693,24 +693,58 @@
 erView(0);%0A%0A
+        if(!isInEditMode()) %7B%0A    
         sm =
@@ -782,24 +782,28 @@
  context);%0A%0A
+    
         fina
@@ -886,24 +886,28 @@
 e);%0A        
+    
 Switch night
@@ -915,32 +915,33 @@
 witch = (Switch)
+ 
 headerView.findV
@@ -968,32 +968,36 @@
 night);%0A        
+  
+  
 boolean nightThe
@@ -1042,18 +1042,23 @@
         
+    
 if
+ 
 (nightTh
@@ -1068,32 +1068,36 @@
 ) %7B%0A            
+    
 nightSwitch.togg
@@ -1102,34 +1102,42 @@
 ggle();%0A        
+   
+ 
 %7D%0A
+    
         nightSwi
@@ -1218,32 +1218,36 @@
 ) %7B%0A            
+   
+ 
 @Override%0A      
@@ -1232,32 +1232,36 @@
       @Override%0A
+    
             publ
@@ -1341,32 +1341,36 @@
                 
+  
+  
 sm.setBool(night
@@ -1405,34 +1405,40 @@
             
-%7D%0A
+    %7D%0A  
         
 %7D);%0A%0A       
@@ -1417,37 +1417,43 @@
     %7D%0A          
+  
 %7D);%0A%0A
+    
         final St
@@ -1530,24 +1530,28 @@
 s);%0A        
+    
 Switch gpsSw
@@ -1557,32 +1557,33 @@
 witch = (Switch)
+ 
 headerView.findV
@@ -1608,32 +1608,36 @@
 h_gps);%0A        
+   
+ 
 boolean gps = sm
@@ -1673,18 +1673,23 @@
         
+    
 if
+ 
 (gps) %7B%0A
@@ -1692,32 +1692,36 @@
 ) %7B%0A            
+ 
+   
 gpsSwitch.toggle
@@ -1724,34 +1724,42 @@
 ggle();%0A        
-%7D%0A
+    %7D%0A    
         gpsSwitc
@@ -1838,32 +1838,36 @@
 ) %7B%0A            
+   
+ 
 @Override%0A      
@@ -1852,32 +1852,36 @@
       @Override%0A
+    
             publ
@@ -1961,32 +1961,36 @@
                 
+    
 sm.setBool(gpsSe
@@ -2023,34 +2023,40 @@
             
-%7D%0A
+    %7D%0A  
         
 %7D);%0A%0A       
@@ -2035,37 +2035,43 @@
     %7D%0A          
+  
 %7D);%0A%0A
+    
         final St
@@ -2148,24 +2148,28 @@
 t);%0A        
+    
 Switch mapSw
@@ -2183,16 +2183,17 @@
 (Switch)
+ 
 headerVi
@@ -2226,16 +2226,20 @@
 h_map);%0A
+    
         
@@ -2298,18 +2298,23 @@
         
+    
 if
+ 
 (mapDefa
@@ -2316,24 +2316,28 @@
 pDefault) %7B%0A
+    
             
@@ -2364,26 +2364,32 @@
         
-%7D%0A
+    %7D%0A  
         
 mapSwitc
@@ -2372,32 +2372,34 @@
     %7D%0A          
+  
 mapSwitch.setOnC
@@ -2470,32 +2470,36 @@
 ) %7B%0A            
+    
 @Override%0A      
@@ -2496,32 +2496,36 @@
 ide%0A            
+ 
+   
 public void onCh
@@ -2577,32 +2577,36 @@
 an isChecked) %7B%0A
+    
                 
@@ -2651,34 +2651,42 @@
 d);%0A            
-%7D%0A
+    %7D%0A    
         %7D);%0A    
@@ -2681,16 +2681,66 @@
     %7D);%0A
+        %7D else %7B%0A            sm = null;%0A        %7D%0A
     %7D%0A%0A 

Check for android studio editor