diff options
author | Alex Ning <chineseperson5@gmail.com> | 2021-04-01 16:24:47 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2021-04-01 16:24:47 +0000 |
commit | 8c40ee1bc3ce8b7100221a740a382150a093e11e (patch) | |
tree | a5b79c5750074e5c1de3a5308f223d48d2747fe7 /app/src | |
parent | 68d247867f1e67ed7e62e396cdb622d8970a572f (diff) | |
download | infinity-for-reddit-8c40ee1bc3ce8b7100221a740a382150a093e11e.tar infinity-for-reddit-8c40ee1bc3ce8b7100221a740a382150a093e11e.tar.gz infinity-for-reddit-8c40ee1bc3ce8b7100221a740a382150a093e11e.tar.bz2 infinity-for-reddit-8c40ee1bc3ce8b7100221a740a382150a093e11e.tar.lz infinity-for-reddit-8c40ee1bc3ce8b7100221a740a382150a093e11e.tar.xz infinity-for-reddit-8c40ee1bc3ce8b7100221a740a382150a093e11e.tar.zst infinity-for-reddit-8c40ee1bc3ce8b7100221a740a382150a093e11e.zip |
Suggest title when submitting link post.
Diffstat (limited to '')
4 files changed, 123 insertions, 56 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostLinkActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostLinkActivity.java index 48e012a4..ab1852bc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostLinkActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostLinkActivity.java @@ -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(); - return true; - } - - if (titleEditText.getText() == null || titleEditText.getText().toString().equals("")) { - Snackbar.make(coordinatorLayout, R.string.title_required, Snackbar.LENGTH_SHORT).show(); + } else { + if (!titleEditText.getText().toString().equals("") || !linkEditText.getText().toString().equals("")) { + promptAlertDialog(R.string.discard, R.string.discard_detail); return true; } + } + 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 (linkEditText.getText() == null || linkEditText.getText().toString().equals("")) { - Snackbar.make(coordinatorLayout, R.string.link_required, 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; + } - isPosting = true; + if (linkEditText.getText() == null || linkEditText.getText().toString().equals("")) { + Snackbar.make(coordinatorLayout, R.string.link_required, Snackbar.LENGTH_SHORT).show(); + return true; + } - item.setEnabled(false); - item.getIcon().setAlpha(130); + isPosting = true; - mPostingSnackbar.show(); + item.setEnabled(false); + item.getIcon().setAlpha(130); - String subredditName; - if (subredditIsUser) { - subredditName = "u_" + subredditNameTextView.getText().toString(); - } else { - subredditName = subredditNameTextView.getText().toString(); - } + mPostingSnackbar.show(); - 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); + String subredditName; + if (subredditIsUser) { + subredditName = "u_" + subredditNameTextView.getText().toString(); + } else { + subredditName = subredditNameTextView.getText().toString(); + } - return true; + 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; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/apis/TitleSuggestion.java b/app/src/main/java/ml/docilealligator/infinityforreddit/apis/TitleSuggestion.java new file mode 100644 index 00000000..76c754fb --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/apis/TitleSuggestion.java @@ -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); +} diff --git a/app/src/main/res/layout/activity_post_link.xml b/app/src/main/res/layout/activity_post_link.xml index 076764a2..1e16bada 100644 --- a/app/src/main/res/layout/activity_post_link.xml +++ b/app/src/main/res/layout/activity_post_link.xml @@ -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" diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 52700094..5808c3bd 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -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> |