/examples/TwitterAirDemo_Flex4.5/src/Main.mxml

http://github.com/dborisenko/twitter-actionscript-api · Macromedia eXtensible Markup Language · 208 lines · 177 code · 31 blank · 0 comment · 0 complexity · e373532caa9e7fac899b8c7fcd0c0765 MD5 · raw file

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
  3. xmlns:s="library://ns.adobe.com/flex/spark"
  4. xmlns:mx="library://ns.adobe.com/flex/mx"
  5. width="800"
  6. height="500"
  7. creationComplete="windowedapplication1_creationCompleteHandler(event)" xmlns:local="*"
  8. >
  9. <s:states>
  10. <s:State name="normal"/>
  11. <s:State name="loggedIn"/>
  12. </s:states>
  13. <fx:Style>
  14. @namespace s "library://ns.adobe.com/flex/spark";
  15. @namespace mx "library://ns.adobe.com/flex/halo";
  16. s|List#tweetsList s|Scroller {
  17. horizontalScrollPolicy: off;
  18. }
  19. </fx:Style>
  20. <fx:Script>
  21. <![CDATA[
  22. import com.dborisenko.api.twitter.TwitterAPI;
  23. import com.dborisenko.api.twitter.commands.status.UpdateStatus;
  24. import com.dborisenko.api.twitter.commands.timeline.LoadHomeTimeline;
  25. import com.dborisenko.api.twitter.events.TwitterEvent;
  26. import com.dborisenko.api.twitter.net.TwitterOperation;
  27. import com.dborisenko.api.twitter.oauth.events.OAuthTwitterEvent;
  28. import mx.collections.ArrayCollection;
  29. import mx.events.FlexEvent;
  30. protected static const CONSUMER_KEY:String = "3jetSG8TzkMaPiTGEBILvQ";
  31. protected static const CONSUMER_SECRET:String = "lITX9DWs4QpLVOsvRqz1vcHgWX8XZVhMDOgI4LsshfU";
  32. [Bindable]
  33. protected var twitterApi:TwitterAPI = new TwitterAPI();
  34. [Bindable]
  35. protected var twitterStatuses:ArrayCollection;
  36. protected function loadTweets():void
  37. {
  38. status = "Loading tweets";
  39. var op:TwitterOperation = new LoadHomeTimeline();
  40. var handler:Function = function (event:TwitterEvent):void
  41. {
  42. op.removeEventListener(TwitterEvent.COMPLETE, handler);
  43. if (event.success)
  44. {
  45. status = "Tweets loaded";
  46. twitterStatuses = event.data as ArrayCollection;
  47. }
  48. else
  49. {
  50. status = "Loading error: " + event.data.toString();
  51. }
  52. };
  53. op.addEventListener(TwitterEvent.COMPLETE, handler);
  54. twitterApi.post(op);
  55. }
  56. protected function sendTweet():void
  57. {
  58. var text:String = tweetTextInput.text;
  59. status = "Sending tweet";
  60. var op:TwitterOperation = new UpdateStatus(text);
  61. var handler:Function = function (event:TwitterEvent):void
  62. {
  63. op.removeEventListener(TwitterEvent.COMPLETE, handler);
  64. if (event.success)
  65. {
  66. status = "Tweet sent";
  67. loadTweets();
  68. tweetTextInput.text = "";
  69. }
  70. else
  71. {
  72. status = "Error of status sending: " + event.data.toString();
  73. }
  74. };
  75. op.addEventListener(TwitterEvent.COMPLETE, handler);
  76. twitterApi.post(op);
  77. }
  78. protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
  79. {
  80. twitterApi.connection.addEventListener(OAuthTwitterEvent.REQUEST_TOKEN_RECEIVED, handleRequestTokenReceived);
  81. twitterApi.connection.addEventListener(OAuthTwitterEvent.REQUEST_TOKEN_ERROR, handleRequestTokenError);
  82. twitterApi.connection.addEventListener(OAuthTwitterEvent.ACCESS_TOKEN_ERROR, handleAccessTokenError);
  83. twitterApi.connection.addEventListener(OAuthTwitterEvent.AUTHORIZED, handleAuthorized);
  84. twitterApi.connection.authorize(CONSUMER_KEY, CONSUMER_SECRET);
  85. }
  86. protected function tweetTextInput_textInputHandler(event:TextEvent):void
  87. {
  88. if (event.text == "\n")
  89. {
  90. event.preventDefault();
  91. sendTweet();
  92. }
  93. }
  94. protected function sendTweetButton_clickHandler(event:MouseEvent):void
  95. {
  96. sendTweet();
  97. }
  98. protected function pinOkButton_clickHandler(event:MouseEvent):void
  99. {
  100. twitterApi.connection.grantAccess(pinTextInput.text);
  101. }
  102. protected function handleRequestTokenReceived(event:OAuthTwitterEvent):void
  103. {
  104. authHTML.url = twitterApi.connection.authorizeURL;
  105. }
  106. protected function handleRequestTokenError(event:OAuthTwitterEvent):void
  107. {
  108. status = "Connection error";
  109. }
  110. protected function handleAccessTokenError(event:OAuthTwitterEvent):void
  111. {
  112. status = "Error of receiving access token";
  113. }
  114. protected function handleAuthorized(event:OAuthTwitterEvent):void
  115. {
  116. status = "Authorized";
  117. currentState = "loggedIn";
  118. loadTweets();
  119. }
  120. ]]>
  121. </fx:Script>
  122. <fx:Declarations>
  123. <!-- Place non-visual elements (e.g., services, value objects) here -->
  124. </fx:Declarations>
  125. <s:Group id="viewstack"
  126. width="100%"
  127. height="100%">
  128. <s:VGroup width="100%"
  129. height="100%"
  130. id="authScreen"
  131. excludeFrom="loggedIn">
  132. <s:TextInput text="{authHTML.location}"
  133. editable="false"
  134. width="100%"/>
  135. <local:StageWebViewUIComponent id="authHTML"
  136. width="100%"
  137. height="100%"
  138. />
  139. <s:HGroup width="100%"
  140. horizontalAlign="center"
  141. verticalAlign="middle"
  142. >
  143. <s:Label text="Enter pin here:"/>
  144. <s:TextInput id="pinTextInput"/>
  145. <s:Button label="Ok"
  146. click="pinOkButton_clickHandler(event)"
  147. id="pinOkButton"/>
  148. </s:HGroup>
  149. </s:VGroup>
  150. <s:VGroup width="100%"
  151. height="100%"
  152. id="twitterScreen"
  153. includeIn="loggedIn">
  154. <s:List width="100%"
  155. height="100%"
  156. dataProvider="{twitterStatuses}"
  157. id="tweetsList"
  158. itemRenderer="TweetItemRenderer"
  159. autoLayout="true"
  160. >
  161. </s:List>
  162. <s:HGroup width="100%"
  163. horizontalAlign="center"
  164. verticalAlign="middle">
  165. <s:TextArea width="100%"
  166. height="100%"
  167. id="tweetTextInput"
  168. textInput="tweetTextInput_textInputHandler(event)"/>
  169. <s:Button label="Send"
  170. click="sendTweetButton_clickHandler(event)"
  171. enabled="{tweetTextInput.text != ''}"
  172. height="100%"
  173. id="sendTweetButton"/>
  174. </s:HGroup>
  175. </s:VGroup>
  176. </s:Group>
  177. </s:WindowedApplication>