From fb1fab2e1593ba13fae8eae519f5482525eb68a1 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 1 Apr 2013 13:34:49 +0400 Subject: AppViewModel --- Juick/App.xaml.cs | 43 +++---------- Juick/Controls/MessageList.xaml | 1 - Juick/Juick.csproj | 4 +- Juick/LoginView.xaml.cs | 4 +- Juick/MainPage.xaml | 7 ++- Juick/MainPage.xaml.cs | 28 +++++---- Juick/NewPostView.xaml.cs | 4 +- Juick/ThreadView.xaml.cs | 2 +- Juick/ViewModels/AppViewModel.cs | 67 ++++++++++++++++++++ Juick/ViewModels/PageViewModel.cs | 111 ++++++++++++++++++++++++++++++++ Juick/ViewModels/ThreadViewModel.cs | 7 ++- Juick/ViewModels/ViewModelBase.cs | 122 +++--------------------------------- 12 files changed, 231 insertions(+), 169 deletions(-) create mode 100644 Juick/ViewModels/AppViewModel.cs create mode 100644 Juick/ViewModels/PageViewModel.cs 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; - /// - /// A static _myfeed used by the views to bind against. - /// - /// The MessageListViewModelBase object. - 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 }); } - } - + } /// /// Provides easy access to the root frame of the Phone Application. /// 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 @@ ThreadView.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 @@ - + + + + + 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 _pages; + public ObservableCollection Pages + { + get + { + if (_pages == null) + _pages = new ObservableCollection(); + return _pages; + } + } + + static readonly string IsDataLoadingPropertyName = ExpressionHelper.GetPropertyName(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(); + 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"; } } + + /// + /// A collection for MessageViewModel objects. + /// + public ObservableCollection 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)); + } + + } + + /// + /// Creates and adds a few MessageViewModel objects into the Items collection. + /// + 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>(request, ProcessResponse); + context.IsDataLoading = true; + } + + void ProcessResponse(IRestResponse> 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(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(x => x.IsDataLoading); - bool isDataLoading; - - public ViewModelBase() - { - 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"; } } - - /// - /// A collection for MessageViewModel objects. - /// - public ObservableCollection 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)); - } - - } - - /// - /// Creates and adds a few MessageViewModel objects into the Items collection. - /// - 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>(request, ProcessResponse); - IsDataLoading = true; - } - - void ProcessResponse(IRestResponse> 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) -- cgit v1.2.3