Project

General

Profile

« Previous | Next » 

Revision ce0c7ca8

Added by Leszek Koltunski over 4 years ago

RubikCube: major progress with a separate RubikSettingsEnum enum - almost finished.

View differences:

src/main/java/org/distorted/magic/RubikSettings.java
20 20
package org.distorted.magic;
21 21

  
22 22
import android.app.Dialog;
23
import android.content.Context;
24
import android.content.DialogInterface;
25 23
import android.os.Bundle;
26 24
import android.support.annotation.NonNull;
27
import android.support.annotation.Nullable;
28 25
import android.support.v4.app.FragmentActivity;
29 26
import android.support.v7.app.AlertDialog;
30 27
import android.support.v7.app.AppCompatDialogFragment;
......
42 39

  
43 40
public class RubikSettings extends AppCompatDialogFragment implements SeekBar.OnSeekBarChangeListener, AdapterView.OnItemSelectedListener
44 41
  {
45
  public interface OnCompleteListener
46
    {
47
    void onComplete(int index, int position, int type);
48
    }
49

  
50
  private OnCompleteListener mListener;
51

  
52 42
  private TextView[] mDurationText;
53
  private int[] mSeekBarID;
54
  private int[] mSpinnerID;
55
  private int[] mPos;
56
  private int[] mType;
57 43

  
58 44
///////////////////////////////////////////////////////////////////////////////////////////////////
59 45

  
60 46
  public RubikSettings()
61 47
    {
62 48
    mDurationText = new TextView[RubikSettingsEnum.LENGTH];
63
    mSeekBarID    = new int[RubikSettingsEnum.LENGTH];
64
    mSpinnerID    = new int[RubikSettingsEnum.LENGTH];
65
    mPos          = new int[RubikSettingsEnum.LENGTH];
66
    mType         = new int[RubikSettingsEnum.LENGTH];
67
    }
68

  
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

  
71
  @Override
72
  public void onAttach(Context context)
73
    {
74
    super.onAttach(context);
75

  
76
    try
77
      {
78
      mListener = (OnCompleteListener)context;
79
      }
80
    catch (final ClassCastException e)
81
      {
82
      throw new ClassCastException(context.toString() + " must implement OnCompleteListener");
83
      }
84
    }
85

  
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

  
88
  @Override
89
  public void onCreate(@Nullable Bundle savedInstanceState)
90
    {
91
    super.onCreate(savedInstanceState);
92

  
93
    Bundle args = getArguments();
94

  
95
    String name;
96

  
97
    for (int i=0; i<RubikSettingsEnum.LENGTH; i++)
98
      {
99
      RubikSettingsEnum rse = RubikSettingsEnum.getEnum(i);
100
      name = rse.name();
101

  
102
      try
103
        {
104
        mPos[i]  = args.getInt(name+"_Pos" );
105
        mType[i] = args.getInt(name+"_Type");
106
        }
107
      catch(NullPointerException ex)
108
        {
109
        mPos[i]  = rse.getDefaultPos();
110
        mType[i] = rse.getDefaultType();
111
        }
112
      }
113 49
    }
114 50

  
115 51
///////////////////////////////////////////////////////////////////////////////////////////////////
......
120 56
    {
121 57
    FragmentActivity act = getActivity();
122 58
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
123

  
124 59
    builder.setCancelable(false);
125
    builder.setPositiveButton( R.string.save, new DialogInterface.OnClickListener()
126
      {
127
      @Override
128
      public void onClick(DialogInterface dialog, int which)
129
        {
130
        saveOptions();
131
        }
132
      });
133 60

  
134 61
    LayoutInflater inflater = act.getLayoutInflater();
135 62
    final View view = inflater.inflate(R.layout.settings, null);
......
162 89
      ///// TEXT ///////////////////////////////////////////////////////////////////////////
163 90

  
164 91
      int layoutHeight = (int)(scale*48 + 0.5f);
165
      int padding      = (int)(scale*15 + 0.5f);
92
      int padding      = (int)(scale*10 + 0.5f);
166 93

  
167 94
      LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,layoutHeight);
168 95

  
......
233 160
      seekBar.setId(index);
234 161
      innerLayout1.addView(seekBar);
235 162

  
236
      mSeekBarID[index] = seekBar.getId();
237

  
238 163
      seekBar.setOnSeekBarChangeListener(this);
239
      seekBar.setProgress(mPos[index]);
164
      seekBar.setProgress(rsEnum.getCurrentPos());
240 165

  
241 166
      ///// INNER LAYOUT2 //////////////////////////////////////////////////////////////////
242 167

  
......
272 197
      spinner.setId(index);
273 198
      innerLayout2.addView(spinner);
274 199

  
275
      mSpinnerID[index] = spinner.getId();
276

  
277 200
      spinner.setOnItemSelectedListener(this);
278 201
      String[] appear = RubikSettingsEnum.getNames(index);
279 202
      ArrayAdapter<String> adapterType = new ArrayAdapter<>(act,android.R.layout.simple_spinner_item, appear);
280 203
      adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
281 204
      spinner.setAdapter(adapterType);
282

  
283
      int type = mType[index];
284

  
285
      if(type>=0 && type<appear.length)
286
        {
287
        spinner.setSelection(type);
288
        }
289
      }
290

  
291

  
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

  
294
    private void saveOptions()
295
      {
296
      for (int i=0; i<RubikSettingsEnum.LENGTH; i++)
297
        {
298
        mListener.onComplete(i, mPos[i], mType[i]);
299
        }
205
      spinner.setSelection(rsEnum.getCurrentType());
300 206
      }
301 207

  
302 208
///////////////////////////////////////////////////////////////////////////////////////////////////
......
305 211
      {
306 212
      int parentID = parent.getId();
307 213

  
308
      for (int i=0; i<RubikSettingsEnum.LENGTH; i++)
214
      if( parentID>=0 && parentID< RubikSettingsEnum.LENGTH) // ith spinner's ID is equal to i (see createSettingSection)
309 215
        {
310
        if( mSpinnerID[i] == parentID )
311
          {
312
          mType[i] = pos;
313
          break;
314
          }
216
        RubikSettingsEnum.getEnum(parentID).setCurrentType(pos);
315 217
        }
316 218
      }
317 219

  
......
321 223
      {
322 224
      int barID = bar.getId();
323 225

  
324
      for (int i=0; i<RubikSettingsEnum.LENGTH; i++)
226
      if( barID>=0 && barID< RubikSettingsEnum.LENGTH) // ith seekbar's ID is equal to i (see createSettingSection)
325 227
        {
326
        if( mSeekBarID[i] == barID )
327
          {
328
          mPos[i] = progress;
329
          int ms = RubikActivity.translateDuration(mPos[i]);
330
          mDurationText[i].setText(getString(R.string.ms_placeholder,ms));
331
          break;
332
          }
228
        RubikSettingsEnum.getEnum(barID).setCurrentPos(progress);
229
        int ms = RubikSettingsEnum.translatePos(progress);
230
        mDurationText[barID].setText(getString(R.string.ms_placeholder,ms));
333 231
        }
334 232
      }
335 233

  

Also available in: Unified diff