/
RankingView.cs
130 lines (116 loc) · 4.17 KB
/
RankingView.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using MonoTouch.UIKit;
using System.Collections.Generic;
using MonoTouch.Foundation;
using System.Drawing;
namespace PinballScoreEntry.iOS
{
public class RankingView : UITableViewSource {
Ranking[] tableList;
string cellIdentifier = "TableCell";
public event EventHandler<EventArgs> ItemSelected;
public Ranking SelectedItem { get; private set; }
public float FontSize { get; private set; }
public RankingView (List<Ranking> list, float fontSize = 12f)
{
var copy = new List<Ranking> (list);
FontSize = fontSize;
if (copy.Count == 0)
copy.Add (new Ranking { Name = "No data found." });
tableList = copy.ToArray ();
}
public override int RowsInSection (UITableView tableview, int section)
{
return tableList.Length;
}
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
RankingCell cell = tableView.DequeueReusableCell (cellIdentifier) as RankingCell;
if (cell == null)
cell = new RankingCell ((NSString)NSObject.FromObject(cellIdentifier), FontSize);
cell.UpdateCell (tableList[indexPath.Row] );
return cell;
}
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
SelectedItem = tableList[indexPath.Row];
if (this.ItemSelected != null)
this.ItemSelected (this, new EventArgs ());
}
}
public class RankingCell : UITableViewCell {
UILabel nameLabel, positionLabel, scoreLabel, pointsLabel;
public RankingCell (NSString cellId, float fontSize) : base (UITableViewCellStyle.Default, cellId)
{
SelectionStyle = UITableViewCellSelectionStyle.Default;
var font = UIFont.SystemFontOfSize(fontSize);
positionLabel = new UILabel () { Font = font };
nameLabel = new UILabel () { Font = font };
scoreLabel = new UILabel () { Font = font, TextAlignment = UITextAlignment.Right };
pointsLabel = new UILabel () { Font = font, TextAlignment = UITextAlignment.Right };
ContentView.Add (nameLabel);
ContentView.Add (positionLabel);
ContentView.Add (scoreLabel);
ContentView.Add (pointsLabel);
}
public void UpdateCell (Ranking record)
{
bool isBonus = false;
if (record.IsBonusGame.HasValue)
isBonus = (bool)record.IsBonusGame;
long score;
if (record.TournamentId > 0) {
if (record.RankingType == RankingType.Global || record.RankingType == RankingType.Missing)
score = (long)record.GamesPlayed;
else
score = (long)record.Score;
}
else
score = 0;
if (isBonus || (record.RankingType == RankingType.Global && record.BonusPlayed > 0)) {
positionLabel.TextColor = UIColor.FromRGB(0.4f, 1.0f, 0.4f);
nameLabel.TextColor = UIColor.FromRGB(0.4f, 1.0f, 0.4f);
scoreLabel.TextColor = UIColor.FromRGB(0.4f, 1.0f, 0.4f);
pointsLabel.TextColor = UIColor.FromRGB(0.4f, 1.0f, 0.4f);
} else {
positionLabel.TextColor = UIColor.Black;
nameLabel.TextColor = UIColor.Black;
scoreLabel.TextColor = UIColor.Black;
pointsLabel.TextColor = UIColor.Black;
}
if (record.RankingType == RankingType.Missing) {
positionLabel.Text = string.Format ("{0}.", record.Position);
scoreLabel.Text = string.Format ("{0:n0}", record.GamesPlayed);
pointsLabel.Text = string.Format ("{0:n0}", record.BonusPlayed);
} else if (score > 0) {
scoreLabel.Text = string.Format ("{0:n0}", score);
if (record.Position > 0) {
positionLabel.Text = string.Format ("{0}.", record.Position);
pointsLabel.Text = record.Points.ToString ();
} else {
positionLabel.Text = "";
pointsLabel.Text = "";
}
}
else
{
scoreLabel.Text = "";
positionLabel.Text = "";
pointsLabel.Text = "";
}
if (record.EntryNumber.HasValue) {
nameLabel.Text = string.Format("{0} ({1})", record.Name, record.EntryNumber);
} else {
nameLabel.Text = record.Name;
}
}
public override void LayoutSubviews ()
{
base.LayoutSubviews ();
pointsLabel.Frame = new RectangleF(ContentView.Bounds.Width - 30, 4, 25, 26);
scoreLabel.Frame = new RectangleF(ContentView.Bounds.Width - 150, 4, 105, 26);
positionLabel.Frame = new RectangleF(20, 4, 30, 26);
nameLabel.Frame = new RectangleF(50, 4, ContentView.Bounds.Width - 180, 26);
}
}
}