commit 432a5f2c1134acd316e24e2e999d1b44ac88f92e
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Fri Aug 12 01:23:25 2022 +0200

    IAP, part 2: unify the way we create object popups in ScreenPlay and ConfigActivity.

diff --git a/src/main/java/org/distorted/config/ConfigScreen.java b/src/main/java/org/distorted/config/ConfigScreen.java
index 0e6efc36..74294000 100644
--- a/src/main/java/org/distorted/config/ConfigScreen.java
+++ b/src/main/java/org/distorted/config/ConfigScreen.java
@@ -14,18 +14,17 @@ import android.util.TypedValue;
 import android.view.Gravity;
 import android.view.View;
 import android.widget.GridLayout;
-import android.widget.ImageButton;
 import android.widget.LinearLayout;
 import android.widget.PopupWindow;
 import android.widget.ScrollView;
 import android.widget.TextView;
 
+import org.distorted.helpers.PopupCreator;
 import org.distorted.objectlib.main.ObjectControl;
 
 import org.distorted.helpers.TransparentImageButton;
 import org.distorted.main.R;
 import org.distorted.main.RubikActivity;
-import org.distorted.objects.RubikObject;
 import org.distorted.objects.RubikObjectList;
 
 import static android.view.View.inflate;
@@ -51,45 +50,24 @@ public class ConfigScreen
 
   private void setupObjectWindow(final ConfigActivity act, final float width, final float height)
     {
-    int cubeWidth = (int)(width/9);
-    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
-    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
-    mMaxRowCount = (int)((height-mBarHeight)/mObjectSize);
+    int numObjects= RubikObjectList.getNumObjects();
+    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
+    mColCount = NUM_COLUMNS;
+
+    int cubeSize  = (int)(width/9);
+    int margin    = (int)(width*RubikActivity.LARGE_MARGIN);
+    mObjectSize   = (int)(cubeSize + 2*margin + 0.5f);
+    mMaxRowCount  = (int)((height-mBarHeight)/mObjectSize);
 
     ScrollView view = (ScrollView)inflate( act, R.layout.popup_object_simple, null);
     GridLayout objectGrid = view.findViewById(R.id.objectGrid);
 
-    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
-    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
-
-    objectGrid.setColumnCount(mColCount);
-    objectGrid.setRowCount(mRowCount);
-
-    mObjectPopup = new PopupWindow(act);
-    mObjectPopup.setFocusable(true);
-    mObjectPopup.setContentView(view);
-
-    int[] nextInRow = new int[mRowCount];
-
-    for(int row=0; row<mRowCount; row++)
-      {
-      rowSpecs[row] = GridLayout.spec(row);
-      nextInRow[row]= 0;
-      }
-    for(int col=0; col<mColCount; col++)
-      {
-      colSpecs[col] = GridLayout.spec(col);
-      }
-
-    int numObjects = RubikObjectList.getNumObjects();
+    PopupCreator.createObjectGrid(objectGrid,act,mRowCount,mColCount,numObjects,margin,cubeSize);
 
-    for(int object=0; object<numObjects; object++)
+    for(int child=0; child<numObjects; child++)
       {
-      final int ordinal = object;
-      RubikObject rObject = RubikObjectList.getObject(ordinal);
-      int row = object/NUM_COLUMNS;
-      ImageButton button = new ImageButton(act);
-      if( rObject!=null ) rObject.setIconTo(act,button);
+      View button = objectGrid.getChildAt(child);
+      final int ordinal = child;
 
       button.setOnClickListener( new View.OnClickListener()
         {
@@ -107,22 +85,11 @@ public class ConfigScreen
           mObjectPopup.dismiss();
           }
         });
-
-      GridLayout.Spec rowS = rowSpecs[row];
-      GridLayout.Spec colS = colSpecs[nextInRow[row]];
-      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowS,colS);
-      params.bottomMargin = margin;
-      params.topMargin    = margin;
-      params.leftMargin   = margin;
-      params.rightMargin  = margin;
-
-      params.width = cubeWidth;
-      params.height= cubeWidth;
-
-      nextInRow[row]++;
-
-      objectGrid.addView(button, params);
       }
+
+    mObjectPopup = new PopupWindow(act);
+    mObjectPopup.setFocusable(true);
+    mObjectPopup.setContentView(view);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -312,10 +279,6 @@ public class ConfigScreen
     int width = act.getScreenWidthInPixels();
     mBarHeight = act.getHeightBar();
     mButtonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
-
-    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
-    mColCount = NUM_COLUMNS;
-
     mObjectOrdinal = objectOrdinal;
 
     LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(width/4, LinearLayout.LayoutParams.MATCH_PARENT);
diff --git a/src/main/java/org/distorted/helpers/PopupCreator.java b/src/main/java/org/distorted/helpers/PopupCreator.java
new file mode 100644
index 00000000..7a3a63f1
--- /dev/null
+++ b/src/main/java/org/distorted/helpers/PopupCreator.java
@@ -0,0 +1,64 @@
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// Copyright 2022 Leszek Koltunski                                                               //
+//                                                                                               //
+// This file is part of Magic Cube.                                                              //
+//                                                                                               //
+// Magic Cube is proprietary software licensed under an EULA which you should have received      //
+// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+package org.distorted.helpers;
+
+import android.app.Activity;
+import android.widget.GridLayout;
+import android.widget.ImageButton;
+
+import org.distorted.objects.RubikObject;
+import org.distorted.objects.RubikObjectList;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+public class PopupCreator
+  {
+  public static void createObjectGrid(GridLayout grid, Activity act, int rowCount, int colCount, int numObjects, int margin, int size)
+    {
+    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[rowCount];
+    GridLayout.Spec[] colSpecs = new GridLayout.Spec[colCount];
+
+    grid.setColumnCount(colCount);
+    grid.setRowCount(rowCount);
+
+    int[] nextInRow = new int[rowCount];
+
+    for(int row=0; row<rowCount; row++)
+      {
+      rowSpecs[row] = GridLayout.spec(row);
+      nextInRow[row]= 0;
+      }
+    for(int col=0; col<colCount; col++)
+      {
+      colSpecs[col] = GridLayout.spec(col);
+      }
+
+    for(int object=0; object<numObjects; object++)
+      {
+      final RubikObject rObject = RubikObjectList.getObject(object);
+      int row = object/colCount;
+      ImageButton button = new ImageButton(act);
+      if( rObject!=null ) rObject.setIconTo(act,button);
+
+      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
+      params.bottomMargin = margin;
+      params.topMargin    = margin;
+      params.leftMargin   = margin;
+      params.rightMargin  = margin;
+
+      params.width = size;
+      params.height= size;
+
+      nextInRow[row]++;
+
+      grid.addView(button, params);
+      }
+    }
+  }
diff --git a/src/main/java/org/distorted/screens/RubikScreenPlay.java b/src/main/java/org/distorted/screens/RubikScreenPlay.java
index c32ed31e..ffbb6af1 100644
--- a/src/main/java/org/distorted/screens/RubikScreenPlay.java
+++ b/src/main/java/org/distorted/screens/RubikScreenPlay.java
@@ -35,6 +35,7 @@ import org.distorted.external.RubikNetwork;
 import org.distorted.external.RubikScores;
 import org.distorted.external.RubikUpdates;
 
+import org.distorted.helpers.PopupCreator;
 import org.distorted.main.R;
 import org.distorted.main.RubikActivity;
 import org.distorted.dialogs.RubikDialogAbout;
@@ -182,39 +183,15 @@ public class RubikScreenPlay extends RubikScreenBase implements RubikNetwork.Upd
 
     LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
     GridLayout objectGrid = view.findViewById(R.id.objectGrid);
-
-    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
-    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
-
-    objectGrid.setColumnCount(mColCount);
-    objectGrid.setRowCount(mRowCount);
-
     RelativeLayout bottomLayout = view.findViewById(R.id.bottomLayout);
     setupBottomLayout(act,bottomLayout);
 
-    mObjectPopup = new PopupWindow(act);
-    mObjectPopup.setFocusable(true);
-    mObjectPopup.setContentView(view);
-
-    int[] nextInRow = new int[mRowCount];
-
-    for(int row=0; row<mRowCount; row++)
-      {
-      rowSpecs[row] = GridLayout.spec(row);
-      nextInRow[row]= 0;
-      }
-    for(int col=0; col<mColCount; col++)
-      {
-      colSpecs[col] = GridLayout.spec(col);
-      }
+    PopupCreator.createObjectGrid(objectGrid,act,mRowCount,mColCount,numObjects,margin,cubeSize);
 
-    for(int object=0; object<numObjects; object++)
+    for(int child=0; child<numObjects; child++)
       {
-      final int ordinal = object;
-      final RubikObject rObject = RubikObjectList.getObject(object);
-      int row = object/NUM_COLUMNS;
-      ImageButton button = new ImageButton(act);
-      if( rObject!=null ) rObject.setIconTo(act,button);
+      View button = objectGrid.getChildAt(child);
+      final int ordinal = child;
 
       button.setOnClickListener( new View.OnClickListener()
         {
@@ -232,20 +209,11 @@ public class RubikScreenPlay extends RubikScreenBase implements RubikNetwork.Upd
           if( mObjectPopup!=null ) mObjectPopup.dismiss();
           }
         });
-
-      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
-      params.bottomMargin = margin;
-      params.topMargin    = margin;
-      params.leftMargin   = margin;
-      params.rightMargin  = margin;
-
-      params.width = cubeSize;
-      params.height= cubeSize;
-
-      nextInRow[row]++;
-
-      objectGrid.addView(button, params);
       }
+
+    mObjectPopup = new PopupWindow(act);
+    mObjectPopup.setFocusable(true);
+    mObjectPopup.setContentView(view);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
