| 1 | package be.nikiroo.fanfix.reader.ui; |
| 2 | |
| 3 | import be.nikiroo.fanfix.data.MetaData; |
| 4 | import be.nikiroo.fanfix.library.BasicLibrary; |
| 5 | import be.nikiroo.utils.Image; |
| 6 | |
| 7 | public class GuiReaderBookInfo { |
| 8 | public enum Type { |
| 9 | /** A normal story, which can be "read". */ |
| 10 | STORY, |
| 11 | /** |
| 12 | * A special, empty story that represents a source/type common to one or |
| 13 | * more normal stories. |
| 14 | */ |
| 15 | SOURCE, |
| 16 | /** A special, empty story that represents an author. */ |
| 17 | AUTHOR |
| 18 | } |
| 19 | |
| 20 | private Type type; |
| 21 | private String id; |
| 22 | private String value; |
| 23 | private String count; |
| 24 | |
| 25 | private MetaData meta; |
| 26 | |
| 27 | // use the fromXXX methods |
| 28 | private GuiReaderBookInfo(Type type, String id, String value) { |
| 29 | this.type = type; |
| 30 | this.id = id; |
| 31 | this.value = value; |
| 32 | } |
| 33 | |
| 34 | public String getMainInfo() { |
| 35 | return value; |
| 36 | } |
| 37 | |
| 38 | public String getSecondaryInfo(boolean seeCount) { |
| 39 | String author = meta == null ? null : meta.getAuthor(); |
| 40 | String secondaryInfo = seeCount ? count : author; |
| 41 | |
| 42 | if (secondaryInfo != null && !secondaryInfo.trim().isEmpty()) { |
| 43 | secondaryInfo = "(" + secondaryInfo + ")"; |
| 44 | } else { |
| 45 | secondaryInfo = ""; |
| 46 | } |
| 47 | |
| 48 | return secondaryInfo; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * A unique ID for this {@link GuiReaderBookInfo}. |
| 53 | * |
| 54 | * @return the unique ID |
| 55 | */ |
| 56 | public String getId() { |
| 57 | return id; |
| 58 | } |
| 59 | |
| 60 | // can return null for non-books |
| 61 | public MetaData getMeta() { |
| 62 | return meta; |
| 63 | } |
| 64 | |
| 65 | public Image getBaseImage(BasicLibrary lib) { |
| 66 | switch (type) { |
| 67 | case STORY: |
| 68 | return lib.getCover(meta.getLuid()); |
| 69 | case SOURCE: |
| 70 | return lib.getSourceCover(value); |
| 71 | case AUTHOR: |
| 72 | return lib.getAuthorCover(value); |
| 73 | } |
| 74 | |
| 75 | return null; |
| 76 | } |
| 77 | |
| 78 | static public GuiReaderBookInfo fromMeta(MetaData meta) { |
| 79 | String uid = meta.getUuid(); |
| 80 | if (uid == null || uid.trim().isEmpty()) { |
| 81 | uid = meta.getLuid(); |
| 82 | } |
| 83 | |
| 84 | GuiReaderBookInfo info = new GuiReaderBookInfo(Type.STORY, uid, |
| 85 | meta.getTitle()); |
| 86 | |
| 87 | info.meta = meta; |
| 88 | info.count = formatNumber(meta.getWords(), |
| 89 | meta.isImageDocument() ? "images" : "words"); |
| 90 | |
| 91 | return info; |
| 92 | } |
| 93 | |
| 94 | static public GuiReaderBookInfo fromSource(BasicLibrary lib, String source) { |
| 95 | GuiReaderBookInfo info = new GuiReaderBookInfo(Type.SOURCE, "source_" |
| 96 | + source, source); |
| 97 | |
| 98 | info.count = formatNumber(lib.getListBySource(source).size(), "stories"); |
| 99 | |
| 100 | return info; |
| 101 | } |
| 102 | |
| 103 | static public GuiReaderBookInfo fromAuthor(BasicLibrary lib, String author) { |
| 104 | GuiReaderBookInfo info = new GuiReaderBookInfo(Type.AUTHOR, "author_" |
| 105 | + author, author); |
| 106 | |
| 107 | info.count = formatNumber(lib.getListByAuthor(author).size(), "stories"); |
| 108 | |
| 109 | return info; |
| 110 | } |
| 111 | |
| 112 | static private String formatNumber(long number, String ofWhat) { |
| 113 | String displayNumber; |
| 114 | if (number >= 4000) { |
| 115 | displayNumber = "" + (number / 1000) + "k"; |
| 116 | } else if (number > 0) { |
| 117 | displayNumber = "" + number; |
| 118 | } else { |
| 119 | displayNumber = ""; |
| 120 | } |
| 121 | |
| 122 | if (!displayNumber.isEmpty()) { |
| 123 | displayNumber += " " + ofWhat; |
| 124 | } |
| 125 | |
| 126 | return displayNumber; |
| 127 | } |
| 128 | } |