Added some media uploading.

Updated comments.
This commit is contained in:
Snowyfox 2022-06-01 06:54:02 -04:00
parent 3ca2b58277
commit e7aeee7ba9
3 changed files with 87 additions and 11 deletions

View File

@ -102,19 +102,84 @@ ComposeWindow extends JFrame {
if (composition.contentWarning != null) if (composition.contentWarning != null)
assert !composition.contentWarning.trim().isEmpty(); assert !composition.contentWarning.trim().isEmpty();
// Oh, this is gonna be hell. //tabs.setCursor(new Cursor(Cursor.WAIT_CURSOR));
/* /*
* () For every attachment in composition, * setCursor only works for components that are enabled.
* attempt to upload it, then fill the media ID. * I don't think there's any technical reason for this,
* Then change MastodonApi#submit to accept an * but it's what it is.. rely on the enablement visuals
* array of media IDs. If any upload fails, * or a statusbar to indicate to user.
* show an error message then return. *
* If we really wanted it, I suspect we could use a glass
* pane (or a scroll pane with no scrolling) to have an
* enabled pass-through on top.
*
* Technically contentsDisplay and attachmentsDisplay
* themselves aren't disabled. But disabling the tab pane
* covers both the tab area and content area. We can't
* just the tab area, except maybe by disabling the
* individual tabs.
*/ */
tabs.setEnabled(false);
tabs.setSelectedComponent(contentsDisplay);
contentsDisplay.setSubmitting(true);
tabs.paintImmediately(tabs.getBounds());
contentsDisplay.setSubmitting(true); boolean uploadsOkay = true;
api.submit( for (Attachment a: composition.attachments)
{
if (a.id != null) continue;
// Assume it had already been uploaded.
api.uploadFile(a.uploadee, new RequestListener() {
public void
connectionFailed(IOException eIo)
{
JOptionPane.showMessageDialog(
ComposeWindow.this,
"Tried to upload attachment, failed..."
+ "\n" + eIo.getMessage()
);
}
public void
requestFailed(int httpCode, Tree<String> json)
{
JOptionPane.showMessageDialog(
ComposeWindow.this,
"Tried to upload attachment, failed..."
+ "\n" + json.get("error").value
+ "\n(HTTP code: " + httpCode + ")"
);
}
public void
requestSucceeded(Tree<String> json)
{
a.id = json.get("id").value;
}
});
uploadsOkay &= a.id != null;
}
if (!uploadsOkay)
{
contentsDisplay.setSubmitting(false);
tabs.setEnabled(true);
//tabs.setCursor(null);
return;
}
int amt = composition.attachments.length;
String[] mediaIDs = new String[amt];
for (int o = 0; o < mediaIDs.length; ++o)
mediaIDs[o] = composition.attachments[o].id;
api.submit(
composition.text, composition.visibility, composition.text, composition.visibility,
composition.replyToPostId, composition.contentWarning, composition.replyToPostId, composition.contentWarning,
mediaIDs,
new RequestListener() { new RequestListener() {
public void public void
@ -146,7 +211,10 @@ ComposeWindow extends JFrame {
} }
); );
contentsDisplay.setSubmitting(false); contentsDisplay.setSubmitting(false);
tabs.setEnabled(true);
tabs.setCursor(null);
} }
// - -%- - // - -%- -
@ -339,14 +407,12 @@ implements ActionListener, CaretListener, KeyListener {
text.setEnabled(false); text.setEnabled(false);
visibility.setEnabled(false); visibility.setEnabled(false);
submit.setEnabled(false); submit.setEnabled(false);
setCursor(new Cursor(Cursor.WAIT_CURSOR));
} }
else else
{ {
text.setEnabled(true); text.setEnabled(true);
visibility.setEnabled(true); visibility.setEnabled(true);
submit.setEnabled(true); submit.setEnabled(true);
setCursor(null);
} }
} }

View File

@ -276,6 +276,7 @@ MastodonApi {
submit( submit(
String text, PostVisibility visibility, String text, PostVisibility visibility,
String replyTo, String contentWarning, String replyTo, String contentWarning,
String[] mediaIDs,
RequestListener handler) RequestListener handler)
{ {
String token = accessToken.get("access_token").value; String token = accessToken.get("access_token").value;
@ -315,6 +316,10 @@ MastodonApi {
if (contentWarning != null) { if (contentWarning != null) {
output.write("&spoiler_text=" + contentWarning); output.write("&spoiler_text=" + contentWarning);
} }
for (String mediaID: mediaIDs) {
output.write("&media_ids[]=" + mediaID);
}
output.close(); output.close();
doStandardJsonReturn(conn, handler); doStandardJsonReturn(conn, handler);
@ -516,7 +521,7 @@ MastodonApi {
istream.close(); istream.close();
ostream.close(); ostream.close();
wrapResponseInTree(conn, handler); doStandardJsonReturn(conn, handler);
} }
catch (IOException eIo) { handler.connectionFailed(eIo); } catch (IOException eIo) { handler.connectionFailed(eIo); }
} }

View File

@ -469,6 +469,11 @@ implements ActionListener {
* this, using MediaTracker. That assumes what I'm doing * this, using MediaTracker. That assumes what I'm doing
* right now definitely works. * right now definitely works.
*/ */
/*
* () It doesn't work. I might be forced to use
* BufferedImage with ImageIO. Or have
* ImageApi#getScaledInstance spam until it works.
*/
profile.setImage(n); profile.setImage(n);
} }