gui: better refresh for changeSTA
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderBookInfo.java
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 if (meta != null) {
36 return meta.getTitle();
37 }
38
39 return value;
40 }
41
42 public String getSecondaryInfo(boolean seeCount) {
43 String author = meta == null ? null : meta.getAuthor();
44 String secondaryInfo = seeCount ? count : author;
45
46 if (secondaryInfo != null && !secondaryInfo.trim().isEmpty()) {
47 secondaryInfo = "(" + secondaryInfo + ")";
48 } else {
49 secondaryInfo = "";
50 }
51
52 return secondaryInfo;
53 }
54
55 /**
56 * A unique ID for this {@link GuiReaderBookInfo}.
57 *
58 * @return the unique ID
59 */
60 public String getId() {
61 return id;
62 }
63
64 // can return null for non-books
65 public MetaData getMeta() {
66 return meta;
67 }
68
69 public Image getBaseImage(BasicLibrary lib) {
70 switch (type) {
71 case STORY:
72 return lib.getCover(meta.getLuid());
73 case SOURCE:
74 return lib.getSourceCover(value);
75 case AUTHOR:
76 return lib.getAuthorCover(value);
77 }
78
79 return null;
80 }
81
82 static public GuiReaderBookInfo fromMeta(MetaData meta) {
83 String uid = meta.getUuid();
84 if (uid == null || uid.trim().isEmpty()) {
85 uid = meta.getLuid();
86 }
87
88 GuiReaderBookInfo info = new GuiReaderBookInfo(Type.STORY, uid,
89 meta.getTitle());
90
91 info.meta = meta;
92 info.count = formatNumber(meta.getWords(),
93 meta.isImageDocument() ? "images" : "words");
94
95 return info;
96 }
97
98 static public GuiReaderBookInfo fromSource(BasicLibrary lib, String source) {
99 GuiReaderBookInfo info = new GuiReaderBookInfo(Type.SOURCE, "source_"
100 + source, source);
101
102 info.count = formatNumber(lib.getListBySource(source).size(), "stories");
103
104 return info;
105 }
106
107 static public GuiReaderBookInfo fromAuthor(BasicLibrary lib, String author) {
108 GuiReaderBookInfo info = new GuiReaderBookInfo(Type.AUTHOR, "author_"
109 + author, author);
110
111 info.count = formatNumber(lib.getListByAuthor(author).size(), "stories");
112
113 return info;
114 }
115
116 static private String formatNumber(long number, String ofWhat) {
117 String displayNumber;
118 if (number >= 4000) {
119 displayNumber = "" + (number / 1000) + "k";
120 } else if (number > 0) {
121 displayNumber = "" + number;
122 } else {
123 displayNumber = "";
124 }
125
126 if (!displayNumber.isEmpty()) {
127 displayNumber += " " + ofWhat;
128 }
129
130 return displayNumber;
131 }
132 }