Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 3ad83da74bb16108487ffaab36389445fd5ed8f3 @naoya committed Jul 24, 2012
Showing with 11,607 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +25 −0 Rakefile
  3. +11,373 −0 TAGS
  4. +10 −0 app/app_delegate.rb
  5. +15 −0 app/bookmark.rb
  6. +70 −0 app/bookmark_cell.rb
  7. +45 −0 app/timeline_view_controller.rb
  8. +55 −0 app/web_view_controller.rb
  9. +9 −0 spec/main_spec.rb
@@ -0,0 +1,5 @@
+.repl_history
+build
+resources/*.nib
+resources/*.momd
+resources/*.storyboardc
@@ -0,0 +1,25 @@
+# -*- coding: utf-8 -*-
+$:.unshift("/Library/RubyMotion/lib")
+require 'motion/project'
+
+require 'rubygems'
+require 'bubble-wrap'
+require 'bubble-wrap/http'
+# require 'bubble-wrap/ui'
+# require 'bubble-wrap/reactor'
+require 'sugarcube'
+
+Motion::Project::App.setup do |app|
+ # Use `rake config' to see complete project settings.
+ app.name = 'TableViewSample'
+end
+
+desc "Checks the syntax"
+task :syntax do
+ Motion::Project::App.setup do |app|
+ app.files.each do |file|
+ result = `macruby -c #{file}`.chomp
+ raise "Syntax Error: #{file}" unless result == "Syntax OK"
+ end
+ end
+end
11,373 TAGS

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,10 @@
+class AppDelegate
+ def application(application, didFinishLaunchingWithOptions:launchOptions)
+ @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds).tap do |w|
+ w.rootViewController = UINavigationController.alloc.initWithRootViewController(TimelineViewController.alloc.init)
+ w.makeKeyAndVisible
+ end
+ true
+ end
+end
+
@@ -0,0 +1,15 @@
+class Bookmark
+ attr_reader :title, :profile_image_url, :link, :user_name, :created_at, :comment
+ attr_accessor :profile_image, :row
+
+ def initialize(dict)
+ @title = dict[:title]
+ @link = dict[:link]
+ @user_name = dict[:user][:name]
+ @created_at = dict[:created_at]
+ @comment = dict[:comment]
+ @profile_image_url = dict[:user][:profile_image_url]
+ @profile_image = nil
+ @row = nil
+ end
+end
@@ -0,0 +1,70 @@
+# -*- coding: utf-8 -*-
+class BookmarkCell < UITableViewCell
+ CellID = 'CellIdentifier'
+
+ attr_reader :nameLabel, :commentLabel, :dateLabel
+
+ def self.cellForBookmark (bookmark, inTableView:tableView)
+ cell = tableView.dequeueReusableCellWithIdentifier(BookmarkCell::CellID) ||
+ BookmarkCell.alloc.initWithStyle(UITableViewCellStyleDefault, reuseIdentifier:CellID)
+ cell.fillWithBookmark(bookmark, inTableView:tableView)
+ return cell
+ end
+
+ def initWithStyle(style, reuseIdentifier:cellid)
+ if super
+ self.textLabel.tap do |v|
+ v.numberOfLines = 0
+ v.font = UIFont.systemFontOfSize(16)
+ v.textColor = '#3B5998'.to_color
+ end
+
+ @nameLabel = UILabel.new.tap do |v|
+ v.frame = CGRectZero
+ v.font = UIFont.boldSystemFontOfSize(16)
+ self.contentView << v
+ end
+
+ @commentLabel = UILabel.new.tap do |v|
+ v.frame = CGRectZero
+ self.contentView << v
+ end
+
+ @dateLabel = UILabel.new.tap do |v|
+ v.frame = CGRectZero
+ self.contentView << v
+ end
+ end
+ self
+ end
+
+ def fillWithBookmark(bookmark, inTableView:tableView)
+ self.textLabel.text = bookmark.title
+ self.nameLabel.text = bookmark.user_name
+ # self.commentLabel.text = bookmark.comment
+ self.dateLabel.text = bookmark.created_at
+
+ if not bookmark.profile_image
+ self.imageView.image = nil
+ Dispatch::Queue.concurrent.async do
+ data = NSData.dataWithContentsOfURL(bookmark.profile_image_url.nsurl)
+ bookmark.profile_image = UIImage.alloc.initWithData(data)
+ Dispatch::Queue.main.sync do
+ self.imageView.image = bookmark.profile_image
+ ## FIXME: bookmark.row に代入しているのださいなあ
+ tableView.reloadRowsAtIndexPaths([NSIndexPath.indexPathForRow(bookmark.row, inSection: 0)], withRowAnimation:false)
+ end
+ end
+ else
+ self.imageView.image = bookmark.profile_image
+ end
+ end
+
+ def layoutSubviews
+ super
+ self.imageView.frame = [[10, 10], [48, 48]]
+ label_size = self.frame.size
+ self.nameLabel.frame = [[65, 10], [label_size.width - 65 - 10, 16]]
+ self.textLabel.frame = [[65, 10 + 16 + 5], [label_size.width - 65 - 10, label_size.height]]
+ end
+end
@@ -0,0 +1,45 @@
+class TimelineViewController < UITableViewController
+ def init
+ self.navigationItem.title = "HBFav"
+ self.view.backgroundColor = UIColor.whiteColor
+ return self
+ end
+
+ def viewDidLoad
+ super
+ @bookmarks = []
+
+ BW::HTTP.get('http://hbfav.herokuapp.com/naoya') do |response|
+ if response.ok?
+ json = BW::JSON.parse(response.body.to_str)
+ json['bookmarks'].each { |dict| @bookmarks << Bookmark.new(dict) }
+ view.reloadData
+ else
+ App.alert(response.error_message)
+ end
+ end
+ end
+
+ def viewWillAppear(animated)
+ super(animated)
+ self.navigationController.toolbarHidden = true
+ view.deselectRowAtIndexPath(view.indexPathForSelectedRow, animated:animated)
+ end
+
+ def tableView(tableView, numberOfRowsInSection:section)
+ return @bookmarks.size
+ end
+
+ def tableView(tableView, cellForRowAtIndexPath:indexPath)
+ bookmark = @bookmarks[indexPath.row]
+ bookmark.row = indexPath.row # FIXME
+ cell = BookmarkCell.cellForBookmark(bookmark, inTableView:tableView)
+ return cell
+ end
+
+ def tableView(tableView, didSelectRowAtIndexPath:indexPath)
+ wv = WebViewController.new
+ wv.bookmark = @bookmarks[indexPath.row]
+ self.navigationController.pushViewController(wv, animated:true)
+ end
+end
@@ -0,0 +1,55 @@
+class WebViewController < UIViewController
+ attr_accessor :bookmark
+
+ def init
+ return self
+ end
+
+ def viewDidLoad
+ super
+
+ ## Toolbar
+ self.navigationController.toolbarHidden = false
+ button = UIBarButtonItem.alloc.initWithBarButtonSystemItem(UIBarButtonSystemItemRefresh, target:self, action:'refresh')
+ self.toolbarItems = [button]
+
+ ## Title
+ self.navigationItem.title = @bookmark.title
+
+ ## WebView
+ @webview = UIWebView.new.tap do |v|
+ v.frame = self.view.bounds
+ v.scalesPageToFit = true
+ # v.loadRequest(NSURLRequest.requestWithURL(NSURL.URLWithString(@bookmark[:link])))
+ v.loadRequest(NSURLRequest.requestWithURL(@bookmark.link.nsurl))
+ v.delegate = self
+ # self.view.addSubview(v)
+ view << v
+ end
+
+ ## Activity Indicator
+ @indicator = UIActivityIndicatorView.new.tap do |v|
+ # v.frame = [[view.bounds.size.width / 2, view.bounds.size.height / 2], [20, 20]]
+ v.center = [view.frame.size.width / 2, view.frame.size.height / 2 - 42]
+ v.style = UIActivityIndicatorViewStyleGray
+ # self.view.addSubview(v)
+ view << v
+ end
+ end
+
+ def webViewDidStartLoad (webView)
+ @indicator.startAnimating
+ end
+
+ def webViewDidFinishLoad (webView)
+ @indicator.stopAnimating
+ end
+
+ def webView(webView, didFailLoadWithError:error)
+ @indicator.stopAnimating
+ end
+
+ def refresh
+ @webview.reload
+ end
+end
@@ -0,0 +1,9 @@
+describe "Application 'TableViewSample'" do
+ before do
+ @app = UIApplication.sharedApplication
+ end
+
+ it "has one window" do
+ @app.windows.size.should == 1
+ end
+end

0 comments on commit 3ad83da

Please sign in to comment.