Revision 33499c56
Added by Leszek Koltunski over 4 years ago
src/main/java/org/distorted/magic/RubikActivity.java | ||
---|---|---|
19 | 19 |
|
20 | 20 |
package org.distorted.magic; |
21 | 21 |
|
22 |
import android.content.SharedPreferences; |
|
22 | 23 |
import android.os.Bundle; |
24 |
import android.preference.PreferenceManager; |
|
25 |
import android.support.v4.app.FragmentManager; |
|
23 | 26 |
import android.support.v7.app.AppCompatActivity; |
24 | 27 |
import android.view.View; |
25 | 28 |
|
29 |
import org.distorted.effect.BaseEffect; |
|
26 | 30 |
import org.distorted.library.main.DistortedLibrary; |
27 | 31 |
|
28 | 32 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
29 | 33 |
|
30 | 34 |
public class RubikActivity extends AppCompatActivity implements View.OnClickListener |
31 | 35 |
{ |
32 |
private RubikDialogMain mMain;
|
|
36 |
RubikState mCurrentState;
|
|
33 | 37 |
|
34 | 38 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
35 | 39 |
|
... | ... | |
40 | 44 |
setTheme(R.style.CustomActivityThemeNoActionBar); |
41 | 45 |
setContentView(R.layout.main); |
42 | 46 |
|
43 |
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView); |
|
44 |
view.enterMainState(this); |
|
45 |
|
|
46 | 47 |
if( savedState==null ) |
47 | 48 |
{ |
48 |
mMain = new RubikDialogMain();
|
|
49 |
mMain.show(getSupportFragmentManager(), null);
|
|
49 |
RubikDialogMain diag = new RubikDialogMain();
|
|
50 |
diag.show(getSupportFragmentManager(), RubikDialogMain.getDialogTag() );
|
|
50 | 51 |
} |
51 | 52 |
} |
52 | 53 |
|
... | ... | |
59 | 60 |
view.onPause(); |
60 | 61 |
DistortedLibrary.onPause(); |
61 | 62 |
RubikScoresDownloader.onPause(); |
62 |
view.savePreferences();
|
|
63 |
savePreferences(); |
|
63 | 64 |
super.onPause(); |
64 | 65 |
} |
65 | 66 |
|
... | ... | |
71 | 72 |
super.onResume(); |
72 | 73 |
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView); |
73 | 74 |
view.onResume(); |
74 |
view.restorePreferences(); |
|
75 |
restorePreferences(); |
|
76 |
view.enterState(this, mCurrentState); |
|
75 | 77 |
} |
76 | 78 |
|
77 | 79 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
105 | 107 |
|
106 | 108 |
if( id == RubikSurfaceView.BUTTON_ID_BACK ) |
107 | 109 |
{ |
108 |
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView); |
|
109 |
view.enterMainState(this); |
|
110 |
mMain.show(getSupportFragmentManager(), null); |
|
110 |
mCurrentState = mCurrentState.getBack(); |
|
111 |
|
|
112 |
if( mCurrentState!=null ) |
|
113 |
{ |
|
114 |
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView); |
|
115 |
view.enterState(this, mCurrentState); |
|
116 |
|
|
117 |
RubikDialogMain diag = new RubikDialogMain(); |
|
118 |
diag.show(getSupportFragmentManager(), RubikDialogMain.getDialogTag() ); |
|
119 |
} |
|
120 |
else |
|
121 |
{ |
|
122 |
mCurrentState = RubikState.MAIN; |
|
123 |
finish(); |
|
124 |
} |
|
111 | 125 |
} |
112 |
if( id == RubikSurfaceView.BUTTON_ID_EXIT ) |
|
126 |
} |
|
127 |
|
|
128 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
129 |
|
|
130 |
private void savePreferences() |
|
131 |
{ |
|
132 |
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); |
|
133 |
SharedPreferences.Editor editor = preferences.edit(); |
|
134 |
|
|
135 |
for (int i = 0; i< BaseEffect.Type.LENGTH; i++) |
|
136 |
{ |
|
137 |
BaseEffect.Type.getType(i).savePreferences(editor); |
|
138 |
} |
|
139 |
|
|
140 |
editor.putInt("state", mCurrentState.ordinal() ); |
|
141 |
|
|
142 |
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView); |
|
143 |
view.savePreferences(editor); |
|
144 |
|
|
145 |
editor.apply(); |
|
146 |
} |
|
147 |
|
|
148 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
149 |
|
|
150 |
private void restorePreferences() |
|
151 |
{ |
|
152 |
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); |
|
153 |
|
|
154 |
for (int i=0; i< BaseEffect.Type.LENGTH; i++) |
|
113 | 155 |
{ |
114 |
finish();
|
|
156 |
BaseEffect.Type.getType(i).restorePreferences(preferences);
|
|
115 | 157 |
} |
158 |
|
|
159 |
int stateOrdinal = preferences.getInt("state", RubikState.MAIN.ordinal() ); |
|
160 |
mCurrentState = RubikState.getState(stateOrdinal); |
|
161 |
|
|
162 |
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView); |
|
163 |
view.restorePreferences(preferences); |
|
116 | 164 |
} |
117 | 165 |
|
118 | 166 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
121 | 169 |
|
122 | 170 |
public void Play(View v) |
123 | 171 |
{ |
124 |
mMain.dismiss(); |
|
172 |
FragmentManager mana = getSupportFragmentManager(); |
|
173 |
RubikDialogMain diag = (RubikDialogMain) mana.findFragmentByTag(RubikDialogMain.getDialogTag()); |
|
174 |
|
|
175 |
if( diag!=null ) |
|
176 |
{ |
|
177 |
diag.dismiss(); |
|
178 |
} |
|
179 |
else |
|
180 |
{ |
|
181 |
android.util.Log.e("act", "cannot find main dialog!"); |
|
182 |
} |
|
183 |
|
|
184 |
mCurrentState = RubikState.PLAY; |
|
125 | 185 |
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView); |
126 |
view.enterPlayState(this);
|
|
186 |
view.enterState(this,mCurrentState);
|
|
127 | 187 |
} |
128 | 188 |
|
129 | 189 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
Also available in: Unified diff
Major rearrangement of the UI.