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

[WIP] iOS 7 の Background Fetch と Remote Notifications に対応 #73

Merged
merged 10 commits into from
Dec 1, 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
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Motion::Project::App.setup do |app|
]
app.info_plist['UIViewControllerBasedStatusBarAppearance'] = false
app.info_plist['UIStatusBarStyle'] = "UIStatusBarStyleLightContent"
app.info_plist['UIBackgroundModes'] = ['fetch', 'remote-notification']

app.pods do
pod 'PocketAPI', :git => 'git@github.com:naoya/Pocket-ObjC-SDK.git', :branch => 'cocoapods-dependency'
Expand All @@ -69,7 +70,6 @@ Motion::Project::App.setup do |app|
pod 'AFNetworking', '~> 1.3'
pod 'ARChromeActivity'
pod 'HatenaBookmarkSDK', :git => 'git@github.com:hatena/Hatena-Bookmark-iOS-SDK.git'
pod 'iVersion', :git => 'git@github.com:naoya/iVersion.git', :branch => 'fix/japanese'
pod 'MPNotificationView'
pod 'GoogleAnalytics-iOS-SDK'

Expand Down
157 changes: 111 additions & 46 deletions app/app_delegate.rb
Original file line number Diff line number Diff line change
@@ -1,81 +1,110 @@
# -*- coding: utf-8 -*-
class AppDelegate
attr_accessor :viewController
attr_accessor :viewController, :timelineViewController, :bookmarksViewController
include HBFav2::RemoteNotificationDelegate
include HBFav2::GoogleAnalytics

def initialize
IVersion.sharedInstance.appStoreID = 477950722
IVersion.sharedInstance.remoteVersionsPlistURL = 'https://dl.dropboxusercontent.com/u/2586384/hbfav/versions.plist'
## デバッグ
# IVersion.sharedInstance.previewMode = true
end

def application(application, didFinishLaunchingWithOptions:launchOptions)
NSLog("RUBYMOTION_ENV: " + RUBYMOTION_ENV)

app_config = ApplicationConfig.sharedConfig
app_user = ApplicationUser.sharedUser.load

## initialize BugSense
if not Device.simulator? and app_user.send_bugreport?
BugSenseController.sharedControllerWithBugSenseAPIKey(
app_config.vars['bugsense']['api_key']
)
end

## initialize PocketAPI
PocketAPI.sharedAPI.setConsumerKey(
app_config.vars["pocket"]["consumer_key"]
)

## initialize Hatena-Bookmark SDK
HTBHatenaBookmarkManager.sharedManager.setConsumerKey(
app_config.vars['hatena']['consumer_key'],
consumerSecret:app_config.vars['hatena']['consumer_secret']
)

GoogleAPI.sharedAPI.api_key = app_config.vars['google']['api_key']

self.configure_bugsense_service(app_config)
self.configure_pocket_service(app_config)
self.configure_hatena_bookmark_service(app_config)
self.configure_google_api_service(app_config)
self.configure_google_analytics(app_config.vars['google_analytics']['tracking_id'])
self.configure_parse_service(app_config, launchOptions)

self.initialize_audio_session
self.configure_navigation_bar
self.configure_bar_button_item
self.configure_status_bar
self.initialize_window

## Notification Center のプッシュ通知履歴から開いたとき
if launchOptions.present?
notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]
if notification
payload = notification.userInfo
else
payload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]
end
self.handleNotificationPayload(payload) if payload.present?
else
watch_pasteboard
end

self.configure_background_fetch
true
end

def configure_status_bar
if UIDevice.currentDevice.ios7?
UIApplication.sharedApplication.setStatusBarStyle(UIStatusBarStyleLightContent)
else
UIApplication.sharedApplication.setStatusBarStyle(UIStatusBarStyleBlackOpaque)
end
end

def initialize_window
app_user = ApplicationUser.sharedUser.load

@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)

view_controllers = self.initialize_view_controllers(app_user)

@viewController = HBFav2PanelController.sharedController
@viewController.leftGapPercentage = 0.7
@viewController.leftPanel = LeftViewController.new

@leftViewController = LeftViewController.new
@leftViewController.controllers = view_controllers
@viewController.leftPanel = @leftViewController

@viewController.centerPanel = HBFav2NavigationController.alloc.initWithRootViewController(
TimelineViewController.new.tap do |c|
c.user = app_user.to_bookmark_user
c.as_home = true
end
@timelineViewController
)
@window.rootViewController = @viewController
@window.makeKeyAndVisible
end

## Notification Center のプッシュ通知履歴から開いたとき
if launchOptions.present?
notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]
if notification
payload = notification.userInfo
else
payload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]
end
self.handleNotificationPayload(payload) if payload.present?
else
watch_pasteboard
def initialize_view_controllers(app_user)
user = app_user.to_bookmark_user

@timelineViewController = TimelineViewController.new.tap do |c|
c.user = user
c.content_type = :timeline
c.as_home = true
end
true

@bookmarksViewController = TimelineViewController.new.tap do |c|
c.user = user
c.content_type = :bookmark
c.title = user.name
c.as_home = true
end

@hotentryViewController = HotentryViewController.new.tap do |c|
c.list_type = :hotentry
c.as_home = true
end

@entrylistViewController = HotentryViewController.new.tap do |c|
c.list_type = :entrylist
c.as_home = true
end

@accountViewController = AccountViewController.new.tap { |c| c.as_home = true }
@appInfoViewController = AppInfoViewController.new.tap { |c| c.as_home = true }

return {
:timeline => @timelineViewController,
:bookmarks => @bookmarksViewController,
:hotentry => @hotentryViewController,
:entrylist => @entrylistViewController,
:account => @accountViewController,
:appInfo => @appInfoViewController,
}
end

def development?
Expand All @@ -86,6 +115,31 @@ def release?
!development?
end

def configure_bugsense_service(app_config)
if not Device.simulator? and ApplicationUser.sharedUser.send_bugreport?
BugSenseController.sharedControllerWithBugSenseAPIKey(
app_config.vars['bugsense']['api_key']
)
end
end

def configure_pocket_service(app_config)
PocketAPI.sharedAPI.setConsumerKey(
app_config.vars["pocket"]["consumer_key"]
)
end

def configure_hatena_bookmark_service(app_config)
HTBHatenaBookmarkManager.sharedManager.setConsumerKey(
app_config.vars['hatena']['consumer_key'],
consumerSecret:app_config.vars['hatena']['consumer_secret']
)
end

def configure_google_api_service(app_config)
GoogleAPI.sharedAPI.api_key = app_config.vars['google']['api_key']
end

def configure_parse_service(app_config, launchOptions)
## initialize Parse.com
if development?
Expand Down Expand Up @@ -133,6 +187,13 @@ def configure_bar_button_item
end
end

def configure_background_fetch
if UIDevice.currentDevice.ios7?
NSLog("Background Fetch Enabled")
UIApplication.sharedApplication.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
end
end

def application(application, openURL:url, sourceApplication:sourceApplication, annotation:annotation)
if matches = url.absoluteString.match(%r{^hbfav:/entry/(https?://.*)})
self.presentWebViewControllerWithURL(matches[1])
Expand Down Expand Up @@ -220,6 +281,10 @@ def handleNotificationPayload(payload)
end
end
end

def application(application, performFetchWithCompletionHandler:completionHandler)
self.timelineViewController.performBackgroundFetchWithCompletion(completionHandler)
end
end

if RUBYMOTION_ENV == 'release'
Expand Down
59 changes: 17 additions & 42 deletions app/controller/left_view_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# -*- coding: utf-8 -*-
class LeftViewController < UITableViewController
attr_accessor :controllers

def viewDidLoad
super
ApplicationUser.sharedUser.addObserver(self, forKeyPath:'hatena_id', options:0, context:nil)
@dataSource = initialize_controllers_for(ApplicationUser.sharedUser.to_bookmark_user)
@dataSource = initialize_data_source

self.configure_separator_color
@selected = [0, rowForTimeline].nsindexpath
Expand All @@ -20,69 +22,37 @@ def configure_separator_color
end
end

def initialize_controllers_for(user)
## initialize navigation controllers
@timeline = self.sidePanelController.centerPanel
@bookmark = HBFav2NavigationController.alloc.initWithRootViewController(
TimelineViewController.new.tap do |c|
c.user = user
c.content_type = :bookmark
c.title = user.name
c.as_home = true
end
)

@hotentry = HBFav2NavigationController.alloc.initWithRootViewController(
HotentryViewController.new.tap do |c|
c.list_type = :hotentry
c.as_home = true
end
)

@entrylist = HBFav2NavigationController.alloc.initWithRootViewController(
HotentryViewController.new.tap do
|c| c.list_type = :entrylist
c.as_home = true
end
)

@config = HBFav2NavigationController.alloc.initWithRootViewController(
AccountViewController.new.tap { |c| c.as_home = true }
)

@appinfo = HBFav2NavigationController.alloc.initWithRootViewController(
AppInfoViewController.new.tap { |c| c.as_home = true }
)

def initialize_data_source
user = ApplicationUser.sharedUser.to_bookmark_user
src = [
{
:title => user.name,
:controller => @config,
:controller => HBFav2NavigationController.alloc.initWithRootViewController(controllers[:account]),
:image => profile_image_for(user)
},
{
:title => 'タイムライン',
:controller => @timeline,
:controller => HBFav2NavigationController.alloc.initWithRootViewController(controllers[:timeline]),
:image => UIImage.imageNamed('insignia_star')
},
{
:title => 'ブックマーク',
:controller => @bookmark,
:controller => HBFav2NavigationController.alloc.initWithRootViewController(controllers[:bookmarks]),
:image => UIImage.imageNamed('insignia_tags')
},
{
:title => '人気エントリー',
:controller => @hotentry,
:controller => HBFav2NavigationController.alloc.initWithRootViewController(controllers[:hotentry]),
:image => UIImage.imageNamed('insignia_heart')
},
{
:title => '新着エントリー',
:controller => @entrylist,
:controller => HBFav2NavigationController.alloc.initWithRootViewController(controllers[:entrylist]),
:image => UIImage.imageNamed('insignia_file')
},
{
:title => "アプリについて",
:controller => @appinfo,
:controller => HBFav2NavigationController.alloc.initWithRootViewController(controllers[:appInfo]),
:image => UIImage.imageNamed('default_app_logo')
}
]
Expand Down Expand Up @@ -156,7 +126,12 @@ def tableView(tableView, didSelectRowAtIndexPath:indexPath)
def observeValueForKeyPath(keyPath, ofObject:object, change:change, context:context)
if (ApplicationUser.sharedUser == object and keyPath == 'hatena_id')
## FIXME: 生データいじりすぎてて微妙
row = @dataSource[0]
row = if UIDevice.currentDevice.ios7?
@dataSource[1]
else
@dataSource[0]
end

user = ApplicationUser.sharedUser.to_bookmark_user
row[:title] = user.name
row[:image] = profile_image_for(user)
Expand Down
Loading