package Ġcom . m ga et an 8 9 . show sr age . fragment Ċ Ċ import Ġandroid . os . Bundle Ċ import Ġandroid . support . v 4 . app . Fragment Ċ import Ġandroid . support . v 4 . widget . Swipe Refresh Layout Ċ import Ġandroid . support . v 7 . widget . Divider Item Decoration Ċ import Ġandroid . support . v 7 . widget . Grid LayoutManager Ċ import Ġandroid . support . v 7 . widget . RecyclerView Ċ import Ġandroid . view . LayoutInflater Ċ import Ġandroid . view . View Ċ import Ġandroid . view . ViewGroup Ċ import Ġcom . m ga et an 8 9 . show sr age . Constants Ċ import Ġcom . m ga et an 8 9 . show sr age . R Ċ import Ġcom . m ga et an 8 9 . show sr age . adapter . Episode s Adapter Ċ import Ġcom . m ga et an 8 9 . show sr age . extension . get Episode Sort Ċ import Ġcom . m ga et an 8 9 . show sr age . extension . get Episode s Ċ import Ġcom . m ga et an 8 9 . show sr age . extension . get Preferences Ċ import Ġcom . m ga et an 8 9 . show sr age . extension . save Episode s Ċ import Ġcom . m ga et an 8 9 . show sr age . model . Episode Ċ import Ġcom . m ga et an 8 9 . show sr age . model . Episode s Ċ import Ġcom . m ga et an 8 9 . show sr age . model . Sort Ċ import Ġcom . m ga et an 8 9 . show sr age . network . S ick R age Api Ċ import Ġio . realm . Realm Ċ import Ġio . realm . Realm ChangeListener Ċ import Ġio . realm . Realm Results Ċ import Ġkotlinx . android . synt hetic . main . fragment _ season . empty Ċ import Ġkotlinx . android . synt hetic . main . fragment _ season . list Ċ import Ġkotlinx . android . synt hetic . main . fragment _ season . swipe _ refresh Ċ import Ġretrofit . Callback Ċ import Ġretrofit . Ret rofit Error Ċ import Ġretrofit . client . Response Ċ Ċ class ĠSe ason Fragment Ġ: ĠFragment (), ĠCallback < Episode s >, ĠSw ipe Refresh Layout . On Refresh Listener , ĠRealm ChangeListener < Realm Results < Episode >> Ġ{ Ċ ĉ private Ġlateinit Ġvar Ġepisode s : ĠRealm Results < Episode > Ċ ĉ private Ġvar Ġindexer Id : ĠInt Ġ= Ġ 0 Ċ ĉ private Ġlateinit Ġvar Ġrealm : ĠRealm Ċ ĉ private Ġvar Ġreversed Order Ġ= Ġfalse Ċ ĉ private Ġvar Ġseason Number : ĠInt Ġ= Ġ 0 ĊĊ ĉ override Ġfun Ġfailure ( error : ĠRet rofit Error ?) Ġ{ Ċĉ ĉ this . swipe _ refresh ?. is Refresh ing Ġ= Ġfalse ĊĊĉ ĉ error ?. printStackTrace () Ċ ĉ } ĊĊ ĉ override Ġfun ĠonChange ( episodes : ĠRealm Results < Episode >) Ġ{ Ċĉ ĉ if Ġ( this . episodes . isEmpty ()) Ġ{ Ċĉĉ ĉ this . empty ?. visibility Ġ= ĠView . VISIBLE Ċĉĉ ĉ this . list ?. visibility Ġ= ĠView . GONE Ċĉ ĉ } Ġelse Ġ{ Ċĉĉ ĉ this . empty ?. visibility Ġ= ĠView . GONE Ċĉĉ ĉ this . list ?. visibility Ġ= ĠView . VISIBLE Ċĉ ĉ } ĊĊĉ ĉ this . list ?. adapter ?. notify DataSet Changed () Ċ ĉ } ĊĊ ĉ override Ġfun ĠonCreate ( savedInstanceState : ĠBundle ?) Ġ{ Ċĉ ĉ super . onCreate ( savedInstanceState ) ĊĊĉ ĉ this . indexer Id Ġ= Ġthis . arguments ?. getInt ( Constants . Bundle . INDEX ER _ ID ) Ġ?: Ġ 0 Ċĉ ĉ this . reversed Order Ġ= ĠSort . DESC ENDING Ġ== Ġthis . context ?. get Preferences ()?. get Episode Sort () Ċĉ ĉ this . season Number Ġ= Ġthis . arguments ?. getInt ( Constants . Bundle . SE ASON _ NUMBER ) Ġ?: Ġ 0 Ċ ĉ } ĊĊ ĉ override Ġfun ĠonCreate View ( in flater : ĠLayoutInflater , Ġcontainer : ĠViewGroup ?, ĠsavedInstanceState : ĠBundle ?): ĠView ? Ċĉĉ ĉ = Ġinflater . inflate ( R . layout . fragment _ season , Ġcontainer , Ġfalse ) ĊĊ ĉ override Ġfun Ġon Refresh () Ġ{ Ċĉ ĉ this . swipe _ refresh ?. is Refresh ing Ġ= Ġtrue ĊĊĉ ĉ S ick R age Api . instance . services ?. get Episode s ( this . indexer Id , Ġthis . season Number , Ġthis ) Ċ ĉ } ĊĊ ĉ override Ġfun Ġon Resume () Ġ{ Ċĉ ĉ super . on Resume () ĊĊĉ ĉ this . on Refresh () Ċ ĉ } ĊĊ ĉ override Ġfun ĠonStart () Ġ{ Ċĉ ĉ super . onStart () ĊĊĉ ĉ this . realm Ġ= ĠRealm . getDefaultInstance () Ċĉ ĉ this . episodes Ġ= Ġthis . realm . get Episode s ( this . indexer Id , Ġthis . season Number , Ġthis . reversed Order , Ġthis ) Ċĉ ĉ this . list ?. adapter Ġ= ĠEp isode s Adapter ( this . episodes , Ġthis . season Number , Ġthis . indexer Id , Ġthis . reversed Order ) Ċ ĉ } ĊĊ ĉ override Ġfun Ġon Stop () Ġ{ Ċĉ ĉ if Ġ( this . episodes . isValid ) Ġ{ Ċĉĉ ĉ this . episodes . removeAll Change Listeners () Ċĉ ĉ } ĊĊĉ ĉ this . realm . close () ĊĊĉ ĉ super . on Stop () Ċ ĉ } ĊĊ ĉ override Ġfun Ġon View Created ( view : ĠView , ĠsavedInstanceState : ĠBundle ?) Ġ{ Ċĉ ĉ val Ġcolumn Count Ġ= Ġthis . resources . getInteger ( R . integer . shows _ column _ count ) Ċĉ ĉ val Ġlayout Manager Ġ= ĠGrid LayoutManager ( this . activity , Ġcolumn Count ) ĊĊĉ ĉ this . list ?. add On Scroll Listener ( object Ġ: ĠRecyclerView . On Scroll Listener () Ġ{ Ċĉĉ ĉ override Ġfun Ġon Sc rolled ( re cyclerView : ĠRecyclerView ?, Ġdx : ĠInt , Ġdy : ĠInt ) Ġ{ Ċĉĉĉ ĉ super . on Sc rolled ( re cyclerView , Ġdx , Ġdy ) ĊĊĉĉĉ ĉ swipe _ refresh ?. isEnabled Ġ= Ġ!( re cyclerView ?. can Scroll Vert ically (- 1 ) Ġ?: Ġfalse ) Ċĉĉ ĉ } Ċĉ ĉ }) Ċĉ ĉ this . list ?. layout Manager Ġ= Ġlayout Manager Ċĉ ĉ this . list ?. addItem Decoration ( Divider Item Decoration ( this . context , ĠDiv ider Item Decoration . VERTICAL )) ĊĊĉ ĉ this . swipe _ refresh ?. setColor Scheme Resources ( R . color . accent ) Ċĉ ĉ this . swipe _ refresh ?. setOn Refresh Listener ( this ) Ċ ĉ } ĊĊ ĉ override Ġfun Ġsuccess ( episodes : ĠEp isode s ?, Ġresponse : ĠResponse ?) Ġ{ Ċĉ ĉ this . swipe _ refresh ?. is Refresh ing Ġ= Ġfalse ĊĊĉ ĉ val Ġepisode s List Ġ= Ġepisode s ?. data ?. map Ġ{ Ċĉĉ ĉ it . value . number Ġ= Ġit . key Ċĉĉ ĉ it . value Ċĉ ĉ } Ġ?: Ġempty List () ĊĊĉ ĉ Realm . getDefaultInstance (). use Ġ{ Ċĉĉ ĉ it . save Episode s ( episodes List , Ġthis . indexer Id , Ġthis . season Number ) Ċĉ ĉ } Ċ ĉ } Ċ } Ċ