package ani.dantotsu.media import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import ani.dantotsu.connections.anilist.Anilist import java.text.DateFormat import java.util.Date class OtherDetailsViewModel : ViewModel() { private val character: MutableLiveData = MutableLiveData(null) fun getCharacter(): LiveData = character suspend fun loadCharacter(m: Character) { if (character.value == null) character.postValue(Anilist.query.getCharacterDetails(m)) } private val studio: MutableLiveData = MutableLiveData(null) fun getStudio(): LiveData = studio suspend fun loadStudio(m: Studio) { if (studio.value == null) studio.postValue(Anilist.query.getStudioDetails(m)) } private val author: MutableLiveData = MutableLiveData(null) fun getAuthor(): LiveData = author suspend fun loadAuthor(m: Author) { if (author.value == null) author.postValue(Anilist.query.getAuthorDetails(m)) } private var cachedAllCalendarData: Map>? = null private var cachedLibraryCalendarData: Map>? = null private val calendar: MutableLiveData>> = MutableLiveData(null) fun getCalendar(): LiveData>> = calendar suspend fun loadCalendar(showOnlyLibrary: Boolean = false) { if (cachedAllCalendarData == null || cachedLibraryCalendarData == null) { val curr = System.currentTimeMillis() / 1000 val res = Anilist.query.recentlyUpdated(curr - 86400, curr + (86400 * 6)) val df = DateFormat.getDateInstance(DateFormat.FULL) val allMap = mutableMapOf>() val libraryMap = mutableMapOf>() val idMap = mutableMapOf>() val userId = Anilist.userid ?: 0 val userLibrary = Anilist.query.getMediaLists(true, userId) val libraryMediaIds = userLibrary.flatMap { it.value }.map { it.id } res.forEach { val v = it.relation?.split(",")?.map { i -> i.toLong() }!! val dateInfo = df.format(Date(v[1] * 1000)) val list = allMap.getOrPut(dateInfo) { mutableListOf() } val libraryList = if (libraryMediaIds.contains(it.id)) { libraryMap.getOrPut(dateInfo) { mutableListOf() } } else { null } val idList = idMap.getOrPut(dateInfo) { mutableListOf() } it.relation = "Episode ${v[0]}" if (!idList.contains(it.id)) { idList.add(it.id) list.add(it) libraryList?.add(it) } } cachedAllCalendarData = allMap cachedLibraryCalendarData = libraryMap } val cacheToUse: Map> = if (showOnlyLibrary) { cachedLibraryCalendarData ?: emptyMap() } else { cachedAllCalendarData ?: emptyMap() } calendar.postValue(cacheToUse) } }