diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Twitter Service/TwitterPage.xaml b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Twitter Service/TwitterPage.xaml index 34f519262f1..740a49105f2 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Twitter Service/TwitterPage.xaml +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Twitter Service/TwitterPage.xaml @@ -58,10 +58,16 @@ Text="{x:Bind Text}" TextTrimming="CharacterEllipsis" TextWrapping="WrapWholeWords" /> - + + + + + diff --git a/Microsoft.Toolkit.Uwp.Services/Microsoft.Toolkit.Uwp.Services.csproj b/Microsoft.Toolkit.Uwp.Services/Microsoft.Toolkit.Uwp.Services.csproj index 943fe540294..15df60dec9a 100644 --- a/Microsoft.Toolkit.Uwp.Services/Microsoft.Toolkit.Uwp.Services.csproj +++ b/Microsoft.Toolkit.Uwp.Services/Microsoft.Toolkit.Uwp.Services.csproj @@ -89,6 +89,7 @@ + diff --git a/Microsoft.Toolkit.Uwp.Services/Services/Twitter/Tweet.cs b/Microsoft.Toolkit.Uwp.Services/Services/Twitter/Tweet.cs index 092f9b286ab..800479a67aa 100644 --- a/Microsoft.Toolkit.Uwp.Services/Services/Twitter/Tweet.cs +++ b/Microsoft.Toolkit.Uwp.Services/Services/Twitter/Tweet.cs @@ -27,6 +27,12 @@ public class Tweet : SchemaBase [JsonProperty("created_at")] public string CreatedAt { get; set; } + /// + /// Gets or sets the geographic data (latitude and longitude) + /// + [JsonProperty("geo")] + public TwitterGeoData GeoData { get; set; } + /// /// Gets or sets item Id. /// diff --git a/Microsoft.Toolkit.Uwp.Services/Services/Twitter/TwitterGeoData.cs b/Microsoft.Toolkit.Uwp.Services/Services/Twitter/TwitterGeoData.cs new file mode 100644 index 00000000000..7ca887d18d7 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.Services/Services/Twitter/TwitterGeoData.cs @@ -0,0 +1,95 @@ +// ****************************************************************** +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the MIT License (MIT). +// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH +// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE. +// ****************************************************************** + +using System.Globalization; +using Newtonsoft.Json; + +namespace Microsoft.Toolkit.Uwp.Services.Twitter +{ + /// + /// A class to contain the latitude and longitude of a tweet. + /// + public class TwitterGeoData + { + private const int LatitudeIndex = 0; + private const int LongitudeIndex = 1; + private const string PointType = "Point"; + + /// + /// Gets or sets the type of data + /// + [JsonProperty("type")] + public string DataType { get; set; } + + /// + /// Gets the latitude and longitude in a coordinate format. + /// + public string DisplayCoordinates + { + get + { + string result = null; + + if (Coordinates != null) + { + result = $"({Coordinates[LatitudeIndex]}, {Coordinates[LongitudeIndex]})"; + } + + return result; + } + } + + /// + /// Gets or sets the coordinates of the geographic data + /// + [JsonProperty("coordinates")] + public string[] Coordinates { get; set; } + + /// + /// Gets the numeric latitude (null if the value could not be converted) + /// + public double? Latitude + { + get + { + return ParseCoordinate(LatitudeIndex); + } + } + + /// + /// Gets the numeric longitude (null if the value could not be converted) + /// + public double? Longitude + { + get + { + return ParseCoordinate(LongitudeIndex); + } + } + + private double? ParseCoordinate(int index) + { + double? result = null; + double parsed; + + if (DataType == PointType + && Coordinates != null + && !string.IsNullOrEmpty(Coordinates[index]) + && double.TryParse(Coordinates[index], NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out parsed)) + { + result = parsed; + } + + return result; + } + } +} \ No newline at end of file diff --git a/Microsoft.Toolkit.Uwp.Services/Services/Twitter/TwitterStatus.cs b/Microsoft.Toolkit.Uwp.Services/Services/Twitter/TwitterStatus.cs index f1f76ed7c1c..aabca2dc392 100644 --- a/Microsoft.Toolkit.Uwp.Services/Services/Twitter/TwitterStatus.cs +++ b/Microsoft.Toolkit.Uwp.Services/Services/Twitter/TwitterStatus.cs @@ -65,7 +65,7 @@ public class TwitterStatus public bool PossiblySensitive { get; set; } /// - /// Gets a the Request parameters + /// Gets the request parameters /// public string RequestParameters { diff --git a/docs/services/Twitter.md b/docs/services/Twitter.md index 1c3a7e6dfba..1ba56cc6a4a 100644 --- a/docs/services/Twitter.md +++ b/docs/services/Twitter.md @@ -17,6 +17,16 @@ Copy this from the *Keys and Access Tokens* tab on your application page. **Callback URI** Enter a unique URI for your application. This must match the *Callback URL* field on the *Application Details* tab in Twitter. *Example*: http://myapp.company.com - (this does not have to be a working URL) +## Overview + +In the code section below the GetUserTimeLineAsync method returns some Tweet objects. The Tweet class returns some basic information along with the tweet text itself. + +- **CreatedAt** (string) – The date and time of the Tweet formatted by Twitter +- **Text** (string) – The text of the Tweet +- **Id** (string) – The Twitter status identifier +- **GeoData** (TwitterGeoData) - A class containing the latitude and longitude of the Tweet +- **User** (TwitterUser) - A class containing the user ID, Name, ScreenName, and ProfileImageUrl + ## Syntax ```csharp @@ -34,7 +44,7 @@ if (!await TwitterService.Instance.LoginAsync()) var user = await TwitterService.Instance.GetUserAsync(); ProfileImage.DataContext = user; -// Get user timeline +// Get user time line ListView.ItemsSource = await TwitterService.Instance.GetUserTimeLineAsync(user.ScreenName, 50); // Post a tweet