/* Ċ Ġ* ĠCopyright Ġ 2 0 2 0 ĠM os by X Ġcontributors Ċ Ġ* Ċ Ġ* ĠLicensed Ġunder Ġthe ĠApache ĠLicense , ĠVersion Ġ 2 . 0 Ġ( the Ġ" License "); Ċ Ġ* Ġyou Ġmay Ġnot Ġuse Ġthis Ġfile Ġexcept Ġin Ġcompliance Ġwith Ġthe ĠLicense . Ċ Ġ* ĠYou Ġmay Ġobtain Ġa Ġcopy Ġof Ġthe ĠLicense Ġat Ċ Ġ* Ċ Ġ* Ġhttp :// www . apache . org / licenses / LICENSE - 2 . 0 Ċ Ġ* Ċ Ġ* ĠUnless Ġrequired Ġby Ġapplicable Ġlaw Ġor Ġagreed Ġto Ġin Ġwriting , Ġsoftware Ċ Ġ* Ġdistributed Ġunder Ġthe ĠLicense Ġis Ġdistributed Ġon Ġan Ġ" AS ĠIS " ĠBASIS , Ċ Ġ* ĠWITHOUT ĠWARRANTIES ĠOR ĠCONDITIONS ĠOF ĠANY ĠKIND , Ġeither Ġexpress Ġor Ġimplied . Ċ Ġ* ĠSee Ġthe ĠLicense Ġfor Ġthe Ġspecific Ġlanguage Ġgoverning Ġpermissions Ġand Ċ Ġ* Ġlimitations Ġunder Ġthe ĠLicense . Ċ Ġ*/ Ċ package Ġcom . g art es k . mos by x . m vi . sample . view . home Ċ Ċ import Ġandroid . view . LayoutInflater Ċ import Ġandroid . view . ViewGroup Ċ import Ġandroidx . recyclerview . widget . Diff Util Ċ import Ġandroidx . recyclerview . widget . RecyclerView Ċ import Ġcom . g art es k . mos by x . m vi . sample . business logic . model . Feed Item Ċ import Ġcom . g art es k . mos by x . m vi . sample . business logic . model . Feed Item .* Ċ import Ġcom . g art es k . mos by x . m vi . sample . view . home . view holder . Loading ViewHolder Ċ import Ġcom . g art es k . mos by x . m vi . sample . view . home . view holder . More Items ViewHolder Ċ import Ġcom . g art es k . mos by x . m vi . sample . view . home . view holder . More Items ViewHolder . Load Items ClickListener Ċ import Ġcom . g art es k . mos by x . m vi . sample . view . home . view holder . Section Header ViewHolder Ċ import Ġcom . g art es k . mos by x . m vi . sample . view . product . Product ViewHolder Ċ import Ġcom . g art es k . mos by x . m vi . sample . view . product . Product ViewHolder . Product Clicked Listener Ċ import Ġio . reactivex . Observable Ċ import Ġio . reactivex . subjects . Publish Subject Ċ Ċ class ĠHome Adapter ( Ċ ĉ private Ġval Ġlayout Inflater : ĠLayoutInflater , Ċ ĉ private Ġval Ġproduct Clicked Listener : ĠProduct Clicked Listener Ċ ) Ġ: ĠRecyclerView . Adapter < RecyclerView . ViewHolder >(), Ċ ĉ Load Items ClickListener Ġ{ ĊĊ ĉ comp anion Ġobject Ġ{ Ċĉ ĉ const Ġval ĠVIEW _ TYPE _ PRODUCT Ġ= Ġ 0 Ċĉ ĉ const Ġval ĠVIEW _ TYPE _ LOADING _ MORE _ NEXT _ PAGE Ġ= Ġ 1 Ċĉ ĉ const Ġval ĠVIEW _ TYPE _ SECTION _ HEADER Ġ= Ġ 2 Ċĉ ĉ const Ġval ĠVIEW _ TYPE _ MORE _ ITEMS _ AVAILABLE Ġ= Ġ 3 Ċ ĉ } ĊĊ ĉ var Ġloading Next Page Ġ= Ġfalse Ċĉ ĉ private Ġset ĊĊ ĉ var Ġitems : ĠList < Feed Item > Ġ= Ġempty List () Ċĉ ĉ private Ġset ĊĊ ĉ private Ġval Ġload More Items Of Category Observable Ġ= ĠPublish Subject . create < String >() ĊĊ ĉ /** Ċĉ Ġ* Ġ@ return Ġtrue Ġif Ġvalue Ġhas Ġchanged Ġsince Ġlast Ġinvocation Ċĉ Ġ*/ Ċ ĉ fun Ġset Loading Next Page ( loading Next Page : ĠBoolean ): ĠBoolean Ġ{ Ċĉ ĉ val Ġhas Loading More Changed Ġ= Ġloading Next Page Ġ!= Ġthis . loading Next Page Ċĉ ĉ val Ġnotify Inserted Ġ= Ġloading Next Page Ġ&& Ġhas Loading More Changed Ċĉ ĉ val Ġnotify Removed Ġ= Ġ! loading Next Page Ġ&& Ġhas Loading More Changed Ċĉ ĉ this . loading Next Page Ġ= Ġloading Next Page Ċĉ ĉ if Ġ( notify Inserted ) Ġ{ Ċĉĉ ĉ notify Item Inserted ( items . size ) Ċĉ ĉ } Ġelse Ġif Ġ( notify Removed ) Ġ{ Ċĉĉ ĉ notify Item Removed ( items . size ) Ċĉ ĉ } Ċĉ ĉ return Ġhas Loading More Changed Ċ ĉ } ĊĊ ĉ fun Ġset Items ( new Items : ĠList < Feed Item >) Ġ{ Ċĉ ĉ val Ġold Items Ġ= Ġitems Ċĉ ĉ items Ġ= Ġnew Items Ċĉ ĉ Diff Util . calculate Diff ( object Ġ: ĠDiff Util . Callback () Ġ{ Ċĉĉ ĉ override Ġfun Ġget Old List Size (): ĠInt Ġ{ Ċĉĉĉ ĉ return Ġold Items . size Ċĉĉ ĉ } ĊĊĉĉ ĉ override Ġfun Ġget New List Size (): ĠInt Ġ{ Ċĉĉĉ ĉ return Ġnew Items . size Ċĉĉ ĉ } ĊĊĉĉ ĉ override Ġfun Ġare Items The Same ( old Item Position : ĠInt , Ġnew Item Position : ĠInt ): ĠBoolean Ġ{ Ċĉĉĉ ĉ val Ġold Item : ĠAny Ġ= Ġold Items [ old Item Position ] Ċĉĉĉ ĉ val Ġnew Item : ĠAny Ġ= Ġnew Items [ new Item Position ] ĊĊĉĉĉ ĉ val Ġproducts Same Ġ= Ġold Item Ġis ĠProduct Ċĉĉĉĉĉ ĉ && Ġnew Item Ġis ĠProduct Ċĉĉĉĉĉ ĉ && Ġold Item . id Ġ== Ġnew Item . id Ċĉĉĉ ĉ val Ġsection Headers Same Ġ= Ġold Item Ġis ĠSection Header Ċĉĉĉĉĉ ĉ && Ġnew Item Ġis ĠSection Header Ċĉĉĉĉĉ ĉ && Ġold Item . name Ġ== Ġnew Item . name Ċĉĉĉ ĉ val Ġadditional Items Same Ġ= Ġold Item Ġis ĠAdditional Items Load able Ċĉĉĉĉĉ ĉ && Ġnew Item Ġis ĠAdditional Items Load able Ċĉĉĉĉĉ ĉ && Ġold Item . category Name Ġ== Ġnew Item . category Name ĊĊĉĉĉ ĉ return Ġproducts Same Ġ|| Ġsection Headers Same Ġ|| Ġadditional Items Same Ċĉĉ ĉ } ĊĊĉĉ ĉ override Ġfun Ġare Contents The Same ( old Item Position : ĠInt , Ġnew Item Position : ĠInt ): ĠBoolean Ġ{ Ċĉĉĉ ĉ val Ġold Item : ĠAny Ġ= Ġold Items [ old Item Position ] Ċĉĉĉ ĉ val Ġnew Item : ĠAny Ġ= Ġnew Items [ new Item Position ] Ċĉĉĉ ĉ return Ġold Item Ġ== Ġnew Item Ċĉĉ ĉ } Ċĉ ĉ }, Ġtrue ) Ċĉĉ ĉ . dispatch Updates To ( this ) Ċ ĉ } ĊĊ ĉ override Ġfun ĠgetItem ViewType ( position : ĠInt ): ĠInt Ġ{ Ċĉ ĉ if Ġ( loading Next Page Ġ&& Ġposition Ġ== Ġitems . size ) Ġ{ Ċĉĉ ĉ return ĠVIEW _ TYPE _ LOADING _ MORE _ NEXT _ PAGE Ċĉ ĉ } ĊĊĉ ĉ return Ġwhen Ġ( val Ġitem Ġ= Ġitems [ position ]) Ġ{ Ċĉĉ ĉ is ĠProduct Ġ-> ĠVIEW _ TYPE _ PRODUCT Ċĉĉ ĉ is ĠSection Header Ġ-> ĠVIEW _ TYPE _ SECTION _ HEADER Ċĉĉ ĉ is ĠAdditional Items Load able Ġ-> ĠVIEW _ TYPE _ MORE _ ITEMS _ AVAILABLE Ċĉĉ ĉ else Ġ-> Ġthrow ĠIllegalArgumentException (" Not Ġable Ġto Ġdetermine Ġthe Ġview Ġtype Ġfor Ġitem Ġat Ġposition Ġ$ position . ĠItem Ġis : Ġ$ item ") Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ override Ġfun ĠonCreate ViewHolder ( parent : ĠViewGroup , Ġview Type : ĠInt ): ĠRecyclerView . ViewHolder Ġ= Ċĉ ĉ when Ġ( view Type ) Ġ{ Ċĉĉ ĉ VIEW _ TYPE _ PRODUCT Ġ-> ĠProduct ViewHolder . create ( layout Inflater , Ġproduct Clicked Listener ) Ċĉĉ ĉ VIEW _ TYPE _ LOADING _ MORE _ NEXT _ PAGE Ġ-> ĠLoading ViewHolder . create ( layout Inflater ) Ċĉĉ ĉ VIEW _ TYPE _ MORE _ ITEMS _ AVAILABLE Ġ-> ĠMore Items ViewHolder . create ( layout Inflater , Ġthis ) Ċĉĉ ĉ VIEW _ TYPE _ SECTION _ HEADER Ġ-> ĠSection Header ViewHolder . create ( layout Inflater ) Ċĉĉ ĉ else Ġ-> Ġthrow ĠIllegalArgumentException (" Couldn 't Ġcreate Ġa ĠViewHolder Ġfor Ġview Type Ġ Ġ= Ġ$ view Type ") Ċĉ ĉ } ĊĊ ĉ override Ġfun Ġon Bind ViewHolder ( holder : ĠRecyclerView . ViewHolder , Ġposition : ĠInt ) Ġ{ Ċĉ ĉ if Ġ( holder Ġis ĠLoading ViewHolder ) Ġ{ Ċĉĉ ĉ return Ċĉ ĉ } Ċĉ ĉ val Ġitem Ġ= Ġitems [ position ] Ċĉ ĉ when Ġ( holder ) Ġ{ Ċĉĉ ĉ is ĠProduct ViewHolder Ġ-> Ġholder . bind ( item Ġas ĠProduct ) Ċĉĉ ĉ is ĠSection Header ViewHolder Ġ-> Ġholder . on Bind ( item Ġas ĠSection Header ) Ċĉĉ ĉ is ĠMore Items ViewHolder Ġ-> Ġholder . bind ( item Ġas ĠAdditional Items Load able ) Ċĉĉ ĉ else Ġ-> Ġthrow ĠIllegalArgumentException (" co ul dn 't Ġaccept ĠViewHolder Ġ$ holder ") Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ override Ġfun ĠgetItem Count (): ĠInt Ġ{ Ċĉ ĉ return Ġitems . size Ġ+ Ċĉĉĉ ĉ if Ġ( loading Next Page ) Ġ{ Ċĉĉĉĉĉ 1 Ċĉĉĉ ĉ } Ġelse Ġ{ Ċĉĉĉĉĉ 0 Ċĉĉĉ ĉ } Ċ ĉ } ĊĊ ĉ override Ġfun Ġload Items For Category ( category : ĠString ) Ġ{ Ċĉ ĉ load More Items Of Category Observable . onNext ( category ) Ċ ĉ } ĊĊ ĉ fun Ġload More Items Of Category Observable (): ĠObservable < String > Ġ{ Ċĉ ĉ return Ġload More Items Of Category Observable Ċ ĉ } Ċ Ċ }