Suggest title when submitting link post.

This commit is contained in:
Alex Ning 2021-04-02 00:24:47 +08:00
parent 68d247867f
commit 8c40ee1bc3
5 changed files with 131 additions and 62 deletions

View File

@ -173,6 +173,8 @@ dependencies {
implementation 'net.lingala.zip4j:zip4j:2.7.0'
implementation 'org.apache.commons:commons-io:1.3.2'
implementation 'com.github.razir.progressbutton:progressbutton:2.1.0'
/**** Builds and flavors ****/
// debugImplementation because LeakCanary should only run in debug builds.

View File

@ -13,6 +13,7 @@ import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -24,6 +25,7 @@ import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.request.RequestOptions;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.snackbar.Snackbar;
import com.libRG.CustomTextView;
@ -43,6 +45,7 @@ import ml.docilealligator.infinityforreddit.Flair;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.apis.TitleSuggestion;
import ml.docilealligator.infinityforreddit.asynctasks.LoadSubredditIcon;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.FlairBottomSheetFragment;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
@ -52,7 +55,11 @@ import ml.docilealligator.infinityforreddit.services.SubmitPostService;
import ml.docilealligator.infinityforreddit.utils.APIUtils;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
import pl.droidsonroids.gif.GifImageView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;
public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFragment.FlairSelectionCallback {
@ -97,6 +104,8 @@ public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFr
CustomTextView nsfwTextView;
@BindView(R.id.post_title_edit_text_post_link_activity)
EditText titleEditText;
@BindView(R.id.suggest_title_button_post_link_activity)
MaterialButton suggestTitleButton;
@BindView(R.id.post_link_edit_text_post_link_activity)
EditText linkEditText;
@Inject
@ -300,6 +309,32 @@ public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFr
isNSFW = false;
}
});
suggestTitleButton.setOnClickListener(view -> {
Toast.makeText(this, R.string.please_wait, Toast.LENGTH_SHORT).show();
String url = linkEditText.getText().toString().trim();
if (!url.startsWith("https://") || !url.startsWith("http://")) {
url = "https://" + url;
}
new Retrofit.Builder()
.baseUrl("http://localhost/")
.addConverterFactory(ScalarsConverterFactory.create())
.build().create(TitleSuggestion.class).getHtml(url).enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
titleEditText.setText(response.body().substring(response.body().indexOf("<title>") + 7, response.body().indexOf("</title>")));
} else {
Toast.makeText(PostLinkActivity.this, R.string.suggest_title_failed, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
Toast.makeText(PostLinkActivity.this, R.string.suggest_title_failed, Toast.LENGTH_SHORT).show();
}
});
});
}
@Override
@ -336,6 +371,8 @@ public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFr
nsfwTextView.setTextColor(primaryTextColor);
titleEditText.setTextColor(primaryTextColor);
titleEditText.setHintTextColor(secondaryTextColor);
suggestTitleButton.setBackgroundColor(mCustomThemeWrapper.getColorPrimaryLightTheme());
suggestTitleButton.setTextColor(mCustomThemeWrapper.getButtonTextColor());
linkEditText.setTextColor(primaryTextColor);
linkEditText.setHintTextColor(secondaryTextColor);
}
@ -386,62 +423,62 @@ public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFr
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (isPosting) {
promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_submit_post_detail);
return true;
} else {
if (!titleEditText.getText().toString().equals("") || !linkEditText.getText().toString().equals("")) {
promptAlertDialog(R.string.discard, R.string.discard_detail);
return true;
}
}
finish();
int itemId = item.getItemId();
if (itemId == android.R.id.home) {
if (isPosting) {
promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_submit_post_detail);
return true;
case R.id.action_send_post_link_activity:
if (!subredditSelected) {
Snackbar.make(coordinatorLayout, R.string.select_a_subreddit, Snackbar.LENGTH_SHORT).show();
} else {
if (!titleEditText.getText().toString().equals("") || !linkEditText.getText().toString().equals("")) {
promptAlertDialog(R.string.discard, R.string.discard_detail);
return true;
}
if (titleEditText.getText() == null || titleEditText.getText().toString().equals("")) {
Snackbar.make(coordinatorLayout, R.string.title_required, Snackbar.LENGTH_SHORT).show();
return true;
}
if (linkEditText.getText() == null || linkEditText.getText().toString().equals("")) {
Snackbar.make(coordinatorLayout, R.string.link_required, Snackbar.LENGTH_SHORT).show();
return true;
}
isPosting = true;
item.setEnabled(false);
item.getIcon().setAlpha(130);
mPostingSnackbar.show();
String subredditName;
if (subredditIsUser) {
subredditName = "u_" + subredditNameTextView.getText().toString();
} else {
subredditName = subredditNameTextView.getText().toString();
}
Intent intent = new Intent(this, SubmitPostService.class);
intent.putExtra(SubmitPostService.EXTRA_ACCESS_TOKEN, mAccessToken);
intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName);
intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString());
intent.putExtra(SubmitPostService.EXTRA_CONTENT, linkEditText.getText().toString());
intent.putExtra(SubmitPostService.EXTRA_KIND, APIUtils.KIND_LINK);
intent.putExtra(SubmitPostService.EXTRA_FLAIR, flair);
intent.putExtra(SubmitPostService.EXTRA_IS_SPOILER, isSpoiler);
intent.putExtra(SubmitPostService.EXTRA_IS_NSFW, isNSFW);
intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TEXT_OR_LINK);
ContextCompat.startForegroundService(this, intent);
}
finish();
return true;
} else if (itemId == R.id.action_send_post_link_activity) {
if (!subredditSelected) {
Snackbar.make(coordinatorLayout, R.string.select_a_subreddit, Snackbar.LENGTH_SHORT).show();
return true;
}
if (titleEditText.getText() == null || titleEditText.getText().toString().equals("")) {
Snackbar.make(coordinatorLayout, R.string.title_required, Snackbar.LENGTH_SHORT).show();
return true;
}
if (linkEditText.getText() == null || linkEditText.getText().toString().equals("")) {
Snackbar.make(coordinatorLayout, R.string.link_required, Snackbar.LENGTH_SHORT).show();
return true;
}
isPosting = true;
item.setEnabled(false);
item.getIcon().setAlpha(130);
mPostingSnackbar.show();
String subredditName;
if (subredditIsUser) {
subredditName = "u_" + subredditNameTextView.getText().toString();
} else {
subredditName = subredditNameTextView.getText().toString();
}
Intent intent = new Intent(this, SubmitPostService.class);
intent.putExtra(SubmitPostService.EXTRA_ACCESS_TOKEN, mAccessToken);
intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName);
intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString());
intent.putExtra(SubmitPostService.EXTRA_CONTENT, linkEditText.getText().toString());
intent.putExtra(SubmitPostService.EXTRA_KIND, APIUtils.KIND_LINK);
intent.putExtra(SubmitPostService.EXTRA_FLAIR, flair);
intent.putExtra(SubmitPostService.EXTRA_IS_SPOILER, isSpoiler);
intent.putExtra(SubmitPostService.EXTRA_IS_NSFW, isNSFW);
intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TEXT_OR_LINK);
ContextCompat.startForegroundService(this, intent);
return true;
}
return false;

View File

@ -0,0 +1,10 @@
package ml.docilealligator.infinityforreddit.apis;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Url;
public interface TitleSuggestion {
@GET()
Call<String> getHtml(@Url String url);
}

View File

@ -132,18 +132,35 @@
android:layout_width="match_parent"
android:layout_height="1dp" />
<EditText
android:id="@+id/post_title_edit_text_post_link_activity"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="top"
android:hint="@string/post_title_hint"
android:inputType="textCapSentences|textMultiLine"
android:padding="16dp"
android:textColor="?attr/primaryTextColor"
android:textSize="?attr/title_font_18"
android:fontFamily="?attr/title_font_family" />
android:paddingStart="0dp"
android:paddingEnd="16dp">
<EditText
android:id="@+id/post_title_edit_text_post_link_activity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:background="#00000000"
android:gravity="top"
android:hint="@string/post_title_hint"
android:inputType="textCapSentences|textMultiLine"
android:padding="16dp"
android:textColor="?attr/primaryTextColor"
android:textSize="?attr/title_font_18"
android:fontFamily="?attr/title_font_family" />
<com.google.android.material.button.MaterialButton
android:id="@+id/suggest_title_button_post_link_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/suggest_title" />
</LinearLayout>
<View
android:id="@+id/divider_3_post_link_activity"

View File

@ -1091,4 +1091,7 @@
<string name="contain_flair">Contain this flair</string>
<string name="exclude_domain">Exclude Domain</string>
<string name="suggest_title">Suggest Title</string>
<string name="suggest_title_failed">Failed to suggest a title</string>
</resources>