package de.hannesstruss.windfish;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import com.f2prateek.rx.preferences.Preference;
import com.f2prateek.rx.preferences.RxSharedPreferences;

import rx.Observable;

class WindFishState {
  private static final String KEY_ENABLED = "enabled";

  private final Preference<Boolean> enabled;

  WindFishState(Application context) {
    SharedPreferences sharedPreferences =
        context.getSharedPreferences("windfish_state.prefs", Context.MODE_PRIVATE);
    RxSharedPreferences rxPrefs = RxSharedPreferences.create(sharedPreferences);
    enabled = rxPrefs.getBoolean(KEY_ENABLED, false);
  }

  Observable<Boolean> isEnabled() {
    return enabled.asObservable();
  }

  void toggle() {
    //noinspection ConstantConditions
    enabled.set(!enabled.get());
  }
}

@@ -262,16 +262,43 @@
 ervable;
+%0Aimport rx.functions.Func1;
 %0A%0Aclass 
@@ -327,51 +327,189 @@
 ate 
-static final String KEY_ENABLED = %22enabled%22
+enum Mode %7B%0A    ALWAYS_OFF, /*ON_WHEN_ON_POWER,*/ ALWAYS_ON%0A  %7D%0A%0A  private static final String KEY_MODE = %22mode%22;%0A%0A  private static final Func1%3CString, Mode%3E TO_MODE = Mode::valueOf
 ;%0A%0A 
@@ -538,24 +538,20 @@
 nce%3C
-Boolean%3E enabled
+String%3E mode
 ;%0A%0A 
@@ -799,23 +799,20 @@
 s);%0A    
-enabled
+mode
  = rxPre
@@ -821,34 +821,47 @@
 .get
-Boolean(KEY_ENABLED, false
+String(KEY_MODE, Mode.ALWAYS_OFF.name()
 );%0A 
@@ -911,23 +911,20 @@
  return 
-enabled
+mode
 .asObser
@@ -934,99 +934,485 @@
 le()
-;%0A  %7D%0A%0A  void toggle() %7B%0A    //noinspection ConstantConditions%0A    enabled.set(!enabled.get
+%0A        .map(Mode::valueOf)%0A        .map(mode -%3E mode == Mode.ALWAYS_ON);%0A  %7D%0A%0A  void toggle() %7B%0A    mode.asObservable()%0A        .first()%0A        .map(TO_MODE)%0A        .subscribe(mode -%3E %7B%0A          Mode nextMode;%0A          if (mode == Mode.ALWAYS_ON) %7B%0A            nextMode = Mode.ALWAYS_OFF;%0A          %7D else %7B%0A            nextMode = Mode.ALWAYS_ON;%0A          %7D%0A          setMode(nextMode);%0A        %7D);%0A  %7D%0A%0A  private void setMode(Mode nextMode) %7B%0A    mode.set(nextMode.name
 ());

Introduce Mode enum