Sign-in through the Telegram bot with a nickname, follows and followers (the list of followers is for its owner alone), an inbox of new works of the authors one follows, notifications through the bot. Publishing still needs the TikTok account to be proven. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
203 lines
8.5 KiB
Java
203 lines
8.5 KiB
Java
package lihonstore.app;
|
||
|
||
import android.graphics.Bitmap;
|
||
import android.graphics.BitmapFactory;
|
||
import android.net.Uri;
|
||
import android.text.InputFilter;
|
||
import android.view.View;
|
||
import android.widget.EditText;
|
||
import android.widget.LinearLayout;
|
||
import android.widget.ScrollView;
|
||
import android.widget.TextView;
|
||
|
||
import org.json.JSONObject;
|
||
|
||
import java.io.IOException;
|
||
|
||
/**
|
||
* The "+" of the bottom bar: choose what it is (a frame or a chat bubble), a picture, a name and a description, and publish. A picture
|
||
* that is not a frame is cut into a ring here, the same way the mod does it. It goes to the AI and then to the owner before anyone sees it.
|
||
*/
|
||
final class PublishPage {
|
||
private final MainActivity host;
|
||
private final ScrollView scroll;
|
||
private final TextView frameChip;
|
||
private final TextView bubbleChip;
|
||
private final ArtView preview;
|
||
private final EditText name;
|
||
private final EditText description;
|
||
private final TextView send;
|
||
private final TextView hint;
|
||
private boolean frame = true;
|
||
private byte[] png;
|
||
private Bitmap source;
|
||
private int previews;
|
||
|
||
PublishPage(final MainActivity host) {
|
||
this.host = host;
|
||
Theme theme = host.theme();
|
||
scroll = new ScrollView(host);
|
||
LinearLayout page = theme.column(host);
|
||
page.setPadding(theme.dp(20), theme.dp(16), theme.dp(20), theme.dp(24));
|
||
scroll.addView(page);
|
||
page.addView(theme.text(host, "Опубликовать", 26f, theme.onSurface, true));
|
||
TextView about = theme.text(host, "Сначала картинку проверит ИИ, потом модератор. Пока не примут, её никто не увидит.", 14f, theme.muted, false);
|
||
about.setPadding(0, theme.dp(4), 0, theme.dp(12));
|
||
page.addView(about);
|
||
|
||
LinearLayout chips = theme.row(host);
|
||
frameChip = theme.chip(host, "Рамка", true);
|
||
bubbleChip = theme.chip(host, "Пузырь чата", false);
|
||
frameChip.setOnClickListener(v -> kind(true));
|
||
bubbleChip.setOnClickListener(v -> kind(false));
|
||
LinearLayout.LayoutParams gap = new LinearLayout.LayoutParams(-2, -2);
|
||
gap.rightMargin = theme.dp(8);
|
||
chips.addView(frameChip, gap);
|
||
chips.addView(bubbleChip);
|
||
page.addView(chips);
|
||
|
||
preview = new ArtView(host, theme);
|
||
LinearLayout.LayoutParams previewParams = new LinearLayout.LayoutParams(-1, theme.dp(220));
|
||
previewParams.topMargin = theme.dp(16);
|
||
page.addView(preview, previewParams);
|
||
hint = theme.text(host, "", 13f, theme.muted, false);
|
||
hint.setPadding(0, theme.dp(8), 0, theme.dp(8));
|
||
page.addView(hint);
|
||
|
||
TextView choose = theme.button(host, "Выбрать картинку", false);
|
||
choose.setOnClickListener(v -> host.pickPicture());
|
||
page.addView(choose, new LinearLayout.LayoutParams(-1, -2));
|
||
|
||
name = field(theme, "Название", 30, false);
|
||
description = field(theme, "Описание (необязательно)", 200, true);
|
||
page.addView(name, fieldParams(theme));
|
||
page.addView(description, fieldParams(theme));
|
||
|
||
send = theme.button(host, "Опубликовать", true);
|
||
send.setOnClickListener(v -> publish());
|
||
LinearLayout.LayoutParams sendParams = new LinearLayout.LayoutParams(-1, -2);
|
||
sendParams.topMargin = theme.dp(20);
|
||
page.addView(send, sendParams);
|
||
kind(true);
|
||
}
|
||
|
||
View view() {
|
||
return scroll;
|
||
}
|
||
|
||
private EditText field(Theme theme, String label, int limit, boolean multiline) {
|
||
EditText field = new EditText(host);
|
||
field.setHint(label);
|
||
field.setHintTextColor(theme.muted);
|
||
field.setTextColor(theme.onSurface);
|
||
field.setFilters(new InputFilter[] {new InputFilter.LengthFilter(limit)});
|
||
field.setBackground(theme.round(theme.surfaceHigh, 14));
|
||
field.setPadding(theme.dp(16), theme.dp(12), theme.dp(16), theme.dp(12));
|
||
if (multiline) {
|
||
field.setMinLines(2);
|
||
field.setGravity(android.view.Gravity.TOP);
|
||
} else {
|
||
field.setSingleLine(true);
|
||
}
|
||
return field;
|
||
}
|
||
|
||
private LinearLayout.LayoutParams fieldParams(Theme theme) {
|
||
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(-1, -2);
|
||
params.topMargin = theme.dp(12);
|
||
return params;
|
||
}
|
||
|
||
private void kind(boolean isFrame) {
|
||
frame = isFrame;
|
||
Theme theme = host.theme();
|
||
paint(frameChip, isFrame, theme);
|
||
paint(bubbleChip, !isFrame, theme);
|
||
hint.setText(isFrame ? "Рамка: фото само вырежется в кольцо вокруг аватарки. Лучше всего подходит картинка с прозрачной серединой."
|
||
: "Пузырь: картинка станет фоном сообщений. Подойдёт любая, до 512 пикселей по длинной стороне.");
|
||
if (source != null) render();
|
||
else preview.show(null, frame);
|
||
}
|
||
|
||
private void paint(TextView chip, boolean selected, Theme theme) {
|
||
chip.setTextColor(selected ? theme.onAccent : theme.onSurface);
|
||
chip.setTypeface(selected ? android.graphics.Typeface.DEFAULT_BOLD : android.graphics.Typeface.DEFAULT);
|
||
chip.setBackground(theme.round(selected ? theme.accent : theme.surfaceHigh, 20));
|
||
}
|
||
|
||
/** The picture the person chose: it is made ready on a background thread and shown on the preview. */
|
||
void picked(final Uri uri) {
|
||
Work.run(() -> {
|
||
try {
|
||
Bitmap bitmap = Pictures.decode(host.getContentResolver(), uri, 1024);
|
||
if (bitmap == null) throw new Api.Failure("Это не картинка");
|
||
return bitmap;
|
||
} catch (IOException | RuntimeException | OutOfMemoryError unreadable) {
|
||
throw new Api.Failure("Не удалось открыть эту картинку");
|
||
}
|
||
}, new Work.Done<Bitmap>() {
|
||
@Override public void ok(Bitmap bitmap) {
|
||
source = bitmap;
|
||
render();
|
||
}
|
||
|
||
@Override public void failed(String message) {
|
||
host.toast(message);
|
||
}
|
||
});
|
||
}
|
||
|
||
private void render() {
|
||
byte[] made = frame ? Pictures.frame(source) : Pictures.bubble(source);
|
||
png = made;
|
||
if (made == null) {
|
||
host.toast("Эту картинку нельзя использовать");
|
||
return;
|
||
}
|
||
Bitmap shown = BitmapFactory.decodeByteArray(made, 0, made.length);
|
||
String id = "local" + (previews++);
|
||
Images.put(id, shown);
|
||
preview.show(id, frame);
|
||
}
|
||
|
||
private void publish() {
|
||
if (png == null) {
|
||
host.toast("Сначала выберите картинку");
|
||
return;
|
||
}
|
||
if (name.getText().toString().trim().length() == 0) {
|
||
host.toast("Назовите её");
|
||
return;
|
||
}
|
||
final byte[] data = png;
|
||
final boolean isFrame = frame;
|
||
final String title = name.getText().toString().trim();
|
||
final String about = description.getText().toString().trim();
|
||
send.setEnabled(false);
|
||
host.proven(() -> Work.run(() -> {
|
||
JSONObject request = host.body();
|
||
try {
|
||
request.put("kind", isFrame ? "frame" : "bubble").put("name", title).put("description", about)
|
||
.put("png", android.util.Base64.encodeToString(data, android.util.Base64.NO_WRAP));
|
||
} catch (org.json.JSONException impossible) {
|
||
throw new IllegalStateException(impossible);
|
||
}
|
||
return Api.post("publish", request);
|
||
}, new Work.Done<JSONObject>() {
|
||
@Override public void ok(JSONObject reply) {
|
||
send.setEnabled(true);
|
||
String status = reply.optString("status", "new");
|
||
host.toast("approved".equals(status) ? "Уже принято: смотрите в каталоге"
|
||
: "Отправлено на проверку. Статус смотрите в профиле: «Проверка»");
|
||
host.showTab(3);
|
||
}
|
||
|
||
@Override public void failed(String message) {
|
||
send.setEnabled(true);
|
||
host.toast(message);
|
||
}
|
||
}));
|
||
send.postDelayed(() -> send.setEnabled(true), 4000L);
|
||
}
|
||
}
|