feat: filter lists by genre
This commit is contained in:
parent
ff036165df
commit
9e371778b7
5 changed files with 46 additions and 2 deletions
|
@ -646,7 +646,7 @@ class AnilistQueries {
|
||||||
sortOrder: String? = null
|
sortOrder: String? = null
|
||||||
): MutableMap<String, ArrayList<Media>> {
|
): MutableMap<String, ArrayList<Media>> {
|
||||||
val response =
|
val response =
|
||||||
executeQuery<Query.MediaListCollection>("""{ MediaListCollection(userId: $userId, type: ${if (anime) "ANIME" else "MANGA"}) { lists { name isCustomList entries { status progress private score(format:POINT_100) updatedAt media { id idMal isAdult type status chapters episodes nextAiringEpisode {episode} bannerImage meanScore isFavourite format coverImage{large} startDate{year month day} title {english romaji userPreferred } } } } user { id mediaListOptions { rowOrder animeList { sectionOrder } mangaList { sectionOrder } } } } }""")
|
executeQuery<Query.MediaListCollection>("""{ MediaListCollection(userId: $userId, type: ${if (anime) "ANIME" else "MANGA"}) { lists { name isCustomList entries { status progress private score(format:POINT_100) updatedAt media { id idMal isAdult type status chapters episodes nextAiringEpisode {episode} bannerImage genres meanScore isFavourite format coverImage{large} startDate{year month day} title {english romaji userPreferred } } } } user { id mediaListOptions { rowOrder animeList { sectionOrder } mangaList { sectionOrder } } } } }""")
|
||||||
val sorted = mutableMapOf<String, ArrayList<Media>>()
|
val sorted = mutableMapOf<String, ArrayList<Media>>()
|
||||||
val unsorted = mutableMapOf<String, ArrayList<Media>>()
|
val unsorted = mutableMapOf<String, ArrayList<Media>>()
|
||||||
val all = arrayListOf<Media>()
|
val all = arrayListOf<Media>()
|
||||||
|
|
|
@ -109,6 +109,7 @@ data class Media(
|
||||||
this.userScore = mediaList.score?.toInt() ?: 0
|
this.userScore = mediaList.score?.toInt() ?: 0
|
||||||
this.userStatus = mediaList.status?.toString()
|
this.userStatus = mediaList.status?.toString()
|
||||||
this.userUpdatedAt = mediaList.updatedAt?.toLong()
|
this.userUpdatedAt = mediaList.updatedAt?.toLong()
|
||||||
|
this.genres = mediaList.media?.genres?.toMutableList() as? ArrayList<String>? ?: arrayListOf()
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(mediaEdge: MediaEdge) : this(mediaEdge.node!!) {
|
constructor(mediaEdge: MediaEdge) : this(mediaEdge.node!!) {
|
||||||
|
|
|
@ -170,6 +170,21 @@ class ListActivity : AppCompatActivity() {
|
||||||
popup.show()
|
popup.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
binding.filter.setOnClickListener {
|
||||||
|
val genres = PrefManager.getVal<Set<String>>(PrefName.GenresList).toMutableSet().sorted()
|
||||||
|
val popup = PopupMenu(this, it)
|
||||||
|
popup.menu.add("All")
|
||||||
|
genres.forEach { genre ->
|
||||||
|
popup.menu.add(genre)
|
||||||
|
}
|
||||||
|
popup.setOnMenuItemClickListener { menuItem ->
|
||||||
|
val selectedGenre = menuItem.title.toString()
|
||||||
|
model.filterLists(selectedGenre)
|
||||||
|
true
|
||||||
|
}
|
||||||
|
popup.show()
|
||||||
|
}
|
||||||
|
|
||||||
binding.random.setOnClickListener {
|
binding.random.setOnClickListener {
|
||||||
//get the current tab
|
//get the current tab
|
||||||
val currentTab =
|
val currentTab =
|
||||||
|
|
|
@ -13,10 +13,29 @@ class ListViewModel : ViewModel() {
|
||||||
var grid = MutableLiveData(PrefManager.getVal<Boolean>(PrefName.ListGrid))
|
var grid = MutableLiveData(PrefManager.getVal<Boolean>(PrefName.ListGrid))
|
||||||
|
|
||||||
private val lists = MutableLiveData<MutableMap<String, ArrayList<Media>>>()
|
private val lists = MutableLiveData<MutableMap<String, ArrayList<Media>>>()
|
||||||
|
private val unfilteredLists = MutableLiveData<MutableMap<String, ArrayList<Media>>>()
|
||||||
fun getLists(): LiveData<MutableMap<String, ArrayList<Media>>> = lists
|
fun getLists(): LiveData<MutableMap<String, ArrayList<Media>>> = lists
|
||||||
suspend fun loadLists(anime: Boolean, userId: Int, sortOrder: String? = null) {
|
suspend fun loadLists(anime: Boolean, userId: Int, sortOrder: String? = null) {
|
||||||
tryWithSuspend {
|
tryWithSuspend {
|
||||||
lists.postValue(Anilist.query.getMediaLists(anime, userId, sortOrder))
|
val res = Anilist.query.getMediaLists(anime, userId, sortOrder)
|
||||||
|
lists.postValue(res)
|
||||||
|
unfilteredLists.postValue(res)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun filterLists(genre: String) {
|
||||||
|
if (genre == "All") {
|
||||||
|
lists.postValue(unfilteredLists.value)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
val currentLists = unfilteredLists.value ?: return
|
||||||
|
val filteredLists = currentLists.mapValues { entry ->
|
||||||
|
entry.value.filter { media ->
|
||||||
|
genre in media.genres
|
||||||
|
} as ArrayList<Media>
|
||||||
|
}.toMutableMap()
|
||||||
|
|
||||||
|
lists.postValue(filteredLists)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -55,6 +55,15 @@
|
||||||
app:srcCompat="@drawable/ic_shuffle_24"
|
app:srcCompat="@drawable/ic_shuffle_24"
|
||||||
app:tint="?attr/colorOnBackground" />
|
app:tint="?attr/colorOnBackground" />
|
||||||
|
|
||||||
|
<ImageButton
|
||||||
|
android:id="@+id/filter"
|
||||||
|
android:layout_width="48dp"
|
||||||
|
android:layout_height="48dp"
|
||||||
|
android:background="?android:attr/selectableItemBackground"
|
||||||
|
android:contentDescription="@string/filter"
|
||||||
|
app:srcCompat="@drawable/ic_round_filter_alt_24"
|
||||||
|
app:tint="?attr/colorOnBackground" />
|
||||||
|
|
||||||
<ImageButton
|
<ImageButton
|
||||||
android:id="@+id/listSort"
|
android:id="@+id/listSort"
|
||||||
android:layout_width="48dp"
|
android:layout_width="48dp"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue