From 3ae5ca276bf014e569fed3e84253c24897aeeaf9 Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Tue, 4 Sep 2018 12:21:09 +0300 Subject: [PATCH] complete new topic task --- .../activities/CreateContentActivity.java | 53 ++++++++++- .../thmmy/mthmmy/activities/NewTopicTask.java | 95 +++++++++++++++++++ .../activities/board/BoardActivity.java | 14 ++- .../res/layout/activity_create_content.xml | 87 ++++++++++++----- app/src/main/res/values/strings.xml | 3 + 5 files changed, 224 insertions(+), 28 deletions(-) create mode 100644 app/src/main/java/gr/thmmy/mthmmy/activities/NewTopicTask.java diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/CreateContentActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/CreateContentActivity.java index bcc0a334..48610cb7 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/CreateContentActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/CreateContentActivity.java @@ -1,31 +1,57 @@ package gr.thmmy.mthmmy.activities; +import android.content.Intent; import android.os.Bundle; +import android.support.design.widget.TextInputLayout; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.inputmethod.InputConnection; +import android.widget.TextView; +import android.widget.Toast; +import android.widget.Toolbar; import gr.thmmy.mthmmy.R; +import gr.thmmy.mthmmy.base.BaseApplication; import gr.thmmy.mthmmy.editorview.EditorView; import gr.thmmy.mthmmy.editorview.EmojiKeyboard; +import me.zhanghai.android.materialprogressbar.MaterialProgressBar; +import timber.log.Timber; -public class CreateContentActivity extends AppCompatActivity implements EmojiKeyboard.EmojiKeyboardOwner { +public class CreateContentActivity extends AppCompatActivity implements EmojiKeyboard.EmojiKeyboardOwner, + NewTopicTask.NewTopicTaskCallbacks { - EditorView contentEditor; - EmojiKeyboard emojiKeyboard; + public final static String EXTRA_NEW_TOPIC_URL = "new-topic-extra"; + + private EditorView contentEditor; + private EmojiKeyboard emojiKeyboard; + private TextInputLayout subjectInput; + private MaterialProgressBar progressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_create_content); + TextView toolbarTitle = findViewById(R.id.toolbar_title); + toolbarTitle.setText(R.string.new_topic_toolbar); + + progressBar = findViewById(R.id.progressBar); + + Intent callingIntent = getIntent(); + String newTopicUrl = callingIntent.getStringExtra(EXTRA_NEW_TOPIC_URL); + emojiKeyboard = findViewById(R.id.emoji_keyboard); + subjectInput = findViewById(R.id.subject_input); + contentEditor = findViewById(R.id.main_content_editorview); setEmojiKeyboardInputConnection(contentEditor.getInputConnection()); contentEditor.setEmojiKeyboardOwner(this); contentEditor.setOnSubmitListener(v -> { - + if (newTopicUrl != null) { + new NewTopicTask(this).execute(newTopicUrl, subjectInput.getEditText().getText().toString(), + contentEditor.getText().toString()); + } }); } @@ -53,4 +79,23 @@ public class CreateContentActivity extends AppCompatActivity implements EmojiKey super.onBackPressed(); } } + + @Override + public void onNewTopicTaskStarted() { + Timber.i("New topic creation started"); + progressBar.setVisibility(View.VISIBLE); + } + + @Override + public void onNewTopicTaskFinished(boolean success) { + progressBar.setVisibility(View.INVISIBLE); + if (success) { + Timber.i("New topic created successfully"); + finish(); + } else { + Timber.w("New topic creation failed"); + Toast.makeText(getBaseContext(), "Failed to create new topic!", Toast.LENGTH_LONG).show(); + finish(); + } + } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/NewTopicTask.java b/app/src/main/java/gr/thmmy/mthmmy/activities/NewTopicTask.java new file mode 100644 index 00000000..322cac43 --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/NewTopicTask.java @@ -0,0 +1,95 @@ +package gr.thmmy.mthmmy.activities; + +import android.os.AsyncTask; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; + +import java.io.IOException; + +import gr.thmmy.mthmmy.base.BaseApplication; +import okhttp3.MultipartBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import timber.log.Timber; + +import static gr.thmmy.mthmmy.activities.topic.Posting.replyStatus; + +public class NewTopicTask extends AsyncTask { + + private NewTopicTaskCallbacks listener; + + public NewTopicTask(NewTopicTaskCallbacks listener){ + this.listener = listener; + } + + @Override + protected void onPreExecute() { + listener.onNewTopicTaskStarted(); + } + + @Override + protected Boolean doInBackground(String... strings) { + Request request = new Request.Builder() + .url(strings[0] + ";wap2") + .build(); + + OkHttpClient client = BaseApplication.getInstance().getClient(); + + Document document; + String seqnum, sc, topic, createTopicUrl; + try { + Response response = client.newCall(request).execute(); + document = Jsoup.parse(response.body().string()); + + seqnum = document.select("input[name=seqnum]").first().attr("value"); + sc = document.select("input[name=sc]").first().attr("value"); + topic = document.select("input[name=topic]").first().attr("value"); + createTopicUrl = document.select("form").first().attr("action"); + + RequestBody postBody = new MultipartBody.Builder() + .setType(MultipartBody.FORM) + .addFormDataPart("message", strings[2]) + .addFormDataPart("seqnum", seqnum) + .addFormDataPart("sc", sc) + .addFormDataPart("subject", strings[1]) + .addFormDataPart("topic", topic) + .build(); + + Request post = new Request.Builder() + .url(createTopicUrl) + .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(postBody) + .build(); + + try { + client.newCall(post).execute(); + Response response2 = client.newCall(post).execute(); + switch (replyStatus(response2)) { + case SUCCESSFUL: + BaseApplication.getInstance().logFirebaseAnalyticsEvent("new_topic_creation", null); + return true; + default: + Timber.e("Malformed post. Request string: %s", post.toString()); + return false; + } + } catch (IOException e) { + return false; + } + } catch (IOException e) { + return false; + } + } + + @Override + protected void onPostExecute(Boolean success) { + listener.onNewTopicTaskFinished(success); + } + + public interface NewTopicTaskCallbacks { + void onNewTopicTaskStarted(); + void onNewTopicTaskFinished(boolean success); + } +} diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java index 04a03503..9b905fb1 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java @@ -55,6 +55,7 @@ public class BoardActivity extends BaseActivity implements BoardAdapter.OnLoadMo private String boardUrl; private String boardTitle; private String parsedTitle; + private String newTopicUrl; private int numberOfPages = -1; private int pagesLoaded = 0; @@ -104,7 +105,11 @@ public class BoardActivity extends BaseActivity implements BoardAdapter.OnLoadMo newTopicFAB.setOnClickListener(view -> { if (sessionManager.isLoggedIn()) { //TODO create topic - startActivity(new Intent(this, CreateContentActivity.class)); + if (newTopicUrl != null) { + Intent intent = new Intent(this, CreateContentActivity.class); + intent.putExtra(CreateContentActivity.EXTRA_NEW_TOPIC_URL, newTopicUrl); + startActivity(intent); + } } else { new AlertDialog.Builder(BoardActivity.this) .setMessage("You need to be logged in to create a new topic!") @@ -209,6 +214,13 @@ public class BoardActivity extends BaseActivity implements BoardAdapter.OnLoadMo //It just means this board has only one page of topics. } } + + //Finds the url needed to create a new topic + Element newTopicButton = boardPage.select("a:has(img[alt=Start new topic])").first(); + if (newTopicButton == null) + newTopicButton = boardPage.select("a:has(img[alt=Νέο θέμα])").first(); + if (newTopicButton != null) newTopicUrl = newTopicButton.attr("href"); + { //Finds sub boards Elements subBoardRows = boardPage.select("div.tborder>table>tbody>tr"); if (subBoardRows != null && !subBoardRows.isEmpty()) { diff --git a/app/src/main/res/layout/activity_create_content.xml b/app/src/main/res/layout/activity_create_content.xml index 35bb9143..220de6e4 100644 --- a/app/src/main/res/layout/activity_create_content.xml +++ b/app/src/main/res/layout/activity_create_content.xml @@ -1,33 +1,74 @@ - - - - - + android:layout_width="match_parent"> - + android:layout_height="match_parent"> + + + + + + + + + + + + + + + + + + - + android:layout_height="@dimen/progress_bar_height" + android:indeterminate="true" + android:visibility="invisible" + app:layout_anchor="@id/appbar" + app:layout_anchorGravity="bottom|center" + app:mpb_indeterminateTint="@color/accent" + app:mpb_progressStyle="horizontal" /> - \ No newline at end of file + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4b8013a4..a5308050 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -180,4 +180,7 @@ Link URL Link text Required + + + New topic