Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogScoresView.java @ 362807f0

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

    
33
import org.distorted.main.R;
34
import org.distorted.main.RubikActivity;
35
import org.distorted.objects.RubikObjectList;
36
import org.distorted.scores.RubikScores;
37

    
38
import static org.distorted.scores.RubikScoresDownloader.MAX_PLACES;
39

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

    
42
public class RubikDialogScoresView extends FrameLayout
43
  {
44
  private LinearLayout mLayout;
45
  private int mWidth;
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 width, boolean isSubmitting)
64
    {
65
    super(context);
66

    
67
    mWidth = width;
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
  LinearLayout createSection(FragmentActivity act, int tab, int level, final String[] country, final String[] name, final float[] time)
78
    {
79
    LinearLayout levelLayout = (LinearLayout)inflate(act, R.layout.dialog_scores_scramble_title, null);
80
    TextView text = levelLayout.findViewById(R.id.scoresScrambleTitle);
81
    text.setText(act.getString(R.string.lv_placeholder,level+1));
82

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

    
86
    Resources res = act.getResources();
87
    String packageName = act.getPackageName();
88

    
89
    int object   = RubikObjectList.unpackObject(tab);
90
    int sizeIndex= RubikObjectList.unpackSizeIndex(tab);
91
    RubikScores scores = RubikScores.getInstance();
92

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

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

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

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

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

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

    
139
    return levelLayout;
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

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

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

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

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

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

    
162
    return row;
163
    }
164

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

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

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

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

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

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

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

    
192
    if( text!=null )
193
      {
194
      text.setText(mess);
195
      }
196
    }
197
  }
(11-11/14)