package Ġcom . d ant ol . over se ast ool kit . main . em ergency . ui Ċ Ċ import Ġandroidx . lifecycle . LiveData Ċ import Ġandroidx . lifecycle . Mutable LiveData Ċ import Ġandroidx . lifecycle . ViewModel Ċ import Ġandroidx . lifecycle . viewModel Scope Ċ import Ġcom . d ant ol . over se ast ool kit . main . em ergency . database . E mergency Info Dao Ċ import Ġcom . d ant ol . over se ast ool kit . main . em ergency . database . model . E mergency Info Ċ import Ġcom . d ant ol . over se ast ool kit . utils . Logger Ċ import Ġcom . d ant ol . over se ast ool kit . utils . No Info Received Exception Ċ import Ġdagger . h ilt . android . lifecycle . H ilt ViewModel Ċ import Ġkotlinx . coroutines . Dispatch ers Ċ import Ġkotlinx . coroutines . launch Ċ import Ġokhttp 3 .* Ċ import Ġorg . json . JSONArray Ċ import Ġorg . json . JSONObject Ċ import Ġjava . io . IOException Ċ import Ġjava . net . Http URLConnection . HTTP _ OK Ċ import Ġjavax . inject . Inject Ċ Ċ @ H ilt ViewModel Ċ class ĠE mergency Info ViewModel Ġ@ Inject Ġconstructor ( Ċ ĉ private Ġval Ġok HttpClient : ĠOk HttpClient , Ċ ĉ private Ġval Ġem ergency Info Dao : ĠE mergency Info Dao Ċ ) Ġ: ĠViewModel () Ġ{ ĊĊ ĉ private Ġval Ġem ergency Info Fragment State LiveData Ġ= ĠMutable LiveData < E mergency Info Fragment State >() Ċ ĉ val Ġem ergency Info Fragment State : ĠLive Data < E mergency Info Fragment State > Ċĉ ĉ get () Ġ= Ġem ergency Info Fragment State LiveData ĊĊ ĉ fun Ġget E mergency Info () Ġ{ Ċĉ ĉ em ergency Info Fragment State LiveData . post Value ( E mergency Info Fragment State . Fetching Data ) ĊĊĉ ĉ viewModel Scope . launch ( Dispatch ers . IO ) Ġ{ Ċĉĉ ĉ val Ġem ergency InfoList Ġ= Ġem ergency Info Dao . get E mergency InfoList () ĊĊĉĉ ĉ if Ġ( em ergency InfoList . isEmpty ()) Ċĉĉĉ ĉ get E mergency Numbers From Web () Ċĉĉ ĉ else Ċĉĉĉ ĉ show Info ( em ergency InfoList ) Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ private Ġfun Ġget E mergency Numbers From Web () Ġ{ Ċĉ ĉ ok HttpClient . new Call ( Ċĉĉ ĉ Request . Builder () Ċĉĉĉ ĉ . url ( EM ER GEN CY _ NUMBER S _ API ) Ċĉĉĉ ĉ . build () Ċĉ ĉ ). enqueue ( Ċĉĉ ĉ object Ġ: ĠCallback Ġ{ Ċĉĉĉ ĉ override Ġfun Ġon Failure ( call : ĠCall , Ġe : ĠIOException ) Ġ{ Ċĉĉĉĉ ĉ Logger . w (" Failed Ġto Ġfetch Ġem ergency Ġinfo ") Ċĉĉĉĉ ĉ show Failure ( e ) Ċĉĉĉ ĉ } ĊĊĉĉĉ ĉ override Ġfun Ġon Response ( call : ĠCall , Ġresponse : ĠResponse ) Ġ{ Ċĉĉĉĉ ĉ if Ġ( response . code Ġ== ĠHTTP _ OK ) Ċĉĉĉĉĉ ĉ parse Response ( response ) Ċĉĉĉ ĉ } Ċĉĉ ĉ }) Ċ ĉ } ĊĊ ĉ private Ġfun Ġparse Response ( response : ĠResponse ) Ġ{ Ċĉ ĉ val Ġresponse Body Json Ġ= ĠJSONArray ( response . body !!. string ()) Ċĉ ĉ val Ġem ergency InfoList Ġ= ĠArrayList < E mergency Info >() ĊĊĉ ĉ for Ġ( i Ġin Ġ 0 Ġuntil Ġresponse Body Json . length ()) Ċĉĉ ĉ em ergency InfoList . add ( parse E mergency Info ( response Body Json . getJSONObject ( i ))) ĊĊĉ ĉ em ergency Info Dao . insert Bulk ( em ergency InfoList ) Ċĉ ĉ show Info ( em ergency InfoList ) Ċ ĉ } ĊĊ ĉ /** Ċĉ Ġ* ĠParse Ġthe Ġem ergency Ġinfo Ġfrom Ġthe Ġreceived ĠJSON . Ċĉ Ġ* Ċĉ Ġ* ĠJSON Ġexample : Ċĉ Ġ* Ġ ĉ { Ċ ĉ " Country ": Ġ{ Ċ ĉ " Name ": Ġ" Af gh an istan ", Ċ ĉ " ISO Code ": Ġ" AF ", Ċ ĉ " IS ON um eric ": Ġ" 4 " Ċ ĉ }, Ċ ĉ " Amb ul ance ": Ġ{ Ċ ĉ " All ": Ġ[ Ċ ĉ null Ċ ĉ ] Ċ ĉ }, Ċ ĉ " Fire ": Ġ{ Ċ ĉ " All ": Ġ[ Ċ ĉ null Ċ ĉ ] Ċ ĉ }, Ċ ĉ " Pol ice ": Ġ{ Ċ ĉ " All ": Ġ[ Ċ ĉ null Ċ ĉ ] Ċ ĉ }, Ċ ĉ " Dispatch ": Ġ{ Ċ ĉ " All ": Ġ[ Ċ ĉ null Ċ ĉ ] Ċ ĉ }, Ċĉ Ġ* Ċĉ Ġ* Ċĉ Ġ* Ġ*/ Ċ ĉ private Ġfun Ġparse E mergency Info ( info : ĠJSONObject ?): ĠE mergency Info Ġ{ Ċĉ ĉ if Ġ( info Ġ== Ġnull ) Ċĉĉ ĉ throw ĠNo Info Received Exception (" failed Ġto Ġreceive Ġinfo Ġfrom Ġthe Ġserver ") ĊĊĉ ĉ Logger . i (" parsing Ġ$ info ") Ċĉ ĉ val Ġcountry Info Ġ= Ġinfo . getJSONObject ( COUNTRY _ JSON _ FIELD ) Ċĉ ĉ return ĠE mergency Info ( Ċĉĉ ĉ country Code Ġ= Ġcountry Info . getString ( COUNTRY _ CODE _ JSON _ FIELD ), Ċĉĉ ĉ country Name Ġ= Ġcountry Info . getString ( COUNTRY _ NAME _ JSON _ FIELD ), Ċĉĉ ĉ amb ul ance Ġ= Ġinfo . getJSONObject ( AMB UL ANCE _ JSON _ FIELD ) Ċĉĉĉ ĉ . getString JSONArray First Element OrNull ( ALL _ JSON _ FIELD ) Ċĉĉĉ ĉ ?: Ġ" null ", Ċĉĉ ĉ fire Ġ= Ġinfo . getJSONObject ( FIRE _ JSON _ FIELD ) Ċĉĉĉ ĉ . getString JSONArray First Element OrNull ( ALL _ JSON _ FIELD ) Ċĉĉĉ ĉ ?: Ġ" null ", Ċĉĉ ĉ pol ice Ġ= Ġinfo . getJSONObject ( POL ICE _ JSON _ FIELD ) Ċĉĉĉ ĉ . getString JSONArray First Element OrNull ( ALL _ JSON _ FIELD ) Ċĉĉĉ ĉ ?: Ġ" null ", Ċĉĉ ĉ dispatch Ġ= Ġinfo . getJSONObject ( DISPATCH _ JSON _ FIELD ) Ċĉĉĉ ĉ . getString JSONArray First Element OrNull ( ALL _ JSON _ FIELD ) Ċĉĉĉ ĉ ?: Ġ" null ", Ċĉĉ ĉ dispatch G SM Ġ= Ġinfo . getJSONObject ( DISPATCH _ JSON _ FIELD ) Ċĉĉĉ ĉ . getString JSONArray First Element OrNull ( G SM _ JSON _ FIELD ) Ċĉĉĉ ĉ ?: Ġ" null ", Ċĉĉ ĉ dispatch Fixed Ġ= Ġinfo . getJSONObject ( DISPATCH _ JSON _ FIELD ) Ċĉĉĉ ĉ . getString JSONArray First Element OrNull ( FIXED _ JSON _ FIELD ) Ġ?: Ġ" null ", Ċĉ ĉ ) Ċ ĉ } ĊĊ ĉ private Ġfun Ġshow Info ( em ergency InfoList : ĠList < E mergency Info >) Ġ{ Ċĉ ĉ em ergency Info Fragment State LiveData . post Value ( Ċĉĉ ĉ E mergency Info Fragment State . Data Loaded ( Ċĉĉĉ ĉ em ergency InfoList Ċĉĉ ĉ ) Ċĉ ĉ ) Ċ ĉ } ĊĊ ĉ private Ġfun Ġshow Failure ( e : ĠException ) Ġ{ Ċĉ ĉ em ergency Info Fragment State LiveData . post Value ( E mergency Info Fragment State . Error Fetching Data ( e . toString ())) Ċ ĉ } ĊĊ ĉ private Ġfun ĠJSONObject . getString JSONArray First Element OrNull ( name : ĠString ): ĠString ? Ġ{ Ċĉ ĉ return Ġtry Ġ{ Ċĉĉ ĉ get JSONArray ( name ). getString ( 0 ) Ċĉ ĉ } Ġcatch Ġ( e : ĠException ) Ġ{ Ċĉĉ ĉ Logger . e (" Got Ġexception Ġwhile Ġparsing Ġem ergency Ġinfo Ġ- Ġ$ e ") Ċĉĉ ĉ null Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ comp anion Ġobject Ġ{ Ċĉ ĉ private Ġconst Ġval ĠE MER GEN CY _ NUMBER S _ API Ġ= Ġ" http :// em erg enc yn umber api . com / api / data / all " ĊĊĉ ĉ private Ġconst Ġval ĠCOUN TRY _ JSON _ FIELD Ġ= Ġ" Country " Ċĉ ĉ private Ġconst Ġval ĠCOUN TRY _ CODE _ JSON _ FIELD Ġ= Ġ" ISO Code " Ċĉ ĉ private Ġconst Ġval ĠCOUN TRY _ NAME _ JSON _ FIELD Ġ= Ġ" Name " Ċĉ ĉ private Ġconst Ġval ĠA MB UL ANCE _ JSON _ FIELD Ġ= Ġ" Amb ul ance " Ċĉ ĉ private Ġconst Ġval ĠFI RE _ JSON _ FIELD Ġ= Ġ" Fire " Ċĉ ĉ private Ġconst Ġval ĠPOL ICE _ JSON _ FIELD Ġ= Ġ" Pol ice " Ċĉ ĉ private Ġconst Ġval ĠDIS PATCH _ JSON _ FIELD Ġ= Ġ" Dispatch " Ċĉ ĉ private Ġconst Ġval ĠALL _ JSON _ FIELD Ġ= Ġ" All " Ċĉ ĉ private Ġconst Ġval ĠFIX ED _ JSON _ FIELD Ġ= Ġ" Fixed " Ċĉ ĉ private Ġconst Ġval ĠG SM _ JSON _ FIELD Ġ= Ġ" G SM " Ċ ĉ } Ċ }