Added sorting options for anime lists
This commit is contained in:
@ -156,63 +156,6 @@ func (list *AnimeList) User() *User {
|
||||
return user
|
||||
}
|
||||
|
||||
// Sort ...
|
||||
func (list *AnimeList) Sort() {
|
||||
list.Lock()
|
||||
defer list.Unlock()
|
||||
|
||||
sort.Slice(list.Items, func(i, j int) bool {
|
||||
a := list.Items[i]
|
||||
b := list.Items[j]
|
||||
|
||||
if (a.Status != AnimeListStatusWatching && a.Status != AnimeListStatusPlanned) && (b.Status != AnimeListStatusWatching && b.Status != AnimeListStatusPlanned) {
|
||||
if a.Rating.Overall == b.Rating.Overall {
|
||||
return a.Anime().Title.Canonical < b.Anime().Title.Canonical
|
||||
}
|
||||
|
||||
return a.Rating.Overall > b.Rating.Overall
|
||||
}
|
||||
|
||||
epsA := a.Anime().UpcomingEpisode()
|
||||
epsB := b.Anime().UpcomingEpisode()
|
||||
|
||||
if epsA == nil && epsB == nil {
|
||||
if a.Rating.Overall == b.Rating.Overall {
|
||||
return a.Anime().Title.Canonical < b.Anime().Title.Canonical
|
||||
}
|
||||
|
||||
return a.Rating.Overall > b.Rating.Overall
|
||||
}
|
||||
|
||||
if epsA == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if epsB == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
return epsA.Episode.AiringDate.Start < epsB.Episode.AiringDate.Start
|
||||
})
|
||||
}
|
||||
|
||||
// SortByRating sorts the anime list by overall rating.
|
||||
func (list *AnimeList) SortByRating() {
|
||||
list.Lock()
|
||||
defer list.Unlock()
|
||||
|
||||
sort.Slice(list.Items, func(i, j int) bool {
|
||||
a := list.Items[i]
|
||||
b := list.Items[j]
|
||||
|
||||
if a.Rating.Overall == b.Rating.Overall {
|
||||
return a.Anime().Title.Canonical < b.Anime().Title.Canonical
|
||||
}
|
||||
|
||||
return a.Rating.Overall > b.Rating.Overall
|
||||
})
|
||||
}
|
||||
|
||||
// Top returns the top entries.
|
||||
func (list *AnimeList) Top(count int) []*AnimeListItem {
|
||||
list.Lock()
|
||||
|
58
arn/AnimeListSort.go
Normal file
58
arn/AnimeListSort.go
Normal file
@ -0,0 +1,58 @@
|
||||
package arn
|
||||
|
||||
import "sort"
|
||||
|
||||
// Sort sorts the anime list by the given algorithm.
|
||||
func (list *AnimeList) Sort(algorithm string) {
|
||||
list.Lock()
|
||||
defer list.Unlock()
|
||||
|
||||
switch algorithm {
|
||||
case SortByTitle:
|
||||
sort.Slice(list.Items, func(i, j int) bool {
|
||||
a := list.Items[i]
|
||||
b := list.Items[j]
|
||||
|
||||
return a.Anime().Title.Canonical < b.Anime().Title.Canonical
|
||||
})
|
||||
|
||||
case SortByRating:
|
||||
sort.Slice(list.Items, func(i, j int) bool {
|
||||
a := list.Items[i]
|
||||
b := list.Items[j]
|
||||
|
||||
if a.Rating.Overall == b.Rating.Overall {
|
||||
return a.Anime().Title.Canonical < b.Anime().Title.Canonical
|
||||
}
|
||||
|
||||
return a.Rating.Overall > b.Rating.Overall
|
||||
})
|
||||
|
||||
case SortByAiringDate:
|
||||
sort.Slice(list.Items, func(i, j int) bool {
|
||||
a := list.Items[i]
|
||||
b := list.Items[j]
|
||||
|
||||
epsA := a.Anime().UpcomingEpisode()
|
||||
epsB := b.Anime().UpcomingEpisode()
|
||||
|
||||
if epsA == nil && epsB == nil {
|
||||
if a.Rating.Overall == b.Rating.Overall {
|
||||
return a.Anime().Title.Canonical < b.Anime().Title.Canonical
|
||||
}
|
||||
|
||||
return a.Rating.Overall > b.Rating.Overall
|
||||
}
|
||||
|
||||
if epsA == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if epsB == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
return epsA.Episode.AiringDate.Start < epsB.Episode.AiringDate.Start
|
||||
})
|
||||
}
|
||||
}
|
@ -30,10 +30,9 @@ const (
|
||||
// Settings represents user settings.
|
||||
type Settings struct {
|
||||
UserID string `json:"userId"`
|
||||
SortBy string `json:"sortBy"`
|
||||
SortBy string `json:"sortBy" editable:"true"`
|
||||
TitleLanguage string `json:"titleLanguage" editable:"true"`
|
||||
Providers ServiceProviders `json:"providers"`
|
||||
Avatar AvatarSettings `json:"avatar"`
|
||||
Format FormatSettings `json:"format"`
|
||||
Notification NotificationSettings `json:"notification"`
|
||||
Editor EditorSettings `json:"editor"`
|
||||
@ -110,30 +109,20 @@ type ServiceProviders struct {
|
||||
Anime string `json:"anime"`
|
||||
}
|
||||
|
||||
// AvatarSettings ...
|
||||
type AvatarSettings struct {
|
||||
Source string `json:"source" editable:"true"`
|
||||
SourceURL string `json:"sourceUrl" editable:"true"`
|
||||
}
|
||||
|
||||
// CalendarSettings ...
|
||||
type CalendarSettings struct {
|
||||
ShowAddedAnimeOnly bool `json:"showAddedAnimeOnly" editable:"true"`
|
||||
}
|
||||
|
||||
// NewSettings ...
|
||||
// NewSettings creates the default settings for a new user.
|
||||
func NewSettings(user *User) *Settings {
|
||||
return &Settings{
|
||||
UserID: user.ID,
|
||||
SortBy: SortByAiringDate,
|
||||
SortBy: SortByRating,
|
||||
TitleLanguage: TitleLanguageCanonical,
|
||||
Providers: ServiceProviders{
|
||||
Anime: "",
|
||||
},
|
||||
Avatar: AvatarSettings{
|
||||
Source: "",
|
||||
SourceURL: "",
|
||||
},
|
||||
Format: FormatSettings{
|
||||
RatingsPrecision: 1,
|
||||
},
|
||||
|
Reference in New Issue
Block a user