/
MainViewModel.cs
144 lines (108 loc) · 4.86 KB
/
MainViewModel.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
using Microsoft.Phone.Notification;
using Microsoft.WindowsAzure.MobileServices;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace EventBuddyPhone
{
public class MainViewModel : ViewModel
{
public async Task<Category> LoadCategory(int categoryId)
{
var currentCategory = Categories.Single(c => c.Id == categoryId);
// TODO - load real events
var events = await App.MobileService.GetTable<Event>().Where(
e => e.CategoryId == categoryId).ToEnumerableAsync();
currentCategory.Events.SetRange(events);
CurrentCategory = currentCategory;
return currentCategory;
}
public async Task<Event> LoadEvent(int eventId)
{
var currentEvent = CurrentCategory.Events.Single(e => e.Id == eventId);
// TODO - actually load votables
var votables = await App.MobileService.GetTable<Votable>()
.Where(v => v.EventId == eventId).ToEnumerableAsync();
currentEvent.Votables.SetRange(votables);
CurrentEvent = currentEvent;
CurrentEvent.SetMaximum();
return currentEvent;
}
private async Task LoginTwitter()
{
// TODO - ensure login via twitter
await App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.Twitter);
}
private async Task UploadNotificationChannel()
{
var pushChannel = AcquirePushChannel();
// TODO - upload push channel
var channel = new JObject();
channel.Add("channelUri", pushChannel.ChannelUri.AbsoluteUri);
channel.Add("deviceType", "WP8");
await App.MobileService.GetTable("channels").InsertAsync(channel);
}
private void channel_HttpNotificationReceived(object sender, HttpNotificationEventArgs e)
{
string content = new StreamReader(e.Notification.Body).ReadToEnd();
int eventId = int.Parse(content);
// TODO - handle push notification
if (App.ViewModel.CurrentEvent == null || App.ViewModel.CurrentEvent.Id != eventId) return;
_sync.Post(async ignored =>
{
await App.ViewModel.LoadEvent(eventId);
}, null);
}
public static HttpNotificationChannel CurrentChannel { get; private set; }
private HttpNotificationChannel AcquirePushChannel()
{
var channel = HttpNotificationChannel.Find("MyPushChannel");
if (channel == null)
{
channel = new HttpNotificationChannel("MyPushChannel");
channel.Open();
channel.BindToShellTile();
channel.BindToShellToast();
}
channel.HttpNotificationReceived += channel_HttpNotificationReceived;
return channel;
}
private ObservableCollection<Category> _categories = new ObservableCollection<Category>();
public ObservableCollection<Category> Categories
{
get { return this._categories; }
set { this.SetProperty(ref _categories, value); }
}
private Category _currentCategory;
public Category CurrentCategory
{
get { return this._currentCategory; }
set { this.SetProperty(ref _currentCategory, value); }
}
private Event _currentEvent;
public Event CurrentEvent
{
get { return this._currentEvent; }
set { this.SetProperty(ref _currentEvent, value); }
}
private bool _initialized = false;
public async void Initialize()
{
if (_initialized) return;
_initialized = true;
_categories.Clear();
_categories.Add(new Category { Id = 1, Title = "Beer Fest", Image = "http://homebrewfanatic.com/wp-content/uploads/2008/03/beer-fest.jpg", Subtitle = "Enjoy beer at a festival" });
_categories.Add(new Category { Id = 2, Title = "Garage Sales", Image = "http://bacc.cc/wp-content/uploads/2012/05/garage-sale.jpg", Subtitle = "Buy some trash" });
_categories.Add(new Category { Id = 3, Title = "Presentations", Image = "http://www.hearsaycommunications.com/images/presentation.jpg", Subtitle = "Watch a cool presentation" });
await LoginTwitter();
await UploadNotificationChannel();
}
private SynchronizationContext _sync = SynchronizationContext.Current;
}
}