From 0623943dccad5c44831b2ad4a803eab2436e45e6 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 1 Apr 2013 12:05:02 +0400 Subject: Using WPToolkit LongListSelector --- Juick/App.xaml.cs | 4 + Juick/Classes/DelegateCommand.cs | 6 +- Juick/Classes/InvokeDelegateCommandAction.cs | 151 ++++++++++++++++++++++++ Juick/Classes/LowProfileImageLoader.cs | 4 +- Juick/Classes/ScrollViewerMonitor.cs | 64 ---------- Juick/Controls/MessageList.xaml | 78 ++++++------ Juick/Juick.csproj | 14 ++- Juick/MainPage.xaml.cs | 9 +- Juick/ThreadView.xaml.cs | 2 +- Juick/Toolkit.Content/ApplicationBar.Cancel.png | Bin 0 -> 350 bytes Juick/Toolkit.Content/ApplicationBar.Check.png | Bin 0 -> 414 bytes Juick/Toolkit.Content/ApplicationBar.Delete.png | Bin 0 -> 445 bytes Juick/Toolkit.Content/ApplicationBar.Select.png | Bin 0 -> 863 bytes Juick/ViewModels/ViewModelBase.cs | 40 ++++++- Juick/packages.config | 1 + 15 files changed, 258 insertions(+), 115 deletions(-) create mode 100644 Juick/Classes/InvokeDelegateCommandAction.cs delete mode 100644 Juick/Classes/ScrollViewerMonitor.cs create mode 100644 Juick/Toolkit.Content/ApplicationBar.Cancel.png create mode 100644 Juick/Toolkit.Content/ApplicationBar.Check.png create mode 100644 Juick/Toolkit.Content/ApplicationBar.Delete.png create mode 100644 Juick/Toolkit.Content/ApplicationBar.Select.png diff --git a/Juick/App.xaml.cs b/Juick/App.xaml.cs index 194e6fe..e20feec 100644 --- a/Juick/App.xaml.cs +++ b/Juick/App.xaml.cs @@ -11,6 +11,10 @@ namespace Juick { public partial class App : Application { + public void NavigateTo(Uri param) { + var current = ((App)App.Current).RootFrame; + current.Navigate(param); + } private static ViewModelBase _myfeed = null; /// diff --git a/Juick/Classes/DelegateCommand.cs b/Juick/Classes/DelegateCommand.cs index cc7adcd..40256e5 100644 --- a/Juick/Classes/DelegateCommand.cs +++ b/Juick/Classes/DelegateCommand.cs @@ -5,10 +5,10 @@ namespace Juick.Classes { public class DelegateCommand : ICommand { - readonly Action action; + readonly Action action; readonly Func canExecute; - public DelegateCommand(Action execute, Func canExecute) + public DelegateCommand(Action execute, Func canExecute) { this.action = execute; this.canExecute = canExecute; @@ -23,7 +23,7 @@ namespace Juick.Classes public void Execute(object parameter) { - action(); + action(parameter); } public void NotifyCanExecuteChanged() diff --git a/Juick/Classes/InvokeDelegateCommandAction.cs b/Juick/Classes/InvokeDelegateCommandAction.cs new file mode 100644 index 0000000..24861cf --- /dev/null +++ b/Juick/Classes/InvokeDelegateCommandAction.cs @@ -0,0 +1,151 @@ +using System; +using System.Net; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Documents; +using System.Windows.Ink; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Animation; +using System.Windows.Shapes; +using System.Windows.Interactivity; +using System.Reflection; +using System.Linq; + +namespace Juick.Classes +{ + public sealed class InvokeDelegateCommandAction : TriggerAction + { + /// + /// + /// + public static readonly DependencyProperty CommandParameterProperty = + DependencyProperty.Register("CommandParameter", typeof(object), typeof(InvokeDelegateCommandAction), null); + + /// + /// + /// + public static readonly DependencyProperty CommandProperty = DependencyProperty.Register( + "Command", typeof(ICommand), typeof(InvokeDelegateCommandAction), null); + + /// + /// + /// + public static readonly DependencyProperty InvokeParameterProperty = DependencyProperty.Register( + "InvokeParameter", typeof(object), typeof(InvokeDelegateCommandAction), null); + + private string commandName; + + /// + /// + /// + public object InvokeParameter + { + get + { + return this.GetValue(InvokeParameterProperty); + } + set + { + this.SetValue(InvokeParameterProperty, value); + } + } + + /// + /// + /// + public ICommand Command + { + get + { + return (ICommand)this.GetValue(CommandProperty); + } + set + { + this.SetValue(CommandProperty, value); + } + } + + /// + /// + /// + public string CommandName + { + get + { + return this.commandName; + } + set + { + if (this.CommandName != value) + { + this.commandName = value; + } + } + } + + /// + /// + /// + public object CommandParameter + { + get + { + return this.GetValue(CommandParameterProperty); + } + set + { + this.SetValue(CommandParameterProperty, value); + } + } + + /// + /// + /// + /// + protected override void Invoke(object parameter) + { + this.InvokeParameter = parameter; + + if (this.AssociatedObject != null) + { + ICommand command = this.ResolveCommand(); + if ((command != null) && command.CanExecute(this.CommandParameter)) + { + command.Execute(this.CommandParameter); + } + } + } + + private ICommand ResolveCommand() + { + ICommand command = null; + if (this.Command != null) + { + return this.Command; + } + var frameworkElement = this.AssociatedObject as FrameworkElement; + if (frameworkElement != null) + { + object dataContext = frameworkElement.DataContext; + if (dataContext != null) + { + PropertyInfo commandPropertyInfo = dataContext + .GetType() + .GetProperties(BindingFlags.Public | BindingFlags.Instance) + .FirstOrDefault( + p => + typeof(ICommand).IsAssignableFrom(p.PropertyType) && + string.Equals(p.Name, this.CommandName, StringComparison.Ordinal) + ); + + if (commandPropertyInfo != null) + { + command = (ICommand)commandPropertyInfo.GetValue(dataContext, null); + } + } + } + return command; + } + } +} diff --git a/Juick/Classes/LowProfileImageLoader.cs b/Juick/Classes/LowProfileImageLoader.cs index d1eb3da..a69c386 100644 --- a/Juick/Classes/LowProfileImageLoader.cs +++ b/Juick/Classes/LowProfileImageLoader.cs @@ -51,7 +51,7 @@ namespace Juick.Classes [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "UriSource is applicable only to Image elements.")] public static void SetUriSource(Image obj, Uri value) { - if (null == obj) + if (null == obj || null == value) { throw new ArgumentNullException("obj"); } @@ -146,6 +146,8 @@ namespace Juick.Classes pendingRequests[index] = pendingRequests[count - 1]; pendingRequests.RemoveAt(count - 1); count--; + if (pendingRequest.Uri == null) + continue; if (pendingRequest.Uri.IsAbsoluteUri) { // Download from network diff --git a/Juick/Classes/ScrollViewerMonitor.cs b/Juick/Classes/ScrollViewerMonitor.cs deleted file mode 100644 index 4e96797..0000000 --- a/Juick/Classes/ScrollViewerMonitor.cs +++ /dev/null @@ -1,64 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Windows; -using System.Windows.Controls; -using System.Windows.Data; -using System.Windows.Input; -using System.Windows.Media; - -namespace Juick.Classes -{ - public class ScrollViewerMonitor - { - public static DependencyProperty AtEndCommandProperty = - DependencyProperty.RegisterAttached("AtEndCommand", typeof(ICommand), typeof(ScrollViewerMonitor), new PropertyMetadata(OnAtEndCommandChanged)); - - public static ICommand GetAtEndCommand(DependencyObject obj) - { - return (ICommand)obj.GetValue(AtEndCommandProperty); - } - - public static void SetAtEndCommand(DependencyObject obj, ICommand value) - { - obj.SetValue(AtEndCommandProperty, value); - } - - public static void OnAtEndCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) - { - var element = (FrameworkElement)d; - if (element != null) - { - element.Loaded -= element_Loaded; - element.Loaded += element_Loaded; - } - } - - static void element_Loaded(object sender, RoutedEventArgs e) - { - var element = (FrameworkElement)sender; - element.Loaded -= element_Loaded; - var scrollViewer = (ScrollViewer)element.Parent; - if (scrollViewer == null) - { - throw new InvalidOperationException("ScrollViewer not found."); - } - - var listener = new DependencyPropertyListener(); - listener.Changed += (s, eArgs) => - { - var atBottom = scrollViewer.VerticalOffset >= scrollViewer.ScrollableHeight - scrollViewer.ScrollableHeight/3; - if (atBottom) - { - var atEnd = GetAtEndCommand(element); - if (atEnd != null && atEnd.CanExecute(null)) - { - atEnd.Execute(null); - } - } - }; - var binding = new Binding("VerticalOffset") { Source = scrollViewer }; - listener.Attach(scrollViewer, binding); - } - } -} diff --git a/Juick/Controls/MessageList.xaml b/Juick/Controls/MessageList.xaml index 7a5882f..96cd81b 100644 --- a/Juick/Controls/MessageList.xaml +++ b/Juick/Controls/MessageList.xaml @@ -5,8 +5,10 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:bindings="clr-namespace:Juick.Classes" xmlns:usercontrols="clr-namespace:Juick.Controls" - xmlns:converters="clr-namespace:Juick.Converters" + xmlns:converters="clr-namespace:Juick.Converters" + xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" mc:Ignorable="d" + xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" @@ -16,55 +18,57 @@ - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + - - + - - + - - - - - - - - - + + + + + + diff --git a/Juick/Juick.csproj b/Juick/Juick.csproj index 8e47876..173aef8 100644 --- a/Juick/Juick.csproj +++ b/Juick/Juick.csproj @@ -25,6 +25,8 @@ Juick.App true true + ..\ + true true @@ -51,6 +53,9 @@ + + ..\packages\WPtoolkit.4.2012.10.30\lib\sl4-windowsphone71\Microsoft.Phone.Controls.Toolkit.dll + @@ -62,6 +67,7 @@ + @@ -80,8 +86,8 @@ + - @@ -142,6 +148,10 @@ PreserveNewest + + + + @@ -179,6 +189,7 @@ + @@ -191,4 +202,5 @@ --> + \ No newline at end of file diff --git a/Juick/MainPage.xaml.cs b/Juick/MainPage.xaml.cs index d6b0b08..503c023 100644 --- a/Juick/MainPage.xaml.cs +++ b/Juick/MainPage.xaml.cs @@ -110,6 +110,11 @@ namespace Juick { NavigationService.Navigate(new Uri(navigateUri, UriKind.Relative)); } + else + { + ((ViewModels.ViewModelBase)Home.DataContext).LoadData(null); + ((ViewModels.ViewModelBase)Last.DataContext).LoadData(null); + } } void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e) @@ -163,8 +168,8 @@ namespace Juick { App.MyFeedView.Items.Clear(); App.LastView.Items.Clear(); - App.MyFeedView.LoadData(); - App.LastView.LoadData(); + App.MyFeedView.LoadData(null); + App.LastView.LoadData(null); } private void ApplicationBarMenuItemClick(object sender, EventArgs e) diff --git a/Juick/ThreadView.xaml.cs b/Juick/ThreadView.xaml.cs index d17f8f5..be6db02 100644 --- a/Juick/ThreadView.xaml.cs +++ b/Juick/ThreadView.xaml.cs @@ -27,7 +27,7 @@ namespace Juick if (NavigationContext.QueryString.TryGetValue("mid", out _mid)) { Model.Mid = int.Parse(_mid); - Model.LoadData(); + Model.LoadData(null); } } diff --git a/Juick/Toolkit.Content/ApplicationBar.Cancel.png b/Juick/Toolkit.Content/ApplicationBar.Cancel.png new file mode 100644 index 0000000..4dd724f Binary files /dev/null and b/Juick/Toolkit.Content/ApplicationBar.Cancel.png differ diff --git a/Juick/Toolkit.Content/ApplicationBar.Check.png b/Juick/Toolkit.Content/ApplicationBar.Check.png new file mode 100644 index 0000000..7a07466 Binary files /dev/null and b/Juick/Toolkit.Content/ApplicationBar.Check.png differ diff --git a/Juick/Toolkit.Content/ApplicationBar.Delete.png b/Juick/Toolkit.Content/ApplicationBar.Delete.png new file mode 100644 index 0000000..95bb16d Binary files /dev/null and b/Juick/Toolkit.Content/ApplicationBar.Delete.png differ diff --git a/Juick/Toolkit.Content/ApplicationBar.Select.png b/Juick/Toolkit.Content/ApplicationBar.Select.png new file mode 100644 index 0000000..995deaa Binary files /dev/null and b/Juick/Toolkit.Content/ApplicationBar.Select.png differ diff --git a/Juick/ViewModels/ViewModelBase.cs b/Juick/ViewModels/ViewModelBase.cs index 2427a80..80d3d8b 100644 --- a/Juick/ViewModels/ViewModelBase.cs +++ b/Juick/ViewModels/ViewModelBase.cs @@ -9,6 +9,10 @@ using System.Windows.Media.Imaging; using Juick.Classes; using JuickApi; using RestSharp; +using System.Diagnostics; +using System.Windows.Navigation; +using Microsoft.Phone.Controls; +using System.Windows.Controls; namespace Juick.ViewModels { @@ -21,11 +25,14 @@ namespace Juick.ViewModels { Items = new ObservableCollection(); LoadMessagesPageCommand = new DelegateCommand(LoadData, () => !IsDataLoading); + NavigateNextCommand = new DelegateCommand(NavigateToThread, () => true); } public string RestUri { get; set; } public virtual string Caption { get { return "juick"; } } + public string SelectedId { get; set; } + /// /// A collection for MessageViewModel objects. /// @@ -33,6 +40,8 @@ namespace Juick.ViewModels public DelegateCommand LoadMessagesPageCommand { get; private set; } + public DelegateCommand NavigateNextCommand { get; private set; } + public bool IsDataLoading { get { return isDataLoading; } @@ -44,16 +53,36 @@ namespace Juick.ViewModels } } + public void NavigateToThread(object param) + { + var selectionChangedEventArgs = param as SelectionChangedEventArgs; + if (param != null) + { + ((App)App.Current).NavigateTo(new Uri(string.Format("/ThreadView.xaml?mid={0}", ((PostItem)selectionChangedEventArgs.AddedItems[0]).MID), UriKind.Relative)); + } + + } + /// /// Creates and adds a few MessageViewModel objects into the Items collection. /// - public void LoadData() + public void LoadData(object EventArgs) { - if (IsDataLoading) { + const int offset = 5; + if (IsDataLoading) + { return; } + if (EventArgs != null) + { + var item = (EventArgs as LinkUnlinkEventArgs).ContentPresenter.Content; + if (!item.Equals(Items[Items.Count - offset])) { + return; + } + } - const int PageSize = 1; + int count = Items.Count; + if (string.IsNullOrEmpty(RestUri)) { @@ -65,12 +94,12 @@ namespace Juick.ViewModels else if (RestUri.StartsWith("/home?", StringComparison.InvariantCulture) && Items.Count > 0) { var lastItem = Items[Items.Count - 1]; - RestUri = string.Format("/home?before_mid={0}&page={1}", lastItem.MID, PageSize); + RestUri = string.Format("/home?before_mid={0}", lastItem.MID); } else if (RestUri.StartsWith("/messages?", StringComparison.InvariantCulture) && Items.Count > 0) { var lastItem = Items[Items.Count - 1]; - RestUri = string.Format("/messages?before_mid={0}&page={1}", lastItem.MID, PageSize); + RestUri = string.Format("/messages?before_mid={0}", lastItem.MID); } var request = new RestRequest(RestUri + "&rnd=" + Environment.TickCount); @@ -90,7 +119,6 @@ namespace Juick.ViewModels //Items.Clear(); response.Data.Select(x => new PostItem(x)).ToList().ForEach(i => Items.Add(i)); - } diff --git a/Juick/packages.config b/Juick/packages.config index 051648d..73aeefe 100644 --- a/Juick/packages.config +++ b/Juick/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file -- cgit v1.2.3