mirror of
https://github.com/zhigang1992/RubyMotion.git
synced 2026-04-05 22:40:25 +08:00
clean up the samples
This commit is contained in:
@@ -2,14 +2,15 @@ class AppDelegate
|
||||
def application(application, didFinishLaunchingWithOptions:launchOptions)
|
||||
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
|
||||
tabbar = UITabBarController.alloc.init
|
||||
tabbar.viewControllers = [BeerMap.alloc.init, BeerList.alloc.init]
|
||||
tabbar.viewControllers = [BeerMapController.alloc.init, BeerListController.alloc.init]
|
||||
tabbar.selectedIndex = 0
|
||||
@beer_details_controller = BeerDetailsController.alloc.init
|
||||
@window.rootViewController = UINavigationController.alloc.initWithRootViewController(tabbar)
|
||||
@window.rootViewController.wantsFullScreenLayout = true
|
||||
@window.makeKeyAndVisible
|
||||
return true
|
||||
end
|
||||
|
||||
attr_reader :beer_details_controller
|
||||
def beer_details_controller
|
||||
@beer_details_controller ||= BeerDetailsController.alloc.init
|
||||
end
|
||||
end
|
||||
@@ -1,4 +1,4 @@
|
||||
class BeerList < UITableViewController
|
||||
class BeerListController < UITableViewController
|
||||
def init
|
||||
if super
|
||||
self.tabBarItem = UITabBarItem.alloc.initWithTitle('List', image:UIImage.imageNamed('list.png'), tag:1)
|
||||
@@ -1,4 +1,4 @@
|
||||
class BeerMap < UIViewController
|
||||
class BeerMapController < UIViewController
|
||||
def init
|
||||
if super
|
||||
self.tabBarItem = UITabBarItem.alloc.initWithTitle('Map', image:UIImage.imageNamed('map.png'), tag:1)
|
||||
9
sample/hello/app/app_delegate.rb
Normal file
9
sample/hello/app/app_delegate.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
class AppDelegate
|
||||
def application(application, didFinishLaunchingWithOptions:launchOptions)
|
||||
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
|
||||
@window.rootViewController = HelloViewController.alloc.init
|
||||
@window.rootViewController.wantsFullScreenLayout = true
|
||||
@window.makeKeyAndVisible
|
||||
return true
|
||||
end
|
||||
end
|
||||
@@ -31,19 +31,3 @@ class HelloView < UIView
|
||||
setNeedsDisplay
|
||||
end
|
||||
end
|
||||
|
||||
class HelloViewController < UIViewController
|
||||
def loadView
|
||||
self.view = HelloView.alloc.init
|
||||
end
|
||||
end
|
||||
|
||||
class AppDelegate
|
||||
def application(application, didFinishLaunchingWithOptions:launchOptions)
|
||||
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
|
||||
@window.rootViewController = HelloViewController.alloc.init
|
||||
@window.rootViewController.wantsFullScreenLayout = true
|
||||
@window.makeKeyAndVisible
|
||||
return true
|
||||
end
|
||||
end
|
||||
5
sample/hello/app/hello_view_controller.rb
Normal file
5
sample/hello/app/hello_view_controller.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
class HelloViewController < UIViewController
|
||||
def loadView
|
||||
self.view = HelloView.alloc.init
|
||||
end
|
||||
end
|
||||
9
sample/paint/app/app_delegate.rb
Normal file
9
sample/paint/app/app_delegate.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
class AppDelegate
|
||||
def application(application, didFinishLaunchingWithOptions:launchOptions)
|
||||
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
|
||||
@window.rootViewController = PaintViewController.alloc.init
|
||||
@window.rootViewController.wantsFullScreenLayout = true
|
||||
@window.makeKeyAndVisible
|
||||
return true
|
||||
end
|
||||
end
|
||||
@@ -55,27 +55,3 @@ class PaintView < UIView
|
||||
setNeedsDisplay
|
||||
end
|
||||
end
|
||||
|
||||
class PaintViewController < UIViewController
|
||||
def loadView
|
||||
self.view = PaintView.alloc.init
|
||||
end
|
||||
|
||||
def canBecomeFirstResponder
|
||||
true
|
||||
end
|
||||
|
||||
def motionEnded(motion, withEvent:event)
|
||||
self.view.eraseContent if motion == UIEventSubtypeMotionShake
|
||||
end
|
||||
end
|
||||
|
||||
class AppDelegate
|
||||
def application(application, didFinishLaunchingWithOptions:launchOptions)
|
||||
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
|
||||
@window.rootViewController = PaintViewController.alloc.init
|
||||
@window.rootViewController.wantsFullScreenLayout = true
|
||||
@window.makeKeyAndVisible
|
||||
return true
|
||||
end
|
||||
end
|
||||
13
sample/paint/app/paint_view_controller.rb
Normal file
13
sample/paint/app/paint_view_controller.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
class PaintViewController < UIViewController
|
||||
def loadView
|
||||
self.view = PaintView.alloc.init
|
||||
end
|
||||
|
||||
def canBecomeFirstResponder
|
||||
true
|
||||
end
|
||||
|
||||
def motionEnded(motion, withEvent:event)
|
||||
self.view.eraseContent if motion == UIEventSubtypeMotionShake
|
||||
end
|
||||
end
|
||||
9
sample/tweets/app/app_delegate.rb
Normal file
9
sample/tweets/app/app_delegate.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
class AppDelegate
|
||||
def application(application, didFinishLaunchingWithOptions:launchOptions)
|
||||
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.applicationFrame)
|
||||
@window.rootViewController = TweetsController.alloc.initWithStyle(UITableViewStylePlain)
|
||||
@window.rootViewController.wantsFullScreenLayout = true
|
||||
@window.makeKeyAndVisible
|
||||
return true
|
||||
end
|
||||
end
|
||||
@@ -1,151 +0,0 @@
|
||||
class Tweet
|
||||
def initialize(dict)
|
||||
@author = NSString.alloc.initWithString(dict['from_user_name']) # workaround gc bug
|
||||
@message = NSString.alloc.initWithString(dict['text']) # workaround gc bug
|
||||
@profile_image_url = NSString.alloc.initWithString(dict['profile_image_url']) # workaround gc bug
|
||||
@profile_image = nil
|
||||
end
|
||||
|
||||
attr_reader :author, :message, :profile_image_url
|
||||
attr_accessor :profile_image
|
||||
end
|
||||
|
||||
class TweetCell < UITableViewCell
|
||||
CellID = 'CellIdentifier'
|
||||
MessageFontSize = 14
|
||||
|
||||
def self.cellForTweet(tweet, inTableView:tableView)
|
||||
cell = tableView.dequeueReusableCellWithIdentifier(TweetCell::CellID) || TweetCell.alloc.initWithStyle(UITableViewCellStyleDefault, reuseIdentifier:CellID)
|
||||
cell.fillWithTweet(tweet, inTableView:tableView)
|
||||
cell
|
||||
end
|
||||
|
||||
def initWithStyle(style, reuseIdentifier:cellid)
|
||||
if super
|
||||
self.textLabel.numberOfLines = 0
|
||||
self.textLabel.font = UIFont.systemFontOfSize(MessageFontSize)
|
||||
end
|
||||
self
|
||||
end
|
||||
|
||||
def fillWithTweet(tweet, inTableView:tableView)
|
||||
self.textLabel.text = tweet.message
|
||||
|
||||
unless tweet.profile_image
|
||||
self.imageView.image = nil
|
||||
Dispatch::Queue.concurrent.async do
|
||||
profile_image_data = NSData.alloc.initWithContentsOfURL(NSURL.URLWithString(tweet.profile_image_url))
|
||||
if profile_image_data
|
||||
tweet.profile_image = UIImage.alloc.initWithData(profile_image_data)
|
||||
Dispatch::Queue.main.sync do
|
||||
self.imageView.image = tweet.profile_image
|
||||
tableView.delegate.reloadRowForTweet(tweet)
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
self.imageView.image = tweet.profile_image
|
||||
end
|
||||
end
|
||||
|
||||
def self.heightForTweet(tweet, width)
|
||||
constrain = CGSize.new(width - 57, 1000)
|
||||
size = tweet.message.sizeWithFont(UIFont.systemFontOfSize(MessageFontSize), constrainedToSize:constrain)
|
||||
[57, size.height + 8].max
|
||||
end
|
||||
|
||||
def layoutSubviews
|
||||
super
|
||||
self.imageView.frame = CGRectMake(2, 2, 49, 49)
|
||||
label_size = self.frame.size
|
||||
self.textLabel.frame = CGRectMake(57, 0, label_size.width - 59, label_size.height)
|
||||
end
|
||||
end
|
||||
|
||||
class TweetsController < UITableViewController
|
||||
def viewDidLoad
|
||||
@tweets = []
|
||||
searchBar = UISearchBar.alloc.initWithFrame(CGRectMake(0, 0, self.tableView.frame.size.width, 0))
|
||||
searchBar.delegate = self;
|
||||
searchBar.showsCancelButton = true;
|
||||
searchBar.sizeToFit
|
||||
view.tableHeaderView = searchBar
|
||||
view.dataSource = view.delegate = self
|
||||
|
||||
searchBar.text = 'Hello'
|
||||
searchBarSearchButtonClicked(searchBar)
|
||||
end
|
||||
|
||||
def searchBarSearchButtonClicked(searchBar)
|
||||
query = searchBar.text.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
|
||||
url = "http://search.twitter.com/search.json?q=#{query}"
|
||||
|
||||
@tweets.clear
|
||||
Dispatch::Queue.concurrent.async do
|
||||
error_ptr = Pointer.new(:object)
|
||||
data = NSData.alloc.initWithContentsOfURL(NSURL.URLWithString(url), options:NSDataReadingUncached, error:error_ptr)
|
||||
unless data
|
||||
presentError error_ptr[0]
|
||||
return
|
||||
end
|
||||
json = NSJSONSerialization.JSONObjectWithData(data, options:0, error:error_ptr)
|
||||
unless json
|
||||
presentError error_ptr[0]
|
||||
return
|
||||
end
|
||||
|
||||
new_tweets = []
|
||||
json['results'].each do |dict|
|
||||
new_tweets << Tweet.new(dict)
|
||||
end
|
||||
|
||||
Dispatch::Queue.main.sync { load_tweets(new_tweets) }
|
||||
end
|
||||
|
||||
searchBar.resignFirstResponder
|
||||
end
|
||||
|
||||
def searchBarCancelButtonClicked(searchBar)
|
||||
searchBar.resignFirstResponder
|
||||
end
|
||||
|
||||
def load_tweets(tweets)
|
||||
@tweets = tweets
|
||||
view.reloadData
|
||||
end
|
||||
|
||||
def presentError(error)
|
||||
# TODO
|
||||
$stderr.puts error.description
|
||||
end
|
||||
|
||||
def tableView(tableView, numberOfRowsInSection:section)
|
||||
@tweets.size
|
||||
end
|
||||
|
||||
def tableView(tableView, heightForRowAtIndexPath:indexPath)
|
||||
TweetCell.heightForTweet(@tweets[indexPath.row], tableView.frame.size.width)
|
||||
end
|
||||
|
||||
def tableView(tableView, cellForRowAtIndexPath:indexPath)
|
||||
tweet = @tweets[indexPath.row]
|
||||
TweetCell.cellForTweet(tweet, inTableView:tableView)
|
||||
end
|
||||
|
||||
def reloadRowForTweet(tweet)
|
||||
row = @tweets.index(tweet)
|
||||
if row
|
||||
view.reloadRowsAtIndexPaths([NSIndexPath.indexPathForRow(row, inSection:0)], withRowAnimation:false)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class AppDelegate
|
||||
def application(application, didFinishLaunchingWithOptions:launchOptions)
|
||||
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.applicationFrame)
|
||||
@window.rootViewController = TweetsController.alloc.initWithStyle(UITableViewStylePlain)
|
||||
@window.rootViewController.wantsFullScreenLayout = true
|
||||
@window.makeKeyAndVisible
|
||||
return true
|
||||
end
|
||||
end
|
||||
11
sample/tweets/app/tweet.rb
Normal file
11
sample/tweets/app/tweet.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
class Tweet
|
||||
attr_reader :author, :message, :profile_image_url
|
||||
attr_accessor :profile_image
|
||||
|
||||
def initialize(dict)
|
||||
@author = dict['from_user_name']
|
||||
@message = dict['text']
|
||||
@profile_image_url = dict['profile_image_url']
|
||||
@profile_image = nil
|
||||
end
|
||||
end
|
||||
51
sample/tweets/app/tweet_cell.rb
Normal file
51
sample/tweets/app/tweet_cell.rb
Normal file
@@ -0,0 +1,51 @@
|
||||
class TweetCell < UITableViewCell
|
||||
CellID = 'CellIdentifier'
|
||||
MessageFontSize = 14
|
||||
|
||||
def self.cellForTweet(tweet, inTableView:tableView)
|
||||
cell = tableView.dequeueReusableCellWithIdentifier(TweetCell::CellID) || TweetCell.alloc.initWithStyle(UITableViewCellStyleDefault, reuseIdentifier:CellID)
|
||||
cell.fillWithTweet(tweet, inTableView:tableView)
|
||||
cell
|
||||
end
|
||||
|
||||
def initWithStyle(style, reuseIdentifier:cellid)
|
||||
if super
|
||||
self.textLabel.numberOfLines = 0
|
||||
self.textLabel.font = UIFont.systemFontOfSize(MessageFontSize)
|
||||
end
|
||||
self
|
||||
end
|
||||
|
||||
def fillWithTweet(tweet, inTableView:tableView)
|
||||
self.textLabel.text = tweet.message
|
||||
|
||||
unless tweet.profile_image
|
||||
self.imageView.image = nil
|
||||
Dispatch::Queue.concurrent.async do
|
||||
profile_image_data = NSData.alloc.initWithContentsOfURL(NSURL.URLWithString(tweet.profile_image_url))
|
||||
if profile_image_data
|
||||
tweet.profile_image = UIImage.alloc.initWithData(profile_image_data)
|
||||
Dispatch::Queue.main.sync do
|
||||
self.imageView.image = tweet.profile_image
|
||||
tableView.delegate.reloadRowForTweet(tweet)
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
self.imageView.image = tweet.profile_image
|
||||
end
|
||||
end
|
||||
|
||||
def self.heightForTweet(tweet, width)
|
||||
constrain = CGSize.new(width - 57, 1000)
|
||||
size = tweet.message.sizeWithFont(UIFont.systemFontOfSize(MessageFontSize), constrainedToSize:constrain)
|
||||
[57, size.height + 8].max
|
||||
end
|
||||
|
||||
def layoutSubviews
|
||||
super
|
||||
self.imageView.frame = CGRectMake(2, 2, 49, 49)
|
||||
label_size = self.frame.size
|
||||
self.textLabel.frame = CGRectMake(57, 0, label_size.width - 59, label_size.height)
|
||||
end
|
||||
end
|
||||
77
sample/tweets/app/tweets_controller.rb
Normal file
77
sample/tweets/app/tweets_controller.rb
Normal file
@@ -0,0 +1,77 @@
|
||||
class TweetsController < UITableViewController
|
||||
def viewDidLoad
|
||||
@tweets = []
|
||||
searchBar = UISearchBar.alloc.initWithFrame(CGRectMake(0, 0, self.tableView.frame.size.width, 0))
|
||||
searchBar.delegate = self;
|
||||
searchBar.showsCancelButton = true;
|
||||
searchBar.sizeToFit
|
||||
view.tableHeaderView = searchBar
|
||||
view.dataSource = view.delegate = self
|
||||
|
||||
searchBar.text = 'Hello'
|
||||
searchBarSearchButtonClicked(searchBar)
|
||||
end
|
||||
|
||||
def searchBarSearchButtonClicked(searchBar)
|
||||
query = searchBar.text.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
|
||||
url = "http://search.twitter.com/search.json?q=#{query}"
|
||||
|
||||
@tweets.clear
|
||||
Dispatch::Queue.concurrent.async do
|
||||
error_ptr = Pointer.new(:object)
|
||||
data = NSData.alloc.initWithContentsOfURL(NSURL.URLWithString(url), options:NSDataReadingUncached, error:error_ptr)
|
||||
unless data
|
||||
presentError error_ptr[0]
|
||||
return
|
||||
end
|
||||
json = NSJSONSerialization.JSONObjectWithData(data, options:0, error:error_ptr)
|
||||
unless json
|
||||
presentError error_ptr[0]
|
||||
return
|
||||
end
|
||||
|
||||
new_tweets = []
|
||||
json['results'].each do |dict|
|
||||
new_tweets << Tweet.new(dict)
|
||||
end
|
||||
|
||||
Dispatch::Queue.main.sync { load_tweets(new_tweets) }
|
||||
end
|
||||
|
||||
searchBar.resignFirstResponder
|
||||
end
|
||||
|
||||
def searchBarCancelButtonClicked(searchBar)
|
||||
searchBar.resignFirstResponder
|
||||
end
|
||||
|
||||
def load_tweets(tweets)
|
||||
@tweets = tweets
|
||||
view.reloadData
|
||||
end
|
||||
|
||||
def presentError(error)
|
||||
# TODO
|
||||
$stderr.puts error.description
|
||||
end
|
||||
|
||||
def tableView(tableView, numberOfRowsInSection:section)
|
||||
@tweets.size
|
||||
end
|
||||
|
||||
def tableView(tableView, heightForRowAtIndexPath:indexPath)
|
||||
TweetCell.heightForTweet(@tweets[indexPath.row], tableView.frame.size.width)
|
||||
end
|
||||
|
||||
def tableView(tableView, cellForRowAtIndexPath:indexPath)
|
||||
tweet = @tweets[indexPath.row]
|
||||
TweetCell.cellForTweet(tweet, inTableView:tableView)
|
||||
end
|
||||
|
||||
def reloadRowForTweet(tweet)
|
||||
row = @tweets.index(tweet)
|
||||
if row
|
||||
view.reloadRowsAtIndexPaths([NSIndexPath.indexPathForRow(row, inSection:0)], withRowAnimation:false)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user