summaryrefslogtreecommitdiff
path: root/Juick/NewPostView.xaml.cs
blob: 68b7efb00becae854c8778e9b3dec37d2104d101 (plain) (blame)
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Browser;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Resources;
using System.Windows.Shapes;
using Juick.Classes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using Microsoft.Xna.Framework.Media;
using RestSharp;

namespace Juick
{
    public partial class NewPostView : PhoneApplicationPage
    {
        private PhotoChooserTask chooser;
        public NewPostView()
        {
            InitializeComponent();
        }

        // When page is navigated to set data context to selected item in list
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string _rid, _mid;
            if (NavigationContext.QueryString.TryGetValue("mid", out _mid))
            {
                PageTitle.Text = "reply";
                button1.Content = "reply";
                textBox1.Text = "#" + _mid;
            }
            if (NavigationContext.QueryString.TryGetValue("rid", out _rid))
            {
                textBox1.Text += "/" + _rid;
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            
            var request = new RestRequest("/post", Method.POST);
            request.AddParameter("body", textBox1.Text);
            if (image1.Source != null)
            {
                using (var ms = new MemoryStream())
                {
                    var wb = new WriteableBitmap(image1, null);
                    wb.SaveJpeg(ms, (int) image1.Width, (int) image1.Height, 0, 100);
                    request.AddFile("attach", ms.ToArray(), "file.jpg");
                }
            }
            
            App.Client.ExecuteAsync(request, response =>
                                                 {
                                                     if (response.StatusCode != HttpStatusCode.OK)
                                                     {
                                                         MessageBox.Show(response.StatusCode.ToString());
                                                     }
                                                 });

            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
            Dispatcher.BeginInvoke(() => NavigationService.RemoveBackEntry());
        }


        private void button2_Click(object sender, RoutedEventArgs e)
        {
            chooser = new PhotoChooserTask {ShowCamera = true};
            chooser.Completed += (o, result) =>
                                     {
                                         if (result.TaskResult == TaskResult.OK)
                                         {

                                             BinaryReader reader = new BinaryReader(result.ChosenPhoto);
                                             image1.Source = new BitmapImage(new Uri(result.OriginalFileName));

                                         }
                                     };
            chooser.Show();
        }
    }
}