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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" title="HomeView"
    xmlns:twitter="com.soenkerohde.twitter.*"
    xmlns:mx="library://ns.adobe.com/flex/halo"
    currentState="initial" actionBarVisible="false"
    creationComplete="creationCompleteHandler(event)" xmlns:twitter1="com.dipanwita.twitter.*">
  
  <fx:Script>
    <![CDATA[
      import com.dipanwita.twitter.event.TwitterOAuthEvent;
      import com.dipanwita.twitter.event.TwitterUserEvent;
      import com.dipanwita.twitter.event.TwitterStatusEvent;
      
      import flash.events.MouseEvent;
      import flash.net.SharedObject;
      
      import mx.events.FlexEvent;
      import mx.managers.CursorManager;
      
      import org.iotashan.oauth.OAuthToken;
      
      private var accessToken:OAuthToken;
      [Bindable]
      private var pinPending:Boolean = false;
      [Bindable]
      private var statusPending:Boolean = false;
      [Bindable]
      private var screenName:String;
      protected function creationCompleteHandler( event : FlexEvent ) : void {
        authenticate();
      }
      private function authenticate() : void {
        var so:SharedObject = SharedObject.getLocal("twitter");
        var token:Object = so.data["accessToken"];
        // user has already an AccessToken
        if ( token != null ) {
          CursorManager.setBusyCursor();
          accessToken = new OAuthToken( token.key, token.secret );
          verifyAccessToken( accessToken );
          // user is not authenticated yet
        } else {
          currentState = "unauthenticated";
          twitter.authenticate(); 
        }
      }
      protected function verifyAccessToken( token : OAuthToken ) : void {
        twitter.addEventListener( TwitterUserEvent.USER_INFO, userInfoHandler );
        twitter.addEventListener( TwitterUserEvent.USER_ERROR, userErrorHandler );
        twitter.verifyAccessToken(token);
      }
      private function userInfoHandler( event : TwitterUserEvent ) : void {
        currentState = "authenticated";
        CursorManager.removeBusyCursor();
        screenName = event.screenName;
        twitter.removeEventListener( TwitterUserEvent.USER_INFO, userInfoHandler );
        twitter.removeEventListener( TwitterUserEvent.USER_ERROR, userErrorHandler );
      }
      private function userErrorHandler( event : TwitterUserEvent ) : void {
        currentState = "unauthenticated";
        CursorManager.removeBusyCursor();
        twitter.removeEventListener( TwitterUserEvent.USER_INFO, userInfoHandler );
        twitter.removeEventListener( TwitterUserEvent.USER_ERROR, userErrorHandler );
      }
      protected function pinClickHandler( event : MouseEvent ) : void {
        pinPending = true;
        CursorManager.setBusyCursor();
        twitter.addEventListener( TwitterOAuthEvent.ACCESS_TOKEN, accessTokenHandler );
        twitter.obtainAccessToken( int( pin.text ) );
      }
      private function accessTokenHandler( event : TwitterOAuthEvent ) : void {
        var so:SharedObject = SharedObject.getLocal( "twitter" );
        so.data["accessToken"] = event.token;
        so.flush();
        currentState = "authenticated";
        pinPending = false;
        CursorManager.removeBusyCursor();
        twitter.removeEventListener( TwitterOAuthEvent.ACCESS_TOKEN, accessTokenHandler );
        verifyAccessToken( event.token );
      }
      protected function statusClickHandler( event : MouseEvent ) : void {
        statusPending = true;
        CursorManager.setBusyCursor();
        twitter.addEventListener( TwitterStatusEvent.STATUS_SEND, statusSendHandler );
        twitter.setStatus( accessToken, statusMessage.text );
      }
      private function statusSendHandler( event : TwitterStatusEvent ) : void {
        //Alert.show( "Your message was successfully sent.", "Status Updated" );
        statusPending = false;
        statusMessage.text = "";
        CursorManager.removeBusyCursor();
        twitter.removeEventListener( TwitterStatusEvent.STATUS_SEND, statusSendHandler );
      }
      protected function logoutClickHandler( event : MouseEvent ) : void {
        var so:SharedObject = SharedObject.getLocal( "twitter" );
        so.data["accessToken"] = null;
        so.flush();
        currentState = "unauthenticated";
        authenticate();
      }
      
    ]]>
    
  </fx:Script>
  
  <fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
    <s:TraceTarget />
    <twitter1:Twitter  id="twitter"
             consumerKey="consumer key"
             consumerSecret="My secret key"/>
  </fx:Declarations>
  
  
  <s:states>
    <s:State name="initial" />
    <s:State name="unauthenticated" />
    <s:State name="authenticated" />
  </s:states>
  
  <s:Label text="Loading"
       verticalCenter="0"
       horizontalCenter="0"
       includeIn="initial" />
  
  <s:Group includeIn="unauthenticated"
       horizontalCenter="0"
       verticalCenter="0">
    <s:layout>
      <s:HorizontalLayout verticalAlign="middle" />
    </s:layout>
    <s:Label text="PIN" />
    <s:TextInput id="pin"
           enabled="{!pinPending}"
           restrict="0-9" />
    <s:Button label="Validate PIN"
          enabled="{!pinPending &amp;&amp; pin.text != ''}"
          click="pinClickHandler(event)" />
  </s:Group>
  
  <s:Group includeIn="authenticated"
       horizontalCenter="0"
       verticalCenter="0">
    <s:layout>
      <s:VerticalLayout />
    </s:layout>
    <s:Label text="Hello {screenName}" />
    <s:Label text="Set Status"
         enabled="{!statusPending}" />
    <s:TextArea id="statusMessage" />
    <s:Button label="Send"
          enabled="{!statusPending &amp;&amp; statusMessage.text != ''}"
          click="statusClickHandler(event)" />
    <s:Button label="Logout"
          click="logoutClickHandler(event)" />
  </s:Group>
  
  
  
</s:View>