Permalink
Browse files

Adding WallPosts controller

To show off more of the models in practice.
  • Loading branch information...
1 parent 6acf71d commit 9cb0930eb0e1e4b53eee6136f1ae3ff8c97434d5 @clayallsopp committed May 9, 2012
@@ -14,7 +14,7 @@ def initWithUser(user)
def viewDidLoad
super
- self.title = @user.name ? "Friends of #{@user.name}" : "Friends of #{@user.id}"
+ self.title = "About #{@user.name || @user.id}"
defaults = NSUserDefaults.standardUserDefaults
RemoteModule::RemoteModel.set_access_token(defaults["FBAccessTokenKey"])
@@ -31,30 +31,52 @@ def viewDidLoad
end
end
+ def numberOfSectionsInTableView(tableView)
+ return 2
+ end
+
+ def tableView(tableView, titleForHeaderInSection:section)
+ return ["Wall Posts", "Friends"][section]
+ end
+
def tableView(tableView, numberOfRowsInSection:section)
- return @user.friends.count
+ return [1, @user.friends.count][section]
+ end
+
+ def layout_friend_in_cell(friend, cell)
+ cell.textLabel.text = friend.name
+ cell.detailTextLabel.text = friend.id
end
def tableView(tableView, cellForRowAtIndexPath:indexPath)
- reuseIdentifier = "FriendCell"
+ reuseIdentifier = ["WallPostsCell","FriendCell"][indexPath.section]
cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) || begin
cell = UITableViewCell.alloc.initWithStyle(UITableViewCellStyleSubtitle, reuseIdentifier:reuseIdentifier)
- cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton
cell
end
- friend = @user.friends[indexPath.row]
- cell.textLabel.text = friend.name
- cell.detailTextLabel.text = friend.id
+ cell.accessoryType = [UITableViewCellAccessoryDisclosureIndicator, UITableViewCellAccessoryNone][indexPath.section]
+
+ if indexPath.section == 0
+ cell.textLabel.text = "Wall Posts"
+ cell.detailTextLabel.text = ""
+ else
+ friend = @user.friends[indexPath.row]
+ layout_friend_in_cell(friend, cell)
+ end
cell
end
def tableView(tableView, didSelectRowAtIndexPath:indexPath)
tableView.deselectRowAtIndexPath(indexPath, animated:true)
- friend = @user.friends[indexPath.row]
- UIApplication.sharedApplication.delegate.navigationController.pushViewController(FriendsController.alloc.initWithUser(friend), animated: true)
+ if indexPath.section == 0
+ UIApplication.sharedApplication.delegate.navigationController.pushViewController(WallPostsController.alloc.initWithUser(user), animated: true)
+ else
+ friend = @user.friends[indexPath.row]
+ UIApplication.sharedApplication.delegate.navigationController.pushViewController(FriendsController.alloc.initWithUser(friend), animated: true)
+ end
end
end
@@ -0,0 +1,51 @@
+class WallPostsController < UITableViewController
+ attr_reader :user
+
+ def initWithUser(user)
+ raise "User cannot be nil" if user.nil?
+ @user = user
+ self
+ end
+
+ def viewDidLoad
+ super
+ self.title = "Wall Posts for #{@user.name || @user.id}"
+
+ defaults = NSUserDefaults.standardUserDefaults
+ RemoteModule::RemoteModel.set_access_token(defaults["FBAccessTokenKey"])
+
+ @activity = UIActivityIndicatorView.alloc.initWithActivityIndicatorStyle(UIActivityIndicatorViewStyleGray)
+ self.view.addSubview @activity
+ @activity.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2)
+ @activity.startAnimating
+
+ @user.find_wall_posts do |user|
+ @activity.stopAnimating
+ @activity.removeFromSuperview
+ self.tableView.reloadData
+ end
+ end
+
+ def tableView(tableView, numberOfRowsInSection:section)
+ return @user.wall_posts.count
+ end
+
+ def tableView(tableView, cellForRowAtIndexPath:indexPath)
+ reuseIdentifier = "WallPostCell"
+
+ cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) || begin
+ cell = UITableViewCell.alloc.initWithStyle(UITableViewCellStyleSubtitle, reuseIdentifier:reuseIdentifier)
+ cell
+ end
+
+ wall_post = @user.wall_posts[indexPath.row]
+ cell.textLabel.text = wall_post.message
+ cell.detailTextLabel.text = wall_post.created_time_string
+
+ cell
+ end
+
+ def tableView(tableView, didSelectRowAtIndexPath:indexPath)
+ tableView.deselectRowAtIndexPath(indexPath, animated:true)
+ end
+end
@@ -18,11 +18,7 @@ def find_friends(&block)
get(self.friends_url) do |response, json|
self.friends = (json && json[:data]) || []
if json.nil?
- alert = UIAlertView.new
- alert.title = "Friends not given"
- alert.message = "Denied privacy permissions."
- alert.addButtonWithTitle "OK"
- alert.show
+ show_privacy_alert("Friends")
end
if block
block.call self
@@ -32,11 +28,22 @@ def find_friends(&block)
def find_wall_posts(&block)
get(self.wall_posts_url) do |response, json|
-
- self.wall_posts = json[:data]
+ self.wall_posts = (json && json[:data]) || []
+ if json.nil?
+ show_privacy_alert("Wall Posts")
+ end
if block
block.call self
end
end
end
+
+ private
+ def show_privacy_alert(entity)
+ alert = UIAlertView.new
+ alert.title = "#{entity} not given"
+ alert.message = "Denied privacy permissions."
+ alert.addButtonWithTitle "OK"
+ alert.show
+ end
end
@@ -1,10 +1,43 @@
class WallPost < RemoteModule::RemoteModel
attr_accessor :id, :message
+ attr_accessor :created_time
# if we encounter "from" in the JSON return,
# use the User class.
has_one :from => :user
collection_url ""
member_url ":id"
+
+ def self.from_string_date_formatter
+ @from_string_date_formatter ||= begin
+ dateFormat = NSDateFormatter.alloc.init
+ dateFormat.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
+ dateFormat
+ end
+ end
+
+ def self.to_string_date_formatter
+ @to_string_date_formatter ||= begin
+ dateFormat = NSDateFormatter.alloc.init
+ dateFormat.dateFormat = "yyyy'-'MM'-'dd"
+ dateFormat
+ end
+ end
+
+ # EX 2012-05-09T21:57:42+0000
+ def created_time=(created_time)
+ if created_time.class == String
+ @created_time = WallPost.from_string_date_formatter.dateFromString(created_time)
+ elsif created_time.class == NSDate
+ @created_time = created_time
+ else
+ raise "Incorrect class for created_time: #{created_time.class.to_s}"
+ end
+ @created_time
+ end
+
+ def created_time_string
+ @created_time.nil? ? "" : WallPost.to_string_date_formatter.stringFromDate(@created_time)
+ end
end

0 comments on commit 9cb0930

Please sign in to comment.