Example #1
0
    // Tweet generating. The code here could be a bit cleaner and nicer
    protected void GenerateTweet(ITweet tweetData, string divIdentifier)
    {
        BLTwitterClient twitterClient = new BLTwitterClient();
        // Find the div to append to. Different for mentions and normal timeline
        Control divToAppend = Master.FindControl("ContentPlaceHolder1").FindControl(divIdentifier);

        HtmlGenericControl tweetSenderPictureContainer = twitterClient.GenerateTweetSenderPictureContainer(tweetData);
        HtmlGenericControl tweetControlsContainer      = twitterClient.GenerateTweetControlsContainer();
        HtmlGenericControl tweetContainer     = twitterClient.GenerateTweetContainer(tweetData);
        HtmlGenericControl tweetDataContainer = twitterClient.GenerateTweetDataContainer();

        #region Rest of the tweetControlsContainer
        // Buttons are generated here. I ran into some logic problems when trying to generate buttons in BL, so they're here
        // Reply button
        Button replyButton = new Button();
        replyButton.Text = " ";
        replyButton.Attributes["class"]   = "reply";
        replyButton.Attributes["onClick"] = "replyButton_Click";
        replyButton.Command        += replyButton_Click;
        replyButton.CommandArgument = tweetData.ToJson();
        // Retweet button
        Button retweetButton = new Button();
        retweetButton.Text = tweetData.RetweetCount.ToString();
        retweetButton.Attributes["class"] = "retweet";
        retweetButton.Command            += retweetButton_Click;
        retweetButton.CommandArgument     = tweetData.Id.ToString();
        if (tweetData.Retweeted)
        {
            isRetweeted = true;
            retweetButton.Style.Add("background-image", "url('../images/retweetGreen.png')");
            retweetButton.Style.Add("color", "#19CF8D");
        }
        else
        {
            retweetButton.Style.Add("background-image", "url('../images/retweet.png')");
        }
        // Like button
        Button likeButton = new Button();
        // Using tweetData.Retweeted didn't work here as it did above, so I had to check if RetweetedTweet is empty or not
        if (tweetData.RetweetedTweet != null)
        {
            // This throws an exception all the time. FavoriteCount is in every retweet, and you can get it without try but it still throws an exception
            // I have no idea why it does this, but this fixes it in a way
            try
            {
                likeButton.Text = tweetData.RetweetedTweet.FavoriteCount.ToString();
            }
            catch (Exception)
            {
            }
        }
        else
        {
            likeButton.Text = tweetData.FavoriteCount.ToString();
        }
        likeButton.Attributes["class"] = "like";
        likeButton.Command            += likeButton_Click;
        likeButton.CommandArgument     = tweetData.Id.ToString();
        if (tweetData.Favorited)
        {
            isFavorited = true;
            likeButton.Style.Add("background-image", "url('../images/likeRed.png')");
            likeButton.Style.Add("color", "#E2264D");
        }
        else
        {
            likeButton.Style.Add("background-image", "url('../images/like.png')");
        }
        // Delete button
        if (tweetData.CreatedBy.ScreenName == ((IAuthenticatedUser)Session["user"]).ScreenName)
        {
            Button deleteButton = new Button();
            deleteButton.Text = " ";
            deleteButton.Attributes["class"] = "delete";
            deleteButton.Command            += deleteButton_Click;
            deleteButton.CommandArgument     = tweetData.Id.ToString();
            deleteButton.OnClientClick       = "return confirm('Are you sure you want to delete this tweet?');";
            deleteButton.Style.Add("background-image", "url('../images/delete.png')");
            tweetControlsContainer.Controls.Add(deleteButton);
        }
        #endregion
        // Add everything to controls
        tweetControlsContainer.Controls.Add(replyButton);
        tweetControlsContainer.Controls.Add(retweetButton);
        tweetControlsContainer.Controls.Add(likeButton);
        tweetContainer.Controls.Add(tweetControlsContainer);
        tweetDataContainer.Controls.Add(tweetSenderPictureContainer);
        tweetDataContainer.Controls.Add(tweetContainer);
        divToAppend.Controls.Add(tweetDataContainer);
    }