Project

General

Profile

« Previous | Next » 

Revision 946d9762

Added by Leszek Koltunski almost 5 years ago

Downloading High Scores: more dynamic way of displaying scores

View differences:

src/main/java/org/distorted/magic/RubikScores.java
24 24
import android.os.Bundle;
25 25
import android.support.annotation.NonNull;
26 26
import android.support.v4.app.FragmentActivity;
27
import android.support.v4.view.ViewPager;
27 28
import android.support.v7.app.AlertDialog;
28 29
import android.support.v7.app.AppCompatDialogFragment;
29 30
import android.support.design.widget.TabLayout;
......
37 38
public class RubikScores extends AppCompatDialogFragment
38 39
  {
39 40
  RubikScoresPagerAdapter mPagerAdapter;
40
  RubikScoresViewPager    mViewPager;
41 41

  
42 42
///////////////////////////////////////////////////////////////////////////////////////////////////
43 43

  
......
67 67
    final View view = inflater.inflate(R.layout.scores, null);
68 68
    builder.setView(view);
69 69

  
70
    mViewPager = view.findViewById(R.id.viewpager);
71
    mViewPager.startDownload(act);
72

  
70
    ViewPager viewPager = view.findViewById(R.id.viewpager);
73 71
    TabLayout tabLayout = view.findViewById(R.id.sliding_tabs);
74
    mPagerAdapter = new RubikScoresPagerAdapter(act,mViewPager);
75
    tabLayout.setupWithViewPager(mViewPager);
72
    mPagerAdapter = new RubikScoresPagerAdapter(act,viewPager);
73
    tabLayout.setupWithViewPager(viewPager);
76 74

  
77
    mViewPager.setCurrentItem(0);
75
    viewPager.setCurrentItem(0);
78 76

  
79 77
    for (int i = 0; i< RubikSize.LENGTH; i++)
80 78
      {
src/main/java/org/distorted/magic/RubikScoresPagerAdapter.java
22 22
import android.support.annotation.NonNull;
23 23
import android.support.v4.app.FragmentActivity;
24 24
import android.support.v4.view.PagerAdapter;
25
import android.support.v4.view.ViewPager;
25 26
import android.view.View;
26 27
import android.view.ViewGroup;
27 28

  
28 29
///////////////////////////////////////////////////////////////////////////////////////////////////
29 30

  
30
class RubikScoresPagerAdapter extends PagerAdapter
31
class RubikScoresPagerAdapter extends PagerAdapter implements RubikScoresDownloader.Receiver
31 32
  {
32 33
  private FragmentActivity mAct;
33
  private RubikScoresViewPager mPager;
34
  private RubikScoresView[] mViews;
34 35

  
35 36
///////////////////////////////////////////////////////////////////////////////////////////////////
36 37

  
37
  RubikScoresPagerAdapter(FragmentActivity act, RubikScoresViewPager viewPager)
38
  public void receive(final int[][][] country, final String[][][] name, final String[][][] time)
39
    {
40
    mAct.runOnUiThread(new Runnable()
41
      {
42
      @Override
43
      public void run()
44
        {
45
        for(int i=0; i<RubikSize.LENGTH; i++)
46
          {
47
          mViews[i].prepareView(mAct);
48
          }
49
        }
50
      });
51
    try
52
      {
53
      Thread.sleep(200);
54
      }
55
    catch( InterruptedException ie)
56
      {
57

  
58
      }
59

  
60
    for(int i=0; i<RubikSize.LENGTH; i++)
61
      {
62
      final RubikScoresView view = mViews[i];
63

  
64
      for(int j=0; j<RubikActivity.MAX_SCRAMBLE; j++)
65
        {
66
        final int scramble = j;
67
        final int[] c = country[i][j];
68
        final String[] n = name[i][j];
69
        final String[] t = time[i][j];
70

  
71
        mAct.runOnUiThread(new Runnable()
72
          {
73
          @Override
74
          public void run()
75
            {
76
            view.addScramble(mAct, scramble, c, n, t);
77
            }
78
          });
79

  
80
        try
81
          {
82
          Thread.sleep(50);
83
          }
84
        catch( InterruptedException ie)
85
          {
86

  
87
          }
88
        }
89
      }
90
    }
91

  
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

  
94
  public void exception(final String exce)
95
    {
96
    mAct.runOnUiThread(new Runnable()
97
      {
98
      @Override
99
      public void run()
100
        {
101
        for(int i=0; i<RubikSize.LENGTH; i++)
102
          {
103
          mViews[i].exception(exce);
104
          }
105
        }
106
      });
107
    }
108

  
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

  
111
  RubikScoresPagerAdapter(FragmentActivity act, ViewPager viewPager)
38 112
    {
39 113
    mAct = act;
40
    mPager = viewPager;
114
    mViews = new RubikScoresView[RubikSize.LENGTH];
41 115

  
42 116
    viewPager.setAdapter(this);
43 117
    viewPager.setOffscreenPageLimit( RubikSize.LENGTH-1 );
......
49 123
  @NonNull
50 124
  public Object instantiateItem(@NonNull ViewGroup collection, int position)
51 125
    {
52
    RubikScoresView view = new RubikScoresView(mAct);
53
    mPager.setView(position,view);
54
    collection.addView(view);
55

  
56
    return view;
126
    mViews[position] = new RubikScoresView(mAct);
127
    collection.addView(mViews[position]);
128

  
129
    boolean allCreated = true;
130

  
131
    for(int i=0; i<RubikSize.LENGTH; i++)
132
      {
133
      if( mViews[i]==null )
134
        {
135
        allCreated = false;
136
        break;
137
        }
138
      }
139

  
140
    if( allCreated )
141
      {
142
      RubikScoresDownloader downloader = new RubikScoresDownloader();
143
      downloader.download(this, mAct.getResources(), mAct.getPackageName());
144
      }
145

  
146
    return mViews[position];
57 147
    }
58 148

  
59 149
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/magic/RubikScoresView.java
20 20
package org.distorted.magic;
21 21

  
22 22
import android.content.Context;
23
import android.content.res.Resources;
24
import android.graphics.BitmapFactory;
25 23
import android.support.v4.app.FragmentActivity;
26 24
import android.util.AttributeSet;
27 25
import android.view.View;
......
34 32

  
35 33
public class RubikScoresView extends FrameLayout
36 34
  {
37
  private boolean mCreated;
35
  LinearLayout mLayout;
38 36

  
39 37
///////////////////////////////////////////////////////////////////////////////////////////////////
40 38

  
41 39
  public RubikScoresView(Context context, AttributeSet attrs, int defStyle)
42 40
    {
43 41
    super(context, attrs, defStyle);
44

  
45
    mCreated = false;
46 42
    }
47 43

  
48 44
///////////////////////////////////////////////////////////////////////////////////////////////////
......
72 68

  
73 69
///////////////////////////////////////////////////////////////////////////////////////////////////
74 70

  
75
  void prepareView(FragmentActivity act, final int[][] country, final String[][] name, final String[][] time)
71
  void prepareView(FragmentActivity act)
76 72
    {
77
    if( mCreated ) return;
78

  
79 73
    removeAllViews();
80 74

  
81 75
    View tab = inflate(act, R.layout.scores_tab, null);
82
    LinearLayout layout = tab.findViewById(R.id.tabLayout);
76
    mLayout = tab.findViewById(R.id.tabLayout);
83 77
    addView(tab);
78
    }
84 79

  
85
    for(int i=0; i<RubikActivity.MAX_SCRAMBLE; i++)
86
      {
87
      LinearLayout level = (LinearLayout)inflate(act, R.layout.level_records, null);
88
      TextView text = level.findViewById(R.id.levelTitle);
89
      text.setText(act.getString(R.string.sc_placeholder,(i+1)));
80
///////////////////////////////////////////////////////////////////////////////////////////////////
90 81

  
91
      for(int j=0; j<RubikScoresDownloader.MAX_PLACES; j++)
82
  void addScramble(FragmentActivity act, int scramble, final int[] country, final String[] name, final String[] time)
83
    {
84
    LinearLayout level = (LinearLayout)inflate(act, R.layout.level_records, null);
85
    TextView text = level.findViewById(R.id.levelTitle);
86
    text.setText(act.getString(R.string.sc_placeholder,(scramble+1)));
87

  
88
    for(int j=0; j<RubikScoresDownloader.MAX_PLACES; j++)
89
      {
90
      if( name[j] != null )
92 91
        {
93
        if( name[i][j] != null )
94
          {
95
          View row = inflate(act, R.layout.level_row, null);
92
        View row = inflate(act, R.layout.level_row, null);
96 93

  
97
          ImageView imgCoun = row.findViewById(R.id.level_row_country);
98
          TextView textName = row.findViewById(R.id.level_row_name);
99
          TextView textTime = row.findViewById(R.id.level_row_time);
94
        ImageView imgCoun = row.findViewById(R.id.level_row_country);
95
        TextView textName = row.findViewById(R.id.level_row_name);
96
        TextView textTime = row.findViewById(R.id.level_row_time);
100 97

  
101
          imgCoun.setImageResource(country[i][j]);
102
          textName.setText(name[i][j]);
103
          textTime.setText(time[i][j]);
98
        imgCoun.setImageResource(country[j]);
99
        textName.setText(name[j]);
100
        textTime.setText(time[j]);
104 101

  
105
          level.addView(row);
106
          }
102
        level.addView(row);
107 103
        }
108

  
109
      layout.addView(level);
110 104
      }
111 105

  
112
    mCreated = true;
106
    mLayout.addView(level);
113 107
    }
114 108
  }
src/main/java/org/distorted/magic/RubikScoresViewPager.java
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.magic;
21

  
22
import android.content.Context;
23
import android.support.v4.app.FragmentActivity;
24
import android.support.v4.view.ViewPager;
25
import android.util.AttributeSet;
26

  
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

  
29
class RubikScoresViewPager extends ViewPager implements RubikScoresDownloader.Receiver
30
  {
31
  FragmentActivity mAct;
32
  private RubikScoresView[] mViews;
33
  private int mSelected;
34

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

  
37
  public RubikScoresViewPager(Context context, AttributeSet attrs)
38
    {
39
    super(context,attrs);
40

  
41
    mViews = new RubikScoresView[RubikSize.LENGTH];
42
    mSelected = 0;
43

  
44
    addOnPageChangeListener(new ViewPager.OnPageChangeListener()
45
      {
46
      @Override
47
      public void onPageSelected(int position)
48
        {
49
        mSelected = position;
50
        }
51

  
52
      @Override
53
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
54
        { }
55

  
56
      @Override
57
      public void onPageScrollStateChanged(int state)
58
        { }
59
      });
60
    }
61

  
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

  
64
  void startDownload(FragmentActivity act)
65
    {
66
    mAct = act;
67

  
68
    RubikScoresDownloader downloader = new RubikScoresDownloader();
69
    downloader.download(this, mAct.getResources(), mAct.getPackageName());
70
    }
71

  
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

  
74
  void setView(int position, RubikScoresView view)
75
    {
76
    if( position>=0 && position<RubikSize.LENGTH )
77
      {
78
      mViews[position] = view;
79
      }
80
    }
81

  
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83

  
84
  public void receive(final int[][][] country, final String[][][] name, final String[][][] time)
85
    {
86
    mAct.runOnUiThread(new Runnable()
87
      {
88
      @Override
89
      public void run()
90
        {
91
        long time1 = System.currentTimeMillis();
92

  
93
        for(int i=0; i<RubikSize.LENGTH; i++)
94
          {
95
          mViews[i].prepareView(mAct, country[i], name[i], time[i]);
96
          }
97

  
98
        long time2 = System.currentTimeMillis();
99

  
100
        android.util.Log.e("dd", "time="+(time2-time1));
101
        }
102
      });
103
    }
104

  
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

  
107
  public void exception(final String exce)
108
    {
109
    mAct.runOnUiThread(new Runnable()
110
      {
111
      @Override
112
      public void run()
113
        {
114
        for(int i=0; i<RubikSize.LENGTH; i++)
115
          {
116
          mViews[i].exception(exce);
117
          }
118
        }
119
      });
120
    }
121
  }
src/main/res/layout/scores.xml
12 12
        android:theme="@style/Theme.AppCompat.NoActionBar">
13 13
    </android.support.design.widget.TabLayout>
14 14

  
15
    <org.distorted.magic.RubikScoresViewPager
15
    <android.support.v4.view.ViewPager
16 16
        android:id="@+id/viewpager"
17 17
        android:layout_width="match_parent"
18 18
        android:layout_height="0dp"

Also available in: Unified diff