1 package be
.nikiroo
.fanfix
.reader
.ui
;
3 import be
.nikiroo
.fanfix
.bundles
.StringIdGui
;
4 import be
.nikiroo
.fanfix
.data
.MetaData
;
5 import be
.nikiroo
.fanfix
.data
.Story
;
6 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
7 import be
.nikiroo
.utils
.Image
;
8 import be
.nikiroo
.utils
.StringUtils
;
11 * Some meta information related to a "book" (which can either be a
12 * {@link Story}, a fake-story grouping some authors or a fake-story grouping
13 * some sources/types).
17 public class GuiReaderBookInfo
{
19 /** A normal story, which can be "read". */
22 * A special, empty story that represents a source/type common to one or
23 * more normal stories.
26 /** A special, empty story that represents an author. */
35 private MetaData meta
;
38 * For private use, see the "fromXXX" constructors instead for public use.
43 * the main id, which must uniquely identify this book and will
44 * be used as a unique ID later on
46 * the main value to show (see
47 * {@link GuiReaderBookInfo#getMainInfo()})
49 private GuiReaderBookInfo(Type type
, String id
, String value
) {
56 * Get the main info to display for this book (a title, an author, a
57 * source/type name...).
59 * Note that when {@link MetaData} about the book are present, the title
60 * inside is returned instead of the actual value (that way, we can update
61 * the {@link MetaData} and see the changes here).
63 * @return the main info, usually the title
65 public String
getMainInfo() {
67 return meta
.getTitle();
74 * Get the secondary info, of the given type.
77 * TRUE for word/image/story count, FALSE for author name
79 * @return the secondary info
81 public String
getSecondaryInfo(boolean seeCount
) {
82 String author
= meta
== null ?
null : meta
.getAuthor();
83 String secondaryInfo
= seeCount ? count
: author
;
85 if (secondaryInfo
!= null && !secondaryInfo
.trim().isEmpty()) {
86 secondaryInfo
= "(" + secondaryInfo
+ ")";
95 * A unique ID for this {@link GuiReaderBookInfo}.
97 * @return the unique ID
99 public String
getId() {
104 * The {@link MetaData} associated with this book, if this book is a
107 * Can be NULL for non-story books (authors or sources/types).
109 * @return the {@link MetaData} or NULL
111 public MetaData
getMeta() {
116 * Get the base image to use to represent this book.
118 * The image is <b>NOT</b> resized in any way, this is the original version.
120 * It can be NULL if no image can be found for this book.
123 * the {@link BasicLibrary} to use to fetch the image
125 * @return the base image
127 public Image
getBaseImage(BasicLibrary lib
) {
130 return lib
.getCover(meta
.getLuid());
132 return lib
.getSourceCover(value
);
134 return lib
.getAuthorCover(value
);
141 * Create a new book describing the given {@link Story}.
144 * the {@link MetaData} representing the {@link Story}
148 static public GuiReaderBookInfo
fromMeta(MetaData meta
) {
149 String uid
= meta
.getUuid();
150 if (uid
== null || uid
.trim().isEmpty()) {
151 uid
= meta
.getLuid();
154 GuiReaderBookInfo info
= new GuiReaderBookInfo(Type
.STORY
, uid
,
158 info
.count
= formatNumber(meta
.getWords());
159 if (!info
.count
.isEmpty()) {
160 info
.count
= GuiReader
.trans(
161 meta
.isImageDocument() ? StringIdGui
.BOOK_COUNT_IMAGES
162 : StringIdGui
.BOOK_COUNT_WORDS
, info
.count
);
169 * Create a new book describing the given source/type.
172 * the {@link BasicLibrary} to use to retrieve some more
173 * information about the source
179 static public GuiReaderBookInfo
fromSource(BasicLibrary lib
, String source
) {
180 GuiReaderBookInfo info
= new GuiReaderBookInfo(Type
.SOURCE
, "source_"
183 info
.count
= formatNumber(lib
.getListBySource(source
).size());
184 if (!info
.count
.isEmpty()) {
185 info
.count
= GuiReader
.trans(StringIdGui
.BOOK_COUNT_STORIES
,
193 * Create a new book describing the given author.
196 * the {@link BasicLibrary} to use to retrieve some more
197 * information about the author
203 static public GuiReaderBookInfo
fromAuthor(BasicLibrary lib
, String author
) {
204 GuiReaderBookInfo info
= new GuiReaderBookInfo(Type
.AUTHOR
, "author_"
207 info
.count
= formatNumber(lib
.getListByAuthor(author
).size());
208 if (!info
.count
.isEmpty()) {
209 info
.count
= GuiReader
.trans(StringIdGui
.BOOK_COUNT_STORIES
,
217 * Format a number for display (use the "k" notation if higher or equal to
221 * the number to parse
223 * @return the displayable version of the number
225 * @deprecated use {@link StringUtils} after update instead
228 static private String
formatNumber(long number
) {
229 // TODO: replace with StringUtils after update
230 String displayNumber
;
231 if (number
>= 4000) {
232 displayNumber
= "" + (number
/ 1000) + "k";
233 } else if (number
> 0) {
234 displayNumber
= "" + number
;
239 return displayNumber
;