From 9a4edde8fb5550b5caa66eeaba4e686a6b1ff442 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 2 Jul 2020 18:59:53 +0800 Subject: [PATCH] Add an option to change time format. Properly show message time in ViewPrivateMessagesActivity. --- .../Activity/ViewPrivateMessagesActivity.java | 3 +- .../CommentAndPostRecyclerViewAdapter.java | 35 ++++++------- .../CommentsListingRecyclerViewAdapter.java | 28 +++++++---- .../Adapter/PostRecyclerViewAdapter.java | 17 +++++-- ...vateMessagesDetailRecyclerViewAdapter.java | 21 ++++++-- .../infinityforreddit/CommentData.java | 10 +--- .../Event/ChangeTimeFormatEvent.java | 9 ++++ .../Fragment/CommentsListingFragment.java | 19 ++----- .../Fragment/PostFragment.java | 32 ++++++++---- .../infinityforreddit/ParseComment.java | 9 +--- .../infinityforreddit/Post/ParsePost.java | 38 ++++++-------- .../infinityforreddit/Post/Post.java | 16 ++---- .../Settings/InterfacePreferenceFragment.java | 9 ---- .../TimeFormatPreferenceFragment.java | 38 ++++++++++++++ .../Utils/SharedPreferencesUtils.java | 2 + .../infinityforreddit/Utils/Utils.java | 8 +++ app/src/main/res/values/arrays.xml | 50 +++++++++++++++++++ app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/interface_preference.xml | 7 ++- .../main/res/xml/time_format_preferences.xml | 16 ++++++ 20 files changed, 242 insertions(+), 126 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeTimeFormatEvent.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Settings/TimeFormatPreferenceFragment.java create mode 100644 app/src/main/res/xml/time_format_preferences.xml diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPrivateMessagesActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPrivateMessagesActivity.java index c42ab803..b75f9e8b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPrivateMessagesActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPrivateMessagesActivity.java @@ -158,7 +158,8 @@ public class ViewPrivateMessagesActivity extends BaseActivity implements Activit } private void bindView() { - mAdapter = new PrivateMessagesDetailRecyclerViewAdapter(this, privateMessage, mAccountName, mCustomThemeWrapper); + mAdapter = new PrivateMessagesDetailRecyclerViewAdapter(this, mSharedPreferences, + getResources().getConfiguration().locale, privateMessage, mAccountName, mCustomThemeWrapper); mLinearLayoutManager = new LinearLayoutManager(this); mLinearLayoutManager.setStackFromEnd(true); mRecyclerView.setLayoutManager(mLinearLayoutManager); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java index 3689f9a8..e1392761 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -36,7 +36,6 @@ import androidx.recyclerview.widget.RecyclerView; import com.bumptech.glide.RequestBuilder; import com.bumptech.glide.RequestManager; import com.bumptech.glide.load.DataSource; -import com.bumptech.glide.load.DecodeFormat; import com.bumptech.glide.load.engine.GlideException; import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.RequestOptions; @@ -147,6 +146,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { @@ -41,17 +45,23 @@ public class PrivateMessagesDetailRecyclerViewAdapter extends RecyclerView.Adapt private Message mMessage; private ViewPrivateMessagesActivity mViewPrivateMessagesActivity; private RequestManager mGlide; + private Locale mLocale; private String mAccountName; private Markwon mMarkwon; + private boolean mShowElapsedTime; + private String mTimeFormatPattern; private int mMessageBackgroundColor; private int mSecondaryTextColor; private int mUnreadMessageBackgroundColor; - public PrivateMessagesDetailRecyclerViewAdapter(ViewPrivateMessagesActivity viewPrivateMessagesActivity, Message message, String accountName, + public PrivateMessagesDetailRecyclerViewAdapter(ViewPrivateMessagesActivity viewPrivateMessagesActivity, + SharedPreferences sharedPreferences, Locale locale, + Message message, String accountName, CustomThemeWrapper customThemeWrapper) { mMessage = message; mViewPrivateMessagesActivity = viewPrivateMessagesActivity; mGlide = Glide.with(viewPrivateMessagesActivity); + mLocale = locale; mAccountName = accountName; mMarkwon = Markwon.builder(viewPrivateMessagesActivity) .usePlugin(new AbstractMarkwonPlugin() { @@ -78,6 +88,8 @@ public class PrivateMessagesDetailRecyclerViewAdapter extends RecyclerView.Adapt ) ) .build(); + mShowElapsedTime = sharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_ELAPSED_TIME_KEY, false); + mTimeFormatPattern = sharedPreferences.getString(SharedPreferencesUtils.TIME_FORMAT_KEY, SharedPreferencesUtils.TIME_FORMAT_DEFAULT_VALUE); mMessageBackgroundColor = customThemeWrapper.getCardViewBackgroundColor(); mSecondaryTextColor = customThemeWrapper.getSecondaryTextColor(); mUnreadMessageBackgroundColor = customThemeWrapper.getUnreadMessageBackgroundColor(); @@ -115,6 +127,11 @@ public class PrivateMessagesDetailRecyclerViewAdapter extends RecyclerView.Adapt mMarkwon.setMarkdown(((MessageViewHolder) holder).messageTextView, message.getBody()); ((MessageViewHolder) holder).messageTextView.setOnClickListener(view -> ((MessageViewHolder) holder).itemView.performClick()); + if (mShowElapsedTime) { + ((MessageViewHolder) holder).timeTextView.setText(Utils.getElapsedTime(mViewPrivateMessagesActivity, message.getTimeUTC())); + } else { + ((MessageViewHolder) holder).timeTextView.setText(Utils.getFormattedTime(mLocale, message.getTimeUTC(), mTimeFormatPattern)); + } ((MessageViewHolder) holder).messageTextView.setOnClickListener(view -> { if (((MessageViewHolder) holder).timeTextView.getVisibility() != View.VISIBLE) { @@ -125,8 +142,6 @@ public class PrivateMessagesDetailRecyclerViewAdapter extends RecyclerView.Adapt mViewPrivateMessagesActivity.delayTransition(); } }); - - ((MessageViewHolder) holder).timeTextView.setText(Utils.getElapsedTime(mViewPrivateMessagesActivity, message.getTimeUTC())); } if (holder instanceof SentMessageViewHolder) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentData.java index e38ecd11..531ad81e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentData.java @@ -28,7 +28,6 @@ public class CommentData implements Parcelable { private String authorFlair; private String authorFlairHTML; private String linkAuthor; - private String commentTime; private long commentTimeMillis; private String commentMarkdown; private String commentRawText; @@ -55,7 +54,7 @@ public class CommentData implements Parcelable { private boolean loadMoreChildrenFailed; public CommentData(String id, String fullName, String author, String authorFlair, - String authorFlairHTML, String linkAuthor, String commentTime, + String authorFlairHTML, String linkAuthor, long commentTimeMillis, String commentMarkdown, String commentRawText, String linkId, String subredditName, String parentId, int score, int voteType, boolean isSubmitter, String distinguished, String permalink, @@ -67,7 +66,6 @@ public class CommentData implements Parcelable { this.authorFlair = authorFlair; this.authorFlairHTML = authorFlairHTML; this.linkAuthor = linkAuthor; - this.commentTime = commentTime; this.commentTimeMillis = commentTimeMillis; this.commentMarkdown = commentMarkdown; this.commentRawText = commentRawText; @@ -105,7 +103,6 @@ public class CommentData implements Parcelable { authorFlair = in.readString(); authorFlairHTML = in.readString(); linkAuthor = in.readString(); - commentTime = in.readString(); commentTimeMillis = in.readLong(); commentMarkdown = in.readString(); commentRawText = in.readString(); @@ -159,10 +156,6 @@ public class CommentData implements Parcelable { return linkAuthor; } - public String getCommentTime() { - return commentTime; - } - public long getCommentTimeMillis() { return commentTimeMillis; } @@ -355,7 +348,6 @@ public class CommentData implements Parcelable { parcel.writeString(authorFlair); parcel.writeString(authorFlairHTML); parcel.writeString(linkAuthor); - parcel.writeString(commentTime); parcel.writeLong(commentTimeMillis); parcel.writeString(commentMarkdown); parcel.writeString(commentRawText); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeTimeFormatEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeTimeFormatEvent.java new file mode 100644 index 00000000..440e0171 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeTimeFormatEvent.java @@ -0,0 +1,9 @@ +package ml.docilealligator.infinityforreddit.Event; + +public class ChangeTimeFormatEvent { + public String timeFormat; + + public ChangeTimeFormatEvent(String timeFormat) { + this.timeFormat = timeFormat; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/CommentsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/CommentsListingFragment.java index 11faaa49..015d56eb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/CommentsListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/CommentsListingFragment.java @@ -7,7 +7,6 @@ import android.content.SharedPreferences; import android.content.res.Resources; import android.os.Build; import android.os.Bundle; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -98,9 +97,6 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni private LinearLayoutManager mLinearLayoutManager; private CommentsListingRecyclerViewAdapter mAdapter; private SortType sortType; - private boolean mShowElapsedTime; - private boolean mShowCommentDivider; - private boolean mShowAbsoluteNumberOfVotes; public CommentsListingFragment() { // Required empty public constructor @@ -132,10 +128,6 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni } } - mShowElapsedTime = mSharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_ELAPSED_TIME_KEY, false); - mShowCommentDivider = mSharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_COMMENT_DIVIDER, false); - mShowAbsoluteNumberOfVotes = mSharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_ABSOLUTE_NUMBER_OF_VOTES, true); - if (savedInstanceState == null) { getCurrentAccountAndBindView(resources); } else { @@ -167,10 +159,9 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni mLinearLayoutManager = new LinearLayoutManager(mActivity); mCommentRecyclerView.setLayoutManager(mLinearLayoutManager); - boolean voteButtonsOnTheRight = mSharedPreferences.getBoolean(SharedPreferencesUtils.VOTE_BUTTONS_ON_THE_RIGHT_KEY, false); mAdapter = new CommentsListingRecyclerViewAdapter(mActivity, mOauthRetrofit, customThemeWrapper, + getResources().getConfiguration().locale, mSharedPreferences, getArguments().getString(EXTRA_ACCESS_TOKEN), getArguments().getString(EXTRA_ACCOUNT_NAME), - voteButtonsOnTheRight, mShowElapsedTime, mShowCommentDivider, mShowAbsoluteNumberOfVotes, () -> mCommentViewModel.retryLoadingMore()); String username = getArguments().getString(EXTRA_USERNAME); @@ -197,9 +188,9 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni } mCommentViewModel = new ViewModelProvider(this, factory).get(CommentViewModel.class); - mCommentViewModel.getComments().observe(this, comments -> mAdapter.submitList(comments)); + mCommentViewModel.getComments().observe(getViewLifecycleOwner(), comments -> mAdapter.submitList(comments)); - mCommentViewModel.hasComment().observe(this, hasComment -> { + mCommentViewModel.hasComment().observe(getViewLifecycleOwner(), hasComment -> { mSwipeRefreshLayout.setRefreshing(false); if (hasComment) { mFetchCommentInfoLinearLayout.setVisibility(View.GONE); @@ -211,7 +202,7 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni } }); - mCommentViewModel.getInitialLoadingState().observe(this, networkState -> { + mCommentViewModel.getInitialLoadingState().observe(getViewLifecycleOwner(), networkState -> { if (networkState.getStatus().equals(NetworkState.Status.SUCCESS)) { mSwipeRefreshLayout.setRefreshing(false); } else if (networkState.getStatus().equals(NetworkState.Status.FAILED)) { @@ -223,7 +214,7 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni } }); - mCommentViewModel.getPaginationNetworkState().observe(this, networkState -> mAdapter.setNetworkState(networkState)); + mCommentViewModel.getPaginationNetworkState().observe(getViewLifecycleOwner(), networkState -> mAdapter.setNetworkState(networkState)); mSwipeRefreshLayout.setOnRefreshListener(() -> mCommentViewModel.refresh()); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java index 3aa0f16c..4f6a80fe 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java @@ -41,6 +41,8 @@ import com.bumptech.glide.RequestManager; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; +import java.util.Locale; + import javax.inject.Inject; import javax.inject.Named; @@ -65,6 +67,7 @@ import ml.docilealligator.infinityforreddit.Event.ChangePostLayoutEvent; import ml.docilealligator.infinityforreddit.Event.ChangeShowAbsoluteNumberOfVotesEvent; import ml.docilealligator.infinityforreddit.Event.ChangeShowElapsedTimeEvent; import ml.docilealligator.infinityforreddit.Event.ChangeSpoilerBlurEvent; +import ml.docilealligator.infinityforreddit.Event.ChangeTimeFormatEvent; import ml.docilealligator.infinityforreddit.Event.ChangeVideoAutoplayEvent; import ml.docilealligator.infinityforreddit.Event.ChangeVoteButtonsPositionEvent; import ml.docilealligator.infinityforreddit.Event.ChangeWifiStatusEvent; @@ -344,6 +347,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN); boolean nsfw = mSharedPreferences.getBoolean(SharedPreferencesUtils.NSFW_KEY, false); int defaultPostLayout = Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.DEFAULT_POST_LAYOUT_KEY, "0")); + Locale locale = getResources().getConfiguration().locale; if (postType == PostDataSource.TYPE_SEARCH) { String subredditName = getArguments().getString(EXTRA_NAME); @@ -355,7 +359,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_SEARCH_POST, defaultPostLayout); mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, - customThemeWrapper, accessToken, postType, postLayout, true, + customThemeWrapper, locale, accessToken, postType, postLayout, true, mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { @@ -417,7 +421,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, - customThemeWrapper, accessToken, postType, postLayout, displaySubredditName, + customThemeWrapper, locale, accessToken, postType, postLayout, displaySubredditName, mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { @@ -464,7 +468,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, - customThemeWrapper, accessToken, postType, postLayout, true, + customThemeWrapper, locale, accessToken, postType, postLayout, true, mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { @@ -509,7 +513,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_USER_POST_BASE + username, defaultPostLayout); mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, - customThemeWrapper, accessToken, postType, postLayout, true, + customThemeWrapper, locale, accessToken, postType, postLayout, true, mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { @@ -547,7 +551,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_FRONT_PAGE_POST, defaultPostLayout); mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, - customThemeWrapper, accessToken, postType, postLayout, true, + customThemeWrapper, locale, accessToken, postType, postLayout, true, mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { @@ -579,9 +583,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { return new PlaybackInfo(INDEX_UNSET, TIME_UNSET, volumeInfo); }); - mPostViewModel.getPosts().observe(this, posts -> mAdapter.submitList(posts)); + mPostViewModel.getPosts().observe(getViewLifecycleOwner(), posts -> mAdapter.submitList(posts)); - mPostViewModel.hasPost().observe(this, hasPost -> { + mPostViewModel.hasPost().observe(getViewLifecycleOwner(), hasPost -> { this.hasPost = hasPost; mSwipeRefreshLayout.setRefreshing(false); if (hasPost) { @@ -597,7 +601,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } }); - mPostViewModel.getInitialLoadingState().observe(this, networkState -> { + mPostViewModel.getInitialLoadingState().observe(getViewLifecycleOwner(), networkState -> { if (networkState.getStatus().equals(NetworkState.Status.SUCCESS)) { mSwipeRefreshLayout.setRefreshing(false); } else if (networkState.getStatus().equals(NetworkState.Status.FAILED)) { @@ -609,7 +613,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } }); - mPostViewModel.getPaginationNetworkState().observe(this, networkState -> mAdapter.setNetworkState(networkState)); + mPostViewModel.getPaginationNetworkState().observe(getViewLifecycleOwner(), networkState -> mAdapter.setNetworkState(networkState)); return rootView; } @@ -788,13 +792,21 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } @Subscribe - public void onChangeShowElapsedTime(ChangeShowElapsedTimeEvent event) { + public void onChangeShowElapsedTimeEvent(ChangeShowElapsedTimeEvent event) { if (mAdapter != null) { mAdapter.setShowElapsedTime(event.showElapsedTime); refreshAdapter(); } } + @Subscribe + public void onChangeTimeFormatEvent(ChangeTimeFormatEvent changeTimeFormatEvent) { + if (mAdapter != null) { + mAdapter.setTimeFormat(changeTimeFormatEvent.timeFormat); + refreshAdapter(); + } + } + @Subscribe public void onChangeVoteButtonsPositionEvent(ChangeVoteButtonsPositionEvent event) { if (mAdapter != null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java index 6990c287..272294ba 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java @@ -9,9 +9,7 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Calendar; import java.util.Locale; import ml.docilealligator.infinityforreddit.Utils.JSONUtils; @@ -171,11 +169,6 @@ public class ParseComment { boolean scoreHidden = singleCommentData.getBoolean(JSONUtils.SCORE_HIDDEN_KEY); boolean saved = singleCommentData.getBoolean(JSONUtils.SAVED_KEY); - Calendar submitTimeCalendar = Calendar.getInstance(); - submitTimeCalendar.setTimeInMillis(submitTime); - String formattedSubmitTime = new SimpleDateFormat("MMM d, yyyy, HH:mm", - locale).format(submitTimeCalendar.getTime()); - if (singleCommentData.has(JSONUtils.DEPTH_KEY)) { depth = singleCommentData.getInt(JSONUtils.DEPTH_KEY); } @@ -184,7 +177,7 @@ public class ParseComment { boolean hasReply = !(singleCommentData.get(JSONUtils.REPLIES_KEY) instanceof String); return new CommentData(id, fullName, author, authorFlair, authorFlairHTMLBuilder.toString(), - linkAuthor, formattedSubmitTime, submitTime, commentMarkdown, commentRawText, + linkAuthor, submitTime, commentMarkdown, commentRawText, linkId, subredditName, parentId, score, voteType, isSubmitter, distinguished, permalink, awardingsBuilder.toString(),depth, collapsed, hasReply, scoreHidden, saved); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/ParsePost.java index c01f6e20..fd54a613 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/ParsePost.java @@ -8,8 +8,6 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -import java.text.SimpleDateFormat; -import java.util.Calendar; import java.util.LinkedHashSet; import java.util.Locale; @@ -107,10 +105,6 @@ public class ParsePost { score -= voteType; } - Calendar postTimeCalendar = Calendar.getInstance(); - postTimeCalendar.setTimeInMillis(postTime); - String formattedPostTime = new SimpleDateFormat("MMM d, yyyy, HH:mm", - locale).format(postTimeCalendar.getTime()); String permalink = Html.fromHtml(data.getString(JSONUtils.PERMALINK_KEY)).toString(); String previewUrl = ""; @@ -137,7 +131,7 @@ public class ParsePost { data = data.getJSONArray(JSONUtils.CROSSPOST_PARENT_LIST).getJSONObject(0); Post crosspostParent = parseBasicData(data, locale); Post post = parseData(data, permalink, id, fullName, subredditName, subredditNamePrefixed, - author, authorFlair, authorFlairHTMLBuilder.toString(), formattedPostTime, + author, authorFlair, authorFlairHTMLBuilder.toString(), postTime, title, previewUrl, thumbnailPreviewUrl, previewWidth, previewHeight, score, voteType, nComments, flair, awardingsBuilder.toString(), nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, true); @@ -145,7 +139,7 @@ public class ParsePost { return post; } else { return parseData(data, permalink, id, fullName, subredditName, subredditNamePrefixed, - author, authorFlair, authorFlairHTMLBuilder.toString(), formattedPostTime, + author, authorFlair, authorFlairHTMLBuilder.toString(), postTime, title, previewUrl, thumbnailPreviewUrl, previewWidth, previewHeight, score, voteType, nComments, flair, awardingsBuilder.toString(), nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, false); @@ -154,7 +148,7 @@ public class ParsePost { private static Post parseData(JSONObject data, String permalink, String id, String fullName, String subredditName, String subredditNamePrefixed, String author, - String authorFlair, String authorFlairHTML, String formattedPostTime, + String authorFlair, String authorFlairHTML, long postTimeMillis, String title, String previewUrl, String thumbnailPreviewUrl, int previewWidth, int previewHeight, int score, int voteType, int nComments, String flair, @@ -171,7 +165,7 @@ public class ParsePost { //Text post int postType = Post.TEXT_TYPE; post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, - authorFlair, authorFlairHTML, formattedPostTime, postTimeMillis, + authorFlair, authorFlairHTML, postTimeMillis, title, permalink, score, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); if (data.isNull(JSONUtils.SELFTEXT_KEY)) { @@ -196,7 +190,7 @@ public class ParsePost { int postType = Post.IMAGE_TYPE; post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, - authorFlair, authorFlairHTML, formattedPostTime, postTimeMillis, title, + authorFlair, authorFlairHTML, postTimeMillis, title, url, thumbnailPreviewUrl, url, permalink, score, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); @@ -207,7 +201,7 @@ public class ParsePost { //No preview link post int postType = Post.NO_PREVIEW_LINK_TYPE; post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, - authorFlair, authorFlairHTML, formattedPostTime, postTimeMillis, + authorFlair, authorFlairHTML, postTimeMillis, title, previewUrl, thumbnailPreviewUrl, url, permalink, score, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); @@ -232,7 +226,7 @@ public class ParsePost { String videoDownloadUrl = redditVideoObject.getString(JSONUtils.FALLBACK_URL_KEY); post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, - authorFlair, authorFlairHTML, formattedPostTime, postTimeMillis, title, + authorFlair, authorFlairHTML, postTimeMillis, title, previewUrl, thumbnailPreviewUrl, permalink, score, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); @@ -251,7 +245,7 @@ public class ParsePost { .getJSONObject(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY).getString(JSONUtils.FALLBACK_URL_KEY); post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, - authorFlair, authorFlairHTML, formattedPostTime, postTimeMillis, title, + authorFlair, authorFlairHTML, postTimeMillis, title, previewUrl, thumbnailPreviewUrl, permalink, score, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); @@ -265,7 +259,7 @@ public class ParsePost { int postType = Post.IMAGE_TYPE; post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, - authorFlair, authorFlairHTML, formattedPostTime, postTimeMillis, + authorFlair, authorFlairHTML, postTimeMillis, title, url, thumbnailPreviewUrl, url, permalink, score, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); @@ -276,7 +270,7 @@ public class ParsePost { //Gif post int postType = Post.GIF_TYPE; post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, - authorFlair, authorFlairHTML, formattedPostTime, postTimeMillis, + authorFlair, authorFlairHTML, postTimeMillis, title, previewUrl, thumbnailPreviewUrl, url, permalink, score, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); @@ -289,7 +283,7 @@ public class ParsePost { int postType = Post.VIDEO_TYPE; post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, - authorFlair, authorFlairHTML, formattedPostTime, postTimeMillis, title, + authorFlair, authorFlairHTML, postTimeMillis, title, previewUrl, thumbnailPreviewUrl, url, permalink, score, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); @@ -303,7 +297,7 @@ public class ParsePost { int postType = Post.TEXT_TYPE; post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, - authorFlair, authorFlairHTML, formattedPostTime, postTimeMillis, + authorFlair, authorFlairHTML, postTimeMillis, title, permalink, score, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); @@ -332,7 +326,7 @@ public class ParsePost { int postType = Post.LINK_TYPE; post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, - authorFlair, authorFlairHTML, formattedPostTime, postTimeMillis, + authorFlair, authorFlairHTML, postTimeMillis, title, previewUrl, thumbnailPreviewUrl, url, permalink, score, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); @@ -353,7 +347,7 @@ public class ParsePost { int postType = Post.IMAGE_TYPE; post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, - authorFlair, authorFlairHTML, formattedPostTime, postTimeMillis, title, + authorFlair, authorFlairHTML, postTimeMillis, title, previewUrl, thumbnailPreviewUrl, url, permalink, score, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); @@ -364,7 +358,7 @@ public class ParsePost { int postType = Post.VIDEO_TYPE; post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, - authorFlair, authorFlairHTML, formattedPostTime, postTimeMillis, title, + authorFlair, authorFlairHTML, postTimeMillis, title, previewUrl, thumbnailPreviewUrl, url, permalink, score, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); @@ -377,7 +371,7 @@ public class ParsePost { int postType = Post.NO_PREVIEW_LINK_TYPE; post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, - authorFlair, authorFlairHTML, formattedPostTime, postTimeMillis, title, + authorFlair, authorFlairHTML, postTimeMillis, title, url, thumbnailPreviewUrl, url, permalink, score, postType, voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java index 3a345572..9cea2b6a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java @@ -40,7 +40,6 @@ public class Post implements Parcelable { private String authorIconUrl; private String authorFlair; private String authorFlairHTML; - private String postTime; private String title; private String selfText; private String selfTextPlain; @@ -72,7 +71,7 @@ public class Post implements Parcelable { private String crosspostParentId; public Post(String id, String fullName, String subredditName, String subredditNamePrefixed, - String author, String authorFlair, String authorFlairHTML, String postTime, + String author, String authorFlair, String authorFlairHTML, long postTimeMillis, String title, String previewUrl, String thumbnailPreviewUrl, String permalink, int score, int postType, int voteType, int nComments, String flair, String awards, int nAwards, boolean hidden, boolean spoiler, boolean nsfw, boolean stickied, @@ -85,7 +84,6 @@ public class Post implements Parcelable { this.authorNamePrefixed = "u/" + author; this.authorFlair = authorFlair; this.authorFlairHTML = authorFlairHTML; - this.postTime = postTime; this.postTimeMillis = postTimeMillis; this.title = title; this.previewUrl = previewUrl; @@ -109,7 +107,7 @@ public class Post implements Parcelable { } public Post(String id, String fullName, String subredditName, String subredditNamePrefixed, - String author, String authorFlair, String authorFlairHTML, String postTime, + String author, String authorFlair, String authorFlairHTML, long postTimeMillis, String title, String previewUrl, String thumbnailPreviewUrl, String url, String permalink, int score, int postType, int voteType, int nComments, String flair, String awards, int nAwards, boolean hidden, boolean spoiler, boolean nsfw, @@ -122,7 +120,6 @@ public class Post implements Parcelable { this.authorNamePrefixed = "u/" + author; this.authorFlair = authorFlair; this.authorFlairHTML = authorFlairHTML; - this.postTime = postTime; this.postTimeMillis = postTimeMillis; this.title = title; this.previewUrl = previewUrl; @@ -147,7 +144,7 @@ public class Post implements Parcelable { } public Post(String id, String fullName, String subredditName, String subredditNamePrefixed, - String author, String authorFlair, String authorFlairHTML, String postTime, + String author, String authorFlair, String authorFlairHTML, long postTimeMillis, String title, String permalink, int score, int postType, int voteType, int nComments, String flair, String awards, int nAwards, boolean hidden, boolean spoiler, boolean nsfw, boolean stickied, boolean archived, boolean locked, @@ -160,7 +157,6 @@ public class Post implements Parcelable { this.authorNamePrefixed = "u/" + author; this.authorFlair = authorFlair; this.authorFlairHTML = authorFlairHTML; - this.postTime = postTime; this.postTimeMillis = postTimeMillis; this.title = title; this.permalink = APIUtils.API_BASE_URI + permalink; @@ -192,7 +188,6 @@ public class Post implements Parcelable { authorFlair = in.readString(); authorFlairHTML = in.readString(); authorIconUrl = in.readString(); - postTime = in.readString(); postTimeMillis = in.readLong(); title = in.readString(); selfText = in.readString(); @@ -277,10 +272,6 @@ public class Post implements Parcelable { this.authorIconUrl = authorIconUrl; } - public String getPostTime() { - return postTime; - } - public long getPostTimeMillis() { return postTimeMillis; } @@ -494,7 +485,6 @@ public class Post implements Parcelable { parcel.writeString(authorFlair); parcel.writeString(authorFlairHTML); parcel.writeString(authorIconUrl); - parcel.writeString(postTime); parcel.writeLong(postTimeMillis); parcel.writeString(title); parcel.writeString(selfText); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/InterfacePreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/InterfacePreferenceFragment.java index 85ba2a5e..856a3579 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/InterfacePreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/InterfacePreferenceFragment.java @@ -11,7 +11,6 @@ import org.greenrobot.eventbus.EventBus; import ml.docilealligator.infinityforreddit.Event.ChangeDefaultPostLayoutEvent; import ml.docilealligator.infinityforreddit.Event.ChangeShowAbsoluteNumberOfVotesEvent; -import ml.docilealligator.infinityforreddit.Event.ChangeShowElapsedTimeEvent; import ml.docilealligator.infinityforreddit.Event.ChangeVoteButtonsPositionEvent; import ml.docilealligator.infinityforreddit.Event.RecreateActivityEvent; import ml.docilealligator.infinityforreddit.Event.ShowDividerInCompactLayoutPreferenceEvent; @@ -27,7 +26,6 @@ public class InterfacePreferenceFragment extends PreferenceFragmentCompat { SwitchPreference bottomAppBarSwitch = findPreference(SharedPreferencesUtils.BOTTOM_APP_BAR_KEY); SwitchPreference voteButtonsOnTheRightSwitch = findPreference(SharedPreferencesUtils.VOTE_BUTTONS_ON_THE_RIGHT_KEY); - SwitchPreference showElapsedTimeSwitch = findPreference(SharedPreferencesUtils.SHOW_ELAPSED_TIME_KEY); ListPreference defaultPostLayoutSwitch = findPreference(SharedPreferencesUtils.DEFAULT_POST_LAYOUT_KEY); SwitchPreference showDividerInCompactLayout = findPreference(SharedPreferencesUtils.SHOW_DIVIDER_IN_COMPACT_LAYOUT); SwitchPreference showThumbnailOnTheRightInCompactLayout = findPreference(SharedPreferencesUtils.SHOW_THUMBNAIL_ON_THE_RIGHT_IN_COMPACT_LAYOUT); @@ -47,13 +45,6 @@ public class InterfacePreferenceFragment extends PreferenceFragmentCompat { }); } - if (showElapsedTimeSwitch != null) { - showElapsedTimeSwitch.setOnPreferenceChangeListener((preference, newValue) -> { - EventBus.getDefault().post(new ChangeShowElapsedTimeEvent((Boolean) newValue)); - return true; - }); - } - if (defaultPostLayoutSwitch != null) { defaultPostLayoutSwitch.setOnPreferenceChangeListener((preference, newValue) -> { EventBus.getDefault().post(new ChangeDefaultPostLayoutEvent(Integer.parseInt((String) newValue))); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/TimeFormatPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/TimeFormatPreferenceFragment.java new file mode 100644 index 00000000..f0adfe12 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/TimeFormatPreferenceFragment.java @@ -0,0 +1,38 @@ +package ml.docilealligator.infinityforreddit.Settings; + +import android.os.Bundle; + +import androidx.preference.ListPreference; +import androidx.preference.PreferenceFragmentCompat; +import androidx.preference.SwitchPreference; + +import org.greenrobot.eventbus.EventBus; + +import ml.docilealligator.infinityforreddit.Event.ChangeShowElapsedTimeEvent; +import ml.docilealligator.infinityforreddit.Event.ChangeTimeFormatEvent; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; + +public class TimeFormatPreferenceFragment extends PreferenceFragmentCompat { + @Override + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + setPreferencesFromResource(R.xml.time_format_preferences, rootKey); + + SwitchPreference showElapsedTimeSwitch = findPreference(SharedPreferencesUtils.SHOW_ELAPSED_TIME_KEY); + ListPreference timeFormatList = findPreference(SharedPreferencesUtils.TIME_FORMAT_KEY); + + if (showElapsedTimeSwitch != null) { + showElapsedTimeSwitch.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeShowElapsedTimeEvent((Boolean) newValue)); + return true; + }); + } + + if (timeFormatList != null) { + timeFormatList.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeTimeFormatEvent((String) newValue)); + return true; + }); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java index ee25195d..03c64165 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java @@ -74,6 +74,8 @@ public class SharedPreferencesUtils { public static final String PULL_NOTIFICATION_TIME = "pull_notification_time"; public static final String SHOW_ELAPSED_TIME_KEY = "show_elapsed_time"; + public static final String TIME_FORMAT_KEY = "time_format"; + public static final String TIME_FORMAT_DEFAULT_VALUE = "MMM d, yyyy, HH:mm"; public static final String DEFAULT_POST_LAYOUT_KEY = "default_post_layout"; public static final String SHOW_DIVIDER_IN_COMPACT_LAYOUT = "show_divider_in_compact_layout"; public static final String SHOW_THUMBNAIL_ON_THE_RIGHT_IN_COMPACT_LAYOUT = "show_thumbnail_on_the_right_in_compact_layout"; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/Utils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/Utils.java index 52a1972a..d5d7530f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/Utils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/Utils.java @@ -13,6 +13,8 @@ import android.widget.TextView; import androidx.appcompat.widget.Toolbar; +import java.text.SimpleDateFormat; +import java.util.Calendar; import java.util.Locale; import ml.docilealligator.infinityforreddit.R; @@ -46,6 +48,12 @@ public class Utils { return source.subSequence(0, i+1); } + public static String getFormattedTime(Locale locale, long time, String pattern) { + Calendar postTimeCalendar = Calendar.getInstance(); + postTimeCalendar.setTimeInMillis(time); + return new SimpleDateFormat(pattern, locale).format(postTimeCalendar.getTime()); + } + public static String getElapsedTime(Context context, long time) { long now = System.currentTimeMillis(); long diff = now - time; diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 3efd7c75..2d8cdce0 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -168,4 +168,54 @@ 4 5 + + + Jan 2, 2020, 01:00 + Jan 2, 2020, 01:00 PM + 2 Jan, 2020, 01:00 + 2 Jan, 2020, 01:00 PM + 1/2/2020 01:00 (Month first) + 1/2/2020 01:00 PM (Month first) + 2/1/2020 01:00 (Day first) + 2/1/2020 01:00 PM (Day first) + 2020/1/2 01:00 (Month first) + 2020/1/2 01:00 PM (Month first) + 1-2-2020 01:00 (Month first) + 1-2-2020 01:00 PM (Month first) + 2-1-2020 01:00 (Day first) + 2-1-2020 01:00 PM (Day first) + 2020-1-2 01:00 (Month first) + 2020-1-2 01:00 PM (Month first) + 1.2.2020 01:00 (Month first) + 1.2.2020 01:00 PM (Month first) + 2.1.2020 01:00 (Day first) + 2.1.2020 01:00 PM (Day first) + 2020.1.2 01:00 (Month first) + 2020.1.2 01:00 PM (Month first) + + + + MMM d, yyyy, HH:mm + MMM d, yyyy, hh:mm a + d MMM yyyy, HH:mm + d MMM yyyy, hh:mm a + M/d/yyyy HH:mm + M/d/yyyy hh:mm a + d/M/yyyy HH:mm + d/M/yyyy hh:mm a + yyyy/M/d HH:mm + yyyy/M/d hh:mm a + M-d-yyyy HH:mm + M-d-yyyy hh:mm a + d-M-yyyy HH:mm + d-M-yyyy hh:mm a + yyyy-M-d HH:mm + yyyy-M-d hh:mm a + M.d.yyyy HH:mm + M.d.yyyy hh:mm a + d.M.yyyy HH:mm + d.M.yyyy hh:mm a + yyyy.M.d HH:mm + yyyy.M.d hh:mm a + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5d78d9d8..8c874679 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -355,6 +355,7 @@ Comment Toolbar Hidden by Default Show Absolute Number of Votes Show Elapsed Time in Posts and Comments + Time Format Default Post Layout Show Divider in Compact Layout Show Thumbnail on the Right in Compact Layout diff --git a/app/src/main/res/xml/interface_preference.xml b/app/src/main/res/xml/interface_preference.xml index d2525eb6..900ee84d 100644 --- a/app/src/main/res/xml/interface_preference.xml +++ b/app/src/main/res/xml/interface_preference.xml @@ -27,10 +27,9 @@ app:key="vote_buttons_on_the_right" app:title="@string/settings_vote_buttons_on_the_right_title" /> - + + + + + + \ No newline at end of file