/** Ċ * ĠCopyright ĠS or am its u ĠCo ., ĠLtd . ĠAll ĠRights ĠReserved . Ċ * ĠSPDX - License - Identifier : ĠGPL - 3 . 0 Ċ */ Ċ Ċ package Ġjp . co . sor am its u . feature _ vot able _ impl . data . local Ċ Ċ import Ġio . reactivex . Comp letable Ċ import Ġio . reactivex . Observable Ċ import Ġio . reactivex . Single Transformer Ċ import Ġio . reactivex . subjects . Behavior Subject Ċ import Ġjp . co . sor am its u . common . data . Preferences Ċ import Ġjp . co . sor am its u . feature _ vot able _ api . domain . interfaces . Votes DataSource Ċ import Ġjp . co . sor am its u . feature _ vot able _ impl . data . network . Project Network Api Ċ import Ġjp . co . sor am its u . feature _ vot able _ impl . data . network . response . Get Project Votes Response Ċ import Ġjavax . inject . Inject Ċ Ċ class ĠP refs Votes DataSource Ġ@ Inject Ġconstructor ( ĊĠĠĠ Ġprivate Ġval Ġproject Network Api : ĠProject Network Api , ĊĠĠĠ Ġprivate Ġval Ġpreferences : ĠPreferences Ċ ) Ġ: ĠV otes DataSource Ġ{ ĊĊĠĠĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠĠĠĠĠ Ġprivate Ġconst Ġval ĠKEY _ V OT ES Ġ= Ġ" key _ votes " ĊĠĠĠĠĠĠĠ Ġprivate Ġconst Ġval ĠKEY _ LAST _ V OT ES Ġ= Ġ" key _ projects _ last _ v oted " ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġval Ġvotes Observer Ġ= ĠBehavior Subject . create Default ( retrieve Votes ()) ĊĊĠĠĠ Ġoverride Ġfun Ġsync Votes (): ĠComp letable Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġproject Network Api . get Votes () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. compose ( write Votes Composer ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. ignore Element () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġobserve Votes (): ĠObservable < String > Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġif Ġ( has Local Copy ()) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġvotes Observer ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġproject Network Api . get Votes () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. compose ( write Votes Composer ()) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. flatMap Observable Ġ{ Ġvotes Observer Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġsave Votes ( votes : ĠString ) Ġ{ ĊĠĠĠĠĠĠĠ Ġpreferences . putString ( KEY _ V OT ES , Ġvotes ) ĊĊĠĠĠĠĠĠĠ Ġvotes Observer . onNext ( votes ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġretrieve Votes (): ĠString Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġpreferences . getString ( KEY _ V OT ES ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġsave Last Received Votes ( toString : ĠString ) Ġ{ ĊĠĠĠĠĠĠĠ Ġpreferences . putString ( KEY _ LAST _ V OT ES , ĠtoString ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġretrieve Last Received Votes (): ĠString Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġpreferences . getString ( KEY _ LAST _ V OT ES ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġwrite Votes Composer () Ġ= ĠSingle Transformer < Get Project Votes Response , ĠGet Project Votes Response > Ġ{ Ġsingle Ġ-> ĊĠĠĠĠĠĠĠ Ġsingle . do On Success Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġsave Votes ( it . votes . toString ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġsave Last Received Votes ( it . last Received Votes . toString ()) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġhas Local Copy () Ġ= Ġpreferences . contains ( KEY _ V OT ES ) Ċ } Ċ