Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogScoresView.java @ 588ace55

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.annotation.SuppressLint;
23
import android.content.Context;
24
import android.content.res.Resources;
25
import androidx.fragment.app.FragmentActivity;
26
import android.util.AttributeSet;
27
import android.util.TypedValue;
28
import android.view.View;
29
import android.widget.FrameLayout;
30
import android.widget.ImageView;
31
import android.widget.LinearLayout;
32
import android.widget.TextView;
33

    
34
import org.distorted.main.R;
35
import org.distorted.main.RubikActivity;
36
import org.distorted.objectlib.ObjectList;
37
import org.distorted.network.RubikScores;
38

    
39
import static org.distorted.network.RubikNetwork.MAX_PLACES;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
public class RubikDialogScoresView extends FrameLayout
44
  {
45
  private LinearLayout mLayout;
46
  private int mHeight;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
  public RubikDialogScoresView(Context context, AttributeSet attrs, int defStyle)
51
    {
52
    super(context, attrs, defStyle);
53
    }
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
  public RubikDialogScoresView(Context context, AttributeSet attrs)
58
    {
59
    super(context, attrs);
60
    }
61

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

    
64
  public RubikDialogScoresView(Context context, int height, boolean isSubmitting)
65
    {
66
    super(context);
67

    
68
    mHeight = height;
69

    
70
    View view = inflate(context, R.layout.dialog_scores_downloading, null);
71
    addView(view);
72
    TextView text = findViewById(R.id.message_text);
73
    text.setText( context.getString(isSubmitting ? R.string.submitting : R.string.downloading) );
74
    }
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

    
78
  @SuppressLint("DefaultLocale")
79
  LinearLayout createSection(FragmentActivity act, int tab, String title, int level, final String[] country, final String[] name, final float[] time)
80
    {
81
    LinearLayout levelLayout = (LinearLayout)inflate(act, R.layout.dialog_scores_scramble_title, null);
82
    TextView text = levelLayout.findViewById(R.id.scoresScrambleTitle);
83
    text.setText(title);
84

    
85
    int size = (int)(mHeight*RubikActivity.SCORES_LEVEL_TEXT);
86
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
87

    
88
    Resources res = act.getResources();
89
    String packageName = act.getPackageName();
90

    
91
    int object   = ObjectList.unpackObject(tab);
92
    int sizeIndex= ObjectList.unpackSizeIndex(tab);
93
    RubikScores scores = RubikScores.getInstance();
94

    
95
    boolean inserted = false;
96
    long myRecordInMillis = scores.getRecord(object, sizeIndex, level);
97
    float myRecordInSeconds = (myRecordInMillis/10)/100.0f;
98
    String myName = scores.getName();
99
    if( myName.length()==0 ) myName = act.getString(R.string.you);
100
    int myCountryID = res.getIdentifier( scores.getCountry(), "drawable", packageName);
101
    String myRecord = ( myRecordInMillis<RubikScores.NO_RECORD ) ? String.format("%.2f", myRecordInSeconds) : "??";
102
    String theirTime;
103
    int theirCountryID;
104

    
105
    int height = (int)(mHeight* RubikActivity.SCORES_ITEM_TEXT);
106
    int white = res.getColor(R.color.white);
107
    int red   = res.getColor(R.color.red);
108
    boolean equals;
109

    
110
    for(int j=0; j<MAX_PLACES-1; j++)
111
      {
112
      if( name[j] != null )
113
        {
114
        if( myRecordInSeconds<time[j] && !inserted )
115
          {
116
          inserted = true;
117
          View row = createRow(act, myCountryID, myName, myRecord, height, red);
118
          levelLayout.addView(row);
119
          }
120

    
121
        equals = name[j].equals(myName);
122

    
123
        if( !inserted || !equals )
124
          {
125
          if( equals ) inserted=true;
126
          theirCountryID = res.getIdentifier( country[j], "drawable", packageName);
127
          theirTime = String.format("%.2f", time[j]);
128
          View row = createRow(act, theirCountryID, name[j], theirTime, height, equals ? red:white);
129
          levelLayout.addView(row);
130
          }
131
        }
132
      }
133

    
134
    if( !inserted )
135
      {
136
      View row = createRow(act, myCountryID, myName, myRecord, height, red);
137
      levelLayout.addView(row);
138
      }
139

    
140
    return levelLayout;
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  private View createRow(FragmentActivity act, int countryID, String name, String time, int height, int color)
146
    {
147
    View row = inflate(act, R.layout.dialog_scores_scramble_row, null);
148

    
149
    ImageView imgCoun = row.findViewById(R.id.scoresScrambleRowCountry);
150
    TextView textName = row.findViewById(R.id.scoresScrambleRowName);
151
    TextView textTime = row.findViewById(R.id.scoresScrambleRowTime);
152

    
153
    imgCoun.setImageResource(countryID!=0 ? countryID : R.drawable.un);
154
    textName.setText(name);
155
    textTime.setText(time);
156

    
157
    textName.setTextColor(color);
158
    textTime.setTextColor(color);
159

    
160
    textName.setTextSize(TypedValue.COMPLEX_UNIT_PX, height);
161
    textTime.setTextSize(TypedValue.COMPLEX_UNIT_PX, height);
162

    
163
    return row;
164
    }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
// needs to run on UI thread
168

    
169
  void prepareView(FragmentActivity act)
170
    {
171
    removeAllViews();
172

    
173
    View tab = inflate(act, R.layout.dialog_scores_tab, null);
174
    mLayout = tab.findViewById(R.id.tabLayout);
175
    addView(tab);
176
    }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179
// needs to run on UI thread
180

    
181
  void addSection(LinearLayout section)
182
    {
183
    mLayout.addView(section);
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
// needs to run on UI thread
188

    
189
  void message(final String mess)
190
    {
191
    TextView text = findViewById(R.id.message_text);
192

    
193
    if( text!=null )
194
      {
195
      text.setText(mess);
196
      }
197
    }
198
  }
(13-13/19)