summaryrefslogtreecommitdiff
path: root/Juick
diff options
context:
space:
mode:
Diffstat (limited to 'Juick')
-rw-r--r--Juick/App.xaml.cs12
-rw-r--r--Juick/Classes/AccountManager.cs23
-rw-r--r--Juick/Classes/BindingChangedEventArgs.cs15
-rw-r--r--Juick/Classes/DelegateCommand.cs37
-rw-r--r--Juick/Classes/DependencyPropertyListener.cs52
-rw-r--r--Juick/Classes/ScrollViewerMonitor.cs86
-rw-r--r--Juick/Juick.csproj4
-rw-r--r--Juick/LoginView.xaml.cs5
-rw-r--r--Juick/MainPage.xaml11
-rw-r--r--Juick/MainPage.xaml.cs14
-rw-r--r--Juick/NewPostView.xaml.cs2
-rw-r--r--Juick/ViewModels/MessageListViewModelBase.cs126
12 files changed, 291 insertions, 96 deletions
diff --git a/Juick/App.xaml.cs b/Juick/App.xaml.cs
index 1102a91..9d3ac9a 100644
--- a/Juick/App.xaml.cs
+++ b/Juick/App.xaml.cs
@@ -33,7 +33,7 @@ namespace Juick
get
{
return _last ??
- (_last = new MessageListViewModelBase {RestUri = "/messages?1=1"});
+ (_last = new MessageListViewModelBase { RestUri = "/messages?1=1" });
}
}
@@ -47,9 +47,9 @@ namespace Juick
private static RestClient _cl;
public static RestClient Client
{
- get { return _cl ?? (_cl = new RestClient("http://api.juick.com") {UserAgent = "Juick 0.999/Windows Phone " + Environment.OSVersion.Version}); }
+ get { return _cl ?? (_cl = new RestClient("http://api.juick.com") { UserAgent = "Juick 0.999/Windows Phone " + Environment.OSVersion.Version }); }
}
-
+
/// <summary>
/// Provides easy access to the root frame of the Phone Application.
/// </summary>
@@ -96,7 +96,7 @@ namespace Juick
// This code will not execute when the application is reactivated
private void Application_Launching(object sender, LaunchingEventArgs e)
{
-
+
}
// Code to execute when the application is activated (brought to foreground)
@@ -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/AccountManager.cs b/Juick/Classes/AccountManager.cs
index 82fc446..8f8e366 100644
--- a/Juick/Classes/AccountManager.cs
+++ b/Juick/Classes/AccountManager.cs
@@ -1,20 +1,7 @@
using System;
-using System.IO;
using System.IO.IsolatedStorage;
-using System.Linq;
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.Navigation;
-using System.Windows.Shapes;
-using System.Windows.Threading;
-using System.Xml;
-using System.Xml.Serialization;
namespace Juick.Classes
{
@@ -25,12 +12,14 @@ namespace Juick.Classes
{
get
{
+ string userName;
+ string password;
+ IsolatedStorageSettings.ApplicationSettings.TryGetValue<string>("user", out userName);
+ IsolatedStorageSettings.ApplicationSettings.TryGetValue<string>("password", out password);
return new NetworkCredential
{
- UserName = IsolatedStorageSettings.ApplicationSettings.Contains("user") ?
- IsolatedStorageSettings.ApplicationSettings["user"] as string : null,
- Password = IsolatedStorageSettings.ApplicationSettings.Contains("password") ?
- IsolatedStorageSettings.ApplicationSettings["password"] as string : null,
+ UserName = userName,
+ Password = password,
};
}
set
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/LoginView.xaml.cs b/Juick/LoginView.xaml.cs
index 5e86959..4d6114e 100644
--- a/Juick/LoginView.xaml.cs
+++ b/Juick/LoginView.xaml.cs
@@ -17,8 +17,8 @@ namespace Juick
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
- nextUri = NavigationContext.QueryString.ContainsKey("FileId") ?
- new Uri(string.Format("/NewPostView.xaml?FileId={0}", NavigationContext.QueryString["FileId"]), UriKind.Relative)
+ nextUri = NavigationContext.QueryString.ContainsKey("FileId")
+ ? new Uri(string.Format("/NewPostView.xaml?FileId={0}", NavigationContext.QueryString["FileId"]), UriKind.Relative)
: new Uri("/MainPage.xaml", UriKind.Relative);
}
@@ -26,6 +26,7 @@ namespace Juick
{
App.Account.SignIn(this, textBox1.Text, textBox2.Password, nextUri);
}
+
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
while (NavigationService.BackStack.Any())
diff --git a/Juick/MainPage.xaml b/Juick/MainPage.xaml
index bdaff7f..a713a39 100644
--- a/Juick/MainPage.xaml
+++ b/Juick/MainPage.xaml
@@ -42,7 +42,11 @@
<controls:PivotItem Header="My feed" Margin="0, -5, 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>
@@ -95,7 +99,10 @@
<!--<controls:PanoramaItem x:Name="LastPanoramaItem" Header="Last" Margin="0, -40, 0, 0">-->
<controls:PivotItem Header="Last" Margin="0, -5, 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 783265e..eba1635 100644
--- a/Juick/MainPage.xaml.cs
+++ b/Juick/MainPage.xaml.cs
@@ -24,10 +24,10 @@ namespace Juick
public MainPage()
{
// Holds the push channel that is created or found.
- HttpNotificationChannel pushChannel;
+ //HttpNotificationChannel pushChannel; // unused variable
// The name of our push channel.
- string channelName = "JuickChannel";
+ //string channelName = "JuickChannel"; // unused variable
InitializeComponent();
@@ -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/NewPostView.xaml.cs b/Juick/NewPostView.xaml.cs
index e883597..8ad2722 100644
--- a/Juick/NewPostView.xaml.cs
+++ b/Juick/NewPostView.xaml.cs
@@ -79,8 +79,6 @@ namespace Juick
wb.SaveJpeg(ms, _attachedPhoto.PixelWidth, _attachedPhoto.PixelHeight, 0, 100);
request.AddFile("attach", ms.ToArray(), "file.jpg");
}
-
-
}
App.Client.ExecuteAsync(request, response =>
{
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
+}