Skip to content

Commit

Permalink
Skeleton version of post submission
Browse files Browse the repository at this point in the history
Not tested yet
  • Loading branch information
SimonHalvdansson committed Oct 2, 2023
1 parent fc2fd0c commit 64d1e8a
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,7 @@ public void clickComment() {
Intent intent = new Intent(getContext(), ComposeActivity.class);
intent.putExtra(ComposeActivity.EXTRA_ID, adapter.story.id);
intent.putExtra(ComposeActivity.EXTRA_PARENT_TEXT, adapter.story.title);
intent.putExtra(ComposeActivity.EXTRA_TYPE, ComposeActivity.TYPE_TOP_COMMENT);
startActivity(intent);
}

Expand Down Expand Up @@ -1412,6 +1413,7 @@ public void onClick(DialogInterface dialog, int which) {
replyIntent.putExtra(ComposeActivity.EXTRA_ID, comment.id);
replyIntent.putExtra(ComposeActivity.EXTRA_PARENT_TEXT, comment.text);
replyIntent.putExtra(ComposeActivity.EXTRA_USER, comment.by);
replyIntent.putExtra(ComposeActivity.EXTRA_TYPE, ComposeActivity.TYPE_COMMENT_REPLY);
ctx.startActivity(replyIntent);

}
Expand Down
121 changes: 81 additions & 40 deletions app/src/main/java/com/simon/harmonichackernews/ComposeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,23 @@ public class ComposeActivity extends AppCompatActivity {
public final static String EXTRA_ID = "com.simon.harmonichackernews.EXTRA_ID";
public final static String EXTRA_PARENT_TEXT = "com.simon.harmonichackernews.EXTRA_PARENT_TEXT";
public final static String EXTRA_USER = "com.simon.harmonichackernews.EXTRA_USER";
public final static String EXTRA_TYPE = "com.simon.harmonichackernews.EXTRA_TYPE";

public final static int TYPE_TOP_COMMENT = 0;
public final static int TYPE_COMMENT_REPLY = 1;
public final static int TYPE_POST = 2;

private EditText editText;
private EditText editTextTitle;
private EditText editTextUrl;
private Button submitButton;
private HtmlTextView replyingTextView;
private ScrollView replyingScrollView;
private TextView topCommentTextView;
private int id;
private String parentText;
private String user;
private int type;

private OnBackPressedCallback backPressedCallback;

Expand All @@ -67,6 +75,8 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_compose);

editText = findViewById(R.id.compose_edittext);
editTextTitle = findViewById(R.id.compose_edittext_title);
editTextUrl = findViewById(R.id.compose_edittext_url);
submitButton = findViewById(R.id.compose_submit);
replyingTextView = findViewById(R.id.compose_replying_text);
replyingScrollView = findViewById(R.id.compose_replying_scrollview);
Expand All @@ -78,39 +88,36 @@ protected void onCreate(Bundle savedInstanceState) {
id = intent.getIntExtra(EXTRA_ID, -1);
parentText = intent.getStringExtra(EXTRA_PARENT_TEXT);
user = intent.getStringExtra(EXTRA_USER);
type = intent.getIntExtra(EXTRA_TYPE, TYPE_POST);


if (id == -1) {
if (type != TYPE_POST && id == -1) {
Toast.makeText(this, "Invalid comment id", Toast.LENGTH_SHORT).show();
finish();
}

if (TextUtils.isEmpty(user)) {
replyingScrollView.setVisibility(View.GONE);
topCommentTextView.setVisibility(View.VISIBLE);
topCommentTextView.setText("Commenting on: " + parentText);
} else {
replyingScrollView.setVisibility(View.VISIBLE);
topCommentTextView.setVisibility(View.GONE);
replyingTextView.setHtml("<p>Replying to " + user + "'s comment:</p>" + parentText);
switch (type) {
case TYPE_TOP_COMMENT:
replyingScrollView.setVisibility(View.GONE);
topCommentTextView.setVisibility(View.VISIBLE);
topCommentTextView.setText("Commenting on: " + parentText);
break;
case TYPE_COMMENT_REPLY:
replyingScrollView.setVisibility(View.VISIBLE);
topCommentTextView.setVisibility(View.GONE);
replyingTextView.setHtml("<p>Replying to " + user + "'s comment:</p>" + parentText);
break;
case TYPE_POST:
replyingScrollView.setVisibility(View.GONE);
topCommentTextView.setVisibility(View.VISIBLE);
topCommentTextView.setText("Submitting post");
break;
}

submitButton.setEnabled(!TextUtils.isEmpty(editText.getText().toString()));

editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

editText.addTextChangedListener(new ViewUtils.SimpleTextWatcher() {
@Override
public void afterTextChanged(Editable editable) {
backPressedCallback.setEnabled(!TextUtils.isEmpty(editable.toString()));
submitButton.setEnabled(!TextUtils.isEmpty(editable.toString()));
updateEnabledStatuses();
}
});

Expand Down Expand Up @@ -187,7 +194,7 @@ public void onClick(DialogInterface dialog, int whichButton) {
}
};
getOnBackPressedDispatcher().addCallback(this, backPressedCallback);
backPressedCallback.setEnabled(false);
updateEnabledStatuses();
}

@Override
Expand All @@ -214,26 +221,60 @@ public void infoClick(View view) {
dialog.show();
}

private void updateEnabledStatuses() {
boolean hasText = !TextUtils.isEmpty(editText.getText().toString());
boolean hasTitle = !TextUtils.isEmpty(editTextTitle.getText().toString());
boolean hasUrl = !TextUtils.isEmpty(editTextUrl.getText().toString());
boolean enable;

if (type == TYPE_POST) {
enable = hasTitle && (hasText || hasUrl);
} else {
enable = hasText;
}

backPressedCallback.setEnabled(enable);
submitButton.setEnabled(enable);
}

public void submit(View view) {
ProgressDialog dialog = ProgressDialog.show(this, "",
"Posting...", true);

dialog.getWindow().setBackgroundDrawableResource(ThemeUtils.getBackgroundColorResource(view.getContext()));

UserActions.comment(String.valueOf(id), editText.getText().toString(), view.getContext(), new UserActions.ActionCallback() {
@Override
public void onSuccess(Response response) {
dialog.cancel();
Toast.makeText(view.getContext(), "Comment posted, it might take a minute to show up", Toast.LENGTH_SHORT).show();
finish();
}

@Override
public void onFailure(String summary, String response) {
dialog.cancel();
UserActions.showFailureDetailDialog(view.getContext(), summary, response);
Toast.makeText(view.getContext(), "Comment post unsuccessful, see dialog for details", Toast.LENGTH_SHORT).show();
}
});
if (type == TYPE_POST) {
UserActions.submit(editTextTitle.getText().toString(), editText.getText().toString(), editTextUrl.getText().toString(), view.getContext(), new UserActions.ActionCallback() {
@Override
public void onSuccess(Response response) {
dialog.cancel();
Toast.makeText(view.getContext(), "Post submitted, it might take a minute to show up", Toast.LENGTH_SHORT).show();
finish();
}

@Override
public void onFailure(String summary, String response) {
dialog.cancel();
UserActions.showFailureDetailDialog(view.getContext(), summary, response);
Toast.makeText(view.getContext(), "Post submission unsuccessful, see dialog for details", Toast.LENGTH_SHORT).show();
}
});
} else {
UserActions.comment(String.valueOf(id), editText.getText().toString(), view.getContext(), new UserActions.ActionCallback() {
@Override
public void onSuccess(Response response) {
dialog.cancel();
Toast.makeText(view.getContext(), "Comment posted, it might take a minute to show up", Toast.LENGTH_SHORT).show();
finish();
}

@Override
public void onFailure(String summary, String response) {
dialog.cancel();
UserActions.showFailureDetailDialog(view.getContext(), summary, response);
Toast.makeText(view.getContext(), "Comment post unsuccessful, see dialog for details", Toast.LENGTH_SHORT).show();
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.textfield.TextInputEditText;
import com.simon.harmonichackernews.utils.AccountUtils;
import com.simon.harmonichackernews.utils.ViewUtils;

public class LoginDialogFragment extends AppCompatDialogFragment {

Expand All @@ -42,15 +43,7 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Button infoButton = rootView.findViewById(R.id.login_dialog_more_info);
LinearLayout infoContainer = rootView.findViewById(R.id.login_dialog_info_container);

usernameInput.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}

usernameInput.addTextChangedListener(new ViewUtils.SimpleTextWatcher() {
@Override
public void afterTextChanged(Editable editable) {
boolean usernameHasText = !TextUtils.isEmpty(usernameInput.getText().toString());
Expand All @@ -60,15 +53,7 @@ public void afterTextChanged(Editable editable) {
}
});

passwordInput.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}

passwordInput.addTextChangedListener(new ViewUtils.SimpleTextWatcher() {
@Override
public void afterTextChanged(Editable editable) {
boolean usernameHasText = !TextUtils.isEmpty(usernameInput.getText().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -482,7 +483,7 @@ public void moreClick(View view) {
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.menu_settings) {
requireActivity().startActivity(new Intent(requireActivity(), SettingsActivity.class));
} else if (item.getItemId() == R.id.menu_login) {
} else if (item.getItemId() == R.id.menu_log) {
if (TextUtils.isEmpty(AccountUtils.getAccountUsername(requireActivity()))) {
AccountUtils.showLoginPrompt(requireActivity().getSupportFragmentManager());
} else {
Expand All @@ -491,19 +492,23 @@ public boolean onMenuItemClick(MenuItem item) {
}
} else if (item.getItemId() == R.id.menu_profile) {
UserDialogFragment.showUserDialog(requireActivity().getSupportFragmentManager(), AccountUtils.getAccountUsername(requireActivity()));
} else if (item.getItemId() == R.id.menu_submit) {
Intent submitIntent = new Intent(getContext(), ComposeActivity.class);
submitIntent.putExtra(ComposeActivity.EXTRA_TYPE, ComposeActivity.TYPE_POST);
startActivity(submitIntent);
}
return true;
}
});
popup.getMenuInflater().inflate(R.menu.main_menu, popup.getMenu());

if (TextUtils.isEmpty(AccountUtils.getAccountUsername(requireActivity()))) {
popup.getMenu().getItem(0).setTitle("Log in");
popup.getMenu().getItem(1).setVisible(false);
} else {
popup.getMenu().getItem(0).setTitle("Log out");
popup.getMenu().getItem(1).setVisible(true);
}
Menu menu = popup.getMenu();

boolean loggedIn = !TextUtils.isEmpty(AccountUtils.getAccountUsername(requireActivity()));

menu.findItem(R.id.menu_log).setTitle(loggedIn ? "Log out" : "Log in");
menu.findItem(R.id.menu_profile).setVisible(loggedIn);
menu.findItem(R.id.menu_submit).setVisible(loggedIn);

popup.show();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,31 @@ public static void comment(String itemId, String text, Context ctx, ActionCallba
executeRequest(ctx, request, cb);
}

public static void submit(String title, String text, String url, Context ctx, ActionCallback cb) {
Utils.log("Submitting");
Triple<String, String, Integer> account = AccountUtils.getAccountDetails(ctx);

if (AccountUtils.handlePossibleError(account, null, ctx)) {
return;
}

Request request = new Request.Builder()
.url(Objects.requireNonNull(HttpUrl.parse(BASE_WEB_URL))
.newBuilder()
.addPathSegment(SUBMIT_PATH)
.build())
.post(new FormBody.Builder()
.add(LOGIN_PARAM_ACCT, account.getFirst())
.add(LOGIN_PARAM_PW, account.getSecond())
.add(SUBMIT_PARAM_TITLE, title)
.add(SUBMIT_PARAM_TEXT, text)
.add(SUBMIT_PARAM_URL, url)
.build())
.build();

executeRequest(ctx, request, cb);
}

public static void executeRequest(Context ctx, Request request, ActionCallback cb) {
OkHttpClient client = NetworkComponent.getOkHttpClientInstance();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.simon.harmonichackernews.utils;

import android.content.res.Resources;
import android.text.TextWatcher;
import android.view.View;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -69,4 +70,15 @@ public static int getNavigationBarHeight(Resources res) {
}
return 0;
}

public abstract static class SimpleTextWatcher implements TextWatcher {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
}

}
30 changes: 30 additions & 0 deletions app/src/main/res/layout/activity_compose.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,36 @@
android:layout_width="match_parent"
android:layout_height="1dp" />

<androidx.appcompat.widget.AppCompatEditText
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:paddingLeft="16dp"
android:imeOptions="normal"
android:paddingRight="16dp"
android:id="@+id/compose_edittext_title"
android:hint="Title"
android:background="@null"
android:fontFamily="@font/product_sans"
android:gravity="top"
android:inputType="textCapSentences|textMultiLine"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<androidx.appcompat.widget.AppCompatEditText
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:paddingLeft="16dp"
android:imeOptions="normal"
android:paddingRight="16dp"
android:id="@+id/compose_edittext_url"
android:hint="URL"
android:background="@null"
android:fontFamily="@font/product_sans"
android:gravity="top"
android:inputType="textCapSentences|textMultiLine"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<androidx.appcompat.widget.AppCompatEditText
android:paddingLeft="16dp"
android:imeOptions="normal"
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/res/menu/main_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">

<item
android:id="@+id/menu_login"
android:id="@+id/menu_log"
android:title="Log in" />

<item
android:id="@+id/menu_submit"
android:title="Submit" />

<item
android:id="@+id/menu_profile"
android:title="Profile" />
Expand Down
Loading

0 comments on commit 64d1e8a

Please sign in to comment.