summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls/HanselForms/TwitterPage.xaml.cs
blob: 40e488e454540bdb9283f96c5b5c107d04d79a5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;

namespace Xamarin.Forms.Controls
{
	public partial class TwitterPage : ContentPage
	{

		private TwitterViewModel ViewModel
		{
			get { return BindingContext as TwitterViewModel; }
		}
		public TwitterPage()
		{
			InitializeComponent();
			BindingContext = new TwitterViewModel();


			listView.ItemTapped += (sender, args) =>
			{
				if (listView.SelectedItem == null)
					return;
				var tweet = listView.SelectedItem as Tweet;
				this.Navigation.PushAsync(new WebsiteView("http://m.twitter.com/shanselman/status/" + tweet.StatusID, tweet.Date));
				listView.SelectedItem = null;
			};
		}


		protected override void OnAppearing()
		{
			base.OnAppearing();
			if (ViewModel == null || !ViewModel.CanLoadMore || ViewModel.IsBusy || ViewModel.Tweets.Count > 0)
				return;

			ViewModel.LoadTweetsCommand.Execute(null);
		}
	}

	public class TwitterViewModel : HBaseViewModel
	{

		public ObservableCollection<Tweet> Tweets { get; set; }

		public TwitterViewModel()
		{
			Title = "Twitter";
			Icon = "slideout.png";
			Tweets = new ObservableCollection<Tweet>();

		}

		private Command loadTweetsCommand;

		public Command LoadTweetsCommand
		{
			get
			{
				return loadTweetsCommand ??
				  (loadTweetsCommand = new Command(async () =>
				  {
					  await ExecuteLoadTweetsCommand();
				  }, () =>
				  {
					  return !IsBusy;
				  }));
			}
		}

		public async Task ExecuteLoadTweetsCommand()
		{
			if (IsBusy)
				return;

			IsBusy = true;
			LoadTweetsCommand.ChangeCanExecute();
			var error = false;
			try
			{

				Tweets.Clear();
				//var auth = new ApplicationOnlyAuthorizer()
				//{
				//	CredentialStore = new InMemoryCredentialStore
				//	{
				//		ConsumerKey = "ZTmEODUCChOhLXO4lnUCEbH2I",
				//		ConsumerSecret = "Y8z2Wouc5ckFb1a0wjUDT9KAI6DUat5tFNdmIkPLl8T4Nyaa2J",
				//	},
				//};
				//await auth.AuthorizeAsync();

				//var twitterContext = new TwitterContext(auth);

				//var queryResponse = await
				//  (from tweet in twitterContext.Status
				//   where tweet.Type == StatusType.User &&
				//	 tweet.ScreenName == "shanselman" &&
				//	 tweet.Count == 100 &&
				//	 tweet.IncludeRetweets == true &&
				//	 tweet.ExcludeReplies == true
				//   select tweet).ToListAsync();

				//var tweets =
				//  (from tweet in queryResponse
				//   select new Tweet
				//   {
				//	   StatusID = tweet.StatusID,
				//	   ScreenName = tweet.User.ScreenNameResponse,
				//	   Text = tweet.Text,
				//	   CurrentUserRetweet = tweet.CurrentUserRetweet,
				//	   CreatedAt = tweet.CreatedAt,
				//	   Image = tweet.RetweetedStatus != null && tweet.RetweetedStatus.User != null ?
				//					  tweet.RetweetedStatus.User.ProfileImageUrl.Replace("http://", "https://") : (tweet.User.ScreenNameResponse == "shanselman" ? "scott159.png" : tweet.User.ProfileImageUrl.Replace("http://", "https://"))
				//   }).ToList();
				//foreach (var tweet in tweets)
				//{
				//	Tweets.Add(tweet);
				//}

				if (Device.OS == TargetPlatform.iOS)
				{
					// only does anything on iOS, for the Watch
					//	DependencyService.Get<ITweetStore>().Save(tweets);
				}



			}
			catch
			{
				error = true;
			}

			if (error)
			{
				var page = new ContentPage();
				await page.DisplayAlert("Error", "Unable to load tweets.", "OK");
			}

			IsBusy = false;
			LoadTweetsCommand.ChangeCanExecute();
		}
	}

	public class Tweet
	{
		public Tweet()
		{
		}


		public ulong StatusID { get; set; }

		public string ScreenName { get; set; }

		public string Text { get; set; }

		//[JsonIgnore]
		public string Date { get { return CreatedAt.ToString("g"); } }
		//[JsonIgnore]
		public string RTCount { get { return CurrentUserRetweet == 0 ? string.Empty : CurrentUserRetweet + " RT"; } }

		public string Image { get; set; }

		public DateTime CreatedAt
		{
			get;
			set;
		}

		public ulong CurrentUserRetweet
		{
			get;
			set;
		}
	}

	public interface ITweetStore
	{
		void Save(System.Collections.Generic.List<Tweet> tweets);
		//System.Collections.Generic.List<Hanselman.Shared.Tweet> Load ();
	}
}