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 'net.lingala.zip4j:zip4j:2.7.0'
implementation 'org.apache.commons:commons-io:1.3.2' implementation 'org.apache.commons:commons-io:1.3.2'
implementation 'com.github.razir.progressbutton:progressbutton:2.1.0'
/**** Builds and flavors ****/ /**** Builds and flavors ****/
// debugImplementation because LeakCanary should only run in debug builds. // 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.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -24,6 +25,7 @@ import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager; import com.bumptech.glide.RequestManager;
import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.RequestOptions;
import com.google.android.material.appbar.AppBarLayout; 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.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import com.libRG.CustomTextView; import com.libRG.CustomTextView;
@ -43,6 +45,7 @@ import ml.docilealligator.infinityforreddit.Flair;
import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.apis.TitleSuggestion;
import ml.docilealligator.infinityforreddit.asynctasks.LoadSubredditIcon; import ml.docilealligator.infinityforreddit.asynctasks.LoadSubredditIcon;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.FlairBottomSheetFragment; import ml.docilealligator.infinityforreddit.bottomsheetfragments.FlairBottomSheetFragment;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; 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.APIUtils;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
import pl.droidsonroids.gif.GifImageView; import pl.droidsonroids.gif.GifImageView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit; import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;
public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFragment.FlairSelectionCallback { public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFragment.FlairSelectionCallback {
@ -97,6 +104,8 @@ public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFr
CustomTextView nsfwTextView; CustomTextView nsfwTextView;
@BindView(R.id.post_title_edit_text_post_link_activity) @BindView(R.id.post_title_edit_text_post_link_activity)
EditText titleEditText; EditText titleEditText;
@BindView(R.id.suggest_title_button_post_link_activity)
MaterialButton suggestTitleButton;
@BindView(R.id.post_link_edit_text_post_link_activity) @BindView(R.id.post_link_edit_text_post_link_activity)
EditText linkEditText; EditText linkEditText;
@Inject @Inject
@ -300,6 +309,32 @@ public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFr
isNSFW = false; 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 @Override
@ -336,6 +371,8 @@ public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFr
nsfwTextView.setTextColor(primaryTextColor); nsfwTextView.setTextColor(primaryTextColor);
titleEditText.setTextColor(primaryTextColor); titleEditText.setTextColor(primaryTextColor);
titleEditText.setHintTextColor(secondaryTextColor); titleEditText.setHintTextColor(secondaryTextColor);
suggestTitleButton.setBackgroundColor(mCustomThemeWrapper.getColorPrimaryLightTheme());
suggestTitleButton.setTextColor(mCustomThemeWrapper.getButtonTextColor());
linkEditText.setTextColor(primaryTextColor); linkEditText.setTextColor(primaryTextColor);
linkEditText.setHintTextColor(secondaryTextColor); linkEditText.setHintTextColor(secondaryTextColor);
} }
@ -386,8 +423,8 @@ public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFr
@Override @Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) { public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) { int itemId = item.getItemId();
case android.R.id.home: if (itemId == android.R.id.home) {
if (isPosting) { if (isPosting) {
promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_submit_post_detail); promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_submit_post_detail);
return true; return true;
@ -399,7 +436,7 @@ public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFr
} }
finish(); finish();
return true; return true;
case R.id.action_send_post_link_activity: } else if (itemId == R.id.action_send_post_link_activity) {
if (!subredditSelected) { if (!subredditSelected) {
Snackbar.make(coordinatorLayout, R.string.select_a_subreddit, Snackbar.LENGTH_SHORT).show(); Snackbar.make(coordinatorLayout, R.string.select_a_subreddit, Snackbar.LENGTH_SHORT).show();
return true; return true;

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,10 +132,18 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="1dp" /> android:layout_height="1dp" />
<EditText <LinearLayout
android:id="@+id/post_title_edit_text_post_link_activity"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
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:background="#00000000"
android:gravity="top" android:gravity="top"
android:hint="@string/post_title_hint" android:hint="@string/post_title_hint"
@ -145,6 +153,15 @@
android:textSize="?attr/title_font_18" android:textSize="?attr/title_font_18"
android:fontFamily="?attr/title_font_family" /> 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 <View
android:id="@+id/divider_3_post_link_activity" android:id="@+id/divider_3_post_link_activity"
android:layout_width="match_parent" android:layout_width="match_parent"

View File

@ -1091,4 +1091,7 @@
<string name="contain_flair">Contain this flair</string> <string name="contain_flair">Contain this flair</string>
<string name="exclude_domain">Exclude Domain</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> </resources>