Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialog / RubikDialogScoresView.java @ 2dfe43d3

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.dialog;
21

    
22
import android.content.Context;
23
import android.content.res.Resources;
24
import android.support.v4.app.FragmentActivity;
25
import android.util.AttributeSet;
26
import android.view.View;
27
import android.widget.FrameLayout;
28
import android.widget.ImageView;
29
import android.widget.LinearLayout;
30
import android.widget.TextView;
31

    
32
import org.distorted.magic.R;
33
import org.distorted.object.RubikObjectList;
34
import org.distorted.scores.RubikScores;
35

    
36
import static org.distorted.scores.RubikScoresDownloader.MAX_PLACES;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
public class RubikDialogScoresView extends FrameLayout
41
  {
42
  LinearLayout mLayout;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
  public RubikDialogScoresView(Context context, AttributeSet attrs, int defStyle)
47
    {
48
    super(context, attrs, defStyle);
49
    }
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
  public RubikDialogScoresView(Context context, AttributeSet attrs)
54
    {
55
    super(context, attrs);
56
    }
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  public RubikDialogScoresView(Context context, boolean isSubmitting)
61
    {
62
    super(context);
63

    
64
    View view = inflate(context, R.layout.dialog_scores_downloading, null);
65
    addView(view);
66
    TextView text = findViewById(R.id.message_text);
67
    text.setText( context.getString(isSubmitting ? R.string.submitting : R.string.downloading) );
68
    }
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
  LinearLayout createSection(FragmentActivity act, int tab, int scramble, final String[] country, final String[] name, final float[] time)
73
    {
74
    LinearLayout level = (LinearLayout)inflate(act, R.layout.dialog_scores_scramble_title, null);
75
    TextView text = level.findViewById(R.id.scoresScrambleTitle);
76
    text.setText(act.getString(R.string.sc_placeholder,(scramble+1)));
77

    
78
    Resources res = act.getResources();
79
    String packageName = act.getPackageName();
80

    
81
    int object = RubikObjectList.unpackObject(tab);
82
    int size   = RubikObjectList.unpackSize(tab);
83
    RubikScores scores = RubikScores.getInstance();
84

    
85
    boolean inserted = false;
86
    long myRecordInMillis = scores.getRecord(object, size, scramble);
87
    float myRecordInSeconds = (myRecordInMillis/100)/10.0f;
88
    boolean mySubmitted = scores.isSubmitted(object, size, scramble);
89
    String myName = scores.getName();
90
    int myCountryID = res.getIdentifier( scores.getCountry(), "drawable", packageName);
91
    String myRecord = ( myRecordInMillis<RubikScores.NO_RECORD ) ? Float.toString(myRecordInSeconds) : "??";
92
    String theirTime;
93
    int theirCountryID;
94

    
95
    int white = res.getColor(R.color.white);
96
    int red   = res.getColor(R.color.red);
97
    boolean equals;
98

    
99
    for(int j=0; j<MAX_PLACES-1; j++)
100
      {
101
      if( name[j] != null )
102
        {
103
        if( !mySubmitted && myRecordInSeconds<time[j] && !inserted )
104
          {
105
          inserted = true;
106
          View row = createRow(act, myCountryID, myName, myRecord, red);
107
          level.addView(row);
108
          }
109

    
110
        equals = name[j].equals(myName);
111

    
112
        if( !inserted || !equals )
113
          {
114
          if( equals ) inserted=true;
115
          theirCountryID = res.getIdentifier( country[j], "drawable", packageName);
116
          theirTime = Float.toString(time[j]);
117
          View row = createRow(act, theirCountryID, name[j], theirTime, equals ? red:white);
118
          level.addView(row);
119
          }
120
        }
121
      }
122

    
123
    if( !inserted )
124
      {
125
      View row = createRow(act, myCountryID, myName, myRecord, red);
126
      level.addView(row);
127
      }
128

    
129
    return level;
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
  private View createRow(FragmentActivity act, int countryID, String name, String time, int color)
135
    {
136
    View row = inflate(act, R.layout.dialog_scores_scramble_row, null);
137

    
138
    ImageView imgCoun = row.findViewById(R.id.scoresScrambleRowCountry);
139
    TextView textName = row.findViewById(R.id.scoresScrambleRowName);
140
    TextView textTime = row.findViewById(R.id.scoresScrambleRowTime);
141

    
142
    imgCoun.setImageResource(countryID!=0 ? countryID : R.drawable.un);
143
    textName.setText(name);
144
    textTime.setText(time);
145

    
146
    textName.setTextColor(color);
147
    textTime.setTextColor(color);
148

    
149
    return row;
150
    }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
// needs to run on UI thread
154

    
155
  void prepareView(FragmentActivity act)
156
    {
157
    removeAllViews();
158

    
159
    View tab = inflate(act, R.layout.dialog_scores_tab, null);
160
    mLayout = tab.findViewById(R.id.tabLayout);
161
    addView(tab);
162
    }
163

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

    
167
  void addSection(LinearLayout section)
168
    {
169
    mLayout.addView(section);
170
    }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173
// needs to run on UI thread
174

    
175
  void message(final String mess)
176
    {
177
    TextView text = findViewById(R.id.message_text);
178
    text.setText(mess);
179
    }
180
  }
(6-6/8)