commit 946d9762aa83bedcde3a2818ee83f5c5186f4e48
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Tue Feb 11 21:34:56 2020 +0000

    Downloading High Scores: more dynamic way of displaying scores

diff --git a/src/main/java/org/distorted/magic/RubikScores.java b/src/main/java/org/distorted/magic/RubikScores.java
index 0c69ade6..ad4f29ed 100644
--- a/src/main/java/org/distorted/magic/RubikScores.java
+++ b/src/main/java/org/distorted/magic/RubikScores.java
@@ -24,6 +24,7 @@ import android.content.DialogInterface;
 import android.os.Bundle;
 import android.support.annotation.NonNull;
 import android.support.v4.app.FragmentActivity;
+import android.support.v4.view.ViewPager;
 import android.support.v7.app.AlertDialog;
 import android.support.v7.app.AppCompatDialogFragment;
 import android.support.design.widget.TabLayout;
@@ -37,7 +38,6 @@ import android.widget.TextView;
 public class RubikScores extends AppCompatDialogFragment
   {
   RubikScoresPagerAdapter mPagerAdapter;
-  RubikScoresViewPager    mViewPager;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
@@ -67,14 +67,12 @@ public class RubikScores extends AppCompatDialogFragment
     final View view = inflater.inflate(R.layout.scores, null);
     builder.setView(view);
 
-    mViewPager = view.findViewById(R.id.viewpager);
-    mViewPager.startDownload(act);
-
+    ViewPager viewPager = view.findViewById(R.id.viewpager);
     TabLayout tabLayout = view.findViewById(R.id.sliding_tabs);
-    mPagerAdapter = new RubikScoresPagerAdapter(act,mViewPager);
-    tabLayout.setupWithViewPager(mViewPager);
+    mPagerAdapter = new RubikScoresPagerAdapter(act,viewPager);
+    tabLayout.setupWithViewPager(viewPager);
 
-    mViewPager.setCurrentItem(0);
+    viewPager.setCurrentItem(0);
 
     for (int i = 0; i< RubikSize.LENGTH; i++)
       {
diff --git a/src/main/java/org/distorted/magic/RubikScoresPagerAdapter.java b/src/main/java/org/distorted/magic/RubikScoresPagerAdapter.java
index 43d8d1a4..94305a0c 100644
--- a/src/main/java/org/distorted/magic/RubikScoresPagerAdapter.java
+++ b/src/main/java/org/distorted/magic/RubikScoresPagerAdapter.java
@@ -22,22 +22,96 @@ package org.distorted.magic;
 import android.support.annotation.NonNull;
 import android.support.v4.app.FragmentActivity;
 import android.support.v4.view.PagerAdapter;
+import android.support.v4.view.ViewPager;
 import android.view.View;
 import android.view.ViewGroup;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-class RubikScoresPagerAdapter extends PagerAdapter
+class RubikScoresPagerAdapter extends PagerAdapter implements RubikScoresDownloader.Receiver
   {
   private FragmentActivity mAct;
-  private RubikScoresViewPager mPager;
+  private RubikScoresView[] mViews;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  RubikScoresPagerAdapter(FragmentActivity act, RubikScoresViewPager viewPager)
+  public void receive(final int[][][] country, final String[][][] name, final String[][][] time)
+    {
+    mAct.runOnUiThread(new Runnable()
+      {
+      @Override
+      public void run()
+        {
+        for(int i=0; i<RubikSize.LENGTH; i++)
+          {
+          mViews[i].prepareView(mAct);
+          }
+        }
+      });
+    try
+      {
+      Thread.sleep(200);
+      }
+    catch( InterruptedException ie)
+      {
+
+      }
+
+    for(int i=0; i<RubikSize.LENGTH; i++)
+      {
+      final RubikScoresView view = mViews[i];
+
+      for(int j=0; j<RubikActivity.MAX_SCRAMBLE; j++)
+        {
+        final int scramble = j;
+        final int[] c = country[i][j];
+        final String[] n = name[i][j];
+        final String[] t = time[i][j];
+
+        mAct.runOnUiThread(new Runnable()
+          {
+          @Override
+          public void run()
+            {
+            view.addScramble(mAct, scramble, c, n, t);
+            }
+          });
+
+        try
+          {
+          Thread.sleep(50);
+          }
+        catch( InterruptedException ie)
+          {
+
+          }
+        }
+      }
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  public void exception(final String exce)
+    {
+    mAct.runOnUiThread(new Runnable()
+      {
+      @Override
+      public void run()
+        {
+        for(int i=0; i<RubikSize.LENGTH; i++)
+          {
+          mViews[i].exception(exce);
+          }
+        }
+      });
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  RubikScoresPagerAdapter(FragmentActivity act, ViewPager viewPager)
     {
     mAct = act;
-    mPager = viewPager;
+    mViews = new RubikScoresView[RubikSize.LENGTH];
 
     viewPager.setAdapter(this);
     viewPager.setOffscreenPageLimit( RubikSize.LENGTH-1 );
@@ -49,11 +123,27 @@ class RubikScoresPagerAdapter extends PagerAdapter
   @NonNull
   public Object instantiateItem(@NonNull ViewGroup collection, int position)
     {
-    RubikScoresView view = new RubikScoresView(mAct);
-    mPager.setView(position,view);
-    collection.addView(view);
-
-    return view;
+    mViews[position] = new RubikScoresView(mAct);
+    collection.addView(mViews[position]);
+
+    boolean allCreated = true;
+
+    for(int i=0; i<RubikSize.LENGTH; i++)
+      {
+      if( mViews[i]==null )
+        {
+        allCreated = false;
+        break;
+        }
+      }
+
+    if( allCreated )
+      {
+      RubikScoresDownloader downloader = new RubikScoresDownloader();
+      downloader.download(this, mAct.getResources(), mAct.getPackageName());
+      }
+
+    return mViews[position];
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/magic/RubikScoresView.java b/src/main/java/org/distorted/magic/RubikScoresView.java
index 55dfa2a8..e6da84df 100644
--- a/src/main/java/org/distorted/magic/RubikScoresView.java
+++ b/src/main/java/org/distorted/magic/RubikScoresView.java
@@ -20,8 +20,6 @@
 package org.distorted.magic;
 
 import android.content.Context;
-import android.content.res.Resources;
-import android.graphics.BitmapFactory;
 import android.support.v4.app.FragmentActivity;
 import android.util.AttributeSet;
 import android.view.View;
@@ -34,15 +32,13 @@ import android.widget.TextView;
 
 public class RubikScoresView extends FrameLayout
   {
-  private boolean mCreated;
+  LinearLayout mLayout;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   public RubikScoresView(Context context, AttributeSet attrs, int defStyle)
     {
     super(context, attrs, defStyle);
-
-    mCreated = false;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -72,43 +68,41 @@ public class RubikScoresView extends FrameLayout
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  void prepareView(FragmentActivity act, final int[][] country, final String[][] name, final String[][] time)
+  void prepareView(FragmentActivity act)
     {
-    if( mCreated ) return;
-
     removeAllViews();
 
     View tab = inflate(act, R.layout.scores_tab, null);
-    LinearLayout layout = tab.findViewById(R.id.tabLayout);
+    mLayout = tab.findViewById(R.id.tabLayout);
     addView(tab);
+    }
 
-    for(int i=0; i<RubikActivity.MAX_SCRAMBLE; i++)
-      {
-      LinearLayout level = (LinearLayout)inflate(act, R.layout.level_records, null);
-      TextView text = level.findViewById(R.id.levelTitle);
-      text.setText(act.getString(R.string.sc_placeholder,(i+1)));
+///////////////////////////////////////////////////////////////////////////////////////////////////
 
-      for(int j=0; j<RubikScoresDownloader.MAX_PLACES; j++)
+  void addScramble(FragmentActivity act, int scramble, final int[] country, final String[] name, final String[] time)
+    {
+    LinearLayout level = (LinearLayout)inflate(act, R.layout.level_records, null);
+    TextView text = level.findViewById(R.id.levelTitle);
+    text.setText(act.getString(R.string.sc_placeholder,(scramble+1)));
+
+    for(int j=0; j<RubikScoresDownloader.MAX_PLACES; j++)
+      {
+      if( name[j] != null )
         {
-        if( name[i][j] != null )
-          {
-          View row = inflate(act, R.layout.level_row, null);
+        View row = inflate(act, R.layout.level_row, null);
 
-          ImageView imgCoun = row.findViewById(R.id.level_row_country);
-          TextView textName = row.findViewById(R.id.level_row_name);
-          TextView textTime = row.findViewById(R.id.level_row_time);
+        ImageView imgCoun = row.findViewById(R.id.level_row_country);
+        TextView textName = row.findViewById(R.id.level_row_name);
+        TextView textTime = row.findViewById(R.id.level_row_time);
 
-          imgCoun.setImageResource(country[i][j]);
-          textName.setText(name[i][j]);
-          textTime.setText(time[i][j]);
+        imgCoun.setImageResource(country[j]);
+        textName.setText(name[j]);
+        textTime.setText(time[j]);
 
-          level.addView(row);
-          }
+        level.addView(row);
         }
-
-      layout.addView(level);
       }
 
-    mCreated = true;
+    mLayout.addView(level);
     }
   }
diff --git a/src/main/java/org/distorted/magic/RubikScoresViewPager.java b/src/main/java/org/distorted/magic/RubikScoresViewPager.java
deleted file mode 100644
index 11866404..00000000
--- a/src/main/java/org/distorted/magic/RubikScoresViewPager.java
+++ /dev/null
@@ -1,121 +0,0 @@
-///////////////////////////////////////////////////////////////////////////////////////////////////
-// Copyright 2020 Leszek Koltunski                                                               //
-//                                                                                               //
-// This file is part of Magic Cube.                                                              //
-//                                                                                               //
-// Magic Cube is free software: you can redistribute it and/or modify                            //
-// it under the terms of the GNU General Public License as published by                          //
-// the Free Software Foundation, either version 2 of the License, or                             //
-// (at your option) any later version.                                                           //
-//                                                                                               //
-// Magic Cube is distributed in the hope that it will be useful,                                 //
-// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
-// GNU General Public License for more details.                                                  //
-//                                                                                               //
-// You should have received a copy of the GNU General Public License                             //
-// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-package org.distorted.magic;
-
-import android.content.Context;
-import android.support.v4.app.FragmentActivity;
-import android.support.v4.view.ViewPager;
-import android.util.AttributeSet;
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-class RubikScoresViewPager extends ViewPager implements RubikScoresDownloader.Receiver
-  {
-  FragmentActivity mAct;
-  private RubikScoresView[] mViews;
-  private int mSelected;
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  public RubikScoresViewPager(Context context, AttributeSet attrs)
-    {
-    super(context,attrs);
-
-    mViews = new RubikScoresView[RubikSize.LENGTH];
-    mSelected = 0;
-
-    addOnPageChangeListener(new ViewPager.OnPageChangeListener()
-      {
-      @Override
-      public void onPageSelected(int position)
-        {
-        mSelected = position;
-        }
-
-      @Override
-      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
-        { }
-
-      @Override
-      public void onPageScrollStateChanged(int state)
-        { }
-      });
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  void startDownload(FragmentActivity act)
-    {
-    mAct = act;
-
-    RubikScoresDownloader downloader = new RubikScoresDownloader();
-    downloader.download(this, mAct.getResources(), mAct.getPackageName());
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  void setView(int position, RubikScoresView view)
-    {
-    if( position>=0 && position<RubikSize.LENGTH )
-      {
-      mViews[position] = view;
-      }
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  public void receive(final int[][][] country, final String[][][] name, final String[][][] time)
-    {
-    mAct.runOnUiThread(new Runnable()
-      {
-      @Override
-      public void run()
-        {
-        long time1 = System.currentTimeMillis();
-
-        for(int i=0; i<RubikSize.LENGTH; i++)
-          {
-          mViews[i].prepareView(mAct, country[i], name[i], time[i]);
-          }
-
-        long time2 = System.currentTimeMillis();
-
-        android.util.Log.e("dd", "time="+(time2-time1));
-        }
-      });
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  public void exception(final String exce)
-    {
-    mAct.runOnUiThread(new Runnable()
-      {
-      @Override
-      public void run()
-        {
-        for(int i=0; i<RubikSize.LENGTH; i++)
-          {
-          mViews[i].exception(exce);
-          }
-        }
-      });
-    }
-  }
diff --git a/src/main/res/layout/scores.xml b/src/main/res/layout/scores.xml
index 9fdc0030..d70ded5e 100644
--- a/src/main/res/layout/scores.xml
+++ b/src/main/res/layout/scores.xml
@@ -12,7 +12,7 @@
         android:theme="@style/Theme.AppCompat.NoActionBar">
     </android.support.design.widget.TabLayout>
 
-    <org.distorted.magic.RubikScoresViewPager
+    <android.support.v4.view.ViewPager
         android:id="@+id/viewpager"
         android:layout_width="match_parent"
         android:layout_height="0dp"
