Browse Source

add functionality to send shouts

pull/61/merge
Thodoris1999 6 years ago
parent
commit
061667c599
  1. 50
      app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/SendShoutTask.java
  2. 16
      app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutAdapter.java
  3. 55
      app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxFragment.java
  4. 19
      app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxTask.java
  5. 42
      app/src/main/java/gr/thmmy/mthmmy/model/Shoutbox.java

50
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<Void> {
public SendShoutTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener<Void> 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;
}
}

16
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.webkit.WebViewClient;
import android.widget.TextView; import android.widget.TextView;
import java.util.ArrayList;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.activities.board.BoardActivity; 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<ShoutAdapter.ShoutViewHolder> { public class ShoutAdapter extends CustomRecyclerView.Adapter<ShoutAdapter.ShoutViewHolder> {
private Context context; private Context context;
private ArrayList<Shout> shouts; private Shout[] shouts;
public ShoutAdapter(Context context, ArrayList<Shout> shouts) { public ShoutAdapter(Context context, Shout[] shouts) {
this.context = context; this.context = context;
this.shouts = shouts; this.shouts = shouts;
} }
public void setShouts(Shout[] shouts) {
this.shouts = shouts;
}
@NonNull @NonNull
@Override @Override
public ShoutViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { public ShoutViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
@ -52,12 +54,12 @@ public class ShoutAdapter extends CustomRecyclerView.Adapter<ShoutAdapter.ShoutV
@Override @Override
public void onBindViewHolder(@NonNull ShoutViewHolder holder, int position) { public void onBindViewHolder(@NonNull ShoutViewHolder holder, int position) {
Shout currentShout = shouts.get(position); Shout currentShout = shouts[position];
holder.author.setText(currentShout.getShouter()); holder.author.setText(currentShout.getShouter());
holder.author.setOnClickListener(view -> { holder.author.setOnClickListener(view -> {
Intent intent = new Intent(context, ProfileActivity.class); Intent intent = new Intent(context, ProfileActivity.class);
Bundle extras = new Bundle(); 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_THUMBNAIL_URL, "");
extras.putString(BUNDLE_PROFILE_USERNAME, ""); extras.putString(BUNDLE_PROFILE_USERNAME, "");
intent.putExtras(extras); intent.putExtras(extras);
@ -73,7 +75,7 @@ public class ShoutAdapter extends CustomRecyclerView.Adapter<ShoutAdapter.ShoutV
@Override @Override
public int getItemCount() { public int getItemCount() {
return shouts.size(); return shouts.length;
} }
static class ShoutViewHolder extends CustomRecyclerView.ViewHolder { static class ShoutViewHolder extends CustomRecyclerView.ViewHolder {

55
app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxFragment.java

@ -4,10 +4,8 @@ import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnection;
import android.widget.Toast;
import java.util.ArrayList;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -18,6 +16,7 @@ import gr.thmmy.mthmmy.base.BaseFragment;
import gr.thmmy.mthmmy.editorview.EditorView; import gr.thmmy.mthmmy.editorview.EditorView;
import gr.thmmy.mthmmy.editorview.EmojiKeyboard; import gr.thmmy.mthmmy.editorview.EmojiKeyboard;
import gr.thmmy.mthmmy.model.Shout; import gr.thmmy.mthmmy.model.Shout;
import gr.thmmy.mthmmy.model.Shoutbox;
import gr.thmmy.mthmmy.utils.CustomRecyclerView; import gr.thmmy.mthmmy.utils.CustomRecyclerView;
import gr.thmmy.mthmmy.utils.NetworkResultCodes; import gr.thmmy.mthmmy.utils.NetworkResultCodes;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
@ -30,9 +29,9 @@ public class ShoutboxFragment extends BaseFragment implements EmojiKeyboard.Emoj
private ShoutboxTask shoutboxTask; private ShoutboxTask shoutboxTask;
private ShoutAdapter shoutAdapter; private ShoutAdapter shoutAdapter;
private SwipeRefreshLayout swipeRefreshLayout; private SwipeRefreshLayout swipeRefreshLayout;
private ArrayList<Shout> shouts;
private EmojiKeyboard emojiKeyboard; private EmojiKeyboard emojiKeyboard;
private EditorView editorView; private EditorView editorView;
private Shoutbox shoutbox;
public static ShoutboxFragment newInstance(int sectionNumber) { public static ShoutboxFragment newInstance(int sectionNumber) {
ShoutboxFragment fragment = new ShoutboxFragment(); ShoutboxFragment fragment = new ShoutboxFragment();
@ -47,13 +46,34 @@ public class ShoutboxFragment extends BaseFragment implements EmojiKeyboard.Emoj
progressBar.setVisibility(View.VISIBLE); progressBar.setVisibility(View.VISIBLE);
} }
private void onShoutboxTaskFinished(int resultCode, ArrayList<Shout> 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); progressBar.setVisibility(View.INVISIBLE);
swipeRefreshLayout.setRefreshing(false); swipeRefreshLayout.setRefreshing(false);
if (resultCode == NetworkResultCodes.SUCCESSFUL) { if (resultCode == NetworkResultCodes.SUCCESSFUL) {
this.shouts.clear(); shoutAdapter.setShouts(shoutbox.getShouts());
this.shouts.addAll(shouts);
shoutAdapter.notifyDataSetChanged(); 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); progressBar = rootView.findViewById(R.id.progressBar);
CustomRecyclerView recyclerView = rootView.findViewById(R.id.shoutbox_recyclerview); CustomRecyclerView recyclerView = rootView.findViewById(R.id.shoutbox_recyclerview);
shouts = new ArrayList<>(); shoutAdapter = new ShoutAdapter(getContext(), new Shout[0]);
shoutAdapter = new ShoutAdapter(getContext(), shouts);
recyclerView.setAdapter(shoutAdapter); recyclerView.setAdapter(shoutAdapter);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext()); LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
layoutManager.setReverseLayout(true); layoutManager.setReverseLayout(true);
recyclerView.setLayoutManager(layoutManager); recyclerView.setLayoutManager(layoutManager);
shoutboxTask = new ShoutboxTask(this::onShoutboxTaskSarted, this::onShoutboxTaskFinished); 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 = rootView.findViewById(R.id.swiperefresh);
swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.primary); swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.primary);
swipeRefreshLayout.setOnRefreshListener(() -> { swipeRefreshLayout.setOnRefreshListener(() -> {
shoutboxTask = new ShoutboxTask(ShoutboxFragment.this::onShoutboxTaskSarted, ShoutboxFragment.this::onShoutboxTaskFinished); 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); emojiKeyboard = rootView.findViewById(R.id.emoji_keyboard);
editorView = rootView.findViewById(R.id.edior_view); editorView = rootView.findViewById(R.id.edior_view);
editorView.setEmojiKeyboardOwner(this); editorView.setEmojiKeyboardOwner(this);
InputConnection ic = editorView.onCreateInputConnection(new EditorInfo()); InputConnection ic = editorView.getInputConnection();
setEmojiKeyboardInputConnection(ic); 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; return rootView;
} }

19
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 java.util.ArrayList;
import gr.thmmy.mthmmy.model.Shout; import gr.thmmy.mthmmy.model.Shout;
import gr.thmmy.mthmmy.model.Shoutbox;
import gr.thmmy.mthmmy.utils.NetworkResultCodes; import gr.thmmy.mthmmy.utils.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.parsing.NewParseTask; import gr.thmmy.mthmmy.utils.parsing.NewParseTask;
import gr.thmmy.mthmmy.utils.parsing.ParseException; import gr.thmmy.mthmmy.utils.parsing.ParseException;
import gr.thmmy.mthmmy.utils.parsing.ParseHelpers; import gr.thmmy.mthmmy.utils.parsing.ParseHelpers;
import okhttp3.Response; import okhttp3.Response;
import timber.log.Timber;
public class ShoutboxTask extends NewParseTask<ArrayList<Shout>> { public class ShoutboxTask extends NewParseTask<Shoutbox> {
public ShoutboxTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener<ArrayList<Shout>> onParseTaskFinishedListener) { public ShoutboxTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener<Shoutbox> onParseTaskFinishedListener) {
super(onTaskStartedListener, onParseTaskFinishedListener); super(onTaskStartedListener, onParseTaskFinishedListener);
} }
@Override @Override
protected ArrayList<Shout> 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 φωνές:") η στα αγγλικα // shout container: document.select("div[class=smalltext]" && div.text().contains("Τελευταίες 75 φωνές:") η στα αγγλικα
Element shoutboxContainer = document.select("div[style=width: 99%; height: 600px; overflow: auto;]").first(); Element shoutboxContainer = document.select("div[style=width: 99%; height: 600px; overflow: auto;]").first();
ArrayList<Shout> shouts = new ArrayList<>(); ArrayList<Shout> shouts = new ArrayList<>();
@ -38,11 +38,18 @@ public class ShoutboxTask extends NewParseTask<ArrayList<Shout>> {
ParseHelpers.youtubeEmbeddedFix(content); ParseHelpers.youtubeEmbeddedFix(content);
shouts.add(new Shout(profileName, profileUrl, dateString, shoutContent)); 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 @Override
protected int getResultCode(Response response, ArrayList<Shout> data) { protected int getResultCode(Response response, Shoutbox data) {
return NetworkResultCodes.SUCCESSFUL; return NetworkResultCodes.SUCCESSFUL;
} }
} }

42
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;
}
}
Loading…
Cancel
Save