Skip to content

Instantly share code, notes, and snippets.

@j-mcnally
Last active December 30, 2015 18:19
Show Gist options
  • Save j-mcnally/7866928 to your computer and use it in GitHub Desktop.
Save j-mcnally/7866928 to your computer and use it in GitHub Desktop.
Loadmoreable
module ProMotion
module Table
module LoadmoreClassMethods
def load_moreable=(val)
@load_moreable = val
end
def load_moreable
@load_moreable || false
end
def load_more_options=(val)
@load_more_options = val
end
def load_more_options
@load_more_options || false
end
def loadmoreable(options={})
self.load_moreable = true
self.load_more_options = options
end
end
module Loadmoreable
def loadmore_footer_view
return @footerview if @footerview.present?
@footerview = UIView.alloc.initWithFrame(CGRectMake(0,0,self.view.frame.size.width,footerLoadMoreHeight))
@load_more_label = UILabel.alloc.initWithFrame(CGRectMake(0,15,self.view.frame.size.width,footerLoadMoreHeight-15))
@load_more_label.text = @options[:pull_message]
@load_more_label.textAlignment = NSTextAlignmentCenter
@footerview.addSubview(@load_more_label)
@footerview
@footerview
end
def load_footer_active
@footer_activity_view = UIActivityIndicatorView.alloc.initWithActivityIndicatorStyle(UIActivityIndicatorViewStyleGray)
@footerview.addSubview(@footer_activity_view)
@load_more_label.text = @options[:loading_message]
@footer_activity_view.frame = CGRectMake((self.view.frame.size.width/2)-10,10,20,20)
@footer_activity_view.startAnimating
end
def start_load_more
@isLoadingMore = true
load_footer_active
self.send(@options[:callback])
end
def done_load_more
@isLoadingMore = false
hide_footer
end
def load_more
done_load_more
end
def screen_setup
super
set_up_loademoreable
end
def canLoadMore(scrollView)
offset = scrollView.contentInset.top
offsetBottom = scrollView.contentInset.bottom
contentHeight = scrollView.contentSize.height
viewPortHeight = scrollView.frame.size.height
scrollPosition = scrollView.contentOffset.y
(contentHeight+offset+offsetBottom > viewPortHeight)
end
def set_up_loademoreable
if self.class.load_moreable
@loadingMore = false
@isLoadingMore = false
self.make_loadmoreable(self.class.load_more_options)
end
end
def loadmore_defaults
{
:callback => :load_more,
:pull_message => "Pull to Load More...",
:loading_message => "Loading More"
}
end
def make_loadmoreable(params={})
@options = loadmore_defaults.merge(params)
self.tableView.setDelegate(self)
end
def footerLoadMoreHeight
70
end
def show_footer
unless @footerVisible
@footerVisible = true
self.tableView.tableFooterView = self.loadmore_footer_view
end
end
def hide_footer
if @footerVisible
scrollView = self.tableView
offset = scrollView.contentInset.top
offsetBottom = scrollView.contentInset.bottom
contentHeight = scrollView.contentSize.height
viewPortHeight = scrollView.frame.size.height
scrollPosition = scrollView.contentOffset.y
positionBottom = contentHeight-(viewPortHeight-offsetBottom)-footerLoadMoreHeight
UIView.animateWithDuration 0.4, animations:lambda {
self.tableView.setContentOffset(CGPointMake(0, positionBottom), animated:false)
}, completion:lambda {|finished|
self.tableView.tableFooterView = nil
@footer_activity_view.removeFromSuperview if @footer_activity_view.present?
@load_more_label.text = @options[:pull_message] if @load_more_label.present?
@footerVisible = false
}
end
end
def scrollViewDidEndDragging(scrollView, willDecelerate:decel)
if superclass.respond_to?(:scrollViewDidScroll)
super(scrollView)
end
offset = scrollView.contentInset.top
offsetBottom = scrollView.contentInset.bottom
contentHeight = scrollView.contentSize.height
viewPortHeight = scrollView.frame.size.height
scrollPosition = scrollView.contentOffset.y
positionBottom = contentHeight-viewPortHeight-scrollPosition+offsetBottom
if @footerVisible
if positionBottom < 0
start_load_more
else
unless decel
hide_footer
end
end
end
end
def scrollViewDidScroll(scrollView)
if superclass.respond_to?(:scrollViewDidScroll)
super(scrollView)
end
if scrollView.contentSize.height > 0
if canLoadMore(scrollView)
offset = scrollView.contentInset.top
offsetBottom = scrollView.contentInset.bottom
contentHeight = scrollView.contentSize.height
viewPortHeight = scrollView.frame.size.height
scrollPosition = scrollView.contentOffset.y
positionBottom = contentHeight-viewPortHeight-scrollPosition+offsetBottom
if positionBottom < 0
unless @footerVisible
show_footer
end
else
end
end
end
end
def self.included(base)
base.extend(ProMotion::Table::LoadmoreClassMethods)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment