100行以内で作る超簡易板Twitterクライアント



今回はTwitterクライアントの超簡易板を作ってみました。
機能はフレンドタイムラインの表示のみです。


ダウンロードはこちら


今回、勉強になったこと。
AirでのBasic認証のやり方

URLRequestDefaults.setLoginCredentialsForHost("twitter.com", "ユーザー名", "パスワード");

これを書いておくと、これ以降は設定した情報でBasic認証が出来るようになります。
以外と簡単に出来るもんですね。



コード量は100行以内に収めることが出来ました。
以下ソース。

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" backgroundAlpha="0.5" layout="absolute" width="485" height="550" backgroundColor="0x000000">
	<mx:Script>
		<![CDATA[
			import mx.rpc.events.ResultEvent;
			import mx.controls.Alert;
			
			private var timer:Timer;
			
			private function init():void{
				my_timeline_service.request.count = 50;
				my_timeline_service.send();
				
				timer = new Timer(60000);
				timer.addEventListener(TimerEvent.TIMER, my_timeline_service.send);
				timer.start();
			}
			
			private function init_timeline_ary(event:ResultEvent):void{
				var xml:XML = event.result as XML;
				timeline_ary.removeAll();
				var cur_date:Date = new Date();
				for each(var obj:Object in xml.status){
					var new_obj:Object = {};
					new_obj.state = obj.text;
					new_obj.name = obj.user.screen_name;
					new_obj.image = obj.user.profile_image_url;
					new_obj.text = new_obj.name + ":\t\t" + new_obj.state;
					
					var date:Date = new Date(Date.parse(obj.created_at));
					new_obj.time = date.fullYear + "/";
					if(date.month < 10) new_obj.time += "0";
					new_obj.time += date.month + "/";
					if(date.date < 10) new_obj.time += "0";
					new_obj.time += date.date + " ";
					if(date.hours < 10) new_obj.time += "0";
					new_obj.time += date.hours + ":";
					if(date.minutes < 10) new_obj.time += "0";
					new_obj.time += date.minutes + ":";
					if(date.seconds < 10) new_obj.time += "0";
					new_obj.time += date.seconds;
					
					timeline_ary.addItem(new_obj);
				}
			}
			
			private function onItemChange():void{
				var obj:Object = timeline_grid.selectedItem;
				user_img.source = obj.image;
				state_text_area.text = obj.state;
				user_name_txt.text = obj.name;
				time_txt.text = obj.time;
			}
			
			private function send_message():void{
				send_message_service.request.status = state_input.text;
				send_message_service.send();
				state_input.text = "";
			}
			
			private function close_form():void{
				URLRequestDefaults.setLoginCredentialsForHost("twitter.com", name_txt.text, password_txt.text);
				this.removeChild(account_form);
				init();
			}
			
		]]>
	</mx:Script>
	<mx:ArrayCollection id="timeline_ary"/>
	<mx:HTTPService id="my_timeline_service" resultFormat="e4x" result="init_timeline_ary(event)" fault="Alert.show('情報が取得できません。')" url="http://twitter.com/statuses/friends_timeline.xml"/>
	<mx:HTTPService id="send_message_service" method="POST" resultFormat="e4x" fault="Alert.show('送信できませんでした')" url="http://twitter.com/statuses/update.xml"/>
	<mx:Canvas x="0" y="0" width="483" height="100%" backgroundColor="#000000">
		<mx:DataGrid x="10" y="130" width="463" height="324" sortableColumns="false" id="timeline_grid"
			dataProvider="{timeline_ary}" alternatingItemColors="[#E3F3FB, #FFFFFF]" themeColor="#009DFF"
			change="onItemChange()">
			<mx:columns>
				<mx:DataGridColumn headerText="TimeLine" dataField="text"/>
			</mx:columns>
		</mx:DataGrid>
		<mx:Image x="10" y="33" width="80" height="80" id="user_img"/>
		<mx:TextInput x="10" y="476" width="407" id="state_input" enter="send_message()" maxChars="160"/>
		<mx:Button x="425" y="476" label="送信" click="send_message()"/>
		<mx:TextArea x="108" y="59" editable="false" width="342" height="63" backgroundAlpha="0.0" id="state_text_area" borderThickness="0" fontSize="12" color="#FFFFFF"/>
		<mx:Text x="108" y="33" width="189" color="#00D2FF" fontWeight="bold" fontSize="12" id="user_name_txt"/>
		<mx:Text x="305" y="33" width="168" color="#00D2FF" fontWeight="normal" fontSize="12" id="time_txt"/>
	</mx:Canvas>
	<mx:Panel width="262" height="168" layout="absolute" x="116.5" y="195" id="account_form">
		<mx:Button label="OK" click="close_form()" x="166" y="96"/>
		<mx:Form height="88" x="10" width="222" y="10">
			<mx:FormItem label="ユーザー名" required="true">
				<mx:TextInput width="113" id="name_txt"/>
			</mx:FormItem>
			<mx:FormItem label="パスワード" width="181" required="true">
				<mx:TextInput width="112" id="password_txt" displayAsPassword="true" enter="close_form()"/>
			</mx:FormItem>
		</mx:Form>
	</mx:Panel>
</mx:WindowedApplication>