Twitter In Windows Store Application, How It All Works

Introduction 

 
I have been on Twitter for a long time, tweeting all, about what I eat, about where I go, about my girlfriend and so on. Can you still believe that such an intelligent personality (me) has only 37 followers (@Neelesh__)? Shocked!! But yes, this is the reality.
 
Never mind. Though having just a few followers, I am still very much dedicated to making various Twitter applications for Windows 8. In this article, we will see how to set up a Windows Store application to get tweets of a specified person and much more.
 
For that, you need to have Twitter's authentication token. We will see how to get one in the next article. But for this article, we will be concentrating on simply having a project set up, in which we simply will just plug in the tokens.
 
Twitter-Windows8.jpg
 
The first step as always is to create a new Windows Store application project. Without thinking much, let's add a Twitter authentication WinJS helper, that you can download from http://www.4shared.com/document/Lpu3GhGb/winjs-oauth-for-twitter.html?.  Also, let's add a config.js file which in turn possesses the configuration key and access token.
 
Here is what my config.js file contains.
  1. WinJS.Namespace.define("Twitter.OAuth.Config", {  
  2. consumerKey: '<Your consumer Key>',  
  3. consumerSecret: '<Your consumer secret>',  
  4. userOAuthToken: '<Your OAuth Token>',  
  5. userOAuthTokenSecret: '<Your OAuth Token Sceret>'  
  6. });   
That is it. It completes our code for the config.js file. Next is the creation of the user interface. You can create the user interface by editing HTML. Here is what I have in my design on the default.html page.
  1. <body>  
  2.    <div class="findbuddy fragment">  
  3.       <header aria-label="Header content" role="banner">  
  4.          <button class="win-backbutton" aria-label="Back" disabled type="button"></button>  
  5.          <h1 class="titlearea win-type-ellipsis">  
  6.             <span class="pagetitle">Welcome to Tweet Search</span>  
  7.          </h1>  
  8.       </header>  
  9.       <section aria-label="Main content" role="main">  
  10.          <div id="hashTagContainer">  
  11.             <span>Name your Tag:</span>  
  12.             <input type="text" id="hashTagName">  
  13.             <button type="reset" id="btnSearch">Search</button>             
  14.          </div>  
  15.          <div id="mediumListIconTextTemplate" data-win-control="WinJS.Binding.Template">  
  16.             <div id="tweetsTemplate">  
  17.                <div style="background-color:#00639c;">  
  18.                   <img data-win-bind="src: image" id="profilePic"/>  
  19.                </div>  
  20.                <div id="tweetContent">  
  21.                   <!-- Displays the "title" field. -->  
  22.                   <h4 data-win-bind="innerText: name" style="background-color:#00639c;"></h4>  
  23.                   <h5 data-win-bind="innerText: handle" style="background-color:#00639c;"></h5>  
  24.                   <!-- Displays the "text" field. -->    
  25.                   <h6 data-win-bind="innerText: tweet" style="background-color:#00639c;"></h6>  
  26.                   <span data-win-bind="innerText: timeOfTweet"  
  27.                      style="background-color:#00639c;"></span>  
  28.                </div>  
  29.             </div>  
  30.          </div>  
  31.          <div id="basicListView"  
  32.             data-win-control="WinJS.UI.ListView"         
  33.             data-win-options="{ itemTemplate: select('#mediumListIconTextTemplate') }">  
  34.          </div>  
  35.       </section>  
  36.    </div>  
  37. </body>  
The UI isn't good until we write some CSS for it. Here is what my CSS contains.
  1. .TagResult p {  
  2.     margin-left: 120px;  
  3. }  
  4. body {  
  5.     height: 100%;  
  6.     background-color: #00639c;  
  7. #sectionMain {  
  8.     height: auto;  
  9.     background-color: #00639c;  
  10. }    
  11. #hashTagContainer {  
  12.     margin: 20px 0px 0px 54px;  
  13. }    
  14. #hashTagContainer input {  
  15.     margin-left: 10px;  
  16. }    
  17. #basicListView {  
  18.     margin-top: 60px;  
  19.     margin-left: 53px;  
  20.     min-height: 100%;  
  21.     height: 520px;  
  22.     background-color: #00639c;  
  23. }    
  24. #tweetsTemplate {  
  25.     width: 300px;  
  26.     height: 150px;  
  27.     background-color: #00639c;  
  28. }    
  29. #mediumListIconTextTemplate {  
  30.     background-color: #00639c;  
  31. }    
  32. #tweetContent:hover {  
  33.     background-color: lightGRAY;  
  34. }    
  35. #tweetsTemplate h6 {  
  36.     color: white;  
  37.     overflow: auto;  
  38.     max-height: 100px;  
  39.     min-height: 70px;  
  40. }    
  41. #tweetsTemplate h5 {  
  42.     color: darkgray;  
  43. }    
  44. #tweetsTemplate span {  
  45.     color: Yellow;  
  46.     overflow: auto;  
  47.     font-size: 10px;  
  48. }    
  49. #tweetsTemplate #tweetContent {  
  50.     margin-left: 20%;  
  51.     background-color: #00639c;  
  52. }    
  53. #profilePic {  
  54.     position: absolute;  
  55. }    
  56. #progressRing {  
  57.     position: absolute;  
  58.     color: white;  
  59.     margin: -300px 0px 0px 50%;  
  60.     display: none;  
  61. }  
After doing the UI, we need to add code, code that interacts with Twitter's API to get the results we desire. Here's what my code of default.js has.
  1. // For an introduction to the Blank template, see the following documentation:     
  2. // http://go.microsoft.com/fwlink/?LinkId=232509    
  3. (function() {    
  4.     "use strict";    
  5.     
  6.     WinJS.Binding.optimizeBindingReferences = true;    
  7.     
  8.     //Various twitter's api we can use    
  9.     var queryParams, url = 'https://api.twitter.com/1.1/users/show.json';    
  10.     var getHomeTimelineUrl = "https://api.twitter.com/1.1/statuses/home_timeline.json";    
  11.     var userTimeline = "https://api.twitter.com/1/statuses/user_timeline.json";    
  12.     var getMentionsUrl = "https://api.twitter.com/1.1/statuses/mentions_timeline.json";    
  13.     var getRetweetsOfMeUrl = "http://api.twitter.com/1.1/statuses/retweets_of_me.json";    
  14.     var ret = "http://api.twitter.com/1/statuses/retweet/329472492626395130.json";    
  15.     var app = WinJS.Application;    
  16.     var activation = Windows.ApplicationModel.Activation;    
  17.     app.onactivated = function(args) {    
  18.         if (args.detail.kind === activation.ActivationKind.launch) {    
  19.             if (args.detail.previousExecutionState !==    
  20.                 activation.ApplicationExecutionState.terminated) {    
  21.                 //Event handler for button    
  22.                 btnSearch.addEventListener('click', SearchClicked, false);    
  23.             } else {    
  24.                 // TODO: This application has been reactivated from suspension.    
  25.                 // Restore application state here.    
  26.             }    
  27.             args.setPromise(WinJS.UI.processAll());    
  28.         }    
  29.     };    
  30.     app.oncheckpoint = function(args) {};    
  31.     app.start();    
  32.     
  33.     function personAndTweet() {    
  34.         var name;    
  35.         var image;    
  36.         var tweet;    
  37.         var timeOfTweet;    
  38.         var handle;    
  39.     }    
  40.     
  41.     function getSampleTwitterProfile(twitterOAuthInstance, personName) {    
  42.         var pt = new Array();    
  43.         queryParams = {    
  44.             'screen_name': personName,    
  45.             'count''1000'    
  46.         };    
  47.         twitterOAuthInstance.sendAuthorizedRequestForUser(userTimeline, 'GET', queryParams)    
  48.             .then(function(responseTweets) {    
  49.                 var resTweet = JSON.parse(responseTweets.results);    
  50.                 //pt[0].tweet = new Array();    
  51.                 for (var j = 0; j <= resTweet.length - 1; j++) {    
  52.                     pt[j] = new personAndTweet();    
  53.                     pt[j].handle = "@" + resTweet[j].user.screen_name;    
  54.                     pt[j].name = resTweet[j].user.name;    
  55.                     pt[j].image = resTweet[j].user.profile_image_url;    
  56.                     pt[j].tweet = resTweet[j].text;    
  57.                     pt[j].timeOfTweet = resTweet[j].created_at.split("+")[0];    
  58.                 }    
  59.                 var dataList = new WinJS.Binding.List(pt);    
  60.                 basicListView.winControl.itemDataSource = dataList.dataSource;    
  61.             })    
  62.             .done();    
  63.     }    
  64.     
  65.     function setTweetAuthentication(personName) {    
  66.         var twitterOAuthInstance;    
  67.     
  68.         //With all the neccesary credentials in place we can make signed requests to Twitter    
  69.         //on the user's behalf    
  70.         twitterOAuthInstance = new TwitterOAuth(Twitter.OAuth.Config.consumerKey,    
  71.             Twitter.OAuth.Config.consumerSecret,    
  72.             Twitter.OAuth.Config.userOAuthToken,    
  73.             Twitter.OAuth.Config.userOAuthTokenSecret);    
  74.         getSampleTwitterProfile(twitterOAuthInstance, personName);    
  75.     
  76.     }    
  77.     
  78.     function SearchClicked() {    
  79.         setTweetAuthentication(hashTagName.value);    
  80.     }    
  81.     
  82. })();    
The code above possesses data binding and template creation that we will look at in more detail when we study the MVVM pattern in JavaScript. Describing the code is the toughest job in the world right now. We will get over it in the subsequent posts on Windows Store applications.
 
This is what we need to do to create a simple Twitter application. Just some more steps to get the OAuth code and then our application will be fully functional. Do read the next article. Until then. 
 
You can download the project from http://www.4shared.com/rar/_Guw8WNy/TwitterTweets.html?
 

Summary

 
In this article, we learned about Twitter In Windows Store Application, How It All Works.