Add APICache + splashscreen init.

This commit is contained in:
Thomas Andres Gomez 2021-05-09 14:45:37 +02:00
parent e9cb8cd0ac
commit 2ad8d5953a
16 changed files with 309 additions and 15 deletions

View file

@ -0,0 +1,157 @@
package com.pixelized.biblib.network.factory
import com.pixelized.biblib.model.*
import com.pixelized.biblib.network.data.response.BookDetailResponse
import com.pixelized.biblib.network.data.response.BookListResponse
import com.pixelized.biblib.utils.exception.MandatoryFieldMissingException
import com.pixelized.biblib.utils.extention.toBoolean
import java.text.SimpleDateFormat
import java.util.*
class BookFactory {
private val parser get() = SimpleDateFormat(FORMAT, Locale.getDefault())
fun fromListResponseToBook(response: BookListResponse.Book, isNew: Boolean = false): Book {
fun error(name: String) =
MandatoryFieldMissingException("#fromListResponseToBook()", name, response)
val id: Int? = response.book_id
val title: String? = response.book_title
val sort: String? = response.book_sort
val authorId: List<String>? = response.author_id
val authorName: List<String>? = response.author_name
val authorSort: List<String>? = response.author_sort
val authorIdSize = authorId?.size ?: 0
val authorNameSize = authorName?.size ?: 0
val authorSortSize = authorSort?.size ?: 0
val author: List<Author>? =
if (authorId != null && authorName != null && authorSort != null) {
if (authorIdSize == authorNameSize && authorNameSize == authorSortSize) {
(authorId.indices).map { index ->
Author(
authorId[index],
authorName[index],
authorSort[index]
)
}
} else if (authorIdSize == 1) {
listOf(
Author(
authorId.first(),
authorName.joinToString { it },
authorSort.joinToString { it })
)
} else {
null
}
} else {
null
}
val cover: Boolean? = response.book_has_cover?.toBoolean()
val releaseDate = response.book_date?.let { parser.parse(it) }
val languageId = response.lang_id
val languageCode = response.lang_code
val language = if (languageId != null && languageCode != null) {
Language(languageId, languageCode)
} else {
null
}
val rating = response.rating?.toIntOrNull()
val seriesName = response.series_name
val seriesSort = response.series_sort
val seriesIndex: Int? = response.book_series_index?.toInt()
val series: Series? = if (seriesName != null && seriesSort != null) {
Series(id = null, name = seriesName, sort = seriesSort, index = seriesIndex)
} else {
null
}
return Book(
id = id ?: throw error("id"),
title = title ?: throw error("title"),
sort = sort ?: throw error("sort"),
author = author ?: throw error("author"),
haveCover = cover ?: throw error("haveCover"),
releaseDate = releaseDate ?: throw error("releaseDate"),
series = series,
language = language,
rating = rating,
isNew = isNew
)
}
fun fromDetailResponseToBook(response: BookDetailResponse, isNew: Boolean = false): Book {
fun error(name: String) =
MandatoryFieldMissingException("#fromDetailResponseToBook()", name, response)
val id: Int? = response.book_id
val title: String? = response.book_title
val sort: String? = response.book_sort
val authorId: List<String>? = response.author_id
val authorName: List<String>? = response.author_name
val authorSort: List<String>? = response.author_sort
val authorIdSize = authorId?.size
val authorNameSize = authorName?.size
val authorSortSize = authorSort?.size
val author: List<Author>? =
if (authorId != null && authorName != null && authorSort != null && authorIdSize == authorNameSize && authorNameSize == authorSortSize) {
authorId.indices.map { index ->
Author(
authorId[index],
authorName[index],
authorSort[index]
)
}
} else {
null
}
val cover: Boolean? = response.book_has_cover?.toBoolean()
val releaseDate = response.book_date?.let { parser.parse(it) }
val languageId = response.lang_id
val languageCode = response.lang_code
val language = if (languageId != null && languageCode != null) {
Language(languageId, languageCode)
} else {
null
}
val rating = response.rating?.toIntOrNull()
val seriesId = response.series_id
val seriesName = response.series_name
val seriesSort = response.series_sort
val seriesIndex: Int? = response.book_series_index?.toInt()
val series: Series? = if (seriesId != null && seriesName != null && seriesSort != null) {
Series(id = seriesId, name = seriesName, sort = seriesSort, seriesIndex)
} else {
null
}
val tagId: List<String>? = response.tag_id
val tagName: List<String>? = response.tag_name
val tagIdSize: Int? = response.tag_id?.size
val tagNameSize: Int? = response.tag_name?.size
val tag: List<Genre>? = if (tagId != null && tagName != null && tagIdSize == tagNameSize) {
tagId.indices.map { Genre(tagId[it], tagName[it]) }
} else {
null
}
val synopsis: String? = response.comment
return Book(
id = id ?: throw error("id"),
title = title ?: throw error("title"),
sort = sort ?: throw error("sort"),
author = author ?: throw error("author"),
haveCover = cover ?: throw error("cover"),
releaseDate = releaseDate ?: throw error("releaseDate"),
language = language ?: throw error("language"),
rating = rating,
series = series,
genre = tag,
synopsis = synopsis,
isNew = isNew
)
}
companion object {
private const val FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
}
}