More satisfactory..

This commit is contained in:
Snowyfox 2021-07-29 05:26:16 -04:00
parent b47a97e8ad
commit 31277c7871

View File

@ -53,40 +53,34 @@ implements ActionListener {
this.post = post;
List<RepliesComponent.Reply> replies = null;
{
List<Post> posts = null;
// We should make a request to JKomasto here.
}
if (replies == null)
{
RepliesComponent.Reply reply1, reply2, reply3;
reply1 = new RepliesComponent.Reply();
reply1.author = "Black tea";
reply1.text = "Rich..";
reply2 = new RepliesComponent.Reply();
reply2.author = "Green tea";
reply2.text = "Clean!";
reply3 = new RepliesComponent.Reply();
reply3.author = "Coffee";
reply3.text = "sleepy..";
replies = new ArrayList<>();
replies.add(reply1);
replies.add(reply2);
replies.add(reply3);
}
postDisplay.setAuthor(post.authorName);
postDisplay.setTime(post.date);
postDisplay.setText(post.text);
/*
* We also have to display the replies for that post.
* Are we supposed to request those replies ourselves?
* Shouldn't the caller of setPost also give us the
* reply posts?
*/
if (post == null)
{
List<String> authors = new ArrayList<>();
List<String> texts = new ArrayList<>();
authors.add("Black tea");
authors.add("Green tea");
authors.add("White tea");
texts.add("Rich..");
texts.add("Clean!");
texts.add("Myu..");
repliesDisplay.setReplies(authors, texts);
/*
* THe replies display contains a reduction of every
* reply post. Rather than repeatedly loop through the
* replies to assemble lists of individual information,
* we should loop once and assemble a list of structs.
* Then repliesDisplay should support that struct..
*
* Alternatively, repliesDisplay can accept (reply) posts
* directly, avoiding a new struct. But that goes against
* our communication model here.
*/
}
repliesDisplay.setReplies(replies);
}
// - -%- -
@ -238,8 +232,10 @@ PostComponent extends JPanel {
class
RepliesComponent extends JPanel {
private List<String>
authors, texts;
private List<RepliesComponent.Reply>
replies;
// - -%- -
private JButton
prevPage, nextPage;
@ -253,26 +249,11 @@ RepliesComponent extends JPanel {
// ---%-@-%---
public void
setReplies(List<String> authors, List<String> texts)
setReplies(List<RepliesComponent.Reply> replies)
{
assert authors != null;
assert texts != null;
assert authors.size() == texts.size();
this.authors.clear();
this.authors.addAll(authors);
this.texts.clear();
this.texts.addAll(texts);
assert replies != null;
this.replies = replies;
displayPage(1);
int currentPage = authors.isEmpty() ? 0 : 1;
int pages = (7 + authors.size()) / 8;
// (0 + 7) / 8 = 0
// (1 + 7) / 8 = 1
// (8 + 7) / 8 = 1
// (9 + 7) / 8 = 2
// (32 + 7) / 8 = 4
pageLabel.setText(currentPage + "/" + pages);
}
// - -%- -
@ -280,44 +261,61 @@ RepliesComponent extends JPanel {
private void
displayPage(int pageNumber)
{
int oS = (pageNumber - 1) * 8;
if (oS >= authors.size()) return;
// () Quietly fail?
int oE = Math.min(oS + 8, authors.size());
List<String> authors = this.authors.subList(oS, oE);
List<String> texts = this.texts.subList(oS, oE);
int l = oE - oS;
assert pageNumber > 0;
assert this.replies != null;
for (int o = 0; o < 8; ++o)
List<RepliesComponent.Reply> page;
{
int oS = (pageNumber - 1) * 8;
int oE = Math.min(oS + 8, replies.size());
if (oS > oE) page = new ArrayList<>();
else page = this.replies.subList(oS, oE);
}
for (int o = 0; o < page.size(); ++o)
{
assert o < previews.length;
ReplyPreviewComponent preview = previews[o];
Reply reply = replies.get(o);
preview.setAuthor(reply.author);
preview.setText(reply.text);
preview.setVisible(true);
}
for (int o = page.size(); o < previews.length; ++o)
{
ReplyPreviewComponent preview = previews[o];
if (o >= l) // That doesn't seem right..
{
preview.setVisible(false);
}
else
{
preview.setAuthor(authors.get(oS + o));
preview.setText(texts.get(oS + o));
preview.setVisible(true);
}
preview.setVisible(false);
}
int pages = 1 + ((replies.size() - 1) / 8);
pageLabel.setText(pageNumber + "/" + pages);
}
// ---%-@-%---
public static class
Reply {
public String
author;
public String
text;
}
// ---%-@-%---
RepliesComponent()
{
authors = new ArrayList<>();
texts = new ArrayList<>();
prevPage = new JButton("<");
nextPage = new JButton(">");
prevPage.setEnabled(false);
nextPage.setEnabled(false);
pageLabel = new JLabel("0 / 0");
pageLabel = new JLabel();
Box bottom = Box.createHorizontalBox();
bottom.add(Box.createGlue());
@ -344,12 +342,13 @@ RepliesComponent extends JPanel {
add(bottom, BorderLayout.SOUTH);
setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
setReplies(new ArrayList<>());
}
}
class
ReplyPreviewComponent extends JButton {
@ -396,8 +395,4 @@ ReplyPreviewComponent extends JButton {
g.drawString(getText(), 8, 2 * getHeight() / 3);
}
// ---%-@-%---
ReplyPreviewComponent() { }
}