package Ġcom . example . antic ov id . ui . profile . risk _ assessment _ test Ċ Ċ import Ġandroid . content . Context Ċ import Ġandroidx . lifecycle . LiveData Ċ import Ġandroidx . lifecycle . Mutable LiveData Ċ import Ġandroidx . lifecycle . ViewModel Ċ import Ġcom . example . antic ov id . data . model .* Ċ import Ġcom . google . gson . Gson Ċ import Ġcom . google . gson . reflect . Type Token Ċ import Ġjava . text . SimpleDateFormat Ċ import Ġjava . util .* Ċ Ċ class ĠR isk Assessment Tests ViewModel ( private Ġval Ġcontext : ĠContext ) Ġ: ĠViewModel () Ġ{ ĊĊĠĠĠ Ġprivate Ġval Ġ_ is Tod ays Test Performed Ġ= ĠMutable LiveData < Boolean >() ĊĠĠĠ Ġval Ġis Tod ays Test Performed : ĠLive Data < Boolean > Ġ= Ġ_ is Tod ays Test Performed ĊĊĠĠĠ Ġprivate Ġval Ġ_ tests Performed Ġ= ĠMutable LiveData < List < Risk Assessment Test >>() ĊĠĠĠ Ġval Ġtests Performed : ĠLive Data < List < Risk Assessment Test >> Ġ= Ġ_ tests Performed ĊĊĠĠĠ Ġinit Ġ{ ĊĠĠĠĠĠĠĠ Ġload Tests Performed (). also Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ_ tests Performed . value Ġ= Ġit ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif Ġ( it . isNotEmpty ()) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġcheck If Test From Today ( it . first ()) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġcheck If Test From Today ( test : ĠR isk Assessment Test ) Ġ{ ĊĠĠĠĠĠĠĠ Ġwith Ġ( SimpleDateFormat (" dd MM yyyy ")) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġtoday Ġ= Ġformat ( Calendar . getInstance (). time ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ_ is Tod ays Test Performed . value Ġ= Ġformat ( test . date . time ) Ġ== Ġtoday ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġsave Test ( test : ĠR isk Assessment Test ) Ġ{ ĊĠĠĠĠĠĠĠ Ġ_ tests Performed . value Ġ= Ġ(_ tests Performed . value ?. to Mutable List () Ġ?: Ġmutable ListOf ()). apply Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġremove If Ġ{ Ġit . date . time In Millis Ġ== Ġtest . date . time In Millis Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġadd ( test ) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠsortBy Descending Ġ{ Ġit . date Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġcheck If Test From Today ( test ) ĊĠĠĠĠĠĠĠ Ġsave Tests Performed ( Gson (). toJson (_ tests Performed . value )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġload Tests Performed (): ĠList < Risk Assessment Test > Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġshared Preferences Ġ= Ġcontext . get SharedPreferences ( SHARED _ PREFER ENCES _ R IS K _ AS SESS MENT _ TEST , ĠContext . MODE _ PRIVATE ) ĊĠĠĠĠĠĠĠ Ġval Ġtests Performed String Ġ= Ġshared Preferences . getString ( SHARED _ PREFER ENCES _ R IS K _ AS SESS MENT _ TESTS _ PER FOR MED , Ġ"") ĊĠĠĠĠĠĠĠ Ġval Ġlist Type Ġ= Ġobject Ġ: ĠType Token < List < Risk Assessment Test >>() Ġ{} . type ĊĊĠĠĠĠĠĠĠ Ġreturn ĠGson (). fromJson ( tests Performed String , Ġlist Type ) Ġ?: Ġempty List () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġsave Tests Performed ( tests Performed String : ĠString ) Ġ{ ĊĠĠĠĠĠĠĠ Ġwith Ġ( context . get SharedPreferences ( SHARED _ PREFER ENCES _ R IS K _ AS SESS MENT _ TEST , ĠContext . MODE _ PRIVATE ). edit ()) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġput String ( SHARED _ PREFER ENCES _ R IS K _ AS SESS MENT _ TESTS _ PER FOR MED , Ġtests Performed String ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġapply () ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ }