Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogScoresView.java @ 3f7a4363

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.objectlib.main.ObjectList;
35

    
36
import org.distorted.main.R;
37
import org.distorted.main.RubikActivity;
38
import org.distorted.network.RubikScores;
39

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

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

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

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

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

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

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

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

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

    
69
    mHeight = height;
70

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

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

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

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

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

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

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

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

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

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

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

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

    
141
    return levelLayout;
142
    }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

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

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

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

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

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

    
164
    return row;
165
    }
166

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

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

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

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

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

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

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

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