/* Ċ Ġ* ĠMIT ĠLicense Ċ Ġ* Ċ Ġ* ĠCopyright Ġ( c ) Ġ 2 0 2 1 ĠJ i ey i Ċ Ġ* Ċ Ġ* ĠPermission Ġis Ġhereby Ġgranted , Ġfree Ġof Ġcharge , Ġto Ġany Ġperson Ġobtaining Ġa Ġcopy Ċ Ġ* Ġof Ġthis Ġsoftware Ġand Ġassociated Ġdocumentation Ġfiles Ġ( the Ġ" Software "), Ġto Ġdeal Ċ Ġ* Ġin Ġthe ĠSoftware Ġwithout Ġrestriction , Ġincluding Ġwithout Ġlimitation Ġthe Ġrights Ċ Ġ* Ġto Ġuse , Ġcopy , Ġmodify , Ġmerge , Ġpublish , Ġdistribute , Ġsublicense , Ġand / or Ġsell Ċ Ġ* Ġcopies Ġof Ġthe ĠSoftware , Ġand Ġto Ġpermit Ġpersons Ġto Ġwhom Ġthe ĠSoftware Ġis Ċ Ġ* Ġfurnished Ġto Ġdo Ġso , Ġsubject Ġto Ġthe Ġfollowing Ġconditions : Ċ Ġ* Ċ Ġ* ĠThe Ġabove Ġcopyright Ġnotice Ġand Ġthis Ġpermission Ġnotice Ġshall Ġbe Ġincluded Ġin Ġall Ċ Ġ* Ġcopies Ġor Ġsubstantial Ġportions Ġof Ġthe ĠSoftware . Ċ Ġ* Ċ Ġ* ĠTHE ĠSOFTWARE ĠIS ĠPROVIDED Ġ" AS ĠIS ", ĠWITHOUT ĠWARRANTY ĠOF ĠANY ĠKIND , ĠEXPRESS ĠOR Ċ Ġ* ĠIMPLIED , ĠINCLUDING ĠBUT ĠNOT ĠLIMITED ĠTO ĠTHE ĠWARRANTIES ĠOF ĠMERCHANTABILITY , Ċ Ġ* ĠFITNESS ĠFOR ĠA ĠPARTICULAR ĠPURPOSE ĠAND ĠNONINFRINGEMENT . ĠIN ĠNO ĠEVENT ĠSHALL ĠTHE Ċ Ġ* ĠAUTHORS ĠOR ĠCOPYRIGHT ĠHOLDERS ĠBE ĠLIABLE ĠFOR ĠANY ĠCLAIM , ĠDAMAGES ĠOR ĠOTHER Ċ Ġ* ĠLIABILITY , ĠWHETHER ĠIN ĠAN ĠACTION ĠOF ĠCONTRACT , ĠTORT ĠOR ĠOTHERWISE , ĠARISING ĠFROM , Ċ Ġ* ĠOUT ĠOF ĠOR ĠIN ĠCONNECTION ĠWITH ĠTHE ĠSOFTWARE ĠOR ĠTHE ĠUSE ĠOR ĠOTHER ĠDEALINGS ĠIN ĠTHE Ċ Ġ* ĠSOFTWARE . Ċ Ġ*/ Ċ Ċ package Ġt aiwan . no . one . feat . library . presentation . view models Ċ Ċ import Ġandroid . app . Application Ċ import Ġandroidx . annotation . Worker Thread Ċ import Ġandroidx . lifecycle . Mutable LiveData Ċ import Ġandroidx . lifecycle . Saved State Handle Ċ import Ġandroidx . lifecycle . viewModel Scope Ċ import Ġkotlinx . coroutines . Dispatch ers Ċ import Ġkotlinx . coroutines . launch Ċ import Ġorg . kode in . di . instance Ċ import Ġt aiwan . no . one . core . presentation . view model . Result LiveData Ċ import Ġt aiwan . no . one . drop beat . App Res Drawable Ċ import Ġt aiwan . no . one . drop beat . core . Playlist Constant Ċ import Ġt aiwan . no . one . drop beat . core . helpers . Resource Helper Ċ import Ġt aiwan . no . one . drop beat . core . utils . StringUtil Ċ import Ġt aiwan . no . one . drop beat . core . view model . Be hind Android ViewModel Ċ import Ġt aiwan . no . one . drop beat . provider . Ranking Methods Provider Ċ import Ġt aiwan . no . one . entity . Simple Playlist Entity Ċ import Ġt aiwan . no . one . entity . Simple Track Entity Ċ import Ġt aiwan . no . one . feat . library . data . entities . local . Library Entity . Play List Entity Ċ import Ġt aiwan . no . one . feat . library . data . entities . local . Library Entity . Song Entity Ċ import Ġt aiwan . no . one . feat . library . domain . use cases . Add Playlist Case Ċ import Ġt aiwan . no . one . feat . library . domain . use cases . Add Playlist Req Ċ import Ġt aiwan . no . one . feat . library . domain . use cases . Add S ongs Case Ċ import Ġt aiwan . no . one . feat . library . domain . use cases . Add S ongs Req Ċ import Ġt aiwan . no . one . feat . library . domain . use cases . Fetch IsIn The Playlist Case Ċ import Ġt aiwan . no . one . feat . library . domain . use cases . Fetch IsIn The Playlist Req Ċ import Ġt aiwan . no . one . feat . library . domain . use cases . Fetch Playlist Case Ċ import Ġt aiwan . no . one . feat . library . domain . use cases . Fetch Playlist Req Ċ import Ġt aiwan . no . one . feat . library . domain . use cases . Update Playlist Case Ċ import Ġt aiwan . no . one . feat . library . domain . use cases . Update Playlist Req Ċ import Ġt aiwan . no . one . feat . library . domain . use cases . Update Song Case Ċ import Ġt aiwan . no . one . feat . library . domain . use cases . Update Song Req Ċ import Ġt aiwan . no . one . k tx . li ved ata . to LiveData Ċ Ċ internal Ġclass ĠPlaylist ViewModel ( ĊĠĠĠ Ġapplication : ĠApplication , ĊĠĠĠ Ġoverride Ġval Ġhandle : ĠSaved State Handle , Ċ ) Ġ: ĠBe hind Android ViewModel ( application ) Ġ{ ĊĠĠĠ Ġprivate Ġval Ġadd Playlist Case Ġby Ġinstance < Add Playlist Case >() ĊĠĠĠ Ġprivate Ġval Ġfetch Playlist Case Ġby Ġinstance < Fetch Playlist Case >() ĊĠĠĠ Ġprivate Ġval Ġupdate Playlist Case Ġby Ġinstance < Update Playlist Case >() ĊĠĠĠ Ġprivate Ġval Ġadd S ongs Case Ġby Ġinstance < Add S ongs Case >() ĊĠĠĠ Ġprivate Ġval Ġupdate Song Case Ġby Ġinstance < Update Song Case >() ĊĠĠĠ Ġprivate Ġval Ġfetch IsIn The Playlist Case Ġby Ġinstance < Fetch IsIn The Playlist Case >() ĊĠĠĠ Ġprivate Ġval Ġranking Methods Provider Ġby Ġinstance < Ranking Methods Provider >() ĊĠĠĠ Ġprivate Ġval Ġ_ result Ġby Ġlazy Ġ{ ĠResult LiveData < Boolean >() Ġ} ĊĠĠĠ Ġval Ġresult Ġget () Ġ= Ġ_ result . to LiveData () ĊĠĠĠ Ġprivate Ġval Ġ_ result Of Favorite Ġby Ġlazy Ġ{ ĠResult LiveData < Boolean >() Ġ} ĊĠĠĠ Ġval Ġresult Of Favorite Ġget () Ġ= Ġ_ result Of Favorite . to LiveData () ĊĠĠĠ Ġprivate Ġval Ġ_ playlist Ġby Ġlazy Ġ{ ĠResult LiveData < Play List Entity >() Ġ} ĊĠĠĠ Ġval Ġplaylist Ġget () Ġ= Ġ_ playlist . to LiveData () ĊĠĠĠ Ġprivate Ġval Ġ_ playlist Duration Ġby Ġlazy Ġ{ ĠMutable LiveData < String >() Ġ} ĊĠĠĠ Ġval Ġplaylist Duration Ġget () Ġ= Ġ_ playlist Duration . to LiveData () ĊĠĠĠ Ġprivate Ġval Ġ_ tracks Ġby Ġlazy Ġ{ ĠResult LiveData < List < Simple Track Entity >>() Ġ} ĊĠĠĠ Ġval Ġtracks Ġget () Ġ= Ġ_ tracks . to LiveData () ĊĊĠĠĠ Ġfun ĠgetS ongs ( playlist Id : ĠInt ) Ġ= ĠviewModel Scope . launch Ġ{ ĊĠĠĠĠĠĠĠ Ġ_ playlist . value Ġ= Ġkotlin . run Catch ing Ġ{ Ġfetch Playlist Case . execute ( Fetch Playlist Req ( playlist Id )) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġget Playlist ( playlist Id : ĠInt ) Ġ= ĠgetS ongs ( playlist Id ) ĊĊĠĠĠ Ġ@ Worker Thread ĊĠĠĠ Ġfun Ġget Rank S ongs ( rank Id : ĠInt ) Ġ= Ġlaunch Be hind Ġ{ ĊĠĠĠĠĠĠĠ Ġ_ tracks . post Value ( ranking Methods Provider . get Ranking S ongs ( rank Id )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Worker Thread ĊĠĠĠ Ġfun Ġattach Favorite Flag ( songs : ĠList < Simple Track Entity >) Ġ= Ġlaunch Be hind Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġres Ġ= Ġkotlin . run Catch ing Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġsongs . on Each Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġit . is Favorite Ġ= Ġfetch IsIn The Playlist Case . execute ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠFetch IsIn The Playlist Req ( null , Ġit . uri , ĠPlaylist Constant . FA V OR ITE ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ_ tracks . post Value ( res ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Worker Thread ĊĠĠĠ Ġfun Ġcreate Playlist ( playlist : ĠSimple Playlist Entity ?, Ġname : ĠString ) Ġ= Ġlaunch Be hind ( Dispatch ers . Default ) Ġ{ ĊĠĠĠĠĠĠĠ Ġ_ result . post Value ( kotlin . run Catch ing Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġparam Ġ= Ġplaylist ?. let Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠPlay List Entity ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġname Ġ= Ġname , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġsong Ids Ġ= Ġit . song Ids , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġcount Ġ= Ġit . song Ids . size , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġcover Url Ġ= ĠResource Helper . get Uri For Drawable Resource ( App Res Drawable . bg _ default ). toString () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} Ġ?: ĠPlay List Entity ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġname Ġ= Ġname , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġcover Url Ġ= ĠResource Helper . get Uri For Drawable Resource ( App Res Drawable . bg _ default ). toString () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġadd Playlist Case . execute ( Add Playlist Req ( param )) ĊĠĠĠĠĠĠĠ Ġ}) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġupdate Playlist ( playlist Id : ĠInt , Ġplaylist Name : ĠString ) Ġ= ĠviewModel Scope . launch Ġ{ ĊĠĠĠĠĠĠĠ Ġ_ result . value Ġ= Ġkotlin . run Catch ing Ġ{ Ġupdate Playlist Case . execute ( Update Playlist Req ( playlist Id , Ġplaylist Name )) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġcreate S ongs ( songs : ĠList < Song Entity >) Ġ= ĠviewModel Scope . launch Ġ{ ĊĠĠĠĠĠĠĠ Ġ_ result . value Ġ= Ġkotlin . run Catch ing Ġ{ Ġadd S ongs Case . execute ( Add S ongs Req ( songs )) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġupdate Song ( song Id : ĠInt , Ġis Favorite : ĠBoolean ) Ġ= ĠviewModel Scope . launch Ġ{ ĊĠĠĠĠĠĠĠ Ġ_ result Of Favorite . value Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠ Ġkotlin . run Catch ing Ġ{ Ġupdate Song Case . execute ( Update Song Req ( song Id Ġ= Ġsong Id , Ġis Favorite Ġ= Ġis Favorite )) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġupdate Song ( song : ĠSimple Track Entity , Ġis Favorite : ĠBoolean ) Ġ= ĠviewModel Scope . launch Ġ{ ĊĠĠĠĠĠĠĠ Ġ_ result Of Favorite . value Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠ Ġkotlin . run Catch ing Ġ{ Ġupdate Song Case . execute ( Update Song Req ( song Ġ= Ġsong , Ġis Favorite Ġ= Ġis Favorite )) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Worker Thread ĊĠĠĠ Ġfun Ġcum ulate Duration ( songs : ĠList < Simple Track Entity >) Ġ= Ġlaunch Be hind ( Dispatch ers . Default ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġduration Ġ= Ġsongs . fold ( 0 ) Ġ{ Ġacc , Ġsong Ġ-> Ġacc Ġ+ Ġsong . duration Ġ} ĊĠĠĠĠĠĠĠ Ġ// ĠSet Ġthe Ġvisibility Ġfor Ġthis Ġfragment . ĊĠĠĠĠĠĠĠ Ġ_ playlist Duration . post Value ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ"${ songs . size } ĠS ongs ãĥ» ${ StringUtil . build Duration To Time ( duration . to Long ()) } ãĥ» 3 0 Ġmins Ġago Ġplayed " ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ} Ċ } Ċ