From 061667c599b097e697ab837f649339b8fb062ba2 Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Fri, 5 Oct 2018 15:19:11 +0300 Subject: [PATCH] add functionality to send shouts --- .../main/shoutbox/SendShoutTask.java | 50 +++++++++++++++++ .../main/shoutbox/ShoutAdapter.java | 16 +++--- .../main/shoutbox/ShoutboxFragment.java | 55 +++++++++++++++---- .../main/shoutbox/ShoutboxTask.java | 19 +++++-- .../java/gr/thmmy/mthmmy/model/Shoutbox.java | 42 ++++++++++++++ 5 files changed, 157 insertions(+), 25 deletions(-) create mode 100644 app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/SendShoutTask.java create mode 100644 app/src/main/java/gr/thmmy/mthmmy/model/Shoutbox.java diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/SendShoutTask.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/SendShoutTask.java new file mode 100644 index 00000000..bfe9350e --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/SendShoutTask.java @@ -0,0 +1,50 @@ +package gr.thmmy.mthmmy.activities.main.shoutbox; + +import org.jsoup.nodes.Document; + +import java.io.IOException; + +import gr.thmmy.mthmmy.utils.NetworkResultCodes; +import gr.thmmy.mthmmy.utils.NetworkTask; +import okhttp3.MultipartBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; + +public class SendShoutTask extends NetworkTask { + + public SendShoutTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener onNetworkTaskFinishedListener) { + super(onTaskStartedListener, onNetworkTaskFinishedListener); + } + + @Override + protected Response sendRequest(OkHttpClient client, String... input) throws IOException { + MultipartBody.Builder postBodyBuilder = new MultipartBody.Builder() + .setType(MultipartBody.FORM) + .addFormDataPart("sc", input[2]) + .addFormDataPart("tp-shout", input[1]) + .addFormDataPart("tp-shout-name", input[3]) + .addFormDataPart("shout_send", input[4]) + .addFormDataPart("tp-shout-url", input[5]); + + Request voteRequest = new Request.Builder() + .url(input[0]) + .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36") + .post(postBodyBuilder.build()) + .build(); + client.newCall(voteRequest).execute(); + return client.newCall(voteRequest).execute(); + } + + + + @Override + protected Void performTask(Document document, Response response) { + return null; + } + + @Override + protected int getResultCode(Response response, Void data) { + return NetworkResultCodes.SUCCESSFUL; + } +} diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutAdapter.java index dffa7716..23e8ccb2 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutAdapter.java @@ -14,8 +14,6 @@ import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.TextView; -import java.util.ArrayList; - import androidx.annotation.NonNull; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.activities.board.BoardActivity; @@ -35,13 +33,17 @@ import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_URL; public class ShoutAdapter extends CustomRecyclerView.Adapter { private Context context; - private ArrayList shouts; + private Shout[] shouts; - public ShoutAdapter(Context context, ArrayList shouts) { + public ShoutAdapter(Context context, Shout[] shouts) { this.context = context; this.shouts = shouts; } + public void setShouts(Shout[] shouts) { + this.shouts = shouts; + } + @NonNull @Override public ShoutViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { @@ -52,12 +54,12 @@ public class ShoutAdapter extends CustomRecyclerView.Adapter { Intent intent = new Intent(context, ProfileActivity.class); Bundle extras = new Bundle(); - extras.putString(BUNDLE_PROFILE_URL, shouts.get(holder.getAdapterPosition()).getShouterProfileURL()); + extras.putString(BUNDLE_PROFILE_URL, shouts[holder.getAdapterPosition()].getShouterProfileURL()); extras.putString(BUNDLE_PROFILE_THUMBNAIL_URL, ""); extras.putString(BUNDLE_PROFILE_USERNAME, ""); intent.putExtras(extras); @@ -73,7 +75,7 @@ public class ShoutAdapter extends CustomRecyclerView.Adapter shouts; private EmojiKeyboard emojiKeyboard; private EditorView editorView; + private Shoutbox shoutbox; public static ShoutboxFragment newInstance(int sectionNumber) { ShoutboxFragment fragment = new ShoutboxFragment(); @@ -47,13 +46,34 @@ public class ShoutboxFragment extends BaseFragment implements EmojiKeyboard.Emoj progressBar.setVisibility(View.VISIBLE); } - private void onShoutboxTaskFinished(int resultCode, ArrayList shouts) { + private void onSendShoutTaskStarted() { + progressBar.setVisibility(View.VISIBLE); + } + + private void onSendShoutTaskFinished(int resultCode, Void ignored) { + editorView.setAlpha(1f); + editorView.setEnabled(true); + progressBar.setVisibility(View.INVISIBLE); + if (resultCode == NetworkResultCodes.SUCCESSFUL) { + editorView.getEditText().getText().clear(); + shoutboxTask = new ShoutboxTask(ShoutboxFragment.this::onShoutboxTaskSarted, ShoutboxFragment.this::onShoutboxTaskFinished); + shoutboxTask.execute("https://www.thmmy.gr/smf/index.php?action=forum"); + } else if (resultCode == NetworkResultCodes.NETWORK_ERROR) { + Toast.makeText(getContext(), "NetworkError", Toast.LENGTH_SHORT).show(); + } + } + + private void onShoutboxTaskFinished(int resultCode, Shoutbox shoutbox) { progressBar.setVisibility(View.INVISIBLE); swipeRefreshLayout.setRefreshing(false); if (resultCode == NetworkResultCodes.SUCCESSFUL) { - this.shouts.clear(); - this.shouts.addAll(shouts); + shoutAdapter.setShouts(shoutbox.getShouts()); shoutAdapter.notifyDataSetChanged(); + this.shoutbox = shoutbox; + } else if (resultCode == NetworkResultCodes.NETWORK_ERROR) { + Toast.makeText(getContext(), "NetworkError", Toast.LENGTH_SHORT).show(); + } else { + Toast.makeText(getContext(), "Failed to retrieve shoutbox, please contact mthmmy developer team", Toast.LENGTH_LONG).show(); } } @@ -64,27 +84,38 @@ public class ShoutboxFragment extends BaseFragment implements EmojiKeyboard.Emoj progressBar = rootView.findViewById(R.id.progressBar); CustomRecyclerView recyclerView = rootView.findViewById(R.id.shoutbox_recyclerview); - shouts = new ArrayList<>(); - shoutAdapter = new ShoutAdapter(getContext(), shouts); + shoutAdapter = new ShoutAdapter(getContext(), new Shout[0]); recyclerView.setAdapter(shoutAdapter); LinearLayoutManager layoutManager = new LinearLayoutManager(getContext()); layoutManager.setReverseLayout(true); recyclerView.setLayoutManager(layoutManager); shoutboxTask = new ShoutboxTask(this::onShoutboxTaskSarted, this::onShoutboxTaskFinished); - shoutboxTask.execute("https://www.thmmy.gr/smf/index.php?"); + shoutboxTask.execute("https://www.thmmy.gr/smf/index.php?action=forum"); swipeRefreshLayout = rootView.findViewById(R.id.swiperefresh); swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.primary); swipeRefreshLayout.setOnRefreshListener(() -> { shoutboxTask = new ShoutboxTask(ShoutboxFragment.this::onShoutboxTaskSarted, ShoutboxFragment.this::onShoutboxTaskFinished); - shoutboxTask.execute("https://www.thmmy.gr/smf/index.php?"); + shoutboxTask.execute("https://www.thmmy.gr/smf/index.php?action=forum"); }); emojiKeyboard = rootView.findViewById(R.id.emoji_keyboard); editorView = rootView.findViewById(R.id.edior_view); editorView.setEmojiKeyboardOwner(this); - InputConnection ic = editorView.onCreateInputConnection(new EditorInfo()); + InputConnection ic = editorView.getInputConnection(); setEmojiKeyboardInputConnection(ic); + editorView.setOnSubmitListener(view -> { + if (editorView.getText().toString().isEmpty()) { + editorView.setError("Required"); + return; + } + editorView.setAlpha(0.5f); + editorView.setEnabled(false); + setEmojiKeyboardVisible(false); + new SendShoutTask(this::onSendShoutTaskStarted, this::onSendShoutTaskFinished) + .execute(shoutbox.getSendShoutUrl(), editorView.getText().toString(), shoutbox.getSc(), + shoutbox.getShoutName(), shoutbox.getShoutSend(), shoutbox.getShoutUrl()); + }); return rootView; } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxTask.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxTask.java index c88b2960..d25bf89e 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxTask.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxTask.java @@ -6,21 +6,21 @@ import org.jsoup.nodes.Element; import java.util.ArrayList; import gr.thmmy.mthmmy.model.Shout; +import gr.thmmy.mthmmy.model.Shoutbox; import gr.thmmy.mthmmy.utils.NetworkResultCodes; import gr.thmmy.mthmmy.utils.parsing.NewParseTask; import gr.thmmy.mthmmy.utils.parsing.ParseException; import gr.thmmy.mthmmy.utils.parsing.ParseHelpers; import okhttp3.Response; -import timber.log.Timber; -public class ShoutboxTask extends NewParseTask> { +public class ShoutboxTask extends NewParseTask { - public ShoutboxTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener> onParseTaskFinishedListener) { + public ShoutboxTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener onParseTaskFinishedListener) { super(onTaskStartedListener, onParseTaskFinishedListener); } @Override - protected ArrayList parse(Document document, Response response) throws ParseException { + protected Shoutbox parse(Document document, Response response) throws ParseException { // shout container: document.select("div[class=smalltext]" && div.text().contains("Τελευταίες 75 φωνές:") η στα αγγλικα Element shoutboxContainer = document.select("div[style=width: 99%; height: 600px; overflow: auto;]").first(); ArrayList shouts = new ArrayList<>(); @@ -38,11 +38,18 @@ public class ShoutboxTask extends NewParseTask> { ParseHelpers.youtubeEmbeddedFix(content); shouts.add(new Shout(profileName, profileUrl, dateString, shoutContent)); } - return shouts; + + Element shoutboxForm = document.select("form[name=tp-shoutbox]").first(); + String formUrl = shoutboxForm.attr("action"); + String sc = shoutboxForm.select("input[name=sc]").first().attr("value"); + String shoutName = shoutboxForm.select("input[name=tp-shout-name]").first().attr("value"); + String shoutSend = shoutboxForm.select("input[name=shout_send]").first().attr("value"); + String shoutUrl = shoutboxForm.select("input[name=tp-shout-url]").first().attr("value"); + return new Shoutbox(shouts.toArray(new Shout[0]), sc, formUrl, shoutName, shoutSend, shoutUrl); } @Override - protected int getResultCode(Response response, ArrayList data) { + protected int getResultCode(Response response, Shoutbox data) { return NetworkResultCodes.SUCCESSFUL; } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/model/Shoutbox.java b/app/src/main/java/gr/thmmy/mthmmy/model/Shoutbox.java new file mode 100644 index 00000000..73f0cc3e --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/model/Shoutbox.java @@ -0,0 +1,42 @@ +package gr.thmmy.mthmmy.model; + +import androidx.annotation.NonNull; +import timber.log.Timber; + +public class Shoutbox { + private Shout[] shouts; + private String sc, sendShoutUrl, shoutName, shoutSend, shoutUrl; + + public Shoutbox(Shout[] shouts, String sc, String sendShoutUrl, String shoutName, String shoutSend, String shoutUrl) { + this.shouts = shouts; + this.sc = sc; + this.sendShoutUrl = sendShoutUrl; + this.shoutName = shoutName; + this.shoutSend = shoutSend; + this.shoutUrl = shoutUrl; + } + + public Shout[] getShouts() { + return shouts; + } + + public String getSc() { + return sc; + } + + public String getSendShoutUrl() { + return sendShoutUrl; + } + + public String getShoutName() { + return shoutName; + } + + public String getShoutSend() { + return shoutSend; + } + + public String getShoutUrl() { + return shoutUrl; + } +}