forked from xamarin/ios-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cs
167 lines (147 loc) · 4.4 KB
/
main.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using CoreGraphics;
using System.Net;
using Foundation;
using UIKit;
using SystemConfiguration;
using CoreFoundation;
namespace reachability {
public partial class ReachabilityAppDelegate : UIApplicationDelegate {
UITableView tableView;
NetworkStatus remoteHostStatus, internetStatus, localWifiStatus;
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
AddTable ();
UpdateStatus ();
Reachability.ReachabilityChanged += (object sender, EventArgs e) => { UpdateStatus (); };
window.MakeKeyAndVisible ();
return true;
}
void UpdateStatus ()
{
remoteHostStatus = Reachability.RemoteHostStatus ();
internetStatus = Reachability.InternetConnectionStatus ();
localWifiStatus = Reachability.LocalWifiConnectionStatus ();
tableView.ReloadData ();
}
void AddTable ()
{
CGRect tableFrame = UIScreen.MainScreen.ApplicationFrame;
tableView = new UITableView (tableFrame, UITableViewStyle.Grouped) {
AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight,
RowHeight = 44.0f,
SeparatorStyle = UITableViewCellSeparatorStyle.None,
SectionHeaderHeight = 28.0f,
ScrollEnabled = false,
Source = new DataSource (this),
};
contentView.InsertSubviewBelow (tableView, summaryLabel);
contentView.BringSubviewToFront (summaryLabel);
tableView.ReloadData ();
}
class DataSource : UITableViewSource {
ReachabilityAppDelegate parent;
UIImage imageCarrier, imageWiFi, imageStop;
public DataSource (ReachabilityAppDelegate parent)
{
imageCarrier = UIImage.FromFile ("WWAN5.png");
imageWiFi = UIImage.FromFile ("Airport.png");
imageStop = UIImage.FromFile ("stop-32.png");
this.parent = parent;
}
public override NSIndexPath WillSelectRow (UITableView view, NSIndexPath index)
{
return null;
}
public override nint RowsInSection (UITableView view, nint section)
{
return 1;
}
public override nint NumberOfSections (UITableView view)
{
return 3;
}
public override string TitleForHeader (UITableView view, nint section)
{
switch (section) {
case 0:
return Reachability.HostName;
case 1:
return "Access to internet hosts";
case 2:
return "Access to Local Bonjour Hosts";
default:
return "Unknown";
}
}
static NSString ReachabilityTableCellIdentifier = new NSString ("ReachabilityTableCell");
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
var cell = tableView.DequeueReusableCell (ReachabilityTableCellIdentifier);
if (cell == null) {
cell = new UITableViewCell (UITableViewCellStyle.Default, ReachabilityTableCellIdentifier);
var label = cell.TextLabel;
label.Font = UIFont.SystemFontOfSize (12f);
label.TextColor = UIColor.DarkGray;
label.TextAlignment = UITextAlignment.Left;
}
var row = indexPath.Row;
string text = "";
UIImage image = null;
switch (indexPath.Section) {
case 0:
switch (parent.remoteHostStatus) {
case NetworkStatus.NotReachable:
text = "Cannot connect to remote host";
image = imageStop;
break;
case NetworkStatus.ReachableViaCarrierDataNetwork:
text = "Reachable via data carrier network";
image = imageCarrier;
break;
case NetworkStatus.ReachableViaWiFiNetwork:
text = "Reachable via WiFi network";
image = imageWiFi;
break;
}
break;
case 1:
switch (parent.internetStatus) {
case NetworkStatus.NotReachable:
text = "Access not available";
image = imageStop;
break;
case NetworkStatus.ReachableViaCarrierDataNetwork:
text = "Available via data carrier network";
image = imageCarrier;
break;
case NetworkStatus.ReachableViaWiFiNetwork:
text = "Available via WiFi network";
image = imageWiFi;
break;
}
break;
case 2:
switch (parent.localWifiStatus) {
case NetworkStatus.NotReachable:
text = "Access not available";
image = imageStop;
break;
case NetworkStatus.ReachableViaWiFiNetwork:
text = "Available via WiFi network";
image = imageWiFi;
break;
}
break;
}
cell.TextLabel.Text = text;
cell.ImageView.Image = image;
return cell;
}
}
static void Main (string[] args)
{
UIApplication.Main (args, null, (string)null);
}
}
}