Display a content preview in PostFragment for text posts.

This commit is contained in:
Alex Ning 2019-10-28 17:31:47 +08:00
parent 75452b764d
commit 4085c0c40c
14 changed files with 88 additions and 36 deletions

View File

@ -2,6 +2,14 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ml.docilealligator.infinityforreddit"> package="ml.docilealligator.infinityforreddit">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="22" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission-sdk-23 android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application <application
android:name=".Infinity" android:name=".Infinity"
android:allowBackup="true" android:allowBackup="true"
@ -249,17 +257,9 @@
</provider> </provider>
<service <service
android:name=".SubmitPostService" android:name=".Service.SubmitPostService"
android:enabled="true" android:enabled="true"
android:exported="false" /> android:exported="false" />
</application> </application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="22" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission-sdk-23 android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest> </manifest>

View File

@ -51,7 +51,7 @@ import ml.docilealligator.infinityforreddit.Fragment.FlairBottomSheetFragment;
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.SubmitPostService; import ml.docilealligator.infinityforreddit.Service.SubmitPostService;
import pl.droidsonroids.gif.GifImageView; import pl.droidsonroids.gif.GifImageView;
import retrofit2.Retrofit; import retrofit2.Retrofit;

View File

@ -41,7 +41,7 @@ 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.RedditUtils; import ml.docilealligator.infinityforreddit.RedditUtils;
import ml.docilealligator.infinityforreddit.SubmitPostService; import ml.docilealligator.infinityforreddit.Service.SubmitPostService;
import pl.droidsonroids.gif.GifImageView; import pl.droidsonroids.gif.GifImageView;
import retrofit2.Retrofit; import retrofit2.Retrofit;

View File

@ -41,7 +41,7 @@ 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.RedditUtils; import ml.docilealligator.infinityforreddit.RedditUtils;
import ml.docilealligator.infinityforreddit.SubmitPostService; import ml.docilealligator.infinityforreddit.Service.SubmitPostService;
import pl.droidsonroids.gif.GifImageView; import pl.droidsonroids.gif.GifImageView;
import retrofit2.Retrofit; import retrofit2.Retrofit;

View File

@ -53,7 +53,7 @@ import ml.docilealligator.infinityforreddit.Fragment.FlairBottomSheetFragment;
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.SubmitPostService; import ml.docilealligator.infinityforreddit.Service.SubmitPostService;
import pl.droidsonroids.gif.GifImageView; import pl.droidsonroids.gif.GifImageView;
import retrofit2.Retrofit; import retrofit2.Retrofit;

View File

@ -468,9 +468,9 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
break; break;
case Post.TEXT_TYPE: case Post.TEXT_TYPE:
((DataViewHolder) holder).typeTextView.setText(R.string.text); ((DataViewHolder) holder).typeTextView.setText(R.string.text);
if (post.getSelfText() != null && !post.getSelfText().equals("")) { if (post.getSelfTextPlainTrimmed() != null && !post.getSelfTextPlainTrimmed().equals("")) {
((DataViewHolder) holder).contentTextView.setVisibility(View.VISIBLE); ((DataViewHolder) holder).contentTextView.setVisibility(View.VISIBLE);
((DataViewHolder) holder).contentTextView.setText(post.getSelfText()); ((DataViewHolder) holder).contentTextView.setText(post.getSelfTextPlainTrimmed());
} }
break; break;
} }

View File

@ -36,6 +36,7 @@ import ml.docilealligator.infinityforreddit.Fragment.PostFragment;
import ml.docilealligator.infinityforreddit.Fragment.SubredditListingFragment; import ml.docilealligator.infinityforreddit.Fragment.SubredditListingFragment;
import ml.docilealligator.infinityforreddit.Fragment.SubscribedSubredditsListingFragment; import ml.docilealligator.infinityforreddit.Fragment.SubscribedSubredditsListingFragment;
import ml.docilealligator.infinityforreddit.Fragment.UserListingFragment; import ml.docilealligator.infinityforreddit.Fragment.UserListingFragment;
import ml.docilealligator.infinityforreddit.Service.SubmitPostService;
import ml.docilealligator.infinityforreddit.Settings.MainPreferenceFragment; import ml.docilealligator.infinityforreddit.Settings.MainPreferenceFragment;
import ml.docilealligator.infinityforreddit.Settings.NotificationPreferenceFragment; import ml.docilealligator.infinityforreddit.Settings.NotificationPreferenceFragment;

View File

@ -14,6 +14,7 @@ public class JSONUtils {
public static final String NAME_KEY = "name"; public static final String NAME_KEY = "name";
public static final String SUBREDDIT_NAME_PREFIX_KEY = "subreddit_name_prefixed"; public static final String SUBREDDIT_NAME_PREFIX_KEY = "subreddit_name_prefixed";
public static final String SELFTEXT_KEY = "selftext"; public static final String SELFTEXT_KEY = "selftext";
public static final String SELFTEXT_HTML_KEY = "selftext_html";
public static final String AUTHOR_KEY = "author"; public static final String AUTHOR_KEY = "author";
public static final String LINK_AUTHOR_KEY = "link_author"; public static final String LINK_AUTHOR_KEY = "link_author";
public static final String LINK_FLAIR_TEXT_KEY = "link_flair_text"; public static final String LINK_FLAIR_TEXT_KEY = "link_flair_text";

View File

@ -7,11 +7,11 @@ import android.content.Context;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat; import androidx.core.app.NotificationManagerCompat;
class NotificationUtils { public class NotificationUtils {
static final String CHANNEL_SUBMIT_POST = "Submit Post"; public static final String CHANNEL_SUBMIT_POST = "Submit Post";
static final String CHANNEL_ID_NEW_MESSAGES = "new_messages"; static final String CHANNEL_ID_NEW_MESSAGES = "new_messages";
static final String CHANNEL_NEW_MESSAGES = "New Messages"; static final String CHANNEL_NEW_MESSAGES = "New Messages";
static final int SUBMIT_POST_SERVICE_NOTIFICATION_ID = 10000; public static final int SUBMIT_POST_SERVICE_NOTIFICATION_ID = 10000;
private static final int SUMMARY_BASE_ID_UNREAD_MESSAGE = 0; private static final int SUMMARY_BASE_ID_UNREAD_MESSAGE = 0;
private static final int NOTIFICATION_BASE_ID_UNREAD_MESSAGE = 1; private static final int NOTIFICATION_BASE_ID_UNREAD_MESSAGE = 1;

View File

@ -116,6 +116,16 @@ public class ParsePost {
post.setSelfText(""); post.setSelfText("");
} else { } else {
post.setSelfText(Utils.addSubredditAndUserLink(data.getString(JSONUtils.SELFTEXT_KEY).trim())); post.setSelfText(Utils.addSubredditAndUserLink(data.getString(JSONUtils.SELFTEXT_KEY).trim()));
if (data.isNull(JSONUtils.SELFTEXT_HTML_KEY)) {
post.setSelfTextPlainTrimmed("");
} else {
String selfTextPlain = Utils.trimTrailingWhitespace(
Html.fromHtml(data.getString(JSONUtils.SELFTEXT_HTML_KEY))).toString();
if (selfTextPlain.length() > 150) {
selfTextPlain = selfTextPlain.substring(150);
}
post.setSelfTextPlainTrimmed(selfTextPlain);
}
} }
} else { } else {
//No preview link post //No preview link post
@ -202,6 +212,16 @@ public class ParsePost {
post.setSelfText(""); post.setSelfText("");
} else { } else {
post.setSelfText(Utils.addSubredditAndUserLink(data.getString(JSONUtils.SELFTEXT_KEY).trim())); post.setSelfText(Utils.addSubredditAndUserLink(data.getString(JSONUtils.SELFTEXT_KEY).trim()));
if (data.isNull(JSONUtils.SELFTEXT_HTML_KEY)) {
post.setSelfTextPlainTrimmed("");
} else {
String selfTextPlain = Utils.trimTrailingWhitespace(
Html.fromHtml(data.getString(JSONUtils.SELFTEXT_HTML_KEY))).toString();
if (selfTextPlain.length() > 150) {
selfTextPlain = selfTextPlain.substring(150);
}
post.setSelfTextPlainTrimmed(selfTextPlain);
}
} }
} else { } else {
//Link post //Link post

View File

@ -37,6 +37,7 @@ public class Post implements Parcelable {
private String postTime; private String postTime;
private String title; private String title;
private String selfText; private String selfText;
private String selfTextPlainTrimmed;
private String previewUrl; private String previewUrl;
private String url; private String url;
private String videoUrl; private String videoUrl;
@ -162,6 +163,7 @@ public class Post implements Parcelable {
postTime = in.readString(); postTime = in.readString();
title = in.readString(); title = in.readString();
selfText = in.readString(); selfText = in.readString();
selfTextPlainTrimmed = in.readString();
previewUrl = in.readString(); previewUrl = in.readString();
url = in.readString(); url = in.readString();
videoUrl = in.readString(); videoUrl = in.readString();
@ -245,6 +247,14 @@ public class Post implements Parcelable {
this.selfText = selfText; this.selfText = selfText;
} }
public String getSelfTextPlainTrimmed() {
return selfTextPlainTrimmed;
}
public void setSelfTextPlainTrimmed(String selfTextPlainTrimmed) {
this.selfTextPlainTrimmed = selfTextPlainTrimmed;
}
public String getPreviewUrl() { public String getPreviewUrl() {
return previewUrl; return previewUrl;
} }
@ -395,6 +405,7 @@ public class Post implements Parcelable {
parcel.writeString(postTime); parcel.writeString(postTime);
parcel.writeString(title); parcel.writeString(title);
parcel.writeString(selfText); parcel.writeString(selfText);
parcel.writeString(selfTextPlainTrimmed);
parcel.writeString(previewUrl); parcel.writeString(previewUrl);
parcel.writeString(url); parcel.writeString(url);
parcel.writeString(videoUrl); parcel.writeString(videoUrl);

View File

@ -1,4 +1,4 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit.Service;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationChannel; import android.app.NotificationChannel;
@ -33,6 +33,11 @@ import javax.inject.Named;
import ml.docilealligator.infinityforreddit.Event.SubmitImagePostEvent; import ml.docilealligator.infinityforreddit.Event.SubmitImagePostEvent;
import ml.docilealligator.infinityforreddit.Event.SubmitTextOrLinkPostEvent; import ml.docilealligator.infinityforreddit.Event.SubmitTextOrLinkPostEvent;
import ml.docilealligator.infinityforreddit.Event.SubmitVideoPostEvent; import ml.docilealligator.infinityforreddit.Event.SubmitVideoPostEvent;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.NotificationUtils;
import ml.docilealligator.infinityforreddit.Post;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.SubmitPost;
import retrofit2.Retrofit; import retrofit2.Retrofit;
public class SubmitPostService extends Service { public class SubmitPostService extends Service {

View File

@ -28,8 +28,8 @@ import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
import retrofit2.Retrofit; import retrofit2.Retrofit;
class SubmitPost { public class SubmitPost {
static void submitTextOrLinkPost(Retrofit oauthRetrofit, String accessToken, public static void submitTextOrLinkPost(Retrofit oauthRetrofit, String accessToken,
Locale locale, String subredditName, String title, String content, Locale locale, String subredditName, String title, String content,
String flair, boolean isSpoiler, boolean isNSFW, String kind, String flair, boolean isSpoiler, boolean isNSFW, String kind,
SubmitPostListener submitPostListener) { SubmitPostListener submitPostListener) {
@ -37,7 +37,7 @@ class SubmitPost {
flair, isSpoiler, isNSFW, kind, null, submitPostListener); flair, isSpoiler, isNSFW, kind, null, submitPostListener);
} }
static void submitImagePost(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit, public static void submitImagePost(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit,
String accessToken, Locale locale, String accessToken, Locale locale,
String subredditName, String title, Bitmap image, String flair, String subredditName, String title, Bitmap image, String flair,
boolean isSpoiler, boolean isNSFW, SubmitPostListener submitPostListener) { boolean isSpoiler, boolean isNSFW, SubmitPostListener submitPostListener) {
@ -57,7 +57,7 @@ class SubmitPost {
}); });
} }
static void submitVideoPost(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit, public static void submitVideoPost(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit,
Retrofit uploadVideoRetrofit, String accessToken, Retrofit uploadVideoRetrofit, String accessToken,
Locale locale, String subredditName, String title, byte[] buffer, String mimeType, Locale locale, String subredditName, String title, byte[] buffer, String mimeType,
Bitmap posterBitmap, String flair, boolean isSpoiler, boolean isNSFW, Bitmap posterBitmap, String flair, boolean isSpoiler, boolean isNSFW,
@ -341,7 +341,7 @@ class SubmitPost {
} }
} }
interface SubmitPostListener { public interface SubmitPostListener {
void submitSuccessful(Post post); void submitSuccessful(Post post);
void submitFailed(@Nullable String errorMessage); void submitFailed(@Nullable String errorMessage);

View File

@ -2,7 +2,21 @@ package ml.docilealligator.infinityforreddit;
public class Utils { public class Utils {
public static String addSubredditAndUserLink(String markdown) { public static String addSubredditAndUserLink(String markdown) {
return markdown.replaceAll("\n", " ") return markdown.replaceAll("((?<=[\\s])|^)/{0,1}[rRuU]/\\w+/{0,1}", "[$0]($0)");
.replaceAll("((?<=[\\s])|^)/{0,1}[ru]/\\w+/{0,1}", "[$0]($0)"); }
public static CharSequence trimTrailingWhitespace(CharSequence source) {
if(source == null)
return "";
int i = source.length();
// loop back to the first non-whitespace character
do {
i--;
} while (i >= 0 && Character.isWhitespace(source.charAt(i)));
return source.subSequence(0, i+1);
} }
} }