change some poorly thought out design choices

This commit is contained in:
kawaiizenbo 2023-05-05 22:01:29 -07:00
parent 8353d669ae
commit e86899be53
7 changed files with 42 additions and 28 deletions

View file

@ -13,42 +13,42 @@ namespace WMstodon
{ {
static ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings; static ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
// still usin em // still usin em
public static async Task<KeyValuePair<HttpStatusCode, string>> GETAsync(string URL) public static async Task<HttpResponseMessage> GETAsync(string URL)
{ {
HttpClient httpClient = new HttpClient(); HttpClient httpClient = new HttpClient();
httpClient.BaseAddress = new Uri((string)localSettings.Values["instanceURL"]); httpClient.BaseAddress = new Uri((string)localSettings.Values["instanceURL"]);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", (string)localSettings.Values["accessToken"]); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", (string)localSettings.Values["accessToken"]);
HttpResponseMessage response = await httpClient.GetAsync(URL); HttpResponseMessage response = await httpClient.GetAsync(URL);
httpClient.Dispose(); httpClient.Dispose();
return new KeyValuePair<HttpStatusCode, string>(response.StatusCode, await response.Content.ReadAsStringAsync()); return response;
} }
public static async Task<KeyValuePair<HttpStatusCode, string>> GETGenericAsync(string URL) public static async Task<HttpResponseMessage> GETGenericAsync(string URL)
{ {
HttpClient generic = new HttpClient(); HttpClient generic = new HttpClient();
generic.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", (string)localSettings.Values["accessToken"]); generic.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", (string)localSettings.Values["accessToken"]);
HttpResponseMessage response = await generic.GetAsync(new Uri(URL)); HttpResponseMessage response = await generic.GetAsync(new Uri(URL));
generic.Dispose(); generic.Dispose();
return new KeyValuePair<HttpStatusCode, string>(response.StatusCode, await response.Content.ReadAsStringAsync()); return response;
} }
public static async Task<KeyValuePair<HttpStatusCode, string>> POSTAsync(string URL, HttpContent data) public static async Task<HttpResponseMessage> POSTAsync(string URL, HttpContent data)
{ {
HttpClient httpClient = new HttpClient(); HttpClient httpClient = new HttpClient();
httpClient.BaseAddress = new Uri((string)localSettings.Values["instanceURL"]); httpClient.BaseAddress = new Uri((string)localSettings.Values["instanceURL"]);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", (string)localSettings.Values["accessToken"]); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", (string)localSettings.Values["accessToken"]);
HttpResponseMessage response = await httpClient.PostAsync(URL, data); HttpResponseMessage response = await httpClient.PostAsync(URL, data);
httpClient.Dispose(); httpClient.Dispose();
return new KeyValuePair<HttpStatusCode, string>(response.StatusCode, await response.Content.ReadAsStringAsync()); return response;
} }
public static async Task<KeyValuePair<HttpStatusCode, string>> POSTGenericAsync(string URL, HttpContent data) public static async Task<HttpResponseMessage> POSTGenericAsync(string URL, HttpContent data)
{ {
HttpClient generic = new HttpClient(); HttpClient generic = new HttpClient();
generic.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", (string)localSettings.Values["accessToken"]); generic.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", (string)localSettings.Values["accessToken"]);
HttpResponseMessage response = await generic.PostAsync(new Uri(URL), data); HttpResponseMessage response = await generic.PostAsync(new Uri(URL), data);
generic.Dispose(); generic.Dispose();
return new KeyValuePair<HttpStatusCode, string>(response.StatusCode, await response.Content.ReadAsStringAsync()); return response;
} }
} }
} }

View file

@ -52,13 +52,13 @@ namespace WMstodon
postopts["grant_type"] = "authorization_code"; postopts["grant_type"] = "authorization_code";
postopts["code"] = AuthCodeTextBox.Text; postopts["code"] = AuthCodeTextBox.Text;
postopts["scope"] = "read write push"; postopts["scope"] = "read write push";
KeyValuePair<HttpStatusCode, string> response = HttpResponseMessage response =
await HTTPUtils.POSTAsync("/oauth/token", new FormUrlEncodedContent(postopts)); await HTTPUtils.POSTAsync("/oauth/token", new FormUrlEncodedContent(postopts));
if (response.Key == HttpStatusCode.OK) if (response.StatusCode == HttpStatusCode.OK)
{ {
JObject appResponseObj = JObject.Parse(response.Value); JObject appResponseObj = JObject.Parse(await response.Content.ReadAsStringAsync());
localSettings.Values["accessToken"] = (string)appResponseObj["access_token"]; localSettings.Values["accessToken"] = (string)appResponseObj["access_token"];
if ((await HTTPUtils.GETAsync("/api/v1/accounts/verify_credentials")).Key != HttpStatusCode.OK) if ((await HTTPUtils.GETAsync("/api/v1/accounts/verify_credentials")).StatusCode != HttpStatusCode.OK)
{ {
ErrorTextBlock.Text = "Could not log into service"; ErrorTextBlock.Text = "Could not log into service";
return; return;
@ -67,7 +67,7 @@ namespace WMstodon
} }
else else
{ {
ErrorTextBlock.Text = "Could not log into service:\n" + response.Key; ErrorTextBlock.Text = "Could not log into service:\n" + response.StatusCode;
} }
} }
} }

View file

@ -9,7 +9,23 @@
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid> <Grid>
<ListView x:Name="feedListView" Margin="0,0,0,60" ItemsSource="{x:Bind Statuses}" SelectionChanged="FeedListView_SelectionChanged"> <CommandBar>
<CommandBar.Content>
<TextBlock Text="Home Timeline" Margin="12,14"/>
</CommandBar.Content>
<AppBarButton Icon="Add" Label="New Post" Click="NewPostButton_Click"/>
<AppBarButton Icon="Refresh" Label="Refresh" Click="RefreshButton_Click"/>
<CommandBar.SecondaryCommands>
<AppBarButton Label="&#xE80F; Home Timeline" FontFamily="Segoe MDL2 Assets"/>
<AppBarButton Label="&#xE716; Local Timeline" FontFamily="Segoe MDL2 Assets"/>
<AppBarButton Label="&#xE774; Federated Timeline" FontFamily="Segoe MDL2 Assets"/>
<AppBarButton Label="&#xE77B; My Account" FontFamily="Segoe MDL2 Assets"/>
<AppBarButton Label="&#xF3B1; Log Out" Click="LogOutButton_Click" FontFamily="Segoe MDL2 Assets"/>
</CommandBar.SecondaryCommands>
</CommandBar>
<ListView x:Name="feedListView" Margin="0,48,0,60" ItemsSource="{x:Bind Statuses}" SelectionChanged="FeedListView_SelectionChanged">
<ListView.ItemTemplate> <ListView.ItemTemplate>
<DataTemplate x:DataType="local:Status"> <DataTemplate x:DataType="local:Status">
<Grid Width="Auto" Height="Auto" Padding="0,10" Margin="0,0,0,15"> <Grid Width="Auto" Height="Auto" Padding="0,10" Margin="0,0,0,15">
@ -26,13 +42,10 @@
</DataTemplate> </DataTemplate>
</ListView.ItemTemplate> </ListView.ItemTemplate>
</ListView> </ListView>
<Grid Height="60" Margin="0" VerticalAlignment="Bottom"> <Grid Height="60" Margin="0" VerticalAlignment="Bottom" Background="#FFE6E6E6">
<Image x:Name="AvatarImage" HorizontalAlignment="Left" Height="40" Margin="10,0,0,0" VerticalAlignment="Center" Width="40"/> <Image x:Name="AvatarImage" HorizontalAlignment="Left" Height="40" Margin="10,0,0,0" VerticalAlignment="Center" Width="40"/>
<TextBlock x:Name="DisplayNameTextBlock" HorizontalAlignment="Left" Margin="60,0,0,30" TextWrapping="Wrap" Text="Loading..." VerticalAlignment="Bottom"/> <TextBlock x:Name="DisplayNameTextBlock" HorizontalAlignment="Left" Margin="60,0,0,30" TextWrapping="Wrap" Text="Loading..." VerticalAlignment="Bottom"/>
<TextBlock x:Name="UsernameTextBlock" HorizontalAlignment="Left" Margin="60,0,0,15" TextWrapping="Wrap" Text="Loading..." VerticalAlignment="Bottom" FontSize="12" Foreground="#FF777777"/> <TextBlock x:Name="UsernameTextBlock" HorizontalAlignment="Left" Margin="60,0,0,15" TextWrapping="Wrap" Text="Loading..." VerticalAlignment="Bottom" FontSize="12" Foreground="#FF777777"/>
<Button x:Name="LogOutButton" Content="&#xF3B1;" HorizontalAlignment="Right" Margin="0,0,10,0" VerticalAlignment="Center" Width="40" Click="LogOutButton_Click" Background="#00000000" FontFamily="Segoe MDL2 Assets" Height="40" FontSize="24" Padding="0"/>
<Button x:Name="RefreshButton" Content="&#xE72C;" HorizontalAlignment="Right" Margin="0,0,60,0" VerticalAlignment="Center" Width="40" Click="RefreshButton_Click" Background="#00000000" FontFamily="Segoe MDL2 Assets" Height="40" FontSize="24" Padding="0"/>
<Button x:Name="NewPostButton" Content="&#xE710;" HorizontalAlignment="Right" Margin="0,0,110,0" VerticalAlignment="Center" Width="40" Click="NewPostButton_Click" Background="#00000000" FontFamily="Segoe MDL2 Assets" Height="40" FontSize="24" Padding="0"/>
</Grid> </Grid>
</Grid> </Grid>
</Page> </Page>

View file

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Threading.Tasks; using System.Threading.Tasks;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -25,7 +26,7 @@ namespace WMstodon
protected async override void OnNavigatedTo(NavigationEventArgs e) protected async override void OnNavigatedTo(NavigationEventArgs e)
{ {
string accountJSON = (await HTTPUtils.GETAsync("/api/v1/accounts/verify_credentials")).Value; string accountJSON = await (await HTTPUtils.GETAsync("/api/v1/accounts/verify_credentials")).Content.ReadAsStringAsync();
myAccount = JsonConvert.DeserializeObject<Account>(accountJSON); myAccount = JsonConvert.DeserializeObject<Account>(accountJSON);
DisplayNameTextBlock.Text = $"{myAccount.display_name}"; DisplayNameTextBlock.Text = $"{myAccount.display_name}";
@ -40,16 +41,16 @@ namespace WMstodon
private async Task LoadFeed() private async Task LoadFeed()
{ {
string feedJSON = ($"{{\"statuses\": {(await HTTPUtils.GETAsync("/api/v1/timelines/home")).Value}}}"); string feedJSON = ($"{{\"statuses\": {(await HTTPUtils.GETAsync("/api/v1/timelines/home")).Content.ReadAsStringAsync().Result}}}");
Feed feed = new Feed(); Feed feed = new Feed();
feed = JsonConvert.DeserializeObject<Feed>(feedJSON); feed = JsonConvert.DeserializeObject<Feed>(feedJSON);
foreach (Status s in feed.statuses) foreach (Status s in feed.statuses)
{ {
Status status = s; Status status = s;
string usernameFull = $"@{status.account.username}@{status.url.Split('/')[2]}"; string usernameFull = $"@{status.account.username}@{status.account.url.Split('/')[2]}";
if (status.reblog != null) if (status.reblog != null)
{ {
usernameFull = $"@{status.reblog.account.username}@{status.reblog.url.Split('/')[2]}"; usernameFull = $"@{status.reblog.account.username}@{status.reblog.account.url.Split('/')[2]}";
status = status.reblog; status = status.reblog;
status.additional += $"Reblogged by {s.account.display_name} | "; status.additional += $"Reblogged by {s.account.display_name} | ";
} }

View file

@ -29,15 +29,15 @@ namespace WMstodon
postopts["redirect_uris"] = "urn:ietf:wg:oauth:2.0:oob"; postopts["redirect_uris"] = "urn:ietf:wg:oauth:2.0:oob";
postopts["scopes"] = "read write push"; postopts["scopes"] = "read write push";
postopts["website"] = "https://github.com/kawaiizenbo/WMStodon"; postopts["website"] = "https://github.com/kawaiizenbo/WMStodon";
KeyValuePair<HttpStatusCode, string> response = HttpResponseMessage response =
await HTTPUtils.POSTGenericAsync(InstanceURLTextBox.Text + "/api/v1/apps", new FormUrlEncodedContent(postopts)); await HTTPUtils.POSTGenericAsync(InstanceURLTextBox.Text + "/api/v1/apps", new FormUrlEncodedContent(postopts));
if (response.Key == HttpStatusCode.OK) if (response.StatusCode == HttpStatusCode.OK)
{ {
JObject appResponseObj = JObject.Parse(response.Value); JObject appResponseObj = JObject.Parse(await response.Content.ReadAsStringAsync());
localSettings.Values["instanceURL"] = InstanceURLTextBox.Text; localSettings.Values["instanceURL"] = InstanceURLTextBox.Text;
Frame.Navigate(typeof(LoginPage), appResponseObj); Frame.Navigate(typeof(LoginPage), appResponseObj);
} }
else ErrorTextBlock.Text = "Could not create application on instance:\n" + response.Key; else ErrorTextBlock.Text = "Could not create application on instance:\n" + response.StatusCode;
} }
private void Page_Loaded(object sender, RoutedEventArgs e) private void Page_Loaded(object sender, RoutedEventArgs e)

View file

@ -28,7 +28,7 @@ namespace WMstodon
{ {
string statusURL = e.Parameter.ToString(); string statusURL = e.Parameter.ToString();
string statusJSON = string statusJSON =
(await HTTPUtils.GETAsync("https://" + statusURL.Split('/')[2] + "/api/v1/statuses/" + statusURL.Split('/').Last())).Value; await (await HTTPUtils.GETAsync("https://" + statusURL.Split('/')[2] + "/api/v1/statuses/" + statusURL.Split('/').Last())).Content.ReadAsStringAsync();
status = JsonConvert.DeserializeObject<Status>(statusJSON); status = JsonConvert.DeserializeObject<Status>(statusJSON);
DisplayNameTextBlock.Text = status.account.display_name == "" ? status.account.username : status.account.display_name; DisplayNameTextBlock.Text = status.account.display_name == "" ? status.account.username : status.account.display_name;
UsernameTextBlock.Text = $"@{status.account.username}@{status.url.Split('/')[2]}"; UsernameTextBlock.Text = $"@{status.account.username}@{status.url.Split('/')[2]}";

View file

@ -231,7 +231,7 @@
<Version>6.2.14</Version> <Version>6.2.14</Version>
</PackageReference> </PackageReference>
<PackageReference Include="Newtonsoft.Json"> <PackageReference Include="Newtonsoft.Json">
<Version>13.0.2</Version> <Version>13.0.3</Version>
</PackageReference> </PackageReference>
</ItemGroup> </ItemGroup>
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' "> <PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">