package Ġcom . j ur k owski . news app . ui . fragments Ċ Ċ import Ġandroid . os . Bundle Ċ import Ġandroid . view . View Ċ import Ġandroidx . fragment . app . Fragment Ċ import Ġandroidx . lifecycle . Observer Ċ import Ġandroidx . navigation . fragment . find Nav Controller Ċ import Ġandroidx . recyclerview . widget . Item Touch Helper Ċ import Ġandroidx . recyclerview . widget . Linear LayoutManager Ċ import Ġandroidx . recyclerview . widget . RecyclerView Ċ import Ġcom . google . android . material . sn ackbar . Snackbar Ċ import Ġcom . j ur k owski . news app . R Ċ import Ġcom . j ur k owski . news app . adapters . News Adapter Ċ import Ġcom . j ur k owski . news app . ui . News Activity Ċ import Ġcom . j ur k owski . news app . ui . News ViewModel Ċ import Ġkotlinx . android . synt hetic . main . fragment _ saved _ news .* Ċ Ċ class ĠSaved News Fragment Ġ: ĠFragment ( R . layout . fragment _ saved _ news ) Ġ{ ĊĊĠ Ġprivate Ġlateinit Ġvar ĠviewModel : ĠNews ViewModel ĊĠ Ġprivate Ġlateinit Ġvar Ġnews Adapter : ĠNews Adapter ĊĊĠ Ġoverride Ġfun Ġon View Created ( view : ĠView , ĠsavedInstanceState : ĠBundle ?) Ġ{ ĊĠĠĠ Ġsuper . on View Created ( view , ĠsavedInstanceState ) ĊĠĠĠ ĠviewModel Ġ= Ġ( activity Ġas ĠNews Activity ). viewModel ĊĠĠĠ Ġsetup Re cycle View () ĊĊĠĠĠ Ġnews Adapter . setOn ItemClickListener Ġ{ ĊĠĠĠĠĠ Ġval Ġbundle Ġ= ĠBundle (). apply Ġ{ ĊĠĠĠĠĠĠĠ Ġput Serializable (" article ", Ġit ) ĊĠĠĠĠĠ Ġ} ĊĠĠĠĠĠ Ġfind Nav Controller (). navigate ( ĊĠĠĠĠĠĠĠ ĠR . id . action _ saved News Fragment _ to _ article Fragment , ĊĠĠĠĠĠĠĠ Ġbundle ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġval Ġitem Touch Helper Callback Ġ= Ġobject Ġ: ĠItem Touch Helper . Simple Callback ( ĊĠĠĠĠĠ ĠItem Touch Helper . UP Ġor ĠItem Touch Helper . DOWN , ĊĠĠĠĠĠ ĠItem Touch Helper . LEFT Ġor ĠItem Touch Helper . RIGHT ĊĠĠĠ Ġ) Ġ{ ĊĠĠĠĠĠ Ġoverride Ġfun Ġon Move ( ĊĠĠĠĠĠĠĠ Ġre cyclerView : ĠRecyclerView , ĊĠĠĠĠĠĠĠ ĠviewHolder : ĠRecyclerView . ViewHolder , ĊĠĠĠĠĠĠĠ Ġtarget : ĠRecyclerView . ViewHolder ĊĠĠĠĠĠ Ġ): ĠBoolean Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġtrue ĊĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠ Ġoverride Ġfun Ġon Sw ip ed ( view Holder : ĠRecyclerView . ViewHolder , Ġdirection : ĠInt ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġposition Ġ= ĠviewHolder . adapter Position ĊĠĠĠĠĠĠĠ Ġval Ġarticle Ġ= Ġnews Adapter . differ . current List [ position ] ĊĠĠĠĠĠĠĠ ĠviewModel . delete Article ( article ) ĊĠĠĠĠĠĠĠ ĠSn ackbar . make ( view , Ġ" Successfully Ġdeleted Ġarticle ", ĠSn ackbar . LENGTH _ LONG ). apply Ġ{ ĊĠĠĠĠĠĠĠĠĠ Ġset Action (" Undo ") Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠviewModel . save Article ( article ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ}. show () ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ ĠItem Touch Helper ( item Touch Helper Callback ). apply Ġ{ ĊĠĠĠĠĠ Ġattach To RecyclerView ( saved News RecyclerView ) ĊĠĠĠ Ġ} ĊĊĠĠĠ ĠviewModel . get Saved News (). observe ( view Lifecycle Owner , ĠObserver Ġ{ Ġarticles Ġ-> ĊĠĠĠĠĠ Ġnews Adapter . differ . submit List ( articles ) ĊĠĠĠ Ġ}) ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġsetup Re cycle View () Ġ{ ĊĠĠĠ Ġnews Adapter Ġ= ĠNews Adapter () ĊĠĠĠ Ġsaved News RecyclerView . apply Ġ{ ĊĠĠĠĠĠ Ġadapter Ġ= Ġnews Adapter ĊĠĠĠĠĠ Ġlayout Manager Ġ= ĠLinearLayoutManager ( activity ) ĊĠĠĠ Ġ} ĊĠ Ġ} Ċ }