summaryrefslogtreecommitdiff
path: root/Juick/MainPage.xaml.cs
blob: 1654c1be735106f60b57d56925bd5b9872bf76f2 (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
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
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.Shapes;
using Juick.Classes;
using Microsoft.Phone.Controls;

namespace Juick
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            Loaded += MainPage_Loaded;
        }

        // Load data for the ViewModel MyFeed
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                if (string.IsNullOrEmpty(App.Account.Credentials.UserName))
                    NavigationService.Navigate(new Uri("/LoginView.xaml", UriKind.Relative));
                else
                    App.ViewModel.LoadData();
            }
        }

        private void ListBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected index is -1 (no selection) do nothing
            if (((ListBox)sender).SelectedIndex == -1)
                return;

            // Navigate to the new page
            NavigationService.Navigate(new Uri("/ThreadView.xaml?mid=" + App.ViewModel.MyFeed[((ListBox)sender).SelectedIndex].MID, UriKind.Relative));

            // Reset selected index to -1 (no selection)
            ((ListBox)sender).SelectedIndex = -1;
        }

        private void LastBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected index is -1 (no selection) do nothing
            if (((ListBox)sender).SelectedIndex == -1)
                return;

            // Navigate to the new page
            NavigationService.Navigate(new Uri(string.Format("/ThreadView.xaml?mid={0}&last=true", App.ViewModel.Last[((ListBox)sender).SelectedIndex].MID), UriKind.Relative));

            // Reset selected index to -1 (no selection)
            ((ListBox)sender).SelectedIndex = -1;
        }

        public static string GetInlineList(TextBlock element)
        {
            if (element != null)
                return element.GetValue(InlineList) as string;
            return string.Empty;
        }

        public static void SetInlineList(TextBlock element, string value)
        {
            if (element != null)
                element.SetValue(InlineList, value);
        }

        public static readonly DependencyProperty InlineList =
            DependencyProperty.RegisterAttached(
                "InlineList",
                typeof(List<Inline>),
                typeof(MainPage),
                new PropertyMetadata(null, OnInlineListPropertyChanged));

        private static void OnInlineListPropertyChanged(DependencyObject obj,
            DependencyPropertyChangedEventArgs e)
        {
            var tb = obj as TextBlock;
            if (tb != null)
            {
                // clear previous inlines
                tb.Inlines.Clear();

                // add new inlines
                var inlines = e.NewValue as List<Inline>;
                if (inlines != null)
                {
                    inlines.ForEach(inl => tb.Inlines.Add((inl)));
                }
            }
        }

        private void ApplicationBarIconButtonClick(object sender, EventArgs e)
        {
            App.ViewModel.MyFeed.Clear();
            App.ViewModel.Last.Clear();
            App.ViewModel.LoadData();
        }

        private void ApplicationBarMenuItemClick(object sender, EventArgs e)
        {
            App.Account.SignOut(this);
        }

        private void ApplicationBarIconButtonClick1(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/NewPostView.xaml", UriKind.Relative));
        }
    }
}