Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactored a couple of entity classes out of MainPage.xaml.cs; took c…

…are of a disposable resource. Other miscellaneous refactorings.
  • Loading branch information...
commit 0af0a3cb56431d4a981aa90796841973de22d1ad 1 parent bf6784f
@Aaronontheweb authored
View
43 Hacker News/Entities/Article.cs
@@ -0,0 +1,43 @@
+using System;
+
+namespace Hacker_News.Entities
+{
+ public class Article
+ {
+ Uri urlValue = null;
+ public string title { get; set; }
+ public string urlDomainOnly
+ {
+ get
+ {
+ if (this.urlValue == null) return string.Empty;
+
+ var hostParts = this.urlValue.Host.Split('.');
+ var len = hostParts.Length;
+
+ if (len > 1)
+ {
+ return hostParts[len - 2] + "." + hostParts[len - 1];
+ }
+ else
+ {
+ return this.urlValue.Host;
+ }
+ }
+ }
+
+
+ public string url
+ {
+ get { return this.urlValue.ToString(); }
+ set { this.urlValue = new Uri(value); }
+ }
+
+
+ public int id { get; set; }
+ public int commentCount { get; set; }
+ public int points { get; set; }
+ public string postedAgo { get; set; }
+ public string postedBy { get; set; }
+ }
+}
View
41 Hacker News/Entities/News.cs
@@ -0,0 +1,41 @@
+using System;
+using System.Collections.ObjectModel;
+using System.ComponentModel;
+
+namespace Hacker_News.Entities
+{
+ public class News : INotifyPropertyChanged
+ {
+ private string nextIdValue = String.Empty;
+ private string versionValue = String.Empty;
+ private ObservableCollection<Article> itemsValue = new ObservableCollection<Article>();
+
+ #region I don't really understand what's going on here ...
+ public event PropertyChangedEventHandler PropertyChanged;
+ private void NotifyPropertyChanged(String info)
+ {
+ if (PropertyChanged != null)
+ {
+ PropertyChanged(this, new PropertyChangedEventArgs(info));
+ }
+ }
+ #endregion
+
+ public string nextId
+ {
+ get { return this.nextIdValue; }
+ set { this.nextIdValue = value; NotifyPropertyChanged("nextId"); }
+ }
+ public string version
+ {
+ get { return this.versionValue; }
+ set { this.versionValue = value; NotifyPropertyChanged("version"); }
+ }
+
+ public ObservableCollection<Article> items
+ {
+ get { return this.itemsValue; }
+ set { this.itemsValue = value; NotifyPropertyChanged("items"); }
+ }
+ }
+}
View
6 Hacker News/Hacker News.csproj
@@ -54,7 +54,9 @@
<Reference Include="Microsoft.Phone" />
<Reference Include="Microsoft.Phone.Controls, Version=7.0.0.0, Culture=neutral, PublicKeyToken=24eec0d8c86cda1e, processorArchitecture=MSIL" />
<Reference Include="Microsoft.Phone.Interop" />
- <Reference Include="Newtonsoft.Json.Silverlight, Version=3.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL" />
+ <Reference Include="Newtonsoft.Json.Silverlight, Version=3.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
+ <HintPath>Libraries\Json.NET\Newtonsoft.Json.Silverlight.dll</HintPath>
+ </Reference>
<Reference Include="System.Windows" />
<Reference Include="system" />
<Reference Include="System.Core" />
@@ -66,6 +68,7 @@
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
</Compile>
+ <Compile Include="Entities\Article.cs" />
<Compile Include="Common.cs" />
<Compile Include="MainPage.xaml.cs">
<DependentUpon>MainPage.xaml</DependentUpon>
@@ -73,6 +76,7 @@
<Compile Include="Browser.xaml.cs">
<DependentUpon>Browser.xaml</DependentUpon>
</Compile>
+ <Compile Include="Entities\News.cs" />
<Compile Include="Post.xaml.cs">
<DependentUpon>Post.xaml</DependentUpon>
</Compile>
View
1  Hacker News/Hacker News.csproj.user
@@ -2,5 +2,6 @@
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<FullDeploy>false</FullDeploy>
+ <ProjectView>ShowAllFiles</ProjectView>
</PropertyGroup>
</Project>
View
114 Hacker News/MainPage.xaml.cs
@@ -9,91 +9,23 @@
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
+using Hacker_News.Entities;
using Microsoft.Phone.Controls;
using Newtonsoft.Json;
using System.Runtime.Serialization;
using System.IO;
using System.Text;
-using System.Collections.ObjectModel;
-using System.ComponentModel;
using Microsoft.Phone.Tasks;
namespace Hacker_News
{
- public class News : INotifyPropertyChanged
- {
- private string nextIdValue = String.Empty;
- private string versionValue = String.Empty;
- private ObservableCollection<Article> itemsValue = new ObservableCollection<Article>();
-
- #region I don't really understand what's going on here ...
- public event PropertyChangedEventHandler PropertyChanged;
- private void NotifyPropertyChanged(String info)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(info));
- }
- }
- #endregion
-
- public string nextId
- {
- get { return this.nextIdValue; }
- set { this.nextIdValue = value; NotifyPropertyChanged("nextId"); }
- }
- public string version
- {
- get { return this.versionValue; }
- set { this.versionValue = value; NotifyPropertyChanged("version"); }
- }
-
- public ObservableCollection<Article> items
- {
- get { return this.itemsValue; }
- set { this.itemsValue = value; NotifyPropertyChanged("items"); }
- }
- }
- public class Article
- {
- Uri urlValue = null;
- public string title { get; set; }
- public string urlDomainOnly
- {
- get
- {
- if (this.urlValue == null) { return ""; }
- string[] hostParts = this.urlValue.Host.Split('.');
- int len = hostParts.Length;
- if (len > 1)
- {
- return hostParts[len - 2] + "." + hostParts[len - 1];
- }
- else
- {
- return this.urlValue.Host;
- }
- }
- }
- public string url
- {
- get { return this.urlValue.ToString(); }
- set { this.urlValue = new Uri(value); }
- }
- public int id { get; set; }
- public int commentCount { get; set; }
- public int points { get; set; }
- public string postedAgo { get; set; }
- public string postedBy { get; set; }
- }
-
public partial class MainPage : PhoneApplicationPage
{
private News news_page_data = new News();
private News ask_page_data = new News();
private News new_page_data = new News();
- public void setProgressBar(Boolean state)
+ public void SetProgressBar(Boolean state)
{
progressBar.IsIndeterminate = state;
progressBar.Visibility = (state == true) ? Visibility.Visible : Visibility.Collapsed;
@@ -103,11 +35,25 @@ private void HandleNewsResult(IAsyncResult result)
{
try
{
- Common common = new Common();
+ var common = new Common();
var binding = (result.AsyncState as AsyncState).binding as News;
- StreamReader txt = common.makeStreamReaderFromResult(result);
- News rv = common.deserializeStreamReader<News>(txt);
+ News rv; //Our return value.
+
+ /*
+ * StreamReaders implement the IDisposable interface, so the best practice is to
+ * wrap them in using statements so the object's is disposed once the scope passes.
+ *
+ * Only classes with expensive resources (data connections, large buffers, network handles, etc...) implement
+ * IDisposable - in this case StreamReader just uses a buffer and it's probably not that large in this case,
+ * but it's still a good practice. -Aaron
+ */
+ using (var txt = common.makeStreamReaderFromResult(result))
+ {
+ rv = common.deserializeStreamReader<News>(txt);
+ }
+
+
this.Dispatcher.BeginInvoke(
() =>
{
@@ -116,7 +62,7 @@ private void HandleNewsResult(IAsyncResult result)
binding.items = rv.items;
// FIXME: Shouldn't I be able to do this instead?
//binding = processJsonString(txt);
- setProgressBar(false);
+ SetProgressBar(false);
}
);
}
@@ -127,16 +73,16 @@ private void HandleNewsResult(IAsyncResult result)
{
//errorLine.Text = e.Message;
//errorLine.Visibility = Visibility.Visible;
- setProgressBar(false);
+ SetProgressBar(false);
}
);
}
}
- public void populateBinding(News binding, string Url)
+ public void PopulateBinding(News binding, string Url)
{
- AsyncState state = new AsyncState();
- HttpWebRequest request = HttpWebRequest.Create(Url) as HttpWebRequest;
+ var state = new AsyncState();
+ var request = WebRequest.Create(Url) as HttpWebRequest;
request.Accept = "application/json"; //atom+xml";
state.request = request;
state.binding = binding;
@@ -155,7 +101,7 @@ public MainPage()
private Article whatSelected(object sender)
{
- return ((Hacker_News.Article)(((System.Windows.FrameworkElement)(sender)).DataContext));
+ return ((Article)(((System.Windows.FrameworkElement)(sender)).DataContext));
}
private void commentsClicked(object sender, System.Windows.Input.MouseButtonEventArgs e)
@@ -168,11 +114,13 @@ private void commentsClicked(object sender, System.Windows.Input.MouseButtonEven
private void titleClicked(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
var selected = whatSelected(sender);
+
#region If launching the WebBrowser actually worked, this is how we'd do it.
// WebBrowserTask webBrowserTask = new WebBrowserTask();
// webBrowserTask.URL = selected_url;
// webBrowserTask.Show();
#endregion
+
if (selected.url.StartsWith("http"))
{
Browser.url = selected.url;
@@ -185,18 +133,18 @@ private void titleClicked(object sender, System.Windows.Input.MouseButtonEventAr
}
private void PivotSelectionChanged(object sender, SelectionChangedEventArgs e)
{
- setProgressBar(true);
+ SetProgressBar(true);
var selected = ((Microsoft.Phone.Controls.PivotItem)(((Microsoft.Phone.Controls.Pivot)(sender)).SelectedItem)).Header as string;
switch (selected)
{
case "news":
- populateBinding(news_page_data, "http://api.ihackernews.com/page");
+ PopulateBinding(news_page_data, "http://api.ihackernews.com/page");
break;
case "new":
- populateBinding(new_page_data, "http://api.ihackernews.com/new");
+ PopulateBinding(new_page_data, "http://api.ihackernews.com/new");
break;
case "ask":
- populateBinding(ask_page_data, "http://api.ihackernews.com/ask");
+ PopulateBinding(ask_page_data, "http://api.ihackernews.com/ask");
break;
//case "comments":
// populateBinding(comments, "http://api.ihackernews.com/newcomments");
View
28 Hacker News/SampleData/NewsSampleData.xaml
@@ -1,14 +1,14 @@
-<Hacker_News:News xmlns:Hacker_News="clr-namespace:Hacker_News" nextId="Eros sodales pharetra" version="Parturient nibh nisi viverra placerat">
- <Hacker_News:News.items>
- <Hacker_News:Article commentCount="19" id="62" points="72" postedAgo="Cras class" postedBy="Aenean nam aliquam integer" title="Cras class curae nam aliquam maecenas aenean duis sed praesent mauris" url="http://www.adatum.com/" urlDomainOnly="November 5, 2003"/>
- <Hacker_News:Article commentCount="53" id="38" points="76" postedAgo="Quisque parturient vestibulum" postedBy="Est vivamus vestibulum phasellus" title="Integer quisque nullam accumsan vivamus nunc" url="http://www.adventure-works.com/" urlDomainOnly="December 29, 2006"/>
- <Hacker_News:Article commentCount="47" id="31" points="44" postedAgo="Consequat maecenas convallis" postedBy="Fusce lorem" title="Aliquam aliquet amet bibendum aptent donec etiam dictumst auctor dis fusce" url="http://www.adventure-works.com/" urlDomainOnly="January 19, 2004"/>
- <Hacker_News:Article commentCount="16" id="77" points="57" postedAgo="Nunc parturient blandit commodo" postedBy="Conubia praesent cubilia" title="Ante eleifend facilisi arcu congue blandit faucibus cursus est hac diam" url="http://www.alpineskihouse.com/" urlDomainOnly="December 22, 2007"/>
- <Hacker_News:Article commentCount="60" id="52" points="16" postedAgo="Elementum bibendum facilisis aptent" postedBy="Auctor dapibus per" title="Habitant commodo conubia inceptos interdum dictum cubilia" url="http://www.baldwinmuseumofscience.com/" urlDomainOnly="February 2, 2006"/>
- <Hacker_News:Article commentCount="16" id="62" points="32" postedAgo="Euismod eleifend fringilla sem feugiat" postedBy="Augue sit vestibulum" title="Lectus leo eget elit dapibus mus" url="http://www.blueyonderairlines.com/" urlDomainOnly="August 23, 2003"/>
- <Hacker_News:Article commentCount="42" id="37" points="82" postedAgo="Gravida iaculis habitasse faucibus" postedBy="Cursus nam habitant dictum lacinia" title="Libero egestas ligula euismod nec non per" url="http://www.cpandl.com/" urlDomainOnly="May 27, 2007"/>
- <Hacker_News:Article commentCount="36" id="30" points="52" postedAgo="Libero hac leo" postedBy="Vestibulum vestibulum himenaeos" title="Sed feugiat enim sem sit lobortis erat vel nam litora eros" url="http://www.cohovineyard.com/" urlDomainOnly="May 24, 2006"/>
- <Hacker_News:Article commentCount="68" id="81" points="70" postedAgo="Litora eget natoque fames posuere" postedBy="Elit mus potenti" title="Gravida nibh luctus nascetur lorem nisi magnis morbi iaculis nisl pharetra sed" url="http://www.cohowinery.com/" urlDomainOnly="September 19, 2004"/>
- <Hacker_News:Article commentCount="89" id="39" points="46" postedAgo="Nec non vestibulum" postedBy="Ipsum enim luctus" title="Dis est hac placerat mattis pulvinar leo mus mauris sagittis" url="http://www.contoso.com/" urlDomainOnly="August 10, 2003"/>
- </Hacker_News:News.items>
-</Hacker_News:News>
+<Entities:News xmlns:Hacker_News="clr-namespace:Hacker_News" xmlns:Entities="clr-namespace:Hacker_News.Entities" nextId="Eros sodales pharetra" version="Parturient nibh nisi viverra placerat">
+ <Entities:News.items>
+ <Entities:Article commentCount="19" id="62" points="72" postedAgo="Cras class" postedBy="Aenean nam aliquam integer" title="Cras class curae nam aliquam maecenas aenean duis sed praesent mauris" url="http://www.adatum.com/" urlDomainOnly="November 5, 2003"/>
+ <Entities:Article commentCount="53" id="38" points="76" postedAgo="Quisque parturient vestibulum" postedBy="Est vivamus vestibulum phasellus" title="Integer quisque nullam accumsan vivamus nunc" url="http://www.adventure-works.com/" urlDomainOnly="December 29, 2006"/>
+ <Entities:Article commentCount="47" id="31" points="44" postedAgo="Consequat maecenas convallis" postedBy="Fusce lorem" title="Aliquam aliquet amet bibendum aptent donec etiam dictumst auctor dis fusce" url="http://www.adventure-works.com/" urlDomainOnly="January 19, 2004"/>
+ <Entities:Article commentCount="16" id="77" points="57" postedAgo="Nunc parturient blandit commodo" postedBy="Conubia praesent cubilia" title="Ante eleifend facilisi arcu congue blandit faucibus cursus est hac diam" url="http://www.alpineskihouse.com/" urlDomainOnly="December 22, 2007"/>
+ <Entities:Article commentCount="60" id="52" points="16" postedAgo="Elementum bibendum facilisis aptent" postedBy="Auctor dapibus per" title="Habitant commodo conubia inceptos interdum dictum cubilia" url="http://www.baldwinmuseumofscience.com/" urlDomainOnly="February 2, 2006"/>
+ <Entities:Article commentCount="16" id="62" points="32" postedAgo="Euismod eleifend fringilla sem feugiat" postedBy="Augue sit vestibulum" title="Lectus leo eget elit dapibus mus" url="http://www.blueyonderairlines.com/" urlDomainOnly="August 23, 2003"/>
+ <Entities:Article commentCount="42" id="37" points="82" postedAgo="Gravida iaculis habitasse faucibus" postedBy="Cursus nam habitant dictum lacinia" title="Libero egestas ligula euismod nec non per" url="http://www.cpandl.com/" urlDomainOnly="May 27, 2007"/>
+ <Entities:Article commentCount="36" id="30" points="52" postedAgo="Libero hac leo" postedBy="Vestibulum vestibulum himenaeos" title="Sed feugiat enim sem sit lobortis erat vel nam litora eros" url="http://www.cohovineyard.com/" urlDomainOnly="May 24, 2006"/>
+ <Entities:Article commentCount="68" id="81" points="70" postedAgo="Litora eget natoque fames posuere" postedBy="Elit mus potenti" title="Gravida nibh luctus nascetur lorem nisi magnis morbi iaculis nisl pharetra sed" url="http://www.cohowinery.com/" urlDomainOnly="September 19, 2004"/>
+ <Entities:Article commentCount="89" id="39" points="46" postedAgo="Nec non vestibulum" postedBy="Ipsum enim luctus" title="Dis est hac placerat mattis pulvinar leo mus mauris sagittis" url="http://www.contoso.com/" urlDomainOnly="August 10, 2003"/>
+ </Entities:News.items>
+</Entities:News>
Please sign in to comment.
Something went wrong with that request. Please try again.