/* Ċ Ġ* ĠCopyright Ġ 2 0 2 2 ĠP era ĠWallet , ĠL DA Ċ Ġ* Ġ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 . al gor and . android . use case Ċ Ċ import Ġcom . al gor and . android . core . Account Manager Ċ import Ġcom . al gor and . android . core . Base UseCase Ċ import Ġcom . al gor and . android . mapper . Account Summary Mapper Ċ import Ġcom . al gor and . android . models . Account Ċ import Ġcom . al gor and . android . models . Account Detail Ċ import Ġcom . al gor and . android . models . Account Detail Summary Ċ import Ġcom . al gor and . android . models . Account Icon Ċ import Ġcom . al gor and . android . repository . Account Repository Ċ import Ġcom . al gor and . android . utils . Cache Result Ċ import Ġcom . al gor and . android . utils . Data Resource Ċ import Ġcom . al gor and . android . utils . is Re keyed To Another Account Ċ import Ġcom . al gor and . android . utils . to Short ened Address Ċ import Ġjava . math . BigInteger Ċ import Ġjavax . inject . Inject Ċ import Ġkotlinx . coroutines . flow . Flow Ċ import Ġkotlinx . coroutines . flow . distinct Until Changed Ċ import Ġkotlinx . coroutines . flow . map Ċ import Ġkotlinx . coroutines . flow . map NotNull Ċ Ċ class ĠAccount Detail UseCase Ġ@ Inject Ġconstructor ( ĊĠĠĠ Ġprivate Ġval Ġaccount Repository : ĠAccount Repository , ĊĠĠĠ Ġprivate Ġval Ġaccount Information UseCase : ĠAccount Information UseCase , ĊĠĠĠ Ġprivate Ġval Ġaccount Manager : ĠAccount Manager , ĊĠĠĠ Ġprivate Ġval Ġaccount Summary Mapper : ĠAccount Summary Mapper Ċ ) Ġ: ĠBase UseCase () Ġ{ ĊĊĠĠĠ Ġfun Ġget Account Detail Cache Flow () Ġ= Ġaccount Repository . get Account Detail Cache Flow () ĊĊĠĠĠ Ġfun Ġget Account Detail Cache Flow ( publicKey : ĠString ): ĠFlow < Cache Result < Account Detail > ?> Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġaccount Repository . get Account Detail Cache Flow () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. map NotNull Ġ{ Ġit . get OrDefault ( publicKey , Ġnull ) Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. distinct Until Changed () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġget Cached Account Details () Ġ= Ġget Account Detail Cache Flow (). value . values ĊĊĠĠĠ Ġfun Ġget Cached Account Detail ( publicKey : ĠString ): ĠCache Result < Account Detail >? Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġaccount Repository . get Cached Account Detail ( publicKey ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġsuspend Ġfun Ġclear Account Detail Cache () Ġ{ ĊĠĠĠĠĠĠĠ Ġaccount Repository . clear Account Detail Cache () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġsuspend Ġfun Ġcache Account Detail ( account Detail : ĠCache Result . Success < Account Detail >) Ġ{ ĊĠĠĠĠĠĠĠ Ġaccount Repository . cache Account Detail ( account Detail ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġsuspend Ġfun Ġcache Account Detail ( account PublicKey : ĠString , Ġaccount Detail : ĠCache Result . Error < Account Detail >) Ġ{ ĊĠĠĠĠĠĠĠ Ġaccount Repository . cache Account Detail ( account PublicKey , Ġaccount Detail ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġsuspend Ġfun Ġcache Account Details ( account Detail KeyValuePair List : ĠList < Pair < String , ĠCache Result < Account Detail >> >) Ġ{ ĊĠĠĠĠĠĠĠ Ġaccount Repository . cache All Account Details ( account Detail KeyValuePair List ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġis Asset Owned By Account ( publicKey : ĠString , Ġasset Id : ĠLong ): ĠBoolean Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġget Cached Account Detail ( publicKey )?. data ?. account Information ?. getAll Asset Ids ()?. contains ( asset Id ) Ġ?: Ġfalse ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġare All Accounts Cached (): ĠBoolean Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġaccount Manager . accounts . value . size Ġ<= Ġaccount Repository . get Account Detail Cache Flow (). value . size ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġget Cached Accounts Assets (): ĠSet < Long > Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġaccount Repository . get Account Detail Cache Flow (). value ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. map NotNull Ġ{ Ġit . value . data ?. account Information ?. getAll Asset Ids () Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. flatten () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. toSet () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġget Cached Account Algo Amount ( publicKey : ĠString ): ĠBigInteger ? Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġaccount Repository . get Cached Account Detail ( publicKey )?. data ?. account Information ?. amount ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġcan Account Sign Transaction ( publicKey : ĠString ): ĠBoolean Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġaccount Ġ= Ġaccount Manager . get Account ( publicKey ) ĊĠĠĠĠĠĠĠ Ġreturn Ġaccount ?. type Ġ!= Ġnull Ġ&& Ġaccount . type Ġ!= ĠAccount . Type . WATCH Ġ&& Ġaccount . type Ġ!= ĠAccount . Type . RE KEY ED ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġsuspend Ġfun Ġfetch Account Detail ( account : ĠAccount ): ĠFlow < Data Resource < Account Detail >> Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġaccount Information UseCase . get Account Information ( account . address ). map Ġ{ Ġaccount Information Data Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠ Ġwhen Ġ( account Information Data ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġis ĠData Resource . Success Ġ-> Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġaccount Detail Ġ= ĠAccount Detail ( account , Ġaccount Information Data . data ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠData Resource . Success ( account Detail ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġis ĠData Resource . Error . Api Ġ-> Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠData Resource . Error . Api ( account Information Data . exception , Ġaccount Information Data . code ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġelse Ġ-> ĠData Resource . Loading () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġis Account Re keyed ( account Detail : ĠAccount Detail ?): ĠBoolean Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġaccount Detail ?. run Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ! account Information . re key Admin Address . isNull Or Blank () Ġ&& ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġaccount Information . re key Admin Address Ġ!= Ġaccount . address ĊĠĠĠĠĠĠĠ Ġ} Ġ== Ġtrue ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ// ĠTODO Ġthis Ġfunction Ġshouldn 't Ġreturn Ġnullable Ġmodel Ġalso Ġit Ġshould Ġbe Ġmoved Ġinto Ġits Ġown ĠUse Case ĊĠĠĠ Ġfun Ġget Account Summary ( publicKey : ĠString ): ĠAccount Detail Summary ? Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġaccount Detail Ġ= Ġget Cached Account Detail ( publicKey )?. data Ġ?: Ġreturn Ġnull ĊĠĠĠĠĠĠĠ Ġreturn Ġaccount Summary Mapper . map To ( account Detail , Ġcan Account Sign Transaction ( publicKey )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġget Account Type ( publicKey : ĠString ): ĠAccount . Type Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġaccount Ġ= Ġaccount Repository . get Cached Account Detail ( publicKey )?. data ?. account ĊĠĠĠĠĠĠĠ Ġreturn Ġaccount ?. type Ġ?: ĠAccount . Type . STANDARD ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġget AccountName ( publicKey : ĠString ): ĠString Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġaccount Ġ= Ġaccount Repository . get Cached Account Detail ( publicKey )?. data ?. account ĊĠĠĠĠĠĠĠ Ġval Ġaccount Name Ġ= Ġaccount ?. name ĊĠĠĠĠĠĠĠ Ġval Ġaccount Address Ġ= Ġaccount ?. address ĊĠĠĠĠĠĠĠ Ġreturn Ġaccount Name ?. if Empty Ġ{ Ġaccount Address . to Short ened Address () Ġ}. or Empty () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġget Auth Address ( publicKey : ĠString ): ĠString ? Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġaccount Information Ġ= Ġaccount Repository . get Cached Account Detail ( publicKey )?. data ?. account Information ĊĠĠĠĠĠĠĠ Ġreturn Ġaccount Information ?. re key Admin Address ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġget Account Address ( publicKey : ĠString ): ĠString ? Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġaccount Information Ġ= Ġaccount Repository . get Cached Account Detail ( publicKey )?. data ?. account Information ĊĠĠĠĠĠĠĠ Ġreturn Ġaccount Information ?. address ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġget Account Icon ( publicKey : ĠString ): ĠAccount Icon ? Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġaccount Manager . get Account ( publicKey )?. create Account Icon () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġis Account Re keyed ( publicKey : ĠString ): ĠBoolean Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġauth Address Ġ= Ġaccount Repository . get Cached Account Detail ( publicKey ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ? . data ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ? . account Information ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ? . re key Admin Address ĊĠĠĠĠĠĠĠ Ġreturn Ġis Re keyed To Another Account ( auth Address , ĠpublicKey ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġis There Any Account With PublicKey ( publicKey : ĠString ): ĠBoolean Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġaccount Manager . is There Any Account With PublicKey ( publicKey ) ĊĠĠĠ Ġ} Ċ } Ċ