-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
736 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 193 additions & 6 deletions
199
anilist/src/commonMain/kotlin/dev/datlag/aniflow/anilist/model/Character.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,204 @@ | ||
package dev.datlag.aniflow.anilist.model | ||
|
||
import dev.datlag.aniflow.anilist.CharacterQuery | ||
import dev.datlag.aniflow.anilist.MediumQuery | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
open class Character( | ||
/** | ||
* The id of the character | ||
*/ | ||
open val id: Int, | ||
|
||
/** | ||
* The names of the character | ||
*/ | ||
open val name: Name, | ||
|
||
/** | ||
* Character images | ||
*/ | ||
open val image: Image, | ||
|
||
/** | ||
* The character's gender. | ||
* Usually Male, Female, or Non-binary but can be any string. | ||
*/ | ||
open val gender: String?, | ||
|
||
/** | ||
* The characters blood type | ||
*/ | ||
open val bloodType: String?, | ||
|
||
/** | ||
* The character's birthdate | ||
*/ | ||
open val birthDate: Character.BirthDate?, | ||
|
||
/** | ||
* A general description of the character | ||
*/ | ||
open val description: String?, | ||
) { | ||
constructor(char: CharacterQuery.Character) : this( | ||
id = char.id, | ||
gender = char.gender?.ifBlank { null }, | ||
bloodType = char.bloodType?.ifBlank { null }, | ||
) | ||
} | ||
|
||
@Serializable | ||
data class Name( | ||
/** | ||
* The character's given name | ||
*/ | ||
val first: String?, | ||
|
||
/** | ||
* The character's middle name | ||
*/ | ||
val middle: String?, | ||
|
||
/** | ||
* The character's surname | ||
*/ | ||
val last: String?, | ||
|
||
/** | ||
* The character's first and last name | ||
*/ | ||
val full: String?, | ||
|
||
/** | ||
* The character's full name in their native language | ||
*/ | ||
val native: String?, | ||
|
||
/** | ||
* The currently authenticated users preferred name language. Default romaji for | ||
* non-authenticated | ||
*/ | ||
val userPreferred: String? | ||
) { | ||
constructor(name: MediumQuery.Name) : this( | ||
first = name.first?.ifBlank { null }, | ||
middle = name.middle?.ifBlank { null }, | ||
last = name.last?.ifBlank { null }, | ||
full = name.full?.ifBlank { null }, | ||
native = name.native?.ifBlank { null }, | ||
userPreferred = name.userPreferred?.ifBlank { null } | ||
) | ||
|
||
constructor(name: CharacterQuery.Name) : this( | ||
first = name.first?.ifBlank { null }, | ||
middle = name.middle?.ifBlank { null }, | ||
last = name.last?.ifBlank { null }, | ||
full = name.full?.ifBlank { null }, | ||
native = name.native?.ifBlank { null }, | ||
userPreferred = name.userPreferred?.ifBlank { null } | ||
) | ||
} | ||
|
||
@Serializable | ||
data class Image( | ||
val large: String?, | ||
val medium: String? | ||
) { | ||
constructor(image: MediumQuery.Image) : this( | ||
large = image.large?.ifBlank { null }, | ||
medium = image.medium?.ifBlank { null } | ||
) | ||
|
||
constructor(image: CharacterQuery.Image) : this( | ||
large = image.large?.ifBlank { null }, | ||
medium = image.medium?.ifBlank { null }, | ||
) | ||
} | ||
|
||
@Serializable | ||
data class BirthDate( | ||
val day: Int?, | ||
val month: Int?, | ||
val year: Int? | ||
) { | ||
fun format(): String { | ||
return buildString { | ||
if (day != null) { | ||
if (day <= 9) { | ||
append("0$day") | ||
} else { | ||
append(day) | ||
} | ||
append(". ") | ||
} | ||
if (month != null) { | ||
when (month) { | ||
1 -> append("Jan") | ||
2 -> append("Feb") | ||
3 -> append("Mar") | ||
4 -> append("Apr") | ||
5 -> append("May") | ||
6 -> append("Jun") | ||
7 -> append("Jul") | ||
8 -> append("Aug") | ||
9 -> append("Sep") | ||
10 -> append("Oct") | ||
11 -> append("Nov") | ||
12 -> append("Dec") | ||
else -> if (month <= 9) { | ||
append("0$month.") | ||
} else { | ||
append("$month.") | ||
} | ||
} | ||
append(' ') | ||
} | ||
if (year != null) { | ||
append(year) | ||
} | ||
}.trim() | ||
} | ||
|
||
companion object { | ||
operator fun invoke(birth: CharacterQuery.DateOfBirth) : BirthDate? { | ||
if (birth.day == null && birth.month == null && birth.year == null) { | ||
return null | ||
} | ||
|
||
return BirthDate( | ||
day = birth.day, | ||
month = birth.month, | ||
year = birth.year | ||
) | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
operator fun invoke(character: MediumQuery.Node) : Character? { | ||
val name = character.name?.let(::Name) ?: return null | ||
val image = character.image?.let(::Image) ?: return null | ||
|
||
return Character( | ||
id = character.id, | ||
name = name, | ||
image = image, | ||
gender = null, | ||
bloodType = null, | ||
birthDate = null, | ||
description = null | ||
) | ||
} | ||
|
||
operator fun invoke(character: CharacterQuery.Character) : Character? { | ||
val name = character.name?.let(::Name) ?: return null | ||
val image = character.image?.let(::Image) ?: return null | ||
|
||
return Character( | ||
id = character.id, | ||
name = name, | ||
image = image, | ||
gender = character.gender?.ifBlank { null }, | ||
bloodType = character.bloodType?.ifBlank { null }, | ||
birthDate = character.dateOfBirth?.let { BirthDate(it) }, | ||
description = character.description?.ifBlank { null } | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.