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; 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.setAuthor(post.authorName);
postDisplay.setTime(post.date); postDisplay.setTime(post.date);
postDisplay.setText(post.text); postDisplay.setText(post.text);
repliesDisplay.setReplies(replies);
/*
* 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.
*/
}
} }
// - -%- - // - -%- -
@ -238,8 +232,10 @@ PostComponent extends JPanel {
class class
RepliesComponent extends JPanel { RepliesComponent extends JPanel {
private List<String> private List<RepliesComponent.Reply>
authors, texts; replies;
// - -%- -
private JButton private JButton
prevPage, nextPage; prevPage, nextPage;
@ -253,71 +249,73 @@ RepliesComponent extends JPanel {
// ---%-@-%--- // ---%-@-%---
public void public void
setReplies(List<String> authors, List<String> texts) setReplies(List<RepliesComponent.Reply> replies)
{ {
assert authors != null; assert replies != null;
assert texts != null; this.replies = replies;
assert authors.size() == texts.size();
this.authors.clear();
this.authors.addAll(authors);
this.texts.clear();
this.texts.addAll(texts);
displayPage(1); 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);
} }
// - -%- - // - -%- -
private void private void
displayPage(int pageNumber) displayPage(int pageNumber)
{
assert pageNumber > 0;
assert this.replies != null;
List<RepliesComponent.Reply> page;
{ {
int oS = (pageNumber - 1) * 8; int oS = (pageNumber - 1) * 8;
if (oS >= authors.size()) return; int oE = Math.min(oS + 8, replies.size());
// () Quietly fail? if (oS > oE) page = new ArrayList<>();
int oE = Math.min(oS + 8, authors.size()); else page = this.replies.subList(oS, oE);
List<String> authors = this.authors.subList(oS, oE);
List<String> texts = this.texts.subList(oS, oE);
int l = oE - oS;
for (int o = 0; o < 8; ++o)
{
ReplyPreviewComponent preview = previews[o];
if (o >= l) // That doesn't seem right..
{
preview.setVisible(false);
} }
else
for (int o = 0; o < page.size(); ++o)
{ {
preview.setAuthor(authors.get(oS + o)); assert o < previews.length;
preview.setText(texts.get(oS + o));
ReplyPreviewComponent preview = previews[o];
Reply reply = replies.get(o);
preview.setAuthor(reply.author);
preview.setText(reply.text);
preview.setVisible(true); preview.setVisible(true);
} }
for (int o = page.size(); o < previews.length; ++o)
{
ReplyPreviewComponent preview = previews[o];
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() RepliesComponent()
{ {
authors = new ArrayList<>();
texts = new ArrayList<>();
prevPage = new JButton("<"); prevPage = new JButton("<");
nextPage = new JButton(">"); nextPage = new JButton(">");
prevPage.setEnabled(false); prevPage.setEnabled(false);
nextPage.setEnabled(false); nextPage.setEnabled(false);
pageLabel = new JLabel("0 / 0"); pageLabel = new JLabel();
Box bottom = Box.createHorizontalBox(); Box bottom = Box.createHorizontalBox();
bottom.add(Box.createGlue()); bottom.add(Box.createGlue());
@ -344,12 +342,13 @@ RepliesComponent extends JPanel {
add(bottom, BorderLayout.SOUTH); add(bottom, BorderLayout.SOUTH);
setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
setReplies(new ArrayList<>());
} }
} }
class class
ReplyPreviewComponent extends JButton { ReplyPreviewComponent extends JButton {
@ -396,8 +395,4 @@ ReplyPreviewComponent extends JButton {
g.drawString(getText(), 8, 2 * getHeight() / 3); g.drawString(getText(), 8, 2 * getHeight() / 3);
} }
// ---%-@-%---
ReplyPreviewComponent() { }
} }