Project

General

Profile

« Previous | Next » 

Revision 59535491

Added by Leszek Koltunski over 4 years ago

Save the name of the current state to Shared Prefs (and not the ordinal which might change in the future!)

View differences:

src/main/java/org/distorted/states/RubikState.java
44 44
  private final RubikState mBack;
45 45
  private int mMode;
46 46
  private final RubikStateAbstract mClass;
47
  private static final RubikState[] sizes;
47
  private static final RubikState[] states;
48 48

  
49 49
  private static RubikState mCurrState;
50 50

  
51 51
  static
52 52
    {
53 53
    int i = 0;
54
    sizes = new RubikState[LENGTH];
54
    states = new RubikState[LENGTH];
55 55

  
56
    for(RubikState size: RubikState.values())
56
    for(RubikState state: RubikState.values())
57 57
      {
58
      sizes[i] = size;
58
      states[i] = state;
59 59
      i++;
60 60
      }
61 61
    }
......
64 64

  
65 65
  public static RubikState getState(int ordinal)
66 66
    {
67
    return ordinal>=0 && ordinal<LENGTH ?  sizes[ordinal] : PLAY;
67
    return ordinal>=0 && ordinal<LENGTH ?  states[ordinal] : PLAY;
68
    }
69

  
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

  
72
  public static RubikState getStateFromName(String name)
73
    {
74
    for(int i=0; i<LENGTH; i++)
75
      {
76
      if( name.equals(states[i].name()) )
77
        {
78
        return states[i];
79
        }
80
      }
81

  
82
    return PLAY;
68 83
    }
69 84

  
70 85
///////////////////////////////////////////////////////////////////////////////////////////////////
......
85 100

  
86 101
  public static void savePreferences(SharedPreferences.Editor editor)
87 102
    {
88
    editor.putInt("curr_state", mCurrState.ordinal() );
103
    editor.putString("curr_state_name", mCurrState.name() );
89 104
    }
90 105

  
91 106
///////////////////////////////////////////////////////////////////////////////////////////////////
92 107

  
93 108
  public static void restorePreferences(SharedPreferences preferences)
94 109
    {
95
    int currState = preferences.getInt("curr_state", RubikState.PLAY.ordinal() );
96
    mCurrState = getState(currState);
110
    String currStateName = preferences.getString("curr_state_name", RubikState.PLAY.name() );
111
    mCurrState = getStateFromName(currStateName);
97 112
    }
98 113

  
99 114
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff