private async void replybutton_Click(object sender, RoutedEventArgs e)
        {
            replybutton.IsEnabled = false;
            string text = "";            
            ReplyToPostBox.Document.GetText(Windows.UI.Text.TextGetOptions.None, out text);
            if (text == "\r")
            {
                // do nothing
            }
            else
            {
                string forum_postAPIResponse = await APILibrary.apiCall((string)ApplicationData.Current.LocalSettings.Values["Tokens"], "https://api.teamsnap.com/v3/forum_posts");
                if (forum_postAPIResponse == "fail")
                {
                    Frame rootFrame = Window.Current.Content as Frame;
                    rootFrame.Navigate(typeof(NoNetwork));
                }
                else
                {
                    RootObjectForumPosts rfposts_button = forum_posts.forumpostsDataDeserializer(forum_postAPIResponse);
                    CollectionForumPostsTemplate rfptemplate = rfposts_button.collection.template;
                    Dictionary<string, int> rfptemplate_ht = new Dictionary<string, int>();
                    for (int i = 0; i < rfptemplate.data.Count; i++)
                    {
                        rfptemplate_ht.Add(rfptemplate.data[i].name, i);
                    }

                    text = text.Remove(text.Length - 1);
                    ForumsPost rfpt = new ForumsPost();
                    rfptemplate.data[(int)rfptemplate_ht["forum_topic_id"]].value = rftopic.forum_topic_id;
                    rfptemplate.data[(int)rfptemplate_ht["message"]].value = text;
                    RootObjectUsers rusers = Users.usersDataDeserializer(await mlibrary.readSerializedUserClass());
                    rfptemplate.data[(int)rfptemplate_ht["member_id"]].value = (long)ApplicationData.Current.LocalSettings.Values["currentMemberIDforLoggedInUser"];
                    if (broadcast_to_team.IsChecked == true)
                    {
                        rfptemplate.data[(int)rfptemplate_ht["broadcast_to_team"]].value = true;
                    }
                    else
                    {
                        rfptemplate.data[(int)rfptemplate_ht["broadcast_to_team"]].value = false;
                    }
                    rfptemplate.data[(int)rfptemplate_ht["type"]].value = "forum_post";
                    ReplyToPostBox.Document.SetText(Windows.UI.Text.TextSetOptions.None, "");
                    broadcast_to_team.IsChecked = false;
                    string rfpresponse = forum_posts.forumpostsTemplateSerializer(rfptemplate);
                    rfpresponse = "{\"template\":" + rfpresponse + "}";
                    string remove = "{\"name\":\"division_member_id\",\"value\":null},";
                    int index = rfpresponse.IndexOf(remove);
                    rfpresponse = (index < 0) ? rfpresponse : rfpresponse.Remove(index, remove.Length);

                    rfpt.poster_name = rusers.items[0].first_name + " " + rusers.items[0].last_name;
                    rfpt.message = text;
                    rfpt.updated_at = String.Format("{0:ddd, MMM d, hh:mm tt}", (DateTime.Now).ToLocalTime());

                    Debug.WriteLine(rfpresponse);
                    await APILibrary.apiPOST((string)ApplicationData.Current.LocalSettings.Values["Tokens"], rfpresponse, "https://api.teamsnap.com/v3/forum_posts");
                    rfpost.Clear();

                    var rftopicpost = rftopic.post;
                    rfpost.Clear();
                    foreach (var rfp in rftopicpost)
                    {
                        rfpost.Add(rfp);
                    }
                    rfpost.Add(rfpt);
                    PostsListView.DataContext = this;
                }
            }
            replybutton.IsEnabled = true;
        }
Exemple #2
0
        public static async Task<string> fetchForumTopics()
        {
            string hreftopics = "https://api.teamsnap.com/v3/forum_topics/search?team_id=" + teamid.ToString();
            string hrefposts = "https://api.teamsnap.com/v3/forum_posts/search?team_id=" + teamid.ToString();
            string access_token = (string)ApplicationData.Current.LocalSettings.Values["Tokens"];
            string topics_resp = await APILibrary.apiCall(access_token, hreftopics);
            if (topics_resp == "fail")
            {
                return "networkProblem";
            }
            string posts_resp = await APILibrary.apiCall(access_token, hrefposts);
            if (posts_resp == "fail")
            {
                return "networkProblem";
            }
            rforumposts = forum_posts.forumpostsDataDeserializer(posts_resp);
            rforumtopics = forum_topics.forumtopicsDataDeserializer(topics_resp);
            Dictionary<string, int> rforumposts_ht = new Dictionary<string,int>();
            Dictionary<string,int> rforumtopics_ht = new Dictionary<string, int>();
            mlibrary.writeFile("forum_topics", topics_resp);
            mlibrary.writeFile("forum_posts", posts_resp);
            rforumdata.topics = new List<ForumsTopic>();
            try
            {
                int len = 0;
                while (len < rforumtopics.collection.items.Count)
                {
                    ForumsTopic rforumdatatopic = new ForumsTopic();
                    for (int i = 0; i < rforumtopics.collection.items[len].data.Count; i++)
                    {
                        rforumtopics_ht.Add(rforumtopics.collection.items[len].data[i].name, i);
                    }
                    rforumdatatopic.forum_topic_id = (long)rforumtopics.collection.items[len].data[(int)rforumtopics_ht["id"]].value;
                    rforumdatatopic.title = (string)rforumtopics.collection.items[len].data[(int)rforumtopics_ht["title"]].value;
                    rforumdatatopic.post = new List<ForumsPost>();
                    int postslen = 0;
                    while (postslen < rforumposts.collection.items.Count)
                    {

                        for (int i = 0; i < rforumposts.collection.items[postslen].data.Count; i++)
                        {
                            rforumposts_ht.Add(rforumposts.collection.items[postslen].data[i].name, i);
                        }
                        if (postslen < 1)
                        {
                            rforumdatatopic.started_by = (string)rforumposts.collection.items[postslen].data[(int)rforumposts_ht["poster_name"]].value;
                        }
                        if (rforumdatatopic.forum_topic_id == (long)rforumposts.collection.items[postslen].data[(int)rforumposts_ht["forum_topic_id"]].value)
                        {
                            ForumsPost rforumdataposts = new ForumsPost();
                            rforumdataposts.forum_post_id = rforumposts.collection.items[postslen].data[(int)rforumposts_ht["id"]].value == null ? 0 : (long)rforumposts.collection.items[postslen].data[(int)rforumposts_ht["id"]].value;
                            rforumdataposts.member_id = rforumposts.collection.items[postslen].data[(int)rforumposts_ht["member_id"]].value == null ? 0 : (long)rforumposts.collection.items[postslen].data[(int)rforumposts_ht["member_id"]].value;
                            rforumdataposts.message = methodLibrary.StripTagsRegexCompiled((string)rforumposts.collection.items[postslen].data[(int)rforumposts_ht["message"]].value);
                            rforumdataposts.poster_name = (string)rforumposts.collection.items[postslen].data[(int)rforumposts_ht["poster_name"]].value;
                            DateTime updated_at = rforumposts.collection.items[postslen].data[(int)rforumposts_ht["updated_at"]].value == null ? DateTime.MinValue : (DateTime)rforumposts.collection.items[postslen].data[(int)rforumposts_ht["updated_at"]].value;
                            updated_at = updated_at.ToLocalTime();
                            rforumdatatopic.last_updated_at = updated_at == null ? DateTime.MinValue : (DateTime)updated_at;
                            rforumdataposts.updated_at = String.Format("{0:ddd, MMM d, hh:mm tt}", updated_at);
                            rforumdatatopic.last_updated_at_string = rforumdataposts.updated_at;
                            rforumdatatopic.post.Add(rforumdataposts);
                        }
                        if (rforumdatatopic.post.Count == 1)
                        {
                            rforumdatatopic.replies = "No replies";
                        }
                        else if (rforumdatatopic.post.Count == 2)
                        {
                            rforumdatatopic.replies = (rforumdatatopic.post.Count - 1).ToString() + " reply";
                        }
                        else
                        {
                            rforumdatatopic.replies = (rforumdatatopic.post.Count - 1).ToString() + " replies";
                        }

                        rforumposts_ht.Clear();
                        postslen++;
                    }

                    rforumdata.topics.Add(rforumdatatopic);
                    rforumtopics_ht.Clear();
                    len++;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception returned : " + ex);
                return "noData";
            }

            return "allGood";
        }