@ -15,6 +15,7 @@ final class HomeVC: BaseVC, UITableViewDataSource, UITableViewDelegate, NewConve
private var hasLoadedInitialStateData : Bool = false
private var hasLoadedInitialStateData : Bool = false
private var hasLoadedInitialThreadData : Bool = false
private var hasLoadedInitialThreadData : Bool = false
private var isLoadingMore : Bool = false
private var isLoadingMore : Bool = false
private var isAutoLoadingNextPage : Bool = false
// MARK: - I n t i a l i z a t i o n
// MARK: - I n t i a l i z a t i o n
@ -319,6 +320,7 @@ final class HomeVC: BaseVC, UITableViewDataSource, UITableViewDelegate, NewConve
CATransaction . setCompletionBlock { [ weak self ] in
CATransaction . setCompletionBlock { [ weak self ] in
// C o m p l e t e p a g e l o a d i n g
// C o m p l e t e p a g e l o a d i n g
self ? . isLoadingMore = false
self ? . isLoadingMore = false
self ? . autoLoadNextPageIfNeeded ( )
}
}
// R e l o a d t h e t a b l e c o n t e n t ( a n i m a t e c h a n g e s a f t e r t h e f i r s t l o a d )
// R e l o a d t h e t a b l e c o n t e n t ( a n i m a t e c h a n g e s a f t e r t h e f i r s t l o a d )
@ -328,7 +330,7 @@ final class HomeVC: BaseVC, UITableViewDataSource, UITableViewDelegate, NewConve
insertSectionsAnimation : . none ,
insertSectionsAnimation : . none ,
reloadSectionsAnimation : . none ,
reloadSectionsAnimation : . none ,
deleteRowsAnimation : . bottom ,
deleteRowsAnimation : . bottom ,
insertRowsAnimation : . top ,
insertRowsAnimation : . none ,
reloadRowsAnimation : . none ,
reloadRowsAnimation : . none ,
interrupt : { $0 . changeCount > 100 } // P r e v e n t t o o m a n y c h a n g e s f r o m c a u s i n g p e r f o r m a n c e i s s u e s
interrupt : { $0 . changeCount > 100 } // P r e v e n t t o o m a n y c h a n g e s f r o m c a u s i n g p e r f o r m a n c e i s s u e s
) { [ weak self ] updatedData in
) { [ weak self ] updatedData in
@ -338,6 +340,36 @@ final class HomeVC: BaseVC, UITableViewDataSource, UITableViewDelegate, NewConve
CATransaction . commit ( )
CATransaction . commit ( )
}
}
private func autoLoadNextPageIfNeeded ( ) {
guard ! self . isAutoLoadingNextPage && ! self . isLoadingMore else { return }
self . isAutoLoadingNextPage = true
DispatchQueue . main . asyncAfter ( deadline : . now ( ) + PagedData . autoLoadNextPageDelay ) { [ weak self ] in
self ? . isAutoLoadingNextPage = false
// N o t e : W e s o r t t h e h e a d e r s a s w e w a n t t o p r i o r i t i s e l o a d i n g n e w e r p a g e s o v e r o l d e r o n e s
let sections : [ ( HomeViewModel . Section , CGRect ) ] = ( self ? . viewModel . threadData
. enumerated ( )
. map { index , section in ( section . model , ( self ? . tableView . rectForHeader ( inSection : index ) ? ? . zero ) ) } )
. defaulting ( to : [ ] )
let shouldLoadMore : Bool = sections
. contains { section , headerRect in
section = = . loadMore &&
headerRect != . zero &&
( self ? . tableView . bounds . contains ( headerRect ) = = true )
}
guard shouldLoadMore else { return }
self ? . isLoadingMore = true
DispatchQueue . global ( qos : . default ) . async { [ weak self ] in
self ? . viewModel . pagedDataObserver ? . load ( . pageAfter )
}
}
}
private func updateNavBarButtons ( ) {
private func updateNavBarButtons ( ) {
// P r o f i l e p i c t u r e v i e w
// P r o f i l e p i c t u r e v i e w
let profilePictureSize = Values . verySmallProfilePictureSize
let profilePictureSize = Values . verySmallProfilePictureSize