/* Ċ Ġ* ĠCopyright ĠÂ© ĠM obi Lab ĠSolutions ĠGmbH Ċ Ġ*/ Ċ Ċ package Ġcom . mob il ab solutions . stash . sample . data . repositories . payment method Ċ Ċ import Ġcom . mob il ab solutions . stash . sample . data . entities . Error Result Ċ import Ġcom . mob il ab solutions . stash . sample . data . entities . PaymentMethod Ċ import Ġcom . mob il ab solutions . stash . sample . data . entities . Success Ċ import Ġcom . mob il ab solutions . stash . sample . data . repositories . cart . Local Cart Store Ċ import Ġcom . mob il ab solutions . stash . sample . extensions . launch Or Join Ċ import Ġcom . mob il ab solutions . stash . sample . network . request . Authorize Payment Request Ċ import Ġcom . mob il ab solutions . stash . sample . util . App Coroutine Dispatch ers Ċ import Ġkotlinx . coroutines . async Ċ import Ġkotlinx . coroutines . channels . Con fl ated Broadcast Channel Ċ import Ġkotlinx . coroutines . flow . Flow Ċ import Ġkotlinx . coroutines . flow . as Flow Ċ import Ġkotlinx . coroutines . supervisor Scope Ċ import Ġjavax . inject . Inject Ċ import Ġjavax . inject . Singleton Ċ Ċ /** Ċ Ġ* Ġ@ author Ġ< a Ġhref =" y is uk @ mob il ab solutions . com "> Y is uk ĠKim </ a > Ġon Ġ 1 2 - 0 4 - 2 0 1 9 . Ċ Ġ*/ Ċ @ Singleton Ċ class ĠPayment Method Repository Impl Ġ@ Inject Ġconstructor ( ĊĠĠĠ Ġprivate Ġval Ġdispatch ers : ĠApp Coroutine Dispatch ers , ĊĠĠĠ Ġprivate Ġval Ġlocal PaymentMethod Store : ĠLocal PaymentMethod Store , ĊĠĠĠ Ġprivate Ġval Ġlocal Cart Store : ĠLocal Cart Store , ĊĠĠĠ Ġprivate Ġval Ġremote PaymentMethod DataSource : ĠRemote PaymentMethod DataSource Ċ ) Ġ: ĠPayment Method Repository Ġ{ ĊĠĠĠ Ġprivate Ġval Ġ_ payment Completed Channel Ġ= ĠCon fl ated Broadcast Channel < Boolean >() ĊĊĠĠĠ Ġoverride Ġfun Ġobserve Payment Completed (): ĠFlow < Boolean > Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġ_ payment Completed Channel . as Flow () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġsuspend Ġfun Ġcomplete Payment () Ġ{ ĊĠĠĠĠĠĠĠ Ġ_ payment Completed Channel . offer ( false ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġobserve Payment Methods () Ġ= Ġlocal PaymentMethod Store . observe Payment Methods () ĊĊĠĠĠ Ġoverride Ġsuspend Ġfun Ġupdate Payment Methods ( userId : ĠString ) Ġ= Ġsupervisor Scope Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġremote Job Ġ= Ġasync ( dispatch ers . io ) Ġ{ Ġremote PaymentMethod DataSource . get Payment Methods ( userId ) Ġ} ĊĠĠĠĠĠĠĠ Ġwhen Ġ( val Ġresult Ġ= Ġremote Job . await ()) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġis ĠSuccess Ġ-> Ġlocal PaymentMethod Store . save PaymentMethod List ( userId , Ġresult . data ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġis ĠError Result Ġ-> Ġthrow Ġresult . exception ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ ĠUnit ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġsuspend Ġfun Ġadd PaymentMethod ( userId : ĠString , Ġalias Id : ĠString , Ġpayment Method : ĠPayment Method ) Ġ= Ġsupervisor Scope Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġremote Job Ġ= Ġasync ( dispatch ers . io ) Ġ{ Ġremote PaymentMethod DataSource . add PaymentMethod ( userId , Ġalias Id , Ġpayment Method ) Ġ} ĊĠĠĠĠĠĠĠ Ġwhen Ġ( val Ġresult Ġ= Ġremote Job . await ()) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġis ĠSuccess Ġ-> Ġlocal PaymentMethod Store . save PaymentMethod ( payment Method . copy ( userId Ġ= ĠuserId ), Ġresult . data . payment Method Id ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġis ĠError Result Ġ-> Ġthrow Ġresult . exception ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ ĠUnit ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġsuspend Ġfun Ġdelete PaymentMethod ( payment Method : ĠPayment Method ) Ġ= Ġsupervisor Scope Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġremote Job Ġ= Ġasync ( dispatch ers . io ) Ġ{ Ġremote PaymentMethod DataSource . delete PaymentMethod ( payment Method . payment Method Id ) Ġ} ĊĠĠĠĠĠĠĠ Ġwhen Ġ( val Ġresult Ġ= Ġremote Job . await ()) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġis ĠSuccess Ġ-> Ġlocal PaymentMethod Store . delete PaymentMethod ( payment Method ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġis ĠError Result Ġ-> Ġthrow Ġresult . exception ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ ĠUnit ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġsuspend Ġfun Ġauthorize Payment ( authorize Payment Request : ĠAuthor ize Payment Request ) Ġ{ ĊĠĠĠĠĠĠĠ Ġlaunch Or Join (" authorize _ payment _${ authorize Payment Request . payment Method Id }") Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġremote Job Ġ= Ġasync ( dispatch ers . io ) Ġ{ Ġremote PaymentMethod DataSource . authorize Payment ( authorize Payment Request ) Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġwhen Ġ( val Ġresult Ġ= Ġremote Job . await ()) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġis ĠSuccess Ġ-> Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġlocal Cart Store . empty Cart () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ_ payment Completed Channel . offer ( true ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġis ĠError Result Ġ-> Ġthrow Ġresult . exception ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ }