Browse Source

Merge pull request #51 from ThmmyNoLife/new_topic

New topic
pull/55/head
oogee 6 years ago
committed by GitHub
parent
commit
a7127ca04f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      app/src/main/AndroidManifest.xml
  2. 60
      app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java
  3. 115
      app/src/main/java/gr/thmmy/mthmmy/activities/create_content/CreateContentActivity.java
  4. 100
      app/src/main/java/gr/thmmy/mthmmy/activities/create_content/NewTopicTask.java
  5. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/ReplyTask.java
  6. 2
      app/src/main/java/gr/thmmy/mthmmy/editorview/EditorView.java
  7. 78
      app/src/main/res/layout/activity_create_content.xml
  8. 4
      app/src/main/res/values/strings.xml

7
app/src/main/AndroidManifest.xml

@ -20,6 +20,7 @@
<meta-data
android:name="firebase_crashlytics_collection_enabled"
android:value="false" />
<activity
android:name=".activities.main.MainActivity"
android:configChanges="orientation|screenSize"
@ -147,6 +148,12 @@
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<activity
android:name=".activities.create_content.CreateContentActivity"
android:configChanges="orientation|screenSize"
android:parentActivityName=".activities.main.MainActivity"
android:theme="@style/AppTheme.NoActionBar" />
</application>
</manifest>

60
app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java

@ -1,9 +1,11 @@
package gr.thmmy.mthmmy.activities.board;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
@ -20,6 +22,8 @@ import java.util.ArrayList;
import java.util.Objects;
import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.activities.create_content.CreateContentActivity;
import gr.thmmy.mthmmy.activities.LoginActivity;
import gr.thmmy.mthmmy.base.BaseActivity;
import gr.thmmy.mthmmy.model.Board;
import gr.thmmy.mthmmy.model.Bookmark;
@ -51,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;
@ -95,37 +100,31 @@ public class BoardActivity extends BaseActivity implements BoardAdapter.OnLoadMo
progressBar = findViewById(R.id.progressBar);
newTopicFAB = findViewById(R.id.board_fab);
newTopicFAB.setEnabled(false);
newTopicFAB.hide();
/*if (!sessionManager.isLoggedIn()) newTopicFAB.hide();
if (!sessionManager.isLoggedIn()) newTopicFAB.hide();
else {
newTopicFAB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (sessionManager.isLoggedIn()) {
//TODO create topic
} else {
new AlertDialog.Builder(BoardActivity.this)
.setMessage("You need to be logged in to create a new topic!")
.setPositiveButton("Login", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(BoardActivity.this, LoginActivity.class);
startActivity(intent);
finish();
overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.show();
newTopicFAB.setOnClickListener(view -> {
if (sessionManager.isLoggedIn()) {
//TODO create topic
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!")
.setPositiveButton("Login", (dialogInterface, i) -> {
Intent intent = new Intent(BoardActivity.this, LoginActivity.class);
startActivity(intent);
finish();
overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
})
.setNegativeButton("Cancel", (dialogInterface, i) -> {
})
.show();
}
});
}*/
}
boardAdapter = new BoardAdapter(getApplicationContext(), parsedSubBoards, parsedTopics);
RecyclerView mainContent = findViewById(R.id.board_recycler_view);
@ -214,6 +213,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()) {

115
app/src/main/java/gr/thmmy/mthmmy/activities/create_content/CreateContentActivity.java

@ -0,0 +1,115 @@
package gr.thmmy.mthmmy.activities.create_content;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.design.widget.TextInputLayout;
import android.view.View;
import android.view.inputmethod.InputConnection;
import android.widget.Toast;
import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.activities.settings.SettingsActivity;
import gr.thmmy.mthmmy.base.BaseActivity;
import gr.thmmy.mthmmy.editorview.EditorView;
import gr.thmmy.mthmmy.editorview.EmojiKeyboard;
import gr.thmmy.mthmmy.session.SessionManager;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
import timber.log.Timber;
public class CreateContentActivity extends BaseActivity implements EmojiKeyboard.EmojiKeyboardOwner,
NewTopicTask.NewTopicTaskCallbacks {
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);
//Initialize toolbar
toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("Create topic");
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
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) {
boolean includeAppSignature = true;
SessionManager sessionManager = BaseActivity.getSessionManager();
if (sessionManager.isLoggedIn()) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
includeAppSignature = prefs.getBoolean(SettingsActivity.POSTING_APP_SIGNATURE_ENABLE_KEY, true);
}
new NewTopicTask(this, includeAppSignature).execute(newTopicUrl, subjectInput.getEditText().getText().toString(),
contentEditor.getText().toString());
}
});
}
@Override
public void setEmojiKeyboardVisible(boolean visible) {
emojiKeyboard.setVisibility(visible ? View.VISIBLE : View.GONE);
}
@Override
public boolean isEmojiKeyboardVisible() {
return emojiKeyboard.getVisibility() == View.VISIBLE;
}
@Override
public void setEmojiKeyboardInputConnection(InputConnection ic) {
emojiKeyboard.setInputConnection(ic);
}
@Override
public void onBackPressed() {
if (emojiKeyboard.getVisibility() == View.VISIBLE) {
emojiKeyboard.setVisibility(View.GONE);
contentEditor.updateEmojiKeyboardVisibility();
} else {
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();
}
}
}

100
app/src/main/java/gr/thmmy/mthmmy/activities/create_content/NewTopicTask.java

@ -0,0 +1,100 @@
package gr.thmmy.mthmmy.activities.create_content;
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<String, Void, Boolean> {
private NewTopicTaskCallbacks listener;
private boolean includeAppSignature;
public NewTopicTask(NewTopicTaskCallbacks listener, boolean includeAppSignature){
this.listener = listener;
this.includeAppSignature = includeAppSignature;
}
@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");
final String appSignature = "\n[right][size=7pt][i]sent from [url=https://play.google.com/store/apps/" +
"details?id=gr.thmmy.mthmmy]mTHMMY[/url][/i][/size][/right]";
RequestBody postBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("message", strings[2] + (includeAppSignature ? appSignature : ""))
.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);
}
}

2
app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/ReplyTask.java

@ -31,7 +31,7 @@ public class ReplyTask extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... args) {
final String sentFrommTHMMY = includeAppSignature
? "\n[right][size=7pt][i]sent from [url=https://play.google.com/store/apps/details?id=gr.thmmy.mthmmy]mTHMMY[/url][/i] [/size][/right]"
? "\n[right][size=7pt][i]sent from [url=https://play.google.com/store/apps/details?id=gr.thmmy.mthmmy]mTHMMY[/url][/i][/size][/right]"
: "";
RequestBody postBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)

2
app/src/main/java/gr/thmmy/mthmmy/editorview/EditorView.java

@ -205,7 +205,7 @@ public class EditorView extends LinearLayout {
linkText.getEditText().setText(
editText.getText().toString().substring(editText.getSelectionStart(), editText.getSelectionEnd()));
}
new AlertDialog.Builder(context, R.style.AppCompatAlertDialogStyleAccent)
new AlertDialog.Builder(context, R.style.AppTheme_Dark_Dialog)
.setTitle(R.string.dialog_create_link_title)
.setView(dialogBody)
.setPositiveButton(R.string.ok, (dialog, which) -> {

78
app/src/main/res/layout/activity_create_content.xml

@ -0,0 +1,78 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".activities.create_content.CreateContentActivity"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/ToolbarTheme">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:gravity="center"
app:popupTheme="@style/ToolbarTheme">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/subject_input"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_below="@id/appbar"
android:layout_margin="16dp"
android:hint="@string/subject">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="1"
android:maxLines="1"
android:ellipsize="end"
android:inputType="text"/>
</android.support.design.widget.TextInputLayout>
<gr.thmmy.mthmmy.editorview.EditorView
android:id="@+id/main_content_editorview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/subject_input"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
app:hint="topic message"/>
<gr.thmmy.mthmmy.editorview.EmojiKeyboard
android:id="@+id/emoji_keyboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:visibility="gone" />
</RelativeLayout>
<me.zhanghai.android.materialprogressbar.MaterialProgressBar
android:id="@+id/progressBar"
style="@style/Widget.MaterialProgressBar.ProgressBar.Horizontal.NoPadding"
android:layout_width="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" />
</android.support.design.widget.CoordinatorLayout>

4
app/src/main/res/values/strings.xml

@ -180,4 +180,8 @@
<string name="dialog_link_url_hint">Link URL</string>
<string name="dialog_link_text_hint">Link text</string>
<string name="input_field_required">Required</string>
<!-- New topic activity -->
<string name="new_topic_toolbar">New topic</string>
<string name="create_topic">Create topic</string>
</resources>

Loading…
Cancel
Save