forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActivity1.cs
More file actions
218 lines (191 loc) · 7.24 KB
/
Activity1.cs
File metadata and controls
218 lines (191 loc) · 7.24 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Collections.Generic;
using Android.App;
using Android.Widget;
using Android.OS;
using PclTest.ServiceModel;
using PclTest.SharedLogic;
using ServiceStack;
using ServiceStack.Logging;
using ServiceStack.Text;
namespace PclTest.Android
{
[Flags]
public enum DbModelComparisonTypesEnum : int
{
/// <summary>
/// Compare only the PrimaryKey values
/// </summary>
PkOnly = 1,
/// <summary>
/// Compare only the non PrimaryKey values
/// </summary>
NonPkOnly = 2,
/// <summary>
/// Compare all values
/// (The PrimaryKey and non PrimaryKey values too)
/// </summary>
All = 3 // PkOnly & NonPkOnly
}
public partial class Question
{
public static DbModelComparisonTypesEnum DefaultComparisonType { get; set; }
public Guid Id { get; set; }
public string Title { get; set; }
}
[Activity(Label = "PclTest.Android", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity
{
TextView lblResults = null;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
//AndroidPclExportClient.Configure();
AndroidPclExportWithXml.Configure();
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
var btnGoSync = FindViewById<Button>(Resource.Id.btnGoSync);
var btnGoAsync = FindViewById<Button>(Resource.Id.btnGoAsync);
var btnGoShared = FindViewById<Button>(Resource.Id.btnGoShared);
var btnTest = FindViewById<Button>(Resource.Id.btnTest);
var txtName = FindViewById<EditText>(Resource.Id.txtName);
lblResults = FindViewById<TextView>(Resource.Id.lblResults);
//10.0.2.2 = loopback
//http://developer.android.com/tools/devices/emulator.html
var client = new JsonServiceClient("http://10.0.2.2:81/");
var gateway = new SharedGateway("http://10.0.2.2:81/");
btnGoSync.Click += delegate
{
try
{
var response = client.Get(new Hello { Name = txtName.Text });
lblResults.Text = response.Result;
}
catch (Exception ex)
{
lblResults.Text = ex.ToString();
}
};
btnGoAsync.Click += delegate
{
client.GetAsync(new Hello { Name = txtName.Text })
.Success(response => lblResults.Text = response.Result)
.Error(ex => lblResults.Text = ex.ToString());
};
btnGoShared.Click += async delegate
{
try
{
var greeting = await gateway.SayHello(txtName.Text);
lblResults.Text = greeting;
}
catch (Exception ex)
{
var lbl = ex.ToString();
lbl.Print();
lblResults.Text = ex.ToString();
}
};
btnTest.Click += delegate
{
try
{
//var dto = new Question
//{
// Id = Guid.NewGuid(),
// Title = "Title",
//};
//var json = dto.ToJson();
//var q = json.FromJson<Question>();
//lblResults.Text = "{0}:{1}".Fmt(q.Id, q.Title);
ConnectServerEvents();
}
catch (Exception ex)
{
lblResults.Text = ex.ToString();
}
};
LogManager.LogFactory = new LogFactory(AddMessage);
}
void AddMessage(string message)
{
RunOnUiThread(() =>
{
lblResults.Text = "{0} {1}\n".Fmt(DateTime.Now.ToLongTimeString(), message) + lblResults.Text;
});
}
class LogFactory : ILogFactory
{
public Action<string> OnMessage;
public LogFactory(Action<string> onMessage)
{
OnMessage = onMessage;
}
public ILog GetLogger(Type type)
{
return new GenericLog(type) { OnMessage = OnMessage };
}
public ILog GetLogger(string typeName)
{
return new GenericLog(typeName) { OnMessage = OnMessage };
}
}
private static ServerEventsClient serverEventsClient = null;
static ServerEventConnect connectMsg = null;
static List<ServerEventMessage> msgs = new List<ServerEventMessage>();
static List<ServerEventMessage> commands = new List<ServerEventMessage>();
static List<System.Exception> errors = new List<System.Exception>();
static string lastText = "";
private void ConnectServerEvents()
{
if (serverEventsClient == null)
{
// var client = new ServerEventsClient("http://chat.servicestack.net", channels: "home")
// bla.ybookz.com is a copy of the SS Chat sample, that sends 'HAHAHAHA' every second to all listeners
//serverEventsClient = new ServerEventsClient("http://bla.ybookz.com/", channels: "home")
//serverEventsClient = new ServerEventsClient("http://10.0.2.2:1337/", channels: "home")
serverEventsClient = new ServerEventsClient("http://chat.servicestack.net", channels: "home")
{
OnConnect = e => {
connectMsg = e;
},
OnCommand = a => {
commands.Add(a);
},
OnHeartbeat = () => {
RunOnUiThread(() => {
try
{
Toast.MakeText(this, "Heartbeat", ToastLength.Short).Show();
}
catch {}
});
},
OnMessage = a => {
msgs.Add(a);
if (lastText != a.Data)
{
lastText = a.Data ?? "";
RunOnUiThread(() => {
try
{
Toast.MakeText(this, lastText, ToastLength.Short).Show();
}
catch {}
});
}
},
OnException = ex => {
AddMessage("OnException: " + ex.Message);
errors.Add(ex);
},
HeartbeatRequestFilter = x => {
AddMessage("HeartbeatRequestFilter");
},
};
AddMessage("Started Listening...");
serverEventsClient.Start();
}
}
}
}