Project

General

Profile

« Previous | Next » 

Revision 506f7ceb

Added by Leszek Koltunski almost 3 years ago

Beginnings of support for dynamically adding downloaded JSONs to the local RubikObjectList: SharedPrefs part.

View differences:

src/main/java/org/distorted/objects/RubikObjectList.java
40 40
public class RubikObjectList
41 41
{
42 42
  public static final int DEF_OBJECT= ObjectConstants.CUBE_3;
43

  
44 43
  public static int MAX_LEVEL;
45 44

  
46
  private static RubikObjectList mType;
45
  private static RubikObjectList mThis;
47 46
  private static int mNumObjects;
48 47
  private static int mNumExtras;
49 48
  private static ArrayList<RubikObject> mObjects;
50 49
  private static int mObject = DEF_OBJECT;
51 50

  
51
  private static class DownloadedObject
52
    {
53
    String shortName;
54
    boolean icon,object,extras;
55

  
56
    DownloadedObject(String sName, boolean i, boolean o, boolean e)
57
      {
58
      shortName = sName;
59
      icon   = i;
60
      object = o;
61
      extras = e;
62
      }
63
    }
64

  
65
  private static ArrayList<DownloadedObject> mDownloadedObjects;
66

  
52 67
  static
53 68
    {
54 69
    int max = Integer.MIN_VALUE;
......
69 84
    mNumObjects= 0;
70 85
    mNumExtras = 0;
71 86

  
72
    mObjects = new ArrayList<>();
87
    mObjects           = new ArrayList<>();
88
    mDownloadedObjects = new ArrayList<>();
73 89

  
74 90
    createBuiltinObjects();
75 91
    }
......
93 109
      }
94 110
    }
95 111

  
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

  
114
  private static void internalAddDownloadedObject(DownloadedObject object)
115
    {
116

  
117
    }
118

  
96 119
///////////////////////////////////////////////////////////////////////////////////////////////////
97 120
// PUBLIC API
98 121
///////////////////////////////////////////////////////////////////////////////////////////////////
......
125 148

  
126 149
  public static void addDownloadedObject(String shortName, boolean icon, boolean object, boolean extras)
127 150
    {
151
    for( DownloadedObject obj : mDownloadedObjects )
152
      {
153
      if( obj.shortName.equals(shortName) )
154
        {
155
        obj.icon  |= icon;
156
        obj.object|= object;
157
        obj.extras|= extras;
158

  
159
        android.util.Log.e("D", "UPDATING "+shortName);
160

  
161
        return;
162
        }
163
      }
164

  
165
    DownloadedObject extra = new DownloadedObject(shortName,icon,object,extras);
166
    mDownloadedObjects.add(extra);
128 167

  
168
    internalAddDownloadedObject(extra);
129 169
    }
130 170

  
131 171
///////////////////////////////////////////////////////////////////////////////////////////////////
......
148 188
    {
149 189
    RubikObject obj = getObject(mObject);
150 190
    if( obj!=null ) editor.putString("rol_objName", obj.getName() );
191

  
192
    int numDownloaded = mDownloadedObjects.size();
193

  
194
    if( numDownloaded>0 )
195
      {
196
      StringBuilder downloadedObjects = new StringBuilder();
197

  
198
      for(int i=0; i<numDownloaded; i++)
199
        {
200
        if( i>0 ) downloadedObjects.append(',');
201

  
202
        DownloadedObject object = mDownloadedObjects.get(i);
203
        downloadedObjects.append(object.shortName);
204
        downloadedObjects.append(' ');
205
        downloadedObjects.append(object.icon   ? "1":"0");
206
        downloadedObjects.append(' ');
207
        downloadedObjects.append(object.object ? "1":"0");
208
        downloadedObjects.append(' ');
209
        downloadedObjects.append(object.extras ? "1":"0");
210
        }
211

  
212
      editor.putString("rol_downloaded", downloadedObjects.toString() );
213
      }
151 214
    }
152 215

  
153 216
///////////////////////////////////////////////////////////////////////////////////////////////////
......
176 239
    mObject = getOrdinal(objName);
177 240

  
178 241
    if( mObject<0 || mObject>=mNumObjects ) mObject = DEF_OBJECT;
242

  
243
    String downloaded = preferences.getString("rol_downloaded","");
244

  
245
    android.util.Log.e("D", downloaded);
246

  
247
    if( !downloaded.equals(""))
248
      {
249
      String[] dObjects = downloaded.split(",");
250

  
251
      for(String dObj : dObjects)
252
        {
253
        String[] parts = dObj.split(" ");
254

  
255
        if( parts.length==4 )
256
          {
257
          String name = parts[0];
258
          String icon = parts[1];
259
          String obje = parts[2];
260
          String extr = parts[3];
261

  
262
          boolean bIcon = icon.equals("1");
263
          boolean bObje = obje.equals("1");
264
          boolean bExtr = extr.equals("1");
265

  
266
          android.util.Log.e("D", "adding: "+name);
267

  
268
          addDownloadedObject(name,bIcon,bObje,bExtr);
269
          }
270
        }
271
      }
179 272
    }
180 273

  
181 274
///////////////////////////////////////////////////////////////////////////////////////////////////
......
221 314

  
222 315
  public static RubikObject getObject(int ordinal)
223 316
    {
224
    if( mType==null ) mType = new RubikObjectList();
317
    if( mThis==null ) mThis = new RubikObjectList();
225 318
    return ordinal>=0 && ordinal<mNumObjects ? mObjects.get(ordinal) : null;
226 319
    }
227 320

  
......
229 322

  
230 323
  public static int getNumObjects()
231 324
    {
232
    if( mType==null ) mType = new RubikObjectList();
325
    if( mThis==null ) mThis = new RubikObjectList();
233 326
    return mNumObjects;
234 327
    }
235 328

  
......
237 330

  
238 331
  public static int getOrdinal(String name)
239 332
    {
240
    if( mType==null ) mType = new RubikObjectList();
333
    if( mThis==null ) mThis = new RubikObjectList();
241 334

  
242 335
    for(int i=0; i<mNumObjects; i++)
243 336
      {

Also available in: Unified diff