/* Ċ Ġ* ĠCopyright Ġ 2 0 2 0 ĠMar c ĠD onal d Ċ Ġ* Ċ Ġ* Ġ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 . mar cd onal d . hib i . screens . add tag dialog Ċ Ċ import Ġandroid . os . Bundle Ċ import Ġandroid . view . KeyEvent Ċ import Ġandroid . view . LayoutInflater Ċ import Ġandroid . view . View Ċ import Ġandroid . view . ViewGroup Ċ import Ġandroid . widget . EditText Ċ import Ġandroid . widget . TextView Ċ import Ġandroidx . fragment . app . view Models Ċ import Ġandroidx . lifecycle . Observer Ċ import Ġcom . google . android . material . button . Material Button Ċ import Ġcom . mar cd onal d . hib i . R Ċ import Ġcom . mar cd onal d . hib i . internal . TAG _ ID _ KEY Ċ import Ġcom . mar cd onal d . hib i . internal . base . H ib i Dialog Fragment Ċ Ċ class ĠAdd Tag Dialog Ġ: ĠH ib i Dialog Fragment () Ġ{ ĊĊ ĉ private Ġval ĠviewModel Ġby Ġview Models < Add Tag ViewModel > Ġ{ ĠviewModel Factory Ġ} ĊĊ ĉ // Ġ< editor - fold Ġdesc =" UI ĠComponents "> Ċ ĉ private Ġlateinit Ġvar Ġinput : ĠEditText Ċ ĉ private Ġlateinit Ġvar Ġdialog Title : ĠTextView Ċ ĉ // Ġ</ editor - fold > ĊĊ ĉ override Ġfun ĠonCreate View ( in flater : ĠLayoutInflater , Ġcontainer : ĠViewGroup ?, ĠsavedInstanceState : ĠBundle ?): ĠView ? Ġ{ Ċĉ ĉ val Ġview Ġ= Ġinflater . inflate ( R . layout . dialog _ new _ tag , Ġcontainer , Ġfalse ) Ċĉ ĉ bind Views ( view ) Ċĉ ĉ setup Observer s () Ċĉ ĉ return Ġview Ċ ĉ } ĊĊ ĉ override Ġfun Ġon View Created ( view : ĠView , ĠsavedInstanceState : ĠBundle ?) Ġ{ Ċĉ ĉ super . on View Created ( view , ĠsavedInstanceState ) Ċĉ ĉ arguments ?. let Ġ{ Ċĉĉ ĉ viewModel . pass Arguments ( require Arguments (). getInt ( TAG _ ID _ KEY , Ġ 0 )) Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ private Ġfun Ġbind Views ( view : ĠView ) Ġ{ Ċĉ ĉ dialog Title Ġ= Ġview . findViewById ( R . id . txt _ add _ tag _ title ) Ċĉ ĉ input Ġ= Ġview . findViewById ( R . id . ed t _ new _ tag _ input ) Ċĉ ĉ val Ġsave Button : ĠMaterial Button Ġ= Ġview . findViewById ( R . id . btn _ save _ tag ) Ċĉ ĉ save Button . setOnClickListener ( save ClickListener ) Ċĉ ĉ val Ġdelete Button : ĠMaterial Button Ġ= Ġview . findViewById ( R . id . btn _ delete _ tag ) Ċĉ ĉ delete Button . setOnClickListener ( delete ClickListener ) Ċĉ ĉ input . setOn Key Listener ( save On Enter Listener ) Ċĉ ĉ input . request Focus () Ċ ĉ } ĊĊ ĉ private Ġfun Ġsetup Observer s () Ġ{ Ċĉ ĉ viewModel . tagName . observe ( view Lifecycle Owner , ĠObserver Ġ{ Ġvalue Ġ-> Ċĉĉ ĉ value ?. let Ġ{ Ġname Ġ-> Ċĉĉĉ ĉ input . setText ( name ) Ċĉĉ ĉ } Ċĉ ĉ }) ĊĊĉ ĉ viewModel . is Edit Mode . observe ( view Lifecycle Owner , ĠObserver Ġ{ Ġvalue Ġ-> Ċĉĉ ĉ value ?. let Ġ{ Ġis Edit Mode Ġ-> Ċĉĉĉ ĉ if ( is Edit Mode ) Ċĉĉĉĉ ĉ dialog Title . text Ġ= Ġresources . getString ( R . string . edit _ tag ) Ċĉĉ ĉ } Ċĉ ĉ }) ĊĊĉ ĉ viewModel . display Empty Content Warning . observe ( view Lifecycle Owner , ĠObserver Ġ{ Ġvalue Ġ-> Ċĉĉ ĉ value ?. let Ġ{ Ġshow Ġ-> Ċĉĉĉ ĉ if ( show ) Ċĉĉĉĉ ĉ input . error Ġ= Ġresources . getString ( R . string . empty _ content _ warning ) Ċĉĉ ĉ } Ċĉ ĉ }) ĊĊĉ ĉ viewModel . display Duplicate Name Warning . observe ( view Lifecycle Owner , ĠObserver Ġ{ Ġvalue Ġ-> Ċĉĉ ĉ value ?. let Ġ{ Ġshow Ġ-> Ċĉĉĉ ĉ if ( show ) Ċĉĉĉĉ ĉ input . error Ġ= Ġresources . getString ( R . string . tag _ already _ exists ) Ċĉĉ ĉ } Ċĉ ĉ }) ĊĊĉ ĉ viewModel . dismiss . observe ( view Lifecycle Owner , ĠObserver Ġ{ Ġvalue Ġ-> Ċĉĉ ĉ value ?. let Ġ{ Ġdismiss Ġ-> Ċĉĉĉ ĉ if ( dismiss ) Ċĉĉĉĉ ĉ dismiss () Ċĉĉ ĉ } Ċĉ ĉ }) Ċ ĉ } ĊĊ ĉ private Ġval Ġsave ClickListener Ġ= ĠView . OnClickListener Ġ{ Ċĉ ĉ viewModel . save Tag ( input . text . toString ()) Ċ ĉ } ĊĊ ĉ private Ġval Ġdelete ClickListener Ġ= ĠView . OnClickListener Ġ{ Ċĉ ĉ viewModel . delete Tag () Ċ ĉ } ĊĊ ĉ private Ġval Ġsave On Enter Listener : ĠView . On Key Listener Ġ= Ċĉ ĉ View . On Key Listener Ġ{ Ġ_: ĠView , ĠkeyCode : ĠInt , Ġkey Event : ĠKeyEvent Ġ-> Ċĉĉ ĉ if (( key Event . action Ġ== ĠKeyEvent . ACTION _ DOWN ) Ġ&& ĠkeyCode Ġ== ĠKeyEvent . KEYCODE _ ENTER ) Ġ{ Ċĉĉĉ ĉ viewModel . save Tag ( input . text . toString ()) Ċĉĉ ĉ } Ċĉĉ ĉ /* ĠThis Ġis Ġfalse Ġso Ġthat Ġthe Ġevent Ġisn 't Ġconsumed Ġand Ġother Ġbuttons Ġ( such Ġas Ġthe Ġback Ġbutton ) Ċĉĉĉ Ġ* Ġcan Ġbe Ġpressed Ġ*/ Ċĉĉ ĉ false Ċĉ ĉ } Ċ }