From 8316abc300d56f876a5fe3b511dc202556ad776c Mon Sep 17 00:00:00 2001 From: Konstantin Date: Wed, 24 Oct 2012 23:38:06 +0400 Subject: loading data on scrolling at bottom --- Juick/App.xaml.cs | 4 - Juick/Classes/BindingChangedEventArgs.cs | 15 ++++ Juick/Classes/DelegateCommand.cs | 37 ++++++++ Juick/Classes/DependencyPropertyListener.cs | 52 +++++++++++ Juick/Classes/ScrollViewerMonitor.cs | 86 ++++++++++++++++++ Juick/Juick.csproj | 4 + Juick/MainPage.xaml | 11 ++- Juick/MainPage.xaml.cs | 10 --- Juick/ViewModels/MessageListViewModelBase.cs | 126 ++++++++++++++++----------- 9 files changed, 276 insertions(+), 69 deletions(-) create mode 100644 Juick/Classes/BindingChangedEventArgs.cs create mode 100644 Juick/Classes/DelegateCommand.cs create mode 100644 Juick/Classes/DependencyPropertyListener.cs create mode 100644 Juick/Classes/ScrollViewerMonitor.cs diff --git a/Juick/App.xaml.cs b/Juick/App.xaml.cs index e32ae5e..9d3ac9a 100644 --- a/Juick/App.xaml.cs +++ b/Juick/App.xaml.cs @@ -104,10 +104,6 @@ namespace Juick private void Application_Activated(object sender, ActivatedEventArgs e) { // Ensure that application state is restored appropriately - if (!App.MyFeedView.IsDataLoaded) - { - App.MyFeedView.LoadData(); - } } // Code to execute when the application is deactivated (sent to background) diff --git a/Juick/Classes/BindingChangedEventArgs.cs b/Juick/Classes/BindingChangedEventArgs.cs new file mode 100644 index 0000000..3b68751 --- /dev/null +++ b/Juick/Classes/BindingChangedEventArgs.cs @@ -0,0 +1,15 @@ +using System; +using System.Windows; + +namespace Juick.Classes +{ + public class BindingChangedEventArgs : EventArgs + { + public DependencyPropertyChangedEventArgs EventArgs { get; private set; } + + public BindingChangedEventArgs(DependencyPropertyChangedEventArgs e) + { + EventArgs = e; + } + } +} diff --git a/Juick/Classes/DelegateCommand.cs b/Juick/Classes/DelegateCommand.cs new file mode 100644 index 0000000..cc7adcd --- /dev/null +++ b/Juick/Classes/DelegateCommand.cs @@ -0,0 +1,37 @@ +using System; +using System.Windows.Input; + +namespace Juick.Classes +{ + public class DelegateCommand : ICommand + { + readonly Action action; + readonly Func canExecute; + + public DelegateCommand(Action execute, Func canExecute) + { + this.action = execute; + this.canExecute = canExecute; + } + + public bool CanExecute(object parameter) + { + return canExecute(); + } + + public event EventHandler CanExecuteChanged; + + public void Execute(object parameter) + { + action(); + } + + public void NotifyCanExecuteChanged() + { + if (CanExecuteChanged != null) + { + CanExecuteChanged(this, EventArgs.Empty); + } + } + } +} diff --git a/Juick/Classes/DependencyPropertyListener.cs b/Juick/Classes/DependencyPropertyListener.cs new file mode 100644 index 0000000..693c16a --- /dev/null +++ b/Juick/Classes/DependencyPropertyListener.cs @@ -0,0 +1,52 @@ +using System; +using System.Windows; +using System.Windows.Data; + +namespace Juick.Classes +{ + public class DependencyPropertyListener + { + static int index; + + readonly DependencyProperty property; + FrameworkElement target; + public event EventHandler Changed; + + public DependencyPropertyListener() + { + property = DependencyProperty.RegisterAttached( + "DependencyPropertyListener" + DependencyPropertyListener.index++, + typeof(object), + typeof(DependencyPropertyListener), + new PropertyMetadata(null, new PropertyChangedCallback(HandleValueChanged))); + } + + public void Attach(FrameworkElement element, Binding binding) + { + if (element == null) + { + throw new ArgumentNullException("element"); + } + if (target != null) + { + throw new InvalidOperationException("Cannot attach an already attached listener"); + } + target = element; + target.SetBinding(property, binding); + } + + public void Detach() + { + target.ClearValue(property); + target = null; + } + + void HandleValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) + { + if (Changed != null) + { + Changed.Invoke(this, new BindingChangedEventArgs(e)); + } + } + } +} diff --git a/Juick/Classes/ScrollViewerMonitor.cs b/Juick/Classes/ScrollViewerMonitor.cs new file mode 100644 index 0000000..6a65773 --- /dev/null +++ b/Juick/Classes/ScrollViewerMonitor.cs @@ -0,0 +1,86 @@ +using System; +using System.Collections.Generic; +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 = FindChildOfType(element); + if (scrollViewer == null) + { + throw new InvalidOperationException("ScrollViewer not found."); + } + + var listener = new DependencyPropertyListener(); + listener.Changed += (s, eArgs) => + { + var atBottom = scrollViewer.VerticalOffset >= scrollViewer.ScrollableHeight; + 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); + } + + static T FindChildOfType(DependencyObject root) + where T : class + { + var queue = new Queue(); + queue.Enqueue(root); + + while (queue.Count > 0) + { + var current = queue.Dequeue(); + for (int i = VisualTreeHelper.GetChildrenCount(current) - 1; 0 <= i; i--) + { + var child = VisualTreeHelper.GetChild(current, i); + var typedChild = child as T; + if (typedChild != null) + { + return typedChild; + } + queue.Enqueue(child); + } + } + return null; + } + } +} diff --git a/Juick/Juick.csproj b/Juick/Juick.csproj index 01b0392..ec24b06 100644 --- a/Juick/Juick.csproj +++ b/Juick/Juick.csproj @@ -73,8 +73,12 @@ App.xaml + + + + LoginView.xaml diff --git a/Juick/MainPage.xaml b/Juick/MainPage.xaml index 4b0c537..bb7736b 100644 --- a/Juick/MainPage.xaml +++ b/Juick/MainPage.xaml @@ -31,7 +31,11 @@ - +