Project

General

Profile

Download (7.42 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / dialogs / RubikDialogScoresPagerAdapter.java @ 66e777b0

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.dialogs;
21

    
22
import android.os.Bundle;
23
import androidx.annotation.NonNull;
24
import androidx.fragment.app.FragmentActivity;
25
import androidx.viewpager.widget.PagerAdapter;
26
import androidx.viewpager.widget.ViewPager;
27
import android.view.View;
28
import android.view.ViewGroup;
29
import android.widget.LinearLayout;
30

    
31
import org.distorted.scores.RubikScores;
32
import org.distorted.scores.RubikScoresDownloader;
33
import org.distorted.objects.RubikObjectList;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

    
37
class RubikDialogScoresPagerAdapter extends PagerAdapter implements RubikScoresDownloader.Receiver
38
  {
39
  private FragmentActivity mAct;
40
  private RubikDialogScores mDialog;
41
  private RubikDialogScoresView[] mViews;
42
  private ViewPager mViewPager;
43
  private int mNumTabs;
44
  private boolean mIsSubmitting;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
  public void receive(final String[][][] country, final String[][][] name, final float[][][] time)
49
    {
50
    prepareView();
51

    
52
    int c = mViewPager.getCurrentItem();
53

    
54
    addPage(c, mViews[c],country[c],name[c],time[c]);
55

    
56
    for(int i=0; i<mNumTabs; i++)
57
      {
58
      if( i==c ) continue;
59

    
60
      addPage(i, mViews[i],country[i],name[i],time[i]);
61
      }
62
    }
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
  public void message(final String mess)
67
    {
68
    mAct.runOnUiThread(new Runnable()
69
      {
70
      @Override
71
      public void run()
72
        {
73
        for(int i=0; i<mNumTabs; i++)
74
          {
75
          mViews[i].message(mess);
76
          }
77
        }
78
      });
79
    }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

    
83
  public void error(final String error)
84
    {
85
    char errorNumber = error.charAt(0);
86

    
87
    switch(errorNumber)
88
      {
89
      case '1': message("Client error");
90
                break;
91
      case '2': RubikScores scores = RubikScores.getInstance();
92
                Bundle bundle = new Bundle();
93
                bundle.putString("name", scores.getName() );
94

    
95
                RubikDialogSetName nameDiag = new RubikDialogSetName();
96
                nameDiag.setArguments(bundle);
97
                nameDiag.show(mAct.getSupportFragmentManager(), null);
98

    
99
                mDialog.dismiss();
100

    
101
                break;
102
      case '3': message("Server error");
103
                break;
104
      default : message("Unexpected error");
105
      }
106
    }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

    
110
  private void prepareView()
111
    {
112
    mAct.runOnUiThread(new Runnable()
113
      {
114
      @Override
115
      public void run()
116
        {
117
        for(int i=0; i<mNumTabs; i++)
118
          {
119
          mViews[i].prepareView(mAct);
120
          }
121
        }
122
      });
123
    try
124
      {
125
      Thread.sleep(150);
126
      }
127
    catch( InterruptedException ie)
128
      {
129

    
130
      }
131
    }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
  private void addPage(int tab, final RubikDialogScoresView view, final String[][] country, final String[][] name, final float[][] time)
136
    {
137
    int object   = RubikObjectList.unpackObject(tab);
138
    int sizeIndex= RubikObjectList.unpackSizeIndex(tab);
139
    int maxLevel  = RubikObjectList.getMaxLevel(object, sizeIndex);
140

    
141
    for(int i=0; i<maxLevel; i++)
142
      {
143
      final LinearLayout section = view.createSection(mAct, tab, i, country[i], name[i], time[i]);
144

    
145
      mAct.runOnUiThread(new Runnable()
146
        {
147
        @Override
148
        public void run()
149
          {
150
          view.addSection(section);
151
          }
152
        });
153

    
154
      try
155
        {
156
        Thread.sleep(50);
157
        }
158
      catch( InterruptedException ie)
159
        {
160

    
161
        }
162
      }
163
    }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
  RubikDialogScoresPagerAdapter(FragmentActivity act, ViewPager viewPager, boolean isSubmitting, RubikDialogScores diag)
168
    {
169
    mAct = act;
170
    mDialog = diag;
171
    mNumTabs = RubikObjectList.getTotal();
172
    mViews = new RubikDialogScoresView[mNumTabs];
173
    mViewPager = viewPager;
174
    mIsSubmitting = isSubmitting;
175

    
176
    viewPager.setAdapter(this);
177
    viewPager.setOffscreenPageLimit(mNumTabs-1);
178
    }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
  @Override
183
  @NonNull
184
  public Object instantiateItem(@NonNull ViewGroup collection, int position)
185
    {
186
    mViews[position] = new RubikDialogScoresView(mAct, mIsSubmitting);
187
    collection.addView(mViews[position]);
188

    
189
    boolean allCreated = true;
190

    
191
    for(int i=0; i<mNumTabs; i++)
192
      {
193
      if( mViews[i]==null )
194
        {
195
        allCreated = false;
196
        break;
197
        }
198
      }
199

    
200
    if( allCreated )
201
      {
202
      RubikScoresDownloader downloader = RubikScoresDownloader.getInstance();
203

    
204
      if( mIsSubmitting )  downloader.submit  ( this, mAct );
205
      else                 downloader.download( this, mAct );
206
      }
207

    
208
    return mViews[position];
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
  @Override
214
  public void destroyItem(ViewGroup collection, int position, @NonNull Object view)
215
    {
216
    collection.removeView((View) view);
217
    }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
  @Override
222
  public int getCount()
223
    {
224
    return mNumTabs;
225
    }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

    
229
  @Override
230
  public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
231
    {
232
    return view == object;
233
    }
234
  }
(9-9/13)