2021-06-19 12:33:29 +02:00
//
// T a b l e N o d e D i f f a b l e D a t a S o u r c e . s w i f t
// M a s t o d o n
//
// C r e a t e d b y C i r n o M a i n a s u K o n 2 0 2 1 - 6 - 1 9 .
//
2021-06-22 07:41:40 +02:00
#if ASDK
2021-06-19 12:33:29 +02:00
import UIKit
import AsyncDisplayKit
import DiffableDataSources
open class TableNodeDiffableDataSource < SectionIdentifierType : Hashable , ItemIdentifierType : Hashable > : NSObject , ASTableDataSource {
// / T h e t y p e o f c l o s u r e p r o v i d i n g t h e c e l l .
public typealias CellProvider = ( ASTableNode , IndexPath , ItemIdentifierType ) -> ASCellNodeBlock ?
// / T h e d e f a u l t a n i m a t i o n t o u p d a t i n g t h e v i e w s .
public var defaultRowAnimation : UITableView . RowAnimation = . automatic
private weak var tableNode : ASTableNode ?
private let cellProvider : CellProvider
private let core = DiffableDataSourceCore < SectionIdentifierType , ItemIdentifierType > ( )
// / C r e a t e s a n e w d a t a s o u r c e .
// /
// / - P a r a m e t e r s :
// / - t a b l e V i e w : A t a b l e v i e w i n s t a n c e t o b e m a n a g e d .
// / - c e l l P r o v i d e r : A c l o s u r e t o d e q u e u e t h e c e l l f o r r o w s .
public init ( tableNode : ASTableNode , cellProvider : @ escaping CellProvider ) {
self . tableNode = tableNode
self . cellProvider = cellProvider
super . init ( )
tableNode . dataSource = self
}
// / A p p l i e s g i v e n s n a p s h o t t o p e r f o r m a u t o m a t i c d i f f i n g u p d a t e .
// /
// / - P a r a m e t e r s :
// / - s n a p s h o t : A s n a p s h o t o b j e c t t o b e a p p l i e d t o d a t a m o d e l .
// / - a n i m a t i n g D i f f e r e n c e s : A B o o l e a n v a l u e i n d i c a t i n g w h e t h e r t o u p d a t e w i t h
// / d i f f i n g a n i m a t i o n .
// / - c o m p l e t i o n : A n o p t i o n a l c o m p l e t i o n b l o c k w h i c h i s c a l l e d w h e n t h e c o m p l e t e
// / p e r f o r m i n g u p d a t e s .
public func apply ( _ snapshot : DiffableDataSourceSnapshot < SectionIdentifierType , ItemIdentifierType > , animatingDifferences : Bool = true , completion : ( ( ) -> Void ) ? = nil ) {
core . apply ( snapshot , view : tableNode , animatingDifferences : animatingDifferences , completion : completion )
}
// / R e t u r n s a n e w s n a p s h o t o b j e c t o f c u r r e n t s t a t e .
// /
// / - R e t u r n s : A n e w s n a p s h o t o b j e c t o f c u r r e n t s t a t e .
public func snapshot ( ) -> DiffableDataSourceSnapshot < SectionIdentifierType , ItemIdentifierType > {
return core . snapshot ( )
}
// / R e t u r n s a n i t e m i d e n t i f i e r f o r g i v e n i n d e x p a t h .
// /
// / - P a r a m e t e r s :
// / - i n d e x P a t h : A n i n d e x p a t h f o r t h e i t e m i d e n t i f i e r .
// /
// / - R e t u r n s : A n i t e m i d e n t i f i e r f o r g i v e n i n d e x p a t h .
public func itemIdentifier ( for indexPath : IndexPath ) -> ItemIdentifierType ? {
return core . itemIdentifier ( for : indexPath )
}
// / R e t u r n s a n i n d e x p a t h f o r g i v e n i t e m i d e n t i f i e r .
// /
// / - P a r a m e t e r s :
// / - i t e m I d e n t i f i e r : A n i d e n t i f i e r o f i t e m .
// /
// / - R e t u r n s : A n i n d e x p a t h f o r g i v e n i t e m i d e n t i f i e r .
public func indexPath ( for itemIdentifier : ItemIdentifierType ) -> IndexPath ? {
return core . indexPath ( for : itemIdentifier )
}
// / R e t u r n s t h e n u m b e r o f s e c t i o n s i n t h e d a t a s o u r c e .
// /
// / - P a r a m e t e r s :
// / - t a b l e N o d e : A t a b l e n o d e i n s t a n c e m a n a g e d b y ` s e l f ` .
// /
// / - R e t u r n s : T h e n u m b e r o f s e c t i o n s i n t h e d a t a s o u r c e .
public func numberOfSections ( in tableNode : ASTableNode ) -> Int {
return core . numberOfSections ( )
}
// / R e t u r n s t h e n u m b e r o f i t e m s i n t h e s p e c i f i e d s e c t i o n .
// /
// / - P a r a m e t e r s :
// / - t a b l e N o d e : A t a b l e n o d e i n s t a n c e m a n a g e d b y ` s e l f ` .
// / - s e c t i o n : A n i n d e x o f s e c t i o n .
// /
// / - R e t u r n s : T h e n u m b e r o f i t e m s i n t h e s p e c i f i e d s e c t i o n .
public func tableNode ( _ tableNode : ASTableNode , numberOfRowsInSection section : Int ) -> Int {
return core . numberOfItems ( inSection : section )
}
// / R e t u r n s a c e l l f o r r o w a t s p e c i f i e d i n d e x p a t h .
// /
// / - P a r a m e t e r s :
// / - t a b l e V i e w : A t a b l e v i e w i n s t a n c e m a n a g e d b y ` s e l f ` .
// / - i n d e x P a t h : A n i n d e x p a t h f o r c e l l .
// /
// / - R e t u r n s : A c e l l f o r r o w a t s p e c i f i e d i n d e x p a t h .
open func tableNode ( _ tableNode : ASTableNode , nodeBlockForRowAt indexPath : IndexPath ) -> ASCellNodeBlock {
let itemIdentifier = core . unsafeItemIdentifier ( for : indexPath )
guard let block = cellProvider ( tableNode , indexPath , itemIdentifier ) else {
fatalError ( " UITableView dataSource returned a nil cell for row at index path: \( indexPath ) , tableNode: \( tableNode ) , itemIdentifier: \( itemIdentifier ) " )
}
return block
}
}
2021-06-22 07:41:40 +02:00
#endif