From 6cf715078324110ce71d733adf2acea1422b67cc Mon Sep 17 00:00:00 2001 From: Laurent Sansonetti Date: Tue, 28 Feb 2012 14:41:27 +0100 Subject: [PATCH] clean up the samples --- sample/beers/app/{main.rb => app_delegate.rb} | 7 +- ..._details.rb => beer_details_controller.rb} | 0 .../{beer_list.rb => beer_list_controller.rb} | 2 +- .../{beer_map.rb => beer_map_controller.rb} | 2 +- sample/hello/app/app_delegate.rb | 9 ++ sample/hello/app/{main.rb => hello_view.rb} | 16 -- sample/hello/app/hello_view_controller.rb | 5 + sample/paint/app/app_delegate.rb | 9 ++ sample/paint/app/{main.rb => paint_view.rb} | 24 --- sample/paint/app/paint_view_controller.rb | 13 ++ sample/tweets/app/app_delegate.rb | 9 ++ sample/tweets/app/main.rb | 151 ------------------ sample/tweets/app/tweet.rb | 11 ++ sample/tweets/app/tweet_cell.rb | 51 ++++++ sample/tweets/app/tweets_controller.rb | 77 +++++++++ 15 files changed, 190 insertions(+), 196 deletions(-) rename sample/beers/app/{main.rb => app_delegate.rb} (70%) rename sample/beers/app/{beer_details.rb => beer_details_controller.rb} (100%) rename sample/beers/app/{beer_list.rb => beer_list_controller.rb} (96%) rename sample/beers/app/{beer_map.rb => beer_map_controller.rb} (97%) create mode 100644 sample/hello/app/app_delegate.rb rename sample/hello/app/{main.rb => hello_view.rb} (64%) create mode 100644 sample/hello/app/hello_view_controller.rb create mode 100644 sample/paint/app/app_delegate.rb rename sample/paint/app/{main.rb => paint_view.rb} (70%) create mode 100644 sample/paint/app/paint_view_controller.rb create mode 100644 sample/tweets/app/app_delegate.rb delete mode 100644 sample/tweets/app/main.rb create mode 100644 sample/tweets/app/tweet.rb create mode 100644 sample/tweets/app/tweet_cell.rb create mode 100644 sample/tweets/app/tweets_controller.rb diff --git a/sample/beers/app/main.rb b/sample/beers/app/app_delegate.rb similarity index 70% rename from sample/beers/app/main.rb rename to sample/beers/app/app_delegate.rb index 7ca1e2e0..91713e50 100644 --- a/sample/beers/app/main.rb +++ b/sample/beers/app/app_delegate.rb @@ -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 diff --git a/sample/beers/app/beer_details.rb b/sample/beers/app/beer_details_controller.rb similarity index 100% rename from sample/beers/app/beer_details.rb rename to sample/beers/app/beer_details_controller.rb diff --git a/sample/beers/app/beer_list.rb b/sample/beers/app/beer_list_controller.rb similarity index 96% rename from sample/beers/app/beer_list.rb rename to sample/beers/app/beer_list_controller.rb index 00c2fc40..27ba37d3 100644 --- a/sample/beers/app/beer_list.rb +++ b/sample/beers/app/beer_list_controller.rb @@ -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) diff --git a/sample/beers/app/beer_map.rb b/sample/beers/app/beer_map_controller.rb similarity index 97% rename from sample/beers/app/beer_map.rb rename to sample/beers/app/beer_map_controller.rb index cab96d3e..e9d2de6a 100644 --- a/sample/beers/app/beer_map.rb +++ b/sample/beers/app/beer_map_controller.rb @@ -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) diff --git a/sample/hello/app/app_delegate.rb b/sample/hello/app/app_delegate.rb new file mode 100644 index 00000000..55edc1d0 --- /dev/null +++ b/sample/hello/app/app_delegate.rb @@ -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 diff --git a/sample/hello/app/main.rb b/sample/hello/app/hello_view.rb similarity index 64% rename from sample/hello/app/main.rb rename to sample/hello/app/hello_view.rb index 03646432..6f1e7516 100644 --- a/sample/hello/app/main.rb +++ b/sample/hello/app/hello_view.rb @@ -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 diff --git a/sample/hello/app/hello_view_controller.rb b/sample/hello/app/hello_view_controller.rb new file mode 100644 index 00000000..135a8e63 --- /dev/null +++ b/sample/hello/app/hello_view_controller.rb @@ -0,0 +1,5 @@ +class HelloViewController < UIViewController + def loadView + self.view = HelloView.alloc.init + end +end diff --git a/sample/paint/app/app_delegate.rb b/sample/paint/app/app_delegate.rb new file mode 100644 index 00000000..7099ae61 --- /dev/null +++ b/sample/paint/app/app_delegate.rb @@ -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 diff --git a/sample/paint/app/main.rb b/sample/paint/app/paint_view.rb similarity index 70% rename from sample/paint/app/main.rb rename to sample/paint/app/paint_view.rb index 62137ddb..f5a4fe69 100644 --- a/sample/paint/app/main.rb +++ b/sample/paint/app/paint_view.rb @@ -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 diff --git a/sample/paint/app/paint_view_controller.rb b/sample/paint/app/paint_view_controller.rb new file mode 100644 index 00000000..2f56d830 --- /dev/null +++ b/sample/paint/app/paint_view_controller.rb @@ -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 diff --git a/sample/tweets/app/app_delegate.rb b/sample/tweets/app/app_delegate.rb new file mode 100644 index 00000000..892ac3df --- /dev/null +++ b/sample/tweets/app/app_delegate.rb @@ -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 diff --git a/sample/tweets/app/main.rb b/sample/tweets/app/main.rb deleted file mode 100644 index 7a1e8054..00000000 --- a/sample/tweets/app/main.rb +++ /dev/null @@ -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 diff --git a/sample/tweets/app/tweet.rb b/sample/tweets/app/tweet.rb new file mode 100644 index 00000000..ea437c7a --- /dev/null +++ b/sample/tweets/app/tweet.rb @@ -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 diff --git a/sample/tweets/app/tweet_cell.rb b/sample/tweets/app/tweet_cell.rb new file mode 100644 index 00000000..f11e07b4 --- /dev/null +++ b/sample/tweets/app/tweet_cell.rb @@ -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 diff --git a/sample/tweets/app/tweets_controller.rb b/sample/tweets/app/tweets_controller.rb new file mode 100644 index 00000000..4655428f --- /dev/null +++ b/sample/tweets/app/tweets_controller.rb @@ -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