package Ġcom . pix ite . android . billing x Ċ Ċ import Ġandroid . content . SharedPreferences Ċ import Ġcom . android . billing client . api . Billing Client Ċ import Ġcom . android . billing client . api . Billing Client . Sku Type Ċ import Ġcom . android . billing client . api . Internal P urchases Result Ċ import Ġcom . android . billing client . api . Purchase Ċ import Ġcom . android . billing client . api . Purchase . P urchases Result Ċ import Ġcom . android . billing client . api . Sku Details Ċ import Ġcom . android . billing client . api . Sku Details Params Ċ import Ġorg . json . JSONArray Ċ import Ġorg . json . JSONObject Ċ Ċ class ĠBilling Store Impl ( private Ġval Ġprefs : ĠSharedPreferences ) Ġ: ĠBilling Store (){ ĊĊĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠ Ġinternal Ġconst Ġval ĠKEY _ PUR CH ASE S Ġ= Ġ" dbc _ p urchases " ĊĠĠĠ Ġinternal Ġconst Ġval ĠKEY _ SK U _ DETAILS Ġ= Ġ" dbc _ sku _ details " ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun Ġget Sku Details ( params : ĠSk u Details Params ): ĠList < Sku Details > Ġ{ ĊĠĠĠ Ġreturn Ġprefs . getString ( KEY _ SK U _ DETAILS , Ġ" [] "). to Sku Details List () ĊĠĠĠĠĠĠĠ Ġ. filter Ġ{ Ġit . sku Ġin Ġparams . sk us List Ġ&& Ġit . type Ġ== Ġparams . sku Type Ġ} ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun Ġget P urchases (@ Sku Type Ġsku Type : ĠString ): ĠP urchases Result Ġ{ ĊĠĠĠ Ġreturn ĠInternal P urchases Result ( Billing Client . Billing Response . OK , ĊĠĠĠĠĠĠĠ Ġprefs . getString ( KEY _ PUR CH ASE S , Ġ" [] "). to Purchase List () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. filter Ġ{ Ġit . signature . endsWith ( sku Type ) Ġ}) ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun Ġadd Product ( sku Details : ĠSk u Details ): ĠBilling Store Ġ{ ĊĠĠĠ Ġval Ġall Details Ġ= ĠJSONArray ( prefs . getString ( KEY _ SK U _ DETAILS , Ġ" [] ")) ĊĠĠĠ Ġall Details . put ( sku Details . to JSONObject ()) ĊĠĠĠ Ġprefs . edit (). putString ( KEY _ SK U _ DETAILS , Ġall Details . toString ()) Ġ. apply () ĊĠĠĠ Ġreturn Ġthis ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun Ġremove Product ( sku : ĠString ): ĠBilling Store Ġ{ ĊĠĠĠ Ġval Ġall Details Ġ= Ġprefs . getString ( KEY _ SK U _ DETAILS , Ġ" [] "). to Sku Details List () ĊĠĠĠ Ġval Ġfiltered Ġ= Ġall Details . filter Ġ{ Ġit . sku Ġ!= Ġsku Ġ} ĊĠĠĠ Ġval Ġjson Ġ= ĠJSONArray () ĊĠĠĠ Ġfiltered . forEach Ġ{ Ġjson . put ( it . to JSONObject ()) Ġ} ĊĠĠĠ Ġprefs . edit (). putString ( KEY _ SK U _ DETAILS , Ġjson . toString ()). apply () ĊĠĠĠ Ġreturn Ġthis ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun Ġclear Products (): ĠBilling Store Ġ{ ĊĠĠĠ Ġprefs . edit (). remove ( KEY _ SK U _ DETAILS ). apply () ĊĠĠĠ Ġreturn Ġthis ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun Ġadd Purchase ( purchase : ĠPurchase ): ĠBilling Store Ġ{ ĊĠĠĠ Ġval Ġall P urchases Ġ= ĠJSONArray ( prefs . getString ( KEY _ PUR CH ASE S , Ġ" [] ")) ĊĠĠĠ Ġall P urchases . put ( purchase . to JSONObject ()) ĊĠĠĠ Ġprefs . edit (). putString ( KEY _ PUR CH ASE S , Ġall P urchases . toString ()) Ġ. apply () ĊĠĠĠ Ġreturn Ġthis ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun Ġremove Purchase ( sku : ĠString ): ĠBilling Store Ġ{ ĊĠĠĠ Ġval Ġall P urchases Ġ= Ġprefs . getString ( KEY _ PUR CH ASE S , Ġ" [] "). to Purchase List () ĊĠĠĠ Ġval Ġfiltered Ġ= Ġall P urchases . filter Ġ{ Ġit . sku Ġ!= Ġsku Ġ} ĊĠĠĠ Ġval Ġjson Ġ= ĠJSONArray () ĊĠĠĠ Ġfiltered . forEach Ġ{ Ġjson . put ( it . to JSONObject ()) Ġ} ĊĠĠĠ Ġprefs . edit (). putString ( KEY _ PUR CH ASE S , Ġjson . toString ()). apply () ĊĠĠĠ Ġreturn Ġthis ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun Ġclear P urchases (): ĠBilling Store Ġ{ ĊĠĠĠ Ġprefs . edit (). remove ( KEY _ PUR CH ASE S ). apply () ĊĠĠĠ Ġreturn Ġthis ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun ĠPurchase . to JSONObject (): ĠJSONObject Ġ= ĊĠĠĠĠĠ ĠJSONObject (). put (" purchase ", ĠJSONObject ( original Json )). put (" signature ", Ġsignature ) ĊĊĠ Ġprivate Ġfun ĠJSONObject . to Purchase (): ĠPurchase Ġ= ĊĠĠĠĠĠ ĠPurchase ( this . getJSONObject (" purchase "). toString (), Ġthis . getString (" signature ")) ĊĊĠ Ġprivate Ġfun ĠSk u Details . to JSONObject (): ĠJSONObject Ġ= ĠJSONObject ( original Json ) ĊĊĠ Ġprivate Ġfun ĠJSONObject . to Sku Details (): ĠSk u Details Ġ= ĠSk u Details ( toString ()) ĊĊĠ Ġprivate Ġfun ĠString . to Purchase List (): ĠList < Purchase > Ġ{ ĊĠĠĠ Ġval Ġlist Ġ= Ġmutable ListOf < Purchase >() ĊĠĠĠ Ġval Ġarray Ġ= ĠJSONArray ( this ) ĊĠĠĠ Ġ( 0 Ġuntil Ġarray . length ()). map To ( list ) Ġ{ Ġarray . getJSONObject ( it ). to Purchase () Ġ} ĊĠĠĠ Ġreturn Ġlist ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun ĠString . to Sku Details List (): ĠList < Sku Details > Ġ{ ĊĠĠĠ Ġval Ġlist Ġ= Ġmutable ListOf < Sku Details >() ĊĠĠĠ Ġval Ġarray Ġ= ĠJSONArray ( this ) ĊĠĠĠ Ġ( 0 Ġuntil Ġarray . length ()). map To ( list ) Ġ{ Ġarray . getJSONObject ( it ). to Sku Details () Ġ} ĊĠĠĠ Ġreturn Ġlist ĊĠ Ġ} Ċ Ċ }