summaryrefslogtreecommitdiff
path: root/Juick
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2013-04-01 13:34:49 +0400
committerGravatar Vitaly Takmazov2013-04-01 13:34:49 +0400
commitfb1fab2e1593ba13fae8eae519f5482525eb68a1 (patch)
treef3557b2e8991ec53fde46d05346fab2bd8fce37e /Juick
parent57550f203995b61d600fae59832e409fd5feebcd (diff)
AppViewModel
Diffstat (limited to 'Juick')
-rw-r--r--Juick/App.xaml.cs43
-rw-r--r--Juick/Controls/MessageList.xaml1
-rw-r--r--Juick/Juick.csproj4
-rw-r--r--Juick/LoginView.xaml.cs4
-rw-r--r--Juick/MainPage.xaml7
-rw-r--r--Juick/MainPage.xaml.cs28
-rw-r--r--Juick/NewPostView.xaml.cs4
-rw-r--r--Juick/ThreadView.xaml.cs2
-rw-r--r--Juick/ViewModels/AppViewModel.cs67
-rw-r--r--Juick/ViewModels/PageViewModel.cs111
-rw-r--r--Juick/ViewModels/ThreadViewModel.cs7
-rw-r--r--Juick/ViewModels/ViewModelBase.cs122
12 files changed, 231 insertions, 169 deletions
diff --git a/Juick/App.xaml.cs b/Juick/App.xaml.cs
index e20feec..e9fd258 100644
--- a/Juick/App.xaml.cs
+++ b/Juick/App.xaml.cs
@@ -15,46 +15,21 @@ namespace Juick
var current = ((App)App.Current).RootFrame;
current.Navigate(param);
}
- private static ViewModelBase _myfeed = null;
+ private static AppViewModel context = null;
- /// <summary>
- /// A static _myfeed used by the views to bind against.
- /// </summary>
- /// <returns>The MessageListViewModelBase object.</returns>
- public static ViewModelBase MyFeedView
+ public static AppViewModel AppContext
{
get
{
// Delay creation of the view model until necessary
- return _myfeed ?? (_myfeed = new ViewModelBase());
- }
- }
-
- private static ViewModelBase _last;
-
- public static ViewModelBase LastView
- {
- get
- {
- return _last ??
- (_last = new ViewModelBase { RestUri = "/messages?1=1" });
+ if (context == null) {
+ context = new AppViewModel();
+ context.Pages.Add(new PageViewModel(context));
+ context.Pages.Add(new PageViewModel(context) { RestUri = "/messages?1=1" });
+ };
+ return context;
}
- }
-
- private static AccountManager _acc;
-
- public static AccountManager Account
- {
- get { return _acc ?? (_acc = new AccountManager()); }
- }
-
- 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 }); }
- }
-
+ }
/// <summary>
/// Provides easy access to the root frame of the Phone Application.
/// </summary>
diff --git a/Juick/Controls/MessageList.xaml b/Juick/Controls/MessageList.xaml
index 76b90ce..7a7d823 100644
--- a/Juick/Controls/MessageList.xaml
+++ b/Juick/Controls/MessageList.xaml
@@ -5,7 +5,6 @@
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: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"
diff --git a/Juick/Juick.csproj b/Juick/Juick.csproj
index ebf65ba..ebe128c 100644
--- a/Juick/Juick.csproj
+++ b/Juick/Juick.csproj
@@ -102,9 +102,11 @@
<Compile Include="ThreadView.xaml.cs">
<DependentUpon>ThreadView.xaml</DependentUpon>
</Compile>
- <Compile Include="ViewModels\ViewModelBase.cs" />
+ <Compile Include="ViewModels\AppViewModel.cs" />
+ <Compile Include="ViewModels\PageViewModel.cs" />
<Compile Include="ViewModels\PostItem.cs" />
<Compile Include="ViewModels\ThreadViewModel.cs" />
+ <Compile Include="ViewModels\ViewModelBase.cs" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml">
diff --git a/Juick/LoginView.xaml.cs b/Juick/LoginView.xaml.cs
index 4d6114e..098275b 100644
--- a/Juick/LoginView.xaml.cs
+++ b/Juick/LoginView.xaml.cs
@@ -10,7 +10,7 @@ namespace Juick
public LoginView()
{
InitializeComponent();
- DataContext = App.Account.Credentials;
+ DataContext = App.AppContext.Account.Credentials;
}
private Uri nextUri;
@@ -24,7 +24,7 @@ namespace Juick
private void button1_Click(object sender, RoutedEventArgs e)
{
- App.Account.SignIn(this, textBox1.Text, textBox2.Password, nextUri);
+ App.AppContext.Account.SignIn(this, textBox1.Text, textBox2.Password, nextUri);
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
diff --git a/Juick/MainPage.xaml b/Juick/MainPage.xaml
index 8340a9f..c6d6a3a 100644
--- a/Juick/MainPage.xaml
+++ b/Juick/MainPage.xaml
@@ -7,6 +7,7 @@
xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+ xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:bindings="clr-namespace:Juick.Classes"
xmlns:usercontrols="clr-namespace:Juick.Controls"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="696"
@@ -29,7 +30,11 @@
</phoneshell:SystemTray.ProgressIndicator>
<controls:Pivot Title="juick" Grid.Row="1">
-
+ <i:Interaction.Triggers>
+ <i:EventTrigger EventName="SelectionChanged">
+
+ </i:EventTrigger>
+ </i:Interaction.Triggers>
<controls:PivotItem Header="My feed" Margin="0, -5, 0, 0">
<usercontrols:MessageList x:Name="Home" />
</controls:PivotItem>
diff --git a/Juick/MainPage.xaml.cs b/Juick/MainPage.xaml.cs
index 503c023..4861114 100644
--- a/Juick/MainPage.xaml.cs
+++ b/Juick/MainPage.xaml.cs
@@ -7,6 +7,7 @@ using System.Windows.Data;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Notification;
using Microsoft.Phone.Shell;
+using Juick.ViewModels;
namespace Juick
{
@@ -61,8 +62,9 @@ namespace Juick
}*/
// Set the data context of the listbox control to the sample data
- Home.DataContext = App.MyFeedView;
- Last.DataContext = App.LastView;
+ DataContext = App.AppContext;
+ Home.DataContext = ((AppViewModel)DataContext).Pages[0];
+ Last.DataContext = ((AppViewModel)DataContext).Pages[1];
Loaded += (o, args) =>
{
var progressIndicator = SystemTray.ProgressIndicator;
@@ -76,12 +78,12 @@ namespace Juick
SystemTray.SetProgressIndicator(this, progressIndicator);
- Binding binding = new Binding("IsDataLoading") { Source = Home.DataContext };
+ Binding binding = new Binding("IsDataLoading") { Source = DataContext };
BindingOperations.SetBinding(
progressIndicator, ProgressIndicator.IsVisibleProperty, binding);
- binding = new Binding("IsDataLoading") { Source = Home.DataContext };
+ binding = new Binding("IsDataLoading") { Source = DataContext };
BindingOperations.SetBinding(
progressIndicator, ProgressIndicator.IsIndeterminateProperty, binding);
@@ -100,7 +102,7 @@ namespace Juick
// Ensure that there is at least one key in the query string, and check
// whether the "FileId" key is present.
- navigateUri = string.IsNullOrEmpty(App.Account.Credentials.UserName) ? loginUriPart : newPostUriPart;
+ navigateUri = string.IsNullOrEmpty(App.AppContext.Account.Credentials.UserName) ? loginUriPart : newPostUriPart;
if (queryStrings.ContainsKey("FileId"))
{
FileId = queryStrings["FileId"];
@@ -112,8 +114,10 @@ namespace Juick
}
else
{
- ((ViewModels.ViewModelBase)Home.DataContext).LoadData(null);
- ((ViewModels.ViewModelBase)Last.DataContext).LoadData(null);
+ if (((ViewModels.PageViewModel)Home.DataContext).Items.Count == 0)
+ ((ViewModels.PageViewModel)Home.DataContext).LoadData(null);
+ if (((ViewModels.PageViewModel)Last.DataContext).Items.Count == 0)
+ ((ViewModels.PageViewModel)Last.DataContext).LoadData(null);
}
}
@@ -166,15 +170,15 @@ namespace Juick
private void ApplicationBarIconButtonClick(object sender, EventArgs e)
{
- App.MyFeedView.Items.Clear();
- App.LastView.Items.Clear();
- App.MyFeedView.LoadData(null);
- App.LastView.LoadData(null);
+ App.AppContext.Pages[0].Items.Clear();
+ App.AppContext.Pages[1].Items.Clear();
+ App.AppContext.Pages[0].LoadData(null);
+ App.AppContext.Pages[1].LoadData(null);
}
private void ApplicationBarMenuItemClick(object sender, EventArgs e)
{
- App.Account.SignOut(this);
+ App.AppContext.Account.SignOut(this);
}
private void ApplicationBarIconButtonClick1(object sender, EventArgs e)
diff --git a/Juick/NewPostView.xaml.cs b/Juick/NewPostView.xaml.cs
index 8ad2722..4f3cc06 100644
--- a/Juick/NewPostView.xaml.cs
+++ b/Juick/NewPostView.xaml.cs
@@ -43,7 +43,7 @@ namespace Juick
// whether the "FileId" key is present.
if (queryStrings.ContainsKey("FileId"))
{
- App.Client.Authenticator = new HttpBasicAuthenticator(App.Account.Credentials.UserName, App.Account.Credentials.Password);
+ App.AppContext.Client.Authenticator = new HttpBasicAuthenticator(App.AppContext.Account.Credentials.UserName, App.AppContext.Account.Credentials.Password);
// Retrieve the picture from the media library using the FileID
// passed to the application.
MediaLibrary library = new MediaLibrary();
@@ -80,7 +80,7 @@ namespace Juick
request.AddFile("attach", ms.ToArray(), "file.jpg");
}
}
- App.Client.ExecuteAsync(request, response =>
+ App.AppContext.Client.ExecuteAsync(request, response =>
{
if (response.StatusCode != HttpStatusCode.OK)
{
diff --git a/Juick/ThreadView.xaml.cs b/Juick/ThreadView.xaml.cs
index be6db02..50ea979 100644
--- a/Juick/ThreadView.xaml.cs
+++ b/Juick/ThreadView.xaml.cs
@@ -12,7 +12,7 @@ namespace Juick
public ThreadView()
{
InitializeComponent();
- Model = new ThreadViewModel();
+ Model = new ThreadViewModel(App.AppContext);
DataContext = Model;
}
diff --git a/Juick/ViewModels/AppViewModel.cs b/Juick/ViewModels/AppViewModel.cs
new file mode 100644
index 0000000..5d22cab
--- /dev/null
+++ b/Juick/ViewModels/AppViewModel.cs
@@ -0,0 +1,67 @@
+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.Collections.ObjectModel;
+using Juick.Classes;
+using RestSharp;
+
+namespace Juick.ViewModels
+{
+ public class AppViewModel : ViewModelBase
+ {
+ private ObservableCollection<PageViewModel> _pages;
+ public ObservableCollection<PageViewModel> Pages
+ {
+ get
+ {
+ if (_pages == null)
+ _pages = new ObservableCollection<PageViewModel>();
+ return _pages;
+ }
+ }
+
+ static readonly string IsDataLoadingPropertyName = ExpressionHelper.GetPropertyName<AppViewModel>(x => x.IsDataLoading);
+ bool isDataLoading;
+
+ public bool IsDataLoading
+ {
+ get { return isDataLoading; }
+ set
+ {
+ isDataLoading = value;
+ NotifyPropertyChanged(IsDataLoadingPropertyName);
+ foreach (var page in Pages)
+ {
+ page.LoadMessagesPageCommand.NotifyCanExecuteChanged();
+ }
+ }
+ }
+
+ private AccountManager _acc;
+
+ public AccountManager Account
+ {
+ get { return _acc ?? (_acc = new AccountManager()); }
+ }
+
+ private RestClient _cl;
+ public RestClient Client
+ {
+ get
+ {
+ return _cl ?? (_cl = new RestClient("http://api.juick.com")
+ {
+ UserAgent = "Juick 1.1/Windows Phone " + Environment.OSVersion.Version
+ });
+ }
+ }
+
+ }
+}
diff --git a/Juick/ViewModels/PageViewModel.cs b/Juick/ViewModels/PageViewModel.cs
new file mode 100644
index 0000000..9b9cae5
--- /dev/null
+++ b/Juick/ViewModels/PageViewModel.cs
@@ -0,0 +1,111 @@
+using System;
+using System.Linq;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.ComponentModel;
+using System.Net;
+using System.Windows;
+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
+{
+ public class PageViewModel : ViewModelBase
+ {
+ public PageViewModel(AppViewModel context)
+ {
+ this.context = context;
+ Items = new ObservableCollection<PostItem>();
+ LoadMessagesPageCommand = new DelegateCommand(LoadData, () => !context.IsDataLoading);
+ NavigateNextCommand = new DelegateCommand(NavigateToThread, () => true);
+ }
+
+ private AppViewModel context;
+
+ public string RestUri { get; set; }
+ public virtual string Caption { get { return "juick"; } }
+
+ /// <summary>
+ /// A collection for MessageViewModel objects.
+ /// </summary>
+ public ObservableCollection<PostItem> Items { get; private set; }
+
+ public DelegateCommand LoadMessagesPageCommand { get; private set; }
+
+ public DelegateCommand NavigateNextCommand { get; private set; }
+
+ 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));
+ }
+
+ }
+
+ /// <summary>
+ /// Creates and adds a few MessageViewModel objects into the Items collection.
+ /// </summary>
+ public void LoadData(object EventArgs)
+ {
+ const int offset = 5;
+ if (context.IsDataLoading)
+ {
+ return;
+ }
+ if (EventArgs != null)
+ {
+ var item = (EventArgs as LinkUnlinkEventArgs).ContentPresenter.Content;
+ if (!item.Equals(Items[Items.Count - offset])) {
+ return;
+ }
+ }
+
+ int count = Items.Count;
+
+
+ if (string.IsNullOrEmpty(RestUri))
+ {
+ RestUri = "/home?1=1";
+ }
+
+ // 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}", 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}", lastItem.MID);
+ }
+
+ var request = new RestRequest(RestUri + "&rnd=" + Environment.TickCount);
+ context.Client.Authenticator = new HttpBasicAuthenticator(context.Account.Credentials.UserName, context.Account.Credentials.Password);
+ context.Client.ExecuteAsync<List<Message>>(request, ProcessResponse);
+ context.IsDataLoading = true;
+ }
+
+ void ProcessResponse(IRestResponse<List<Message>> response)
+ {
+ context.IsDataLoading = false;
+ if (response.StatusCode != HttpStatusCode.OK)
+ {
+ MessageBox.Show(response.StatusCode.ToString());
+ return;
+ }
+
+ //Items.Clear();
+ response.Data.Select(x => new PostItem(x)).ToList().ForEach(i => Items.Add(i));
+ }
+ }
+}
diff --git a/Juick/ViewModels/ThreadViewModel.cs b/Juick/ViewModels/ThreadViewModel.cs
index 1cbaab7..c004851 100644
--- a/Juick/ViewModels/ThreadViewModel.cs
+++ b/Juick/ViewModels/ThreadViewModel.cs
@@ -2,8 +2,13 @@
namespace Juick.ViewModels
{
- public class ThreadViewModel : ViewModelBase
+ public class ThreadViewModel : PageViewModel
{
+ public ThreadViewModel(AppViewModel context) : base(context)
+ {
+
+ }
+
static readonly string CaptionPropertyName = ExpressionHelper.GetPropertyName<ThreadViewModel>(x => x.Caption);
private int _mid;
diff --git a/Juick/ViewModels/ViewModelBase.cs b/Juick/ViewModels/ViewModelBase.cs
index 746ce04..9ac3023 100644
--- a/Juick/ViewModels/ViewModelBase.cs
+++ b/Juick/ViewModels/ViewModelBase.cs
@@ -1,127 +1,21 @@
using System;
-using System.Linq;
-using System.Collections.Generic;
-using System.Collections.ObjectModel;
-using System.ComponentModel;
using System.Net;
using System.Windows;
-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;
+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.ComponentModel;
+using Juick.Classes;
namespace Juick.ViewModels
{
public class ViewModelBase : INotifyPropertyChanged
{
- static readonly string IsDataLoadingPropertyName = ExpressionHelper.GetPropertyName<ViewModelBase>(x => x.IsDataLoading);
- bool isDataLoading;
-
- public ViewModelBase()
- {
- Items = new ObservableCollection<PostItem>();
- LoadMessagesPageCommand = new DelegateCommand(LoadData, () => !IsDataLoading);
- NavigateNextCommand = new DelegateCommand(NavigateToThread, () => true);
- }
-
- public string RestUri { get; set; }
- public virtual string Caption { get { return "juick"; } }
-
- /// <summary>
- /// A collection for MessageViewModel objects.
- /// </summary>
- public ObservableCollection<PostItem> Items { get; private set; }
-
- public DelegateCommand LoadMessagesPageCommand { get; private set; }
-
- public DelegateCommand NavigateNextCommand { get; private set; }
-
- public bool IsDataLoading
- {
- get { return isDataLoading; }
- set
- {
- isDataLoading = value;
- NotifyPropertyChanged(IsDataLoadingPropertyName);
- LoadMessagesPageCommand.NotifyCanExecuteChanged();
- }
- }
-
- 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));
- }
-
- }
-
- /// <summary>
- /// Creates and adds a few MessageViewModel objects into the Items collection.
- /// </summary>
- public void LoadData(object EventArgs)
- {
- 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;
- }
- }
-
- int count = Items.Count;
-
-
- if (string.IsNullOrEmpty(RestUri))
- {
- RestUri = "/home?1=1";
- }
-
- // 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}", 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}", lastItem.MID);
- }
-
- 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, ProcessResponse);
- IsDataLoading = true;
- }
-
- void ProcessResponse(IRestResponse<List<Message>> response)
- {
- IsDataLoading = false;
- if (response.StatusCode != HttpStatusCode.OK)
- {
- MessageBox.Show(response.StatusCode.ToString());
- return;
- }
-
- //Items.Clear();
- response.Data.Select(x => new PostItem(x)).ToList().ForEach(i => Items.Add(i));
- }
-
-
public event PropertyChangedEventHandler PropertyChanged;
-
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)