Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogScoresView.java @ acabdd83

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.external.RubikScores;
37

    
38
import static org.distorted.external.RubikNetwork.MAX_PLACES;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

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

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

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

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

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

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

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

    
67
    mHeight = height;
68

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

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

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

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

    
87
    Resources res = act.getResources();
88
    String packageName = act.getPackageName();
89
    RubikScores scores = RubikScores.getInstance();
90

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

    
101
    int height = (int)(mHeight* RubikActivity.SCORES_ITEM_TEXT);
102
    int white = res.getColor(R.color.white);
103
    int red   = res.getColor(R.color.red);
104
    boolean equals;
105

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

    
117
        equals = name[j].equals(myName);
118

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

    
130
    if( !inserted )
131
      {
132
      View row = createRow(act, myCountryID, myName, myRecord, height, red);
133
      levelLayout.addView(row);
134
      }
135

    
136
    return levelLayout;
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
  private View createRow(FragmentActivity act, int countryID, String name, String time, int height, int color)
142
    {
143
    View row = inflate(act, R.layout.dialog_scores_scramble_row, null);
144

    
145
    ImageView imgCoun = row.findViewById(R.id.scoresScrambleRowCountry);
146
    TextView textName = row.findViewById(R.id.scoresScrambleRowName);
147
    TextView textTime = row.findViewById(R.id.scoresScrambleRowTime);
148

    
149
    imgCoun.setImageResource(countryID!=0 ? countryID : R.drawable.un);
150
    textName.setText(name);
151
    textTime.setText(time);
152

    
153
    textName.setTextColor(color);
154
    textTime.setTextColor(color);
155

    
156
    textName.setTextSize(TypedValue.COMPLEX_UNIT_PX, height);
157
    textTime.setTextSize(TypedValue.COMPLEX_UNIT_PX, height);
158

    
159
    return row;
160
    }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
// needs to run on UI thread
164

    
165
  void prepareView(FragmentActivity act)
166
    {
167
    removeAllViews();
168

    
169
    View tab = inflate(act, R.layout.dialog_scores_tab, null);
170
    mLayout = tab.findViewById(R.id.tabLayout);
171
    addView(tab);
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175
// needs to run on UI thread
176

    
177
  void addSection(LinearLayout section)
178
    {
179
    mLayout.addView(section);
180
    }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
// needs to run on UI thread
184

    
185
  void message(final String mess)
186
    {
187
    TextView text = findViewById(R.id.message_text);
188

    
189
    if( text!=null )
190
      {
191
      text.setText(mess);
192
      }
193
    }
194
  }
(13-13/21)