-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecondPage.xaml.cs
executable file
·156 lines (139 loc) · 6.4 KB
/
SecondPage.xaml.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
namespace EPicture
{
public sealed partial class SecondPage : Page
{
private EPicture_Imgur Epi;
private string selected_imageId = "";
enum actual_shown {HOME,FAVORITES,USER};
private int actual_flux;
public SecondPage()
{
this.InitializeComponent();
buttonFav.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
buttonDelete.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
buttonSearch.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
searchBox.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Epi = e.Parameter as EPicture_Imgur;
textBlock.Text = "Connected as " + Epi.account_username;
}
private void GridViewAlbum_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
GridView view = (GridView)sender;
GridViewImage image = view.SelectedItem as GridViewImage;
this.selected_imageId = image.image_id;
}
catch
{
}
}
private async void buttonUpload_Click(object sender, RoutedEventArgs e)
{
FileOpenPicker browser = new FileOpenPicker();
browser.FileTypeFilter.Add(".png");
browser.FileTypeFilter.Add(".jpg");
browser.FileTypeFilter.Add(".jpeg");
StorageFile file = await browser.PickSingleFileAsync();
if (file != null)
{
Stream stream = await file.OpenStreamForReadAsync();
using (HttpClient client = new HttpClient())
{
HttpContent content = new StreamContent(stream);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Epi.access_token);
MultipartFormDataContent data = new MultipartFormDataContent();
data.Add(content, "image", "image");
data.Add(content, "name", file.Name);
data.Add(content, "description", "Another Epicture upload =)");
HttpResponseMessage response = await client.PostAsync(new Uri("https://api.imgur.com/3/upload"), data);
string ret = await response.Content.ReadAsStringAsync();
if (this.actual_flux == (int)actual_shown.USER)
{
List<ImgurImage> images = await Epi.getAllUserImages();
LoadImage(images);
}
}
}
}
private void LoadImage(List<ImgurImage> images)
{
string tmp;
string gal_url ="http://im";
List<GridViewImage> list = new List<GridViewImage>();
for (int i = 0; i < 50 && i < images.Count; i++)
{
ImgurImage img = images[i];
if (img.description != null)
tmp = img.description;
else
tmp = "No description";
if (!img.link.StartsWith(gal_url))
list.Add(new GridViewImage() { Name = img.title, Description = tmp, Photo = new BitmapImage(new Uri(img.link, UriKind.Absolute)), image_id = img.id });
else
list.Add(new GridViewImage() { Name = img.title, Description = tmp, Photo = new BitmapImage(new Uri("ms-appx:///Assets/Apps-Gallery-icon.png", UriKind.Absolute)), image_id = img.id });
}
GridViewAlbum.ItemsSource = list;
}
private async void buttonGalHot_Click(object sender, RoutedEventArgs e)
{
this.actual_flux = (int)actual_shown.HOME;
buttonFav.Visibility = Windows.UI.Xaml.Visibility.Visible;
buttonSearch.Visibility = Windows.UI.Xaml.Visibility.Visible;
buttonDelete.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
searchBox.Visibility = Windows.UI.Xaml.Visibility.Visible;
List<ImgurImage> images = await Epi.getGallery();
LoadImage(images);
}
private async void buttonDelete_Click(object sender, RoutedEventArgs e)
{
await Epi.RemoveImage(selected_imageId);
List<ImgurImage> images = await Epi.getAllUserImages();
LoadImage(images);
}
private async void buttonGalFav_Click(object sender, RoutedEventArgs e)
{
this.actual_flux = (int)actual_shown.FAVORITES;
buttonFav.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
buttonSearch.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
buttonDelete.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
searchBox.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
List<ImgurImage> images = await Epi.getFavorites();
LoadImage(images);
}
private async void buttonGalUser_Click(object sender, RoutedEventArgs e)
{
this.actual_flux = (int)actual_shown.USER;
buttonFav.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
buttonSearch.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
buttonDelete.Visibility = Windows.UI.Xaml.Visibility.Visible;
searchBox.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
List<ImgurImage> images = await Epi.getAllUserImages();
LoadImage(images);
}
private async void buttonSearch_Click(object sender, RoutedEventArgs e)
{
List<ImgurImage> images = await Epi.SearchGallery(searchBox.Text);
LoadImage(images);
}
private async void buttonFav_Click(object sender, RoutedEventArgs e)
{
await Epi.AddFavorite(selected_imageId);
}
}
}