package Ġcom . example . android . polit ical prepared ness . represent ative Ċ Ċ import Ġandroid . Manifest Ċ import Ġandroid . annotation . Suppress Lint Ċ import Ġandroid . content . Context Ċ import Ġandroid . content . pm . PackageManager Ċ import Ġandroid . location . Geo coder Ċ import Ġandroid . location . Location Ċ import Ġandroid . os . Bundle Ċ import Ġandroid . view .* Ċ import Ġandroid . view . input method . Input Method Manager Ċ import Ġandroid . widget . Array Adapter Ċ import Ġandroid . widget . Toast Ċ import Ġandroidx . core . app . Activity Compat Ċ import Ġandroidx . core . content . Context Compat Ċ import Ġandroidx . databinding . Data Binding Util Ċ import Ġandroidx . fragment . app . Fragment Ċ import Ġandroidx . lifecycle . Observer Ċ import Ġandroidx . lifecycle . ViewModel Provider Ċ import Ġcom . example . android . polit ical prepared ness . R Ċ import Ġcom . example . android . polit ical prepared ness . databinding . Fragment Represent ative Binding Ċ import Ġcom . example . android . polit ical prepared ness . network . models . Address Ċ import Ġcom . example . android . polit ical prepared ness . represent ative . adapter . Represent ative ClickListener Ċ import Ġcom . example . android . polit ical prepared ness . represent ative . adapter . Represent ative ListAdapter Ċ import Ġcom . example . android . polit ical prepared ness . represent ative . adapter . set NewValue Ċ import Ġcom . example . android . polit ical prepared ness . represent ative . model . Represent ative ViewModel Factory Ċ import Ġcom . google . android . gms . location . F used Location Provider Client Ċ import Ġcom . google . android . gms . location . Location Request Ċ import Ġcom . google . android . gms . location . Location Services Ċ import Ġcom . google . android . gms . location . Location Settings Request Ċ import Ġkotlinx . android . synt hetic . main . fragment _ represent ative .* Ċ import Ġjava . util . Locale Ċ Ċ class ĠDetail Fragment Ġ: ĠFragment () Ġ{ ĊĠĠĠ Ġprivate Ġval ĠviewModel : ĠRepresent ative ViewModel Ġby Ġlazy Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġactivity Ġ= Ġrequire NotNull ( this . activity ) ĊĠĠĠĠĠĠĠ ĠViewModel Provider ( this , ĠRepresent ative ViewModel Factory ( activity . application )) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. get ( Represent ative ViewModel :: class . java ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠĠĠĠĠ Ġ// Done : ĠAdd ĠConstant Ġfor ĠLocation Ġrequest ĊĠĠĠĠĠĠĠ Ġconst Ġval ĠREQUEST _ LOCATION _ PERMISSION Ġ= Ġ 1 ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġbinding : ĠFragment Represent ative Binding ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġf used Location Provider Client : ĠF used Location Provider Client ĊĊĠĠĠ Ġoverride Ġfun ĠonCreate View ( in flater : ĠLayoutInflater , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġcontainer : ĠViewGroup ?, ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠsavedInstanceState : ĠBundle ?): ĠView ? Ġ{ ĊĊĠĠĠĠĠĠĠ Ġ// TODO : ĠEst ablish Ġbindings ĊĠĠĠĠĠĠĠ Ġbinding Ġ= ĠData Binding Util . inflate ( in flater , ĠR . layout . fragment _ represent ative , Ġcontainer , Ġfalse ) ĊĠĠĠĠĠĠĠ Ġbinding . lifecycle Owner Ġ= Ġthis ĊĠĠĠĠĠĠĠ Ġbinding . viewModel Ġ= ĠviewModel ĊĊĠĠĠĠĠĠĠ Ġbinding . address Ġ= ĠAddress ("", Ġ"", Ġ"", Ġ"", Ġ"") ĊĊĠĠĠĠĠĠĠ Ġ// Done : ĠDefine Ġand Ġassign ĠRepresent ative Ġadapter ĊĠĠĠĠĠĠĠ Ġval Ġrepresentative Adapter Ġ= ĠRepresent ative ListAdapter () ĊĠĠĠĠĠĠĠ Ġbinding . recycler Represent atives . adapter Ġ= Ġrepresentative Adapter ĊĊĠĠĠĠĠĠĠ Ġval Ġstates Ġ= Ġresources . getString Array ( R . array . states ) ĊĠĠĠĠĠĠĠ Ġval Ġadapter Ġ= ĠArray Adapter ( require Context (), Ġandroid . R . layout . simple _ spinner _ item , Ġstates ) ĊĠĠĠĠĠĠĠ Ġbinding . state . adapter Ġ= Ġadapter ĊĊĠĠĠĠĠĠĠ Ġ// Done : ĠPopulate ĠRepresent ative Ġadapter ĊĠĠĠĠĠĠĠ ĠviewModel . represent atives . observe ( view Lifecycle Owner , ĠObserver Ġ{ Ġrepresentative Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠ Ġrepresentative ?. let Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġrepresentative Adapter . submit List ( represent ative ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ}) ĊĊĠĠĠĠĠĠĠ Ġf used Location Provider Client Ġ= ĠLocation Services . getF used Location Provider Client ( require Activity ()) ĊĠĠĠĠĠĠĠ Ġreturn Ġbinding . root ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ// Calling Ġthe Ġbutton Ġafter Ġthe Ġlayout Ġget 's Ġcreated Ġin ĠonCreate View . ĊĠĠĠ Ġoverride Ġfun Ġon View Created ( view : ĠView , ĠsavedInstanceState : ĠBundle ?) Ġ{ ĊĠĠĠĠĠĠĠ Ġsuper . on View Created ( view , ĠsavedInstanceState ) ĊĠĠĠĠĠĠĠ Ġbutton _ search . setOnClickListener Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ// Hide Ġthe ĠKeyboard ĊĠĠĠĠĠĠĠĠĠĠĠ Ġhide Keyboard () ĊĠĠĠĠĠĠĠĠĠĠĠ ĠviewModel . get Represent atives ( viewModel . address . value . toString ()) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġbutton _ location . setOnClickListener Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġget Location () ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon Request Permissions Result ( request Code : ĠInt , Ġpermissions : ĠArray < out ĠString >, Ġgrant Results : ĠInt Array ) Ġ{ ĊĠĠĠĠĠĠĠ Ġsuper . on Request Permissions Result ( request Code , Ġpermissions , Ġgrant Results ) ĊĠĠĠĠĠĠĠ Ġ// Done : ĠHandle Ġlocation Ġpermission Ġresult Ġto Ġget Ġlocation Ġon Ġpermission Ġgranted ĊĠĠĠĠĠĠĠ Ġif Ġ( request Code Ġ== ĠREQUEST _ LOCATION _ PERMISSION ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif Ġ( grant Results . isNotEmpty () Ġ&& Ġgrant Results [ 0 ] Ġ== ĠPackage Manager . PERMISSION _ GRAN TED ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġget Location () ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠToast . makeText ( require Context (), Ġ" Please Ġcheck Ġyour ĠLocation ", ĠToast . LENGTH _ SHORT ). show () ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġcheck Location Permissions (): ĠBoolean Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġif Ġ( is Permission Granted ()) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġtrue ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠActivity Compat . request Permissions ( require Activity (), ĠarrayOf ( Manifest . permission . ACCESS _ F INE _ LOCATION ), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠREQUEST _ LOCATION _ PERMISSION ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġfalse ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġis Permission Granted (): ĠBoolean Ġ{ ĊĠĠĠĠĠĠĠ Ġ// DONE : ĠCheck Ġif Ġpermission Ġis Ġalready Ġgranted Ġand Ġreturn Ġ( true Ġ= Ġgranted , Ġfalse Ġ= Ġdenied / other ) ĊĠĠĠĠĠĠĠ Ġreturn ĠContext Compat . check Self Permission ( require Activity (), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠManifest . permission . ACCESS _ F INE _ LOCATION ) Ġ== ĠPackage Manager . PERMISSION _ GRAN TED ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Suppress Lint (" Missing Permission ") ĊĠĠĠ Ġprivate Ġfun Ġget Location () Ġ{ ĊĠĠĠĠĠĠĠ Ġ// Done : ĠGet Ġlocation Ġfrom ĠLocation Services ĊĠĠĠĠĠĠĠ Ġif Ġ( check Location Permissions ()) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġf used Location Provider Client . last Location . add On Success Listener Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠviewModel . set Address From Geo Location ( geo Code Location ( it )) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠviewModel . get Represent atives ( viewModel . address . value . toString ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠActivity Compat . request Permissions ( require Activity (), ĠarrayOf ( Manifest . permission . ACCESS _ F INE _ LOCATION ), ĠREQUEST _ LOCATION _ PERMISSION ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ// The Ġgeo Code Location Ġmethod Ġis Ġa Ġhelper Ġfunction Ġto Ġchange Ġthe Ġlat / long Ġlocation Ġto Ġa Ġhuman Ġreadable Ġstreet Ġaddress ĊĠĠĠ Ġprivate Ġfun Ġgeo Code Location ( location : ĠLocation ): ĠAddress Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġgeo code Ġ= ĠGeo coder ( context , ĠLocale . getDefault ()) ĊĠĠĠĠĠĠĠ Ġreturn Ġgeo code . get From Location ( location . latitude , Ġlocation . longitude , Ġ 1 ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. map Ġ{ Ġaddress Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠAddress ( address . th orough f are , Ġaddress . sub Th orough f are , Ġaddress . locality , Ġaddress . admin Area , Ġaddress . postal Code ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. first () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġhide Keyboard () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġimm Ġ= Ġactivity ?. getSystem Service ( Context . INPUT _ METHOD _ SERVICE ) Ġas ĠInput Method Manager ĊĠĠĠĠĠĠĠ Ġimm . hide Soft Input From Window ( view !!. window Token , Ġ 0 ) ĊĠĠĠ Ġ} Ċ Ċ }