-
Notifications
You must be signed in to change notification settings - Fork 4
/
HotPage.xaml.cs
196 lines (173 loc) · 6.65 KB
/
HotPage.xaml.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Navigation;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Collections.ObjectModel;
namespace sbbs_client_wp7
{
using Sbbs;
public partial class HotPage : PhoneApplicationPage
{
private bool isHotTopicsLoading;
private bool isHotBoardsLoading;
private bool isSectionsLoading;
public HotPage()
{
InitializeComponent();
if (App.ViewModel.Hot == null)
App.ViewModel.Hot = new HotViewModel();
DataContext = App.ViewModel.Hot;
}
// 进入页面时根据参数切换到指定的页面
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (NavigationContext.QueryString.ContainsKey("type"))
{
int type = int.Parse(NavigationContext.QueryString["type"]);
HotPivot.SelectedIndex = type;
}
}
private void SetLoading()
{
App.ViewModel.Hot.IsLoading = isHotTopicsLoading | isHotBoardsLoading | isSectionsLoading;
}
private void LoadSections(bool force = false)
{
if (!force)
{
// 先从本地缓存载入
ObservableCollection<BoardViewModel> sections = LocalCache.Get<ObservableCollection<BoardViewModel>>("sections");
if (sections != null)
{
App.ViewModel.Hot.SectionItems = sections;
return;
}
}
// 没有的话再从网络读取
isSectionsLoading = true;
SetLoading();
App.Service.Sections(delegate(ObservableCollection<BoardViewModel> boards, bool success, string error)
{
isSectionsLoading = false;
SetLoading();
if (boards != null)
{
// 写入缓存
LocalCache.Set<ObservableCollection<BoardViewModel>>("sections", boards);
App.ViewModel.Hot.SectionItems = boards;
}
});
}
private void LoadHotTopics()
{
isHotTopicsLoading = true;
SetLoading();
App.Service.HotTopics(delegate(ObservableCollection<HotTopicsViewModel> topics, bool success, string error)
{
isHotTopicsLoading = false;
SetLoading();
if (topics != null)
{
ObservableCollection<TopicsGroupViewModel> newGroup = new ObservableCollection<TopicsGroupViewModel>();
foreach (HotTopicsViewModel hot in topics)
{
TopicsGroupViewModel newItem = new TopicsGroupViewModel(hot.Description);
foreach (TopicViewModel topic in hot.Topics)
newItem.Add(topic);
newGroup.Add(newItem);
}
App.ViewModel.Hot.TopicsGroupItems = newGroup;
}
});
}
private void LoadHotBoards()
{
isHotBoardsLoading = true;
SetLoading();
App.Service.HotBoards(delegate(ObservableCollection<BoardViewModel> boards, bool success, string error)
{
isHotBoardsLoading = false;
SetLoading();
if (boards != null)
App.ViewModel.Hot.HotboardsItems = boards;
});
}
private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count == 1)
{
// 清除选择,否则同样的项目无法点击第二次
(sender as LongListSelector).SelectedItem = null;
LongListSelector.LongListSelectorItem item = e.AddedItems[0] as LongListSelector.LongListSelectorItem;
TopicViewModel topic = item.Item as TopicViewModel;
// 清除未读
topic.Unread = false;
NavigationService.Navigate(
new Uri("/TopicPage.xaml?board=" + topic.Board + "&id=" + topic.Id + "&title=" + HttpUtility.UrlEncode(topic.Title), UriKind.Relative));
}
}
private void Board_Selected(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count == 1)
{
// 清除选择,否则同样的项目无法点击第二次
(sender as ListBox).SelectedIndex = -1;
BoardViewModel board = e.AddedItems[0] as BoardViewModel;
this.NavigationService.Navigate(new Uri("/BoardPage.xaml?board=" + board.EnglishName + "&description=" + board.Description, UriKind.Relative));
}
}
private void Section_Selected(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count == 1)
{
BoardViewModel board = e.AddedItems[0] as BoardViewModel;
this.NavigationService.Navigate(new Uri("/BoardPage.xaml?board=" + board.EnglishName + "&description=" + board.Description, UriKind.Relative));
}
}
// 刷新按钮
private void Refresh_Click(object sender, EventArgs e)
{
switch (HotPivot.SelectedIndex)
{
case 0:
LoadHotTopics();
break;
case 1:
LoadHotBoards();
break;
case 2:
LoadSections(true);
break;
}
}
// 直到切换到页面时才只刷新必要的页面
private void Pivot_LoadedPivotItem(object sender, PivotItemEventArgs e)
{
switch ((sender as Pivot).SelectedIndex)
{
case 0:
if (App.ViewModel.Hot.TopicsGroupItems == null)
LoadHotTopics();
break;
case 1:
if (App.ViewModel.Hot.HotboardsItems == null)
LoadHotBoards();
break;
case 2:
if (App.ViewModel.Hot.SectionItems == null)
LoadSections();
break;
}
}
}
}