Skip to content
This repository has been archived by the owner on Jan 26, 2023. It is now read-only.

[WIP] タイムラインの自動更新 #70

Merged
merged 9 commits into from
Nov 27, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require 'bundler/setup'
Bundler.require :default

require 'sugarcube-attributedstring'
require 'bubble-wrap/reactor'

Motion::Project::App.setup do |app|
# Use `rake config' to see complete project settings.
Expand Down
2 changes: 1 addition & 1 deletion app/app_delegate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def application(application, didFinishLaunchingWithOptions:launchOptions)
app_user = ApplicationUser.sharedUser.load

## initialize BugSense
if app_user.send_bugreport?
if not Device.simulator? and app_user.send_bugreport?
BugSenseController.sharedControllerWithBugSenseAPIKey(
app_config.vars['bugsense']['api_key']
)
Expand Down
21 changes: 21 additions & 0 deletions app/cocoa/ui_table_view.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class UITableView
def reloadDataWithKeepingSelectedRowAnimated(animated)
indexPath = self.indexPathForSelectedRow
self.reloadData
# self.reloadDataWithRowAnimation(UITableViewRowAnimationNone)
self.selectRowAtIndexPath(indexPath, animated:animated, scrollPosition:UITableViewScrollPositionNone);
end

def reloadDataWithRowAnimation(animation)
indexSet = NSIndexSet.indexSetWithIndex(0)
self.reloadSections(indexSet, withRowAnimation:animation)
end

def reloadDataWithDeselectingRowAnimated(animated)
indexPath = self.indexPathForSelectedRow
self.reloadData
# self.reloadDataWithRowAnimation(UITableViewRowAnimationNone)
self.selectRowAtIndexPath(indexPath, animated:animated, scrollPosition:UITableViewScrollPositionNone);
self.deselectRowAtIndexPath(indexPath, animated:animated);
end
end
19 changes: 19 additions & 0 deletions app/controller/module/remote_push_notification_event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module HBFav2
module RemotePushNotificationEvent
def receive_remote_push_notifcation_event
@receive_remote_push_notification_ = true
nc = NSNotificationCenter.defaultCenter
nc.addObserver(self, selector:"applicationDidReceiveRemoteNotification:", name:"applicationDidReceiveRemoteNotification", object:nil)
end

def applicationDidReceiveRemoteNotification(userInfo)
end

def unreceive_remote_push_notification_event
if @receive_remote_push_notification_
nc = NSNotificationCenter.defaultCenter
nc.removeObserver(self, name:"applicationDidReceiveRemoteNotification:", object:nil)
end
end
end
end
72 changes: 57 additions & 15 deletions app/controller/timeline_view_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
class TimelineViewController < HBFav2::UITableViewController
attr_accessor :user, :content_type
include HBFav2::ApplicationSwitchNotification
include HBFav2::RemotePushNotificationEvent

DefaultTitle = "HBFav"

Expand All @@ -22,6 +23,7 @@ def removeObserver
def viewDidLoad
super

@last_bookmarks_size = 0
@bookmarks = self.initialize_feed_manager(self.user)

self.view.backgroundColor = UIColor.whiteColor
Expand Down Expand Up @@ -49,6 +51,9 @@ def viewDidLoad
self.tableView.addGestureRecognizer(
UILongPressGestureRecognizer.alloc.initWithTarget(self, action:'on_long_press_row:')
)
self.receive_application_switch_notifcation
self.receive_remote_push_notifcation_event
self.start_periodic_update(60.0)
end

def on_long_press_row(recog)
Expand Down Expand Up @@ -83,7 +88,6 @@ def initialize_bookmarks
self.refreshControl.update_title(res.error_message)
else
self.refreshControl.update_title
# tableView.reloadData ## 要らない模様 (observerで更新される)
if @bookmarks.size > 0
@footer_indicator.startAnimating
else
Expand Down Expand Up @@ -131,7 +135,19 @@ def paginate

def observeValueForKeyPath(keyPath, ofObject:object, change:change, context:context)
if (@bookmarks == object and keyPath == 'bookmarks')
view.reloadData
if @bookmarks.timebased? and
@bookmarks.prepended? and
@last_bookmarks_size != 0 and
@last_bookmarks_size != @bookmarks.size

## FIXME: 差分の割り出しは本当は change オブジェクトを使うべき
size = @bookmarks.size - @last_bookmarks_size
@last_bookmarks_size = @bookmarks.size
self.tableView(view, reloadDataWithKeepingContentOffset:size)
else
view.reloadDataWithKeepingSelectedRowAnimated(true)
@last_bookmarks_size = @bookmarks.size
end
end

if (ApplicationUser.sharedUser == object and keyPath == 'hatena_id' and self.home?)
Expand All @@ -143,6 +159,21 @@ def observeValueForKeyPath(keyPath, ofObject:object, change:change, context:cont
end
end

def tableView(tableView, reloadDataWithKeepingContentOffset:prependedRowSize)
offset = tableView.contentOffset
tableView.reloadData
for i in (0..prependedRowSize - 1) do
offset.y += self.tableView(
tableView,
heightForRowAtIndexPath:NSIndexPath.indexPathForRow(i, inSection:0)
)
end
if offset.y > tableView.contentSize.height
offset.y = 0
end
tableView.setContentOffset(offset)
end

## 末尾付近に来たら次のフィードを読み込む (paging)
def tableView(tableView, willDisplayCell:cell, forRowAtIndexPath:indexPath)
if (not @bookmarks.updating? and @bookmarks.size > 0 and indexPath.row >= @bookmarks.size - 5)
Expand All @@ -159,26 +190,15 @@ def update_title
end

def viewWillAppear(animated)
self.receive_application_switch_notifcation
self.update_title
self.navigationController.setToolbarHidden(true, animated:true)

indexPath = tableView.indexPathForSelectedRow
tableView.reloadData
tableView.selectRowAtIndexPath(indexPath, animated:animated, scrollPosition:UITableViewScrollPositionNone);
tableView.deselectRowAtIndexPath(indexPath, animated:animated);

tableView.reloadDataWithDeselectingRowAnimated(animated)
@indicator.center = [ view.center.x, view.center.y - 42 ]
@footer_indicator.center = [@footerView.frame.size.width / 2, @footerView.frame.size.height / 2]

super
end

def viewWillDisappear(animated)
self.unreceive_application_switch_notification
super
end

def tableView(tableView, numberOfRowsInSection:section)
return @bookmarks.size
end
Expand All @@ -204,13 +224,15 @@ def tableView(tableView, cellForRowAtIndexPath:indexPath)

def tableView(tableView, didSelectRowAtIndexPath:indexPath)
bookmark = @bookmarks[indexPath.row]

if bookmark.kind_of? Placeholder
cell = tableView.cellForRowAtIndexPath(indexPath)
cell.beginRefreshing
self.refreshControl.beginRefreshing
@bookmarks.replace_placeholder(bookmark) do |response|
self.refreshControl.endRefreshing
cell.endRefreshing
tableView.deselectRowAtIndexPath(indexPath, animated:true)
end
else
controller = BookmarkViewController.new.tap { |c| c.bookmark = @bookmarks[indexPath.row] }
Expand Down Expand Up @@ -241,12 +263,32 @@ def on_refresh
end

def applicationWillEnterForeground
if self.home? and @bookmarks.timebased?
@bookmarks.update(true)
end
## 相対時刻更新
self.tableView.reloadData
self.view.reloadDataWithKeepingSelectedRowAnimated(true)
end

def applicationDidReceiveRemoteNotification(userInfo)
if self.home? and @bookmarks.timebased?
@bookmarks.update(true)
end
end

def start_periodic_update(interval = 60.0)
EM.add_periodic_timer interval do
if self.home? and @bookmarks.timebased?
NSLog("Attempting to update timeline...")
@bookmarks.update(true)
end
end
end

def dealloc
self.removeObserver
self.unreceive_application_switch_notification
self.unreceive_remote_push_notification_event
NSLog("dealloc: " + self.class.name)
super
end
Expand Down
19 changes: 18 additions & 1 deletion app/model/feed_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def initialize
@url = nil
@bookmarks = []
@updating = nil
@last_update_method = nil
end

def size
Expand Down Expand Up @@ -91,10 +92,12 @@ def prepend(bookmarks)
if @bookmarks.size > 0 and bookmarks.first.id == @bookmarks.first.id
# 新着なし
else
## test
## 以下二行テスト用
# dt = bookmarks[4].datetime
# bookmarks.insert(5, Placeholder.new(5, dt))

@last_update_method = 'prepend'

origin_size = @bookmarks.size
boundary = bookmarks.size
bookmarks.concat(@bookmarks)
Expand All @@ -110,6 +113,7 @@ def prepend(bookmarks)
end

def append(bookmarks)
@last_update_method = 'append'
self << bookmarks
end

Expand All @@ -126,6 +130,7 @@ def uniq!
end

def replace(i, bookmarks)
@last_update_method = 'replace'
## 取ってきたフィードそのものに被りがあると上手くいかないので先に uniq
bookmarks.uniq! { |b| b.id }

Expand Down Expand Up @@ -162,6 +167,14 @@ def replace_placeholder(ph, &cb)
cb.call(response) if cb
end
end

def prepended?
@last_update_method == 'prepend'
end

def timebased?
true
end
end

class Offset < FeedManager
Expand All @@ -181,5 +194,9 @@ def prepend(bookmarks)
def append(bookmarks)
self << bookmarks
end

def timebased?
false
end
end
end
6 changes: 6 additions & 0 deletions app/remote_notification_delegate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ def application(application, didReceiveRemoteNotification:userInfo)
banner.detailTextLabel.font = UIFont.systemFontOfSize(13)
banner.detailTextLabel.textColor = "#333333".uicolor
end

## 他の画面でローカルpushイベントを採れるように、発火
notify = NSNotification.notificationWithName(
"applicationDidReceiveRemoteNotification", object:userInfo
)
NSNotificationCenter.defaultCenter.postNotification(notify)
end
when UIApplicationStateInactive then
PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
Expand Down