summaryrefslogtreecommitdiff
path: root/Juick/NewPostView.xaml.cs
blob: 8ad272299d971dea0428811547cd10d2fb8bc1c8 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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 RestSharp;
using Microsoft.Xna.Framework.Media;

namespace Juick
{
    public partial class NewPostView : PhoneApplicationPage
    {
        private readonly BitmapImage _attachedPhoto = new BitmapImage();
        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)
        {
            // Get a dictionary of query string keys and values.
            IDictionary<string, string> queryStrings = this.NavigationContext.QueryString;

            // Ensure that there is at least one key in the query string, and check 
            // whether the "FileId" key is present.
            if (queryStrings.ContainsKey("FileId"))
            {
                App.Client.Authenticator = new HttpBasicAuthenticator(App.Account.Credentials.UserName, App.Account.Credentials.Password);
                // Retrieve the picture from the media library using the FileID
                // passed to the application.
                MediaLibrary library = new MediaLibrary();
                Picture picture = library.GetPictureFromToken(queryStrings["FileId"]);
                _attachedPhoto.SetSource(picture.GetImage());
                image1.Source = new WriteableBitmap(_attachedPhoto);
            }
            else
            {
                string _rid, _mid;
                if (NavigationContext.QueryString.TryGetValue("mid", out _mid))
                {
                    PageTitle.Text = "reply";
                    textBox1.Text = "#" + _mid;
                }
                if (NavigationContext.QueryString.TryGetValue("rid", out _rid))
                {
                    textBox1.Text += "/" + _rid;
                }
            }
        }

        private void Publish(object sender, EventArgs e)
        {

            var request = new RestRequest("/post", Method.POST);
            request.AddParameter("body", textBox1.Text);
            if (_attachedPhoto.PixelHeight > 0)
            {
                using (var ms = new MemoryStream())
                {
                    var wb = new WriteableBitmap(_attachedPhoto);
                    wb.SaveJpeg(ms, _attachedPhoto.PixelWidth, _attachedPhoto.PixelHeight, 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 AttachFile(object sender, EventArgs e)
        {
            _chooser = new PhotoChooserTask { ShowCamera = true };
            _chooser.Completed += (o, result) =>
                                     {
                                         if (result.TaskResult == TaskResult.OK)
                                         {
                                             _attachedPhoto.SetSource(result.ChosenPhoto);
                                             image1.Source = new BitmapImage(new Uri(result.OriginalFileName));
                                         }
                                     };
            _chooser.Show();
        }
    }
}