diff options
Diffstat (limited to 'Juick')
-rw-r--r-- | Juick/App.xaml.cs | 4 | ||||
-rw-r--r-- | Juick/Classes/BindingChangedEventArgs.cs | 15 | ||||
-rw-r--r-- | Juick/Classes/DelegateCommand.cs | 37 | ||||
-rw-r--r-- | Juick/Classes/DependencyPropertyListener.cs | 52 | ||||
-rw-r--r-- | Juick/Classes/ScrollViewerMonitor.cs | 86 | ||||
-rw-r--r-- | Juick/Juick.csproj | 4 | ||||
-rw-r--r-- | Juick/MainPage.xaml | 11 | ||||
-rw-r--r-- | Juick/MainPage.xaml.cs | 10 | ||||
-rw-r--r-- | Juick/ViewModels/MessageListViewModelBase.cs | 126 |
9 files changed, 276 insertions, 69 deletions
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<bool> canExecute; + + public DelegateCommand(Action execute, Func<bool> 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<BindingChangedEventArgs> 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<ScrollViewer>(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<T>(DependencyObject root) + where T : class + { + var queue = new Queue<DependencyObject>(); + 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 @@ <DependentUpon>App.xaml</DependentUpon>
</Compile>
<Compile Include="Classes\AccountManager.cs" />
+ <Compile Include="Classes\BindingChangedEventArgs.cs" />
+ <Compile Include="Classes\DelegateCommand.cs" />
+ <Compile Include="Classes\DependencyPropertyListener.cs" />
<Compile Include="Classes\ParagraphBindingBehavior.cs" />
<Compile Include="Classes\RichTextConverter.cs" />
+ <Compile Include="Classes\ScrollViewerMonitor.cs" />
<Compile Include="LoginView.xaml.cs">
<DependentUpon>LoginView.xaml</DependentUpon>
</Compile>
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 @@ <!--Use 'Orientation="Horizontal"' to enable a panel that lays out horizontally-->
<controls:PanoramaItem x:Name="MainPanoramaItem" Header="My feed" Margin="0, -40, 0, 0">
<!--Double line list with image placeholder and text wrapping-->
- <ListBox x:Name="Home" Margin="0,0,-12,0" ItemsSource="{Binding Items}" SelectionChanged="ListBoxSelectionChanged">
+ <ListBox x:Name="Home"
+ Margin="0, 0, -12, 0"
+ ItemsSource="{Binding Items}"
+ SelectionChanged="ListBoxSelectionChanged"
+ bindings:ScrollViewerMonitor.AtEndCommand="{Binding LoadMessagesPageCommand}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
@@ -81,7 +85,10 @@ </controls:PanoramaItem>
<controls:PanoramaItem x:Name="LastPanoramaItem" Header="Last" Margin="0, -40, 0, 0">
<!--Double line list with image placeholder and text wrapping-->
- <ListBox x:Name="Last" Margin="0,0,-12,0" ItemsSource="{Binding Items}" SelectionChanged="LastBoxSelectionChanged">
+ <ListBox x:Name="Last" Margin="0,0,-12,0"
+ ItemsSource="{Binding Items}"
+ SelectionChanged="LastBoxSelectionChanged"
+ bindings:ScrollViewerMonitor.AtEndCommand="{Binding LoadMessagesPageCommand}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
diff --git a/Juick/MainPage.xaml.cs b/Juick/MainPage.xaml.cs index 97f7739..eba1635 100644 --- a/Juick/MainPage.xaml.cs +++ b/Juick/MainPage.xaml.cs @@ -71,7 +71,6 @@ namespace Juick // Set the data context of the listbox control to the sample data
Home.DataContext = App.MyFeedView;
Last.DataContext = App.LastView;
- Loaded += MainPage_Loaded;
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
@@ -98,15 +97,6 @@ namespace Juick }
}
- // Load data for the _viewModelBase Items
- private void MainPage_Loaded(object sender, RoutedEventArgs e)
- {
- if (!App.MyFeedView.IsDataLoaded)
- {
- App.MyFeedView.LoadData();
- App.LastView.LoadData();
- }
- }
void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
{
diff --git a/Juick/ViewModels/MessageListViewModelBase.cs b/Juick/ViewModels/MessageListViewModelBase.cs index 2cf5157..5dc8aed 100644 --- a/Juick/ViewModels/MessageListViewModelBase.cs +++ b/Juick/ViewModels/MessageListViewModelBase.cs @@ -1,37 +1,45 @@ using System;
-using System.ComponentModel;
using System.Collections.Generic;
-using System.IO;
-using System.Linq;
-using System.Net;
-using System.Runtime.Serialization.Json;
using System.Collections.ObjectModel;
+using System.ComponentModel;
+using System.Net;
using System.Windows;
-using System.Windows.Controls;
using System.Windows.Media.Imaging;
using Juick.Api;
using RestSharp;
+using System.Windows.Input;
+using Juick.Classes;
namespace Juick.ViewModels
{
public class MessageListViewModelBase : INotifyPropertyChanged
{
+ bool isDataLoading;
+
public MessageListViewModelBase()
{
- this.Items = new ObservableCollection<MessageViewModel>();
+ Items = new ObservableCollection<MessageViewModel>();
+ LoadMessagesPageCommand = new DelegateCommand(LoadData, () => !IsDataLoading);
}
public string RestUri { get; set; }
-
+
/// <summary>
/// A collection for MessageViewModel objects.
/// </summary>
public ObservableCollection<MessageViewModel> Items { get; private set; }
-
- public bool IsDataLoaded
+
+ public DelegateCommand LoadMessagesPageCommand { get; private set; }
+
+ public bool IsDataLoading
{
- get;
- private set;
+ get { return isDataLoading; }
+ set
+ {
+ isDataLoading = value;
+ NotifyPropertyChanged("IsDataLoading");
+ LoadMessagesPageCommand.NotifyCanExecuteChanged();
+ }
}
/// <summary>
@@ -39,57 +47,69 @@ namespace Juick.ViewModels /// </summary>
public void LoadData()
{
+ if (IsDataLoading) {
+ return;
+ }
+
+ const int PageSize = 1;
+
if (string.IsNullOrEmpty(RestUri))
{
RestUri = "/home?1=1";
}
- var request = new RestRequest(RestUri +"&rnd=" + Environment.TickCount);
+
+ // super-костыли
+ // todo: rewrite
+ 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);
+ }
+ 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);
+ }
+
+ var request = new RestRequest(RestUri + "&rnd=" + Environment.TickCount);
App.Client.Authenticator = new HttpBasicAuthenticator(App.Account.Credentials.UserName, App.Account.Credentials.Password);
- App.Client.ExecuteAsync<List<Message>>(request, response =>
- {
- if (response.StatusCode != HttpStatusCode.OK)
- {
- MessageBox.Show(response.StatusCode.ToString());
- return;
- }
-
- var messages = response.Data;
- Items.Clear();
- messages.ForEach(post =>
- {
- var item = new MessageViewModel(post)
- {
- Status =
- string.Format(
- "Posted on: {0}, replies: {1}",
- post.Timestamp,
- post.Replies)
- };
- Items.Add(item);
- var imageUri = new Uri(string.Format("http://i.juick.com/as/{0}.png", post.User.Uid), UriKind.Absolute);
- item.UserAvatar = new BitmapImage
- {
- UriSource = imageUri
- };
- if (post.Photo != null)
- {
- item.Attachment = new BitmapImage { UriSource = new Uri(post.Photo.Small, UriKind.Absolute) };
- }
-
- });
- NotifyPropertyChanged("Items");
- });
+ App.Client.ExecuteAsync<List<Message>>(request, ProcessResponse);
+ IsDataLoading = true;
+ }
+
+ void ProcessResponse(IRestResponse<List<Message>> response)
+ {
+ IsDataLoading = false;
+ if (response.StatusCode != HttpStatusCode.OK)
+ {
+ MessageBox.Show(response.StatusCode.ToString());
+ return;
+ }
+
+ //Items.Clear();
+ foreach (var post in response.Data)
+ {
+ var status = string.Format("Posted on: {0}, replies: {1}", post.Timestamp, post.Replies);
+ var item = new MessageViewModel(post) { Status = status };
+ Items.Add(item);
+ var imageUri = new Uri(string.Format("http://i.juick.com/as/{0}.png", post.User.Uid), UriKind.Absolute);
+ item.UserAvatar = new BitmapImage { UriSource = imageUri };
+ if (post.Photo != null)
+ {
+ item.Attachment = new BitmapImage { UriSource = new Uri(post.Photo.Small, UriKind.Absolute) };
+ }
+ }
}
-
+
public event PropertyChangedEventHandler PropertyChanged;
- public void NotifyPropertyChanged(String propertyName)
+
+ public void NotifyPropertyChanged(string propertyName)
{
- PropertyChangedEventHandler handler = PropertyChanged;
- if (null != handler)
+ if (PropertyChanged != null)
{
- handler(this, new PropertyChangedEventArgs(propertyName));
+ PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
-}
\ No newline at end of file +}
|