From 6ef081a26eeeb9b4a21c0a81a43697a694b384e9 Mon Sep 17 00:00:00 2001 From: Zhao Cheng Date: Mon, 5 Mar 2012 22:37:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E5=8C=BA=E7=83=AD=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HotPage.xaml | 64 ++++++++++++++++++++++++------- HotPage.xaml.cs | 73 ++++++++++++++++++++++++++++++++++++ MainPage.xaml | 6 +-- MainPage.xaml.cs | 18 ++++++++- PostPage.xaml | 2 +- Sbbs/Response.cs | 16 ++++++++ Sbbs/Service.cs | 10 +++++ Sbbs/TopicsGroupViewModel.cs | 61 +++++++++++++++++++++++++++--- ViewModels/MainViewModel.cs | 2 +- 9 files changed, 226 insertions(+), 26 deletions(-) diff --git a/HotPage.xaml b/HotPage.xaml index f16b087..b74bff6 100644 --- a/HotPage.xaml +++ b/HotPage.xaml @@ -11,25 +11,66 @@ FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" - SupportedOrientations="Portrait" Orientation="Portrait" + SupportedOrientations="PortraitOrLandscape" Orientation="Portrait" mc:Ignorable="d" d:DesignHeight="696" d:DesignWidth="480" toolkit:TiltEffect.IsTiltEnabled="True" shell:SystemTray.Opacity="{Binding IsLoading, Converter={StaticResource LoadedOpacityConerter}, ConverterParameter=true}" shell:SystemTray.IsVisible="True"> + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - + + + + + + + + + + + + + + + + + + + + + @@ -41,12 +82,7 @@ - - - - - - + diff --git a/HotPage.xaml.cs b/HotPage.xaml.cs index 7aae57a..8a566df 100644 --- a/HotPage.xaml.cs +++ b/HotPage.xaml.cs @@ -3,6 +3,7 @@ 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; @@ -10,14 +11,86 @@ 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 HotViewModel viewModel = new HotViewModel(); + private bool isHotTopicsLoading = true; + public HotPage() { InitializeComponent(); + + DataContext = viewModel; + } + + protected override void OnNavigatedTo(NavigationEventArgs e) + { + base.OnNavigatedTo(e); + + if (NavigationContext.QueryString.ContainsKey("type")) + { + int type = int.Parse(NavigationContext.QueryString["type"]); + switch (type) + { + case 0: + if (viewModel.TopicsGroupItems == null) + LoadHotTopics(); + break; + } + } + } + + private void SetLoading() + { + viewModel.IsLoading = isHotTopicsLoading; + } + + private void LoadHotTopics() + { + isHotTopicsLoading = true; + SetLoading(); + App.Service.HotTopics(delegate(ObservableCollection topics, bool success, string error) + { + isHotTopicsLoading = false; + SetLoading(); + + if (topics != null) + { + ObservableCollection newGroup = new ObservableCollection(); + foreach (HotTopicsViewModel hot in topics) + { + TopicsGroupViewModel newItem = new TopicsGroupViewModel(hot.Description); + foreach (TopicViewModel topic in hot.Topics) + newItem.Add(topic); + newGroup.Add(newItem); + } + + viewModel.TopicsGroupItems = newGroup; + } + }); + } + + 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)); + } } } } \ No newline at end of file diff --git a/MainPage.xaml b/MainPage.xaml index 673c40d..3e82789 100644 --- a/MainPage.xaml +++ b/MainPage.xaml @@ -101,9 +101,9 @@ - - - + + + diff --git a/MainPage.xaml.cs b/MainPage.xaml.cs index 89fcba4..a493fb9 100644 --- a/MainPage.xaml.cs +++ b/MainPage.xaml.cs @@ -92,11 +92,27 @@ private void RefreshFavorates_Tap(object sender, System.Windows.Input.GestureEve // 分区热点 private void HotTopics_Tap(object sender, System.Windows.Input.GestureEventArgs e) { + NavigationService.Navigate(new Uri("/HotPage.xaml?type=0", UriKind.RelativeOrAbsolute)); + } + + // 热门版面 + private void HotBoards_Tap(object sender, System.Windows.Input.GestureEventArgs e) + { + NavigationService.Navigate(new Uri("/HotPage.xaml?type=1", UriKind.RelativeOrAbsolute)); + } + // 版面分区 + private void Sections_Tap(object sender, System.Windows.Input.GestureEventArgs e) + { + NavigationService.Navigate(new Uri("/HotPage.xaml?type=2", UriKind.RelativeOrAbsolute)); + } + + // 浏览历史 + private void History_Tap(object sender, System.Windows.Input.GestureEventArgs e) + { } // 点击收藏夹 - private void Favorates_Selected(object sender, SelectionChangedEventArgs e) { if (e.AddedItems.Count == 1) diff --git a/PostPage.xaml b/PostPage.xaml index ce9c950..1a315dd 100644 --- a/PostPage.xaml +++ b/PostPage.xaml @@ -17,7 +17,7 @@ shell:SystemTray.IsVisible="True"> - diff --git a/Sbbs/Response.cs b/Sbbs/Response.cs index df6bfcd..181b1ef 100644 --- a/Sbbs/Response.cs +++ b/Sbbs/Response.cs @@ -81,6 +81,22 @@ public ObservableCollection Root } } + // 返回首页热点 + [DataContract] + public class HotTopicsResponse : Response, IResponse> + { + [DataMember(Name = "topics")] + public ObservableCollection topics; + + public ObservableCollection Root + { + get + { + return topics; + } + } + } + // 返回用户认证Token [DataContract] public class TokenResponse : Response, IResponse diff --git a/Sbbs/Service.cs b/Sbbs/Service.cs index a5ef4b5..347a538 100644 --- a/Sbbs/Service.cs +++ b/Sbbs/Service.cs @@ -86,6 +86,16 @@ public void Topic(string board, int id, int start, int limit, Action() { Callback = callback }); } + // 首页热点 + public void HotTopics(Action, bool, string> callback) + { + WebClient wc = new WebClient(); + Uri uri = new Uri(apiBase + "hot/topics" + apiPost); + + wc.DownloadStringCompleted += DownloadedAndParse, HotTopicsResponse>; + wc.DownloadStringAsync(uri, new ServiceArg>() { Callback = callback }); + } + // 发帖 public void TopicPost(string board, int reid, string title, string content, Action callback) { diff --git a/Sbbs/TopicsGroupViewModel.cs b/Sbbs/TopicsGroupViewModel.cs index 2a1926f..d3e1049 100644 --- a/Sbbs/TopicsGroupViewModel.cs +++ b/Sbbs/TopicsGroupViewModel.cs @@ -7,14 +7,65 @@ namespace sbbs_client_wp7.Sbbs { [DataContract] - public class TopicsGroupViewModel : ObservableCollection + public class HotTopicsViewModel : INotifyPropertyChanged { + private string description; + private ObservableCollection topics; + [DataMember(Name = "description")] - public string Title { get; set; } - + public string Description + { + get + { + return description; + } + set + { + if (value != description) + { + description = value; + NotifyPropertyChanged("Description"); + } + } + } + [DataMember(Name = "topics")] - public ObservableCollection Topics { get; set; } + public ObservableCollection Topics + { + get + { + return topics; + } + set + { + if (value != topics) + { + topics = value; + NotifyPropertyChanged("Topics"); + } + } + } + + public event PropertyChangedEventHandler PropertyChanged; + private void NotifyPropertyChanged(String propertyName) + { + PropertyChangedEventHandler handler = PropertyChanged; + if (null != handler) + { + handler(this, new PropertyChangedEventArgs(propertyName)); + } + } + } + public class TopicsGroupViewModel : ObservableCollection + { + public TopicsGroupViewModel(string name) + { + this.Title = name; + } + + public string Title { get; set; } + public bool HasItems { get @@ -26,7 +77,5 @@ private set { } } - - // 将ObservableCollection的全部元素操作转向Topics } } diff --git a/ViewModels/MainViewModel.cs b/ViewModels/MainViewModel.cs index 162a5ab..1d99124 100644 --- a/ViewModels/MainViewModel.cs +++ b/ViewModels/MainViewModel.cs @@ -23,7 +23,7 @@ public MainViewModel() { // 载入保存的设置 App.Service.Token = LocalCache.Get("Token"); - App.Service.BoardMode = LocalCache.Get("BoardMode", 3); + App.Service.BoardMode = LocalCache.Get("BoardMode", 2); // 初始化 CurrentBoard = new CurrentBoardViewModel();