diff options
author | Alex Ning <chineseperson5@gmail.com> | 2020-09-29 14:19:54 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2020-09-29 14:19:54 +0000 |
commit | 6e4519ef93f295178e0b850b739bd0085de6aa17 (patch) | |
tree | 882925d4680567193e06472e4c40e856f6c43acc /app/src/main | |
parent | 8dd4759035bae9cff7a0d8bd0af1d1624193c796 (diff) | |
download | infinity-for-reddit-6e4519ef93f295178e0b850b739bd0085de6aa17.tar infinity-for-reddit-6e4519ef93f295178e0b850b739bd0085de6aa17.tar.gz infinity-for-reddit-6e4519ef93f295178e0b850b739bd0085de6aa17.tar.bz2 infinity-for-reddit-6e4519ef93f295178e0b850b739bd0085de6aa17.tar.lz infinity-for-reddit-6e4519ef93f295178e0b850b739bd0085de6aa17.tar.xz infinity-for-reddit-6e4519ef93f295178e0b850b739bd0085de6aa17.tar.zst infinity-for-reddit-6e4519ef93f295178e0b850b739bd0085de6aa17.zip |
Display search history in SearchActivity. Save suggested sort type and nsfw info in database for SubredditData. Fix an issue in parsing posts.
Diffstat (limited to '')
21 files changed, 512 insertions, 71 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchActivity.java index 78ab544c..cd1989e2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchActivity.java @@ -17,6 +17,9 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.widget.Toolbar; import androidx.coordinatorlayout.widget.CoordinatorLayout; +import androidx.lifecycle.ViewModelProvider; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import com.ferfalk.simplesearchview.SimpleSearchView; import com.google.android.material.appbar.AppBarLayout; @@ -30,10 +33,14 @@ import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.Adapter.SearchActivityRecyclerViewAdapter; +import ml.docilealligator.infinityforreddit.AsyncTask.GetCurrentAccountAsyncTask; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.Event.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RecentSearchQuery.RecentSearchQueryViewModel; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; public class SearchActivity extends BaseActivity { @@ -45,6 +52,8 @@ public class SearchActivity extends BaseActivity { static final String EXTRA_RETURN_SUBREDDIT_NAME = "ERSN"; static final String EXTRA_RETURN_SUBREDDIT_ICON_URL = "ERSIURL"; + private static final String NULL_ACCOUNT_NAME_STATE = "NANS"; + private static final String ACCOUNT_NAME_STATE = "ANS"; private static final String SUBREDDIT_NAME_STATE = "SNS"; private static final String SUBREDDIT_IS_USER_STATE = "SIUS"; @@ -65,14 +74,27 @@ public class SearchActivity extends BaseActivity { TextView searchInTextView; @BindView(R.id.subreddit_name_text_view_search_activity) TextView subredditNameTextView; + @BindView(R.id.divider_search_activity) + View divider; + @BindView(R.id.recent_summary_text_view_search_activity) + TextView recentSummaryTextView; + @BindView(R.id.recycler_view_search_activity) + RecyclerView recyclerView; + @Inject + RedditDataRoomDatabase mRedditDataRoomDatabase; @Inject @Named("default") SharedPreferences mSharedPreferences; @Inject CustomThemeWrapper mCustomThemeWrapper; + private boolean mNullAccountName = false; + private String mAccountName; private String query; private String subredditName; private boolean subredditIsUser; + private boolean searchOnlySubreddits; + private SearchActivityRecyclerViewAdapter adapter; + RecentSearchQueryViewModel mRecentSearchQueryViewModel; @Override protected void onCreate(Bundle savedInstanceState) { @@ -96,7 +118,7 @@ public class SearchActivity extends BaseActivity { setSupportActionBar(toolbar); - boolean searchOnlySubreddits = getIntent().getBooleanExtra(EXTRA_SEARCH_ONLY_SUBREDDITS, false); + searchOnlySubreddits = getIntent().getBooleanExtra(EXTRA_SEARCH_ONLY_SUBREDDITS, false); simpleSearchView.setOnSearchViewListener(new SimpleSearchView.SearchViewListener() { @Override @@ -123,23 +145,7 @@ public class SearchActivity extends BaseActivity { simpleSearchView.setOnQueryTextListener(new SimpleSearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { - if (searchOnlySubreddits) { - Intent intent = new Intent(SearchActivity.this, SearchSubredditsResultActivity.class); - intent.putExtra(SearchSubredditsResultActivity.EXTRA_QUERY, query); - startActivityForResult(intent, SUBREDDIT_SEARCH_REQUEST_CODE); - } else { - Intent intent = new Intent(SearchActivity.this, SearchResultActivity.class); - intent.putExtra(SearchResultActivity.EXTRA_QUERY, query); - if (subredditName != null) { - if (subredditIsUser) { - intent.putExtra(SearchResultActivity.EXTRA_SUBREDDIT_NAME, "u_" + subredditName); - } else { - intent.putExtra(SearchResultActivity.EXTRA_SUBREDDIT_NAME, subredditName); - } - } - startActivity(intent); - finish(); - } + search(query); return true; } @@ -155,6 +161,8 @@ public class SearchActivity extends BaseActivity { }); if (savedInstanceState != null) { + mNullAccountName = savedInstanceState.getBoolean(NULL_ACCOUNT_NAME_STATE); + mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE); subredditIsUser = savedInstanceState.getBoolean(SUBREDDIT_IS_USER_STATE); @@ -163,8 +171,15 @@ public class SearchActivity extends BaseActivity { } else { subredditNameTextView.setText(subredditName); } + + if (!mNullAccountName && mAccountName == null) { + getCurrentAccountAndInitializeViewPager(); + } else { + bindView(); + } } else { query = getIntent().getStringExtra(EXTRA_QUERY); + getCurrentAccountAndInitializeViewPager(); } if (searchOnlySubreddits) { @@ -185,6 +200,60 @@ public class SearchActivity extends BaseActivity { } } + private void getCurrentAccountAndInitializeViewPager() { + new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { + if (account == null) { + mNullAccountName = true; + } else { + mAccountName = account.getUsername(); + } + bindView(); + }).execute(); + } + + private void bindView() { + if (mAccountName != null) { + adapter = new SearchActivityRecyclerViewAdapter(this, mCustomThemeWrapper, this::search); + recyclerView.setNestedScrollingEnabled(false); + recyclerView.setLayoutManager(new LinearLayoutManager(this)); + recyclerView.setAdapter(adapter); + mRecentSearchQueryViewModel = new ViewModelProvider(this, + new RecentSearchQueryViewModel.Factory(mRedditDataRoomDatabase, mAccountName)) + .get(RecentSearchQueryViewModel.class); + + mRecentSearchQueryViewModel.getAllRecentSearchQueries().observe(this, recentSearchQueries -> { + if (recentSearchQueries != null && !recentSearchQueries.isEmpty()) { + divider.setVisibility(View.VISIBLE); + recentSummaryTextView.setVisibility(View.VISIBLE); + } else { + divider.setVisibility(View.GONE); + recentSummaryTextView.setVisibility(View.GONE); + } + adapter.setRecentSearchQueries(recentSearchQueries); + }); + } + } + + private void search(String query) { + if (searchOnlySubreddits) { + Intent intent = new Intent(SearchActivity.this, SearchSubredditsResultActivity.class); + intent.putExtra(SearchSubredditsResultActivity.EXTRA_QUERY, query); + startActivityForResult(intent, SUBREDDIT_SEARCH_REQUEST_CODE); + } else { + Intent intent = new Intent(SearchActivity.this, SearchResultActivity.class); + intent.putExtra(SearchResultActivity.EXTRA_QUERY, query); + if (subredditName != null) { + if (subredditIsUser) { + intent.putExtra(SearchResultActivity.EXTRA_SUBREDDIT_NAME, "u_" + subredditName); + } else { + intent.putExtra(SearchResultActivity.EXTRA_SUBREDDIT_NAME, subredditName); + } + } + startActivity(intent); + finish(); + } + } + @Override public SharedPreferences getDefaultSharedPreferences() { return mSharedPreferences; @@ -205,8 +274,11 @@ public class SearchActivity extends BaseActivity { simpleSearchView.setTextColor(toolbarPrimaryTextAndIconColorColor); simpleSearchView.setBackIconColor(toolbarPrimaryTextAndIconColorColor); simpleSearchView.setHintTextColor(mCustomThemeWrapper.getToolbarPrimaryTextAndIconColor()); - searchInTextView.setTextColor(mCustomThemeWrapper.getColorAccent()); + int colorAccent = mCustomThemeWrapper.getColorAccent(); + searchInTextView.setTextColor(colorAccent); subredditNameTextView.setTextColor(mCustomThemeWrapper.getPrimaryTextColor()); + divider.setBackgroundColor(mCustomThemeWrapper.getDividerColor()); + recentSummaryTextView.setTextColor(colorAccent); } @Override @@ -288,6 +360,8 @@ public class SearchActivity extends BaseActivity { @Override public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); + outState.putBoolean(NULL_ACCOUNT_NAME_STATE, mNullAccountName); + outState.putString(ACCOUNT_NAME_STATE, mAccountName); outState.putString(SUBREDDIT_NAME_STATE, subredditName); outState.putBoolean(SUBREDDIT_IS_USER_STATE, subredditIsUser); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchResultActivity.java index a246bae1..02c2c73a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchResultActivity.java @@ -51,6 +51,7 @@ import ml.docilealligator.infinityforreddit.FragmentCommunicator; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.Post.PostDataSource; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RecentSearchQuery.InsertRecentSearchQuery; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; @@ -65,6 +66,7 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; private static final String ACCESS_TOKEN_STATE = "ATS"; private static final String ACCOUNT_NAME_STATE = "ANS"; + private static final String INSERT_SEARCH_QUERY_SUCCESS_STATE = "ISQSS"; @BindView(R.id.coordinator_layout_search_result_activity) CoordinatorLayout coordinatorLayout; @BindView(R.id.appbar_layout_search_result_activity) @@ -93,6 +95,7 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect private String mAccountName; private String mQuery; private String mSubredditName; + private boolean mInsertSearchQuerySuccess; private FragmentManager fragmentManager; private SectionsPagerAdapter sectionsPagerAdapter; private SearchPostSortTypeBottomSheetFragment searchPostSortTypeBottomSheetFragment; @@ -142,6 +145,17 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect getSupportActionBar().setDisplayHomeAsUpEnabled(true); setToolbarGoToTop(toolbar); + // Get the intent, verify the action and get the query + Intent intent = getIntent(); + String query = intent.getStringExtra(EXTRA_QUERY); + + mSubredditName = intent.getStringExtra(EXTRA_SUBREDDIT_NAME); + + if (query != null) { + mQuery = query; + setTitle(query); + } + fragmentManager = getSupportFragmentManager(); if (savedInstanceState == null) { @@ -150,6 +164,7 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); + mInsertSearchQuerySuccess = savedInstanceState.getBoolean(INSERT_SEARCH_QUERY_SUCCESS_STATE); if (!mNullAccessToken && mAccessToken == null) { getCurrentAccountAndInitializeViewPager(); } else { @@ -166,17 +181,6 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect searchUserAndSubredditSortTypeBottomSheetFragment = new SearchUserAndSubredditSortTypeBottomSheetFragment(); postLayoutBottomSheetFragment = new PostLayoutBottomSheetFragment(); - - // Get the intent, verify the action and get the query - Intent intent = getIntent(); - String query = intent.getStringExtra(EXTRA_QUERY); - - mSubredditName = intent.getStringExtra(EXTRA_SUBREDDIT_NAME); - - if (query != null) { - mQuery = query; - setTitle(query); - } } @Override @@ -245,6 +249,10 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect break; } }).attach(); + if (mAccountName != null && !mInsertSearchQuerySuccess && mQuery != null) { + InsertRecentSearchQuery.insertRecentSearchQueryListener(mRedditDataRoomDatabase, mAccountName, + mQuery, () -> mInsertSearchQuerySuccess = true); + } } @Override @@ -300,6 +308,7 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); outState.putString(ACCESS_TOKEN_STATE, mAccessToken); outState.putString(ACCOUNT_NAME_STATE, mAccountName); + outState.putBoolean(INSERT_SEARCH_QUERY_SUCCESS_STATE, mInsertSearchQuerySuccess); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MarkdownBottomBarRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MarkdownBottomBarRecyclerViewAdapter.java index 3d4b1788..73d149b1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MarkdownBottomBarRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MarkdownBottomBarRecyclerViewAdapter.java @@ -65,7 +65,7 @@ public class MarkdownBottomBarRecyclerViewAdapter extends RecyclerView.Adapter<R ((MarkdownBottomBarItemViewHolder) holder).imageView.setImageResource(R.drawable.ic_strikethrough_black_24dp); break; case HEADER: - ((MarkdownBottomBarItemViewHolder) holder).imageView.setImageResource(R.drawable.ic_header_hashtag_black_24dp); + ((MarkdownBottomBarItemViewHolder) holder).imageView.setImageResource(R.drawable.ic_title_24dp); break; case ORDERED_LIST: ((MarkdownBottomBarItemViewHolder) holder).imageView.setImageResource(R.drawable.ic_ordered_list_black_24dp); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SearchActivityRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SearchActivityRecyclerViewAdapter.java new file mode 100644 index 00000000..08a5fc21 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SearchActivityRecyclerViewAdapter.java @@ -0,0 +1,83 @@ +package ml.docilealligator.infinityforreddit.Adapter; + +import android.app.Activity; +import android.graphics.drawable.Drawable; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import java.util.List; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RecentSearchQuery.RecentSearchQuery; +import ml.docilealligator.infinityforreddit.Utils.Utils; + +public class SearchActivityRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { + private List<RecentSearchQuery> recentSearchQueries; + private int primaryTextColor; + private Drawable historyIcon; + private ItemOnClickListener itemOnClickListener; + + public interface ItemOnClickListener { + void onClick(String query); + } + + public SearchActivityRecyclerViewAdapter(Activity activity, CustomThemeWrapper customThemeWrapper, + ItemOnClickListener itemOnClickListener) { + this.primaryTextColor = customThemeWrapper.getPrimaryTextColor(); + this.historyIcon = Utils.getTintedDrawable(activity, R.drawable.ic_history_24dp, customThemeWrapper.getPrimaryIconColor()); + this.itemOnClickListener = itemOnClickListener; + } + + @NonNull + @Override + public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + return new RecentSearchQueryViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recent_search_query, parent, false)); + } + + @Override + public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { + if (holder instanceof RecentSearchQueryViewHolder) { + if (recentSearchQueries != null && !recentSearchQueries.isEmpty() && position < recentSearchQueries.size()) { + ((RecentSearchQueryViewHolder) holder).recentSearchQueryTextView.setText(recentSearchQueries.get(position).getSearchQuery()); + } + } + } + + @Override + public int getItemCount() { + return recentSearchQueries == null ? 0 : recentSearchQueries.size(); + } + + public void setRecentSearchQueries(List<RecentSearchQuery> recentSearchQueries) { + this.recentSearchQueries = recentSearchQueries; + notifyDataSetChanged(); + } + + class RecentSearchQueryViewHolder extends RecyclerView.ViewHolder { + @BindView(R.id.recent_search_query_text_view_item_recent_search_query) + TextView recentSearchQueryTextView; + + public RecentSearchQueryViewHolder(@NonNull View itemView) { + super(itemView); + + ButterKnife.bind(this, itemView); + + recentSearchQueryTextView.setTextColor(primaryTextColor); + recentSearchQueryTextView.setCompoundDrawablesWithIntrinsicBounds(historyIcon, null, null, null); + + itemView.setOnClickListener(view -> { + if (recentSearchQueries != null && !recentSearchQueries.isEmpty()) { + itemOnClickListener.onClick(recentSearchQueries.get(getAdapterPosition()).getSearchQuery()); + } + }); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java index c8666fa7..40f40146 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java @@ -100,9 +100,12 @@ class ParseSubscribedThing { String sidebarDescription = data.getString(JSONUtils.DESCRIPTION_KEY); int nSubscribers = data.getInt(JSONUtils.SUBSCRIBERS_KEY); long createdUTC = data.getLong(JSONUtils.CREATED_UTC_KEY); + String suggestedCommentSort = data.getString(JSONUtils.SUGGESTED_COMMENT_SORT_KEY); + boolean isNSFW = data.getBoolean(JSONUtils.OVER18_KEY); newSubscribedSubredditData.add(new SubscribedSubredditData(id, name, iconUrl, accountName, isFavorite)); newSubredditData.add(new SubredditData(id, subredditFullName, iconUrl, - bannerImageUrl, description, sidebarDescription, nSubscribers, createdUTC)); + bannerImageUrl, description, sidebarDescription, nSubscribers, createdUTC, + suggestedCommentSort, isNSFW)); } } lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); 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 de812011..2f6b4a1a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/ParsePost.java @@ -118,7 +118,7 @@ public class ParsePost { JSONArray thumbnailPreviews = images.getJSONArray(JSONUtils.RESOLUTIONS_KEY); for (int i = 0; i < thumbnailPreviews.length(); i++) { - JSONObject thumbnailPreview = images.getJSONArray(JSONUtils.RESOLUTIONS_KEY).getJSONObject(2); + JSONObject thumbnailPreview = thumbnailPreviews.getJSONObject(i); String thumbnailPreviewUrl = thumbnailPreview.getString(JSONUtils.URL_KEY); int thumbnailPreviewWidth = thumbnailPreview.getInt(JSONUtils.WIDTH_KEY); int thumbnailPreviewHeight = thumbnailPreview.getInt(JSONUtils.HEIGHT_KEY); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RecentSearchQuery/InsertRecentSearchQuery.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RecentSearchQuery/InsertRecentSearchQuery.java new file mode 100644 index 00000000..19278a70 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RecentSearchQuery/InsertRecentSearchQuery.java @@ -0,0 +1,53 @@ +package ml.docilealligator.infinityforreddit.RecentSearchQuery; + +import android.os.AsyncTask; + +import java.util.List; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class InsertRecentSearchQuery { + public interface InsertRecentSearchQueryListener { + void success(); + } + + public static void insertRecentSearchQueryListener(RedditDataRoomDatabase redditDataRoomDatabase, String username, + String recentSearchQuery, InsertRecentSearchQueryListener insertRecentSearchQueryListener) { + new InsertRecentSearchQueryAsyncTask(redditDataRoomDatabase, username, recentSearchQuery, insertRecentSearchQueryListener).execute(); + } + + private static class InsertRecentSearchQueryAsyncTask extends AsyncTask<Void, Void, Void> { + + private RecentSearchQueryDao recentSearchQueryDao; + private String username; + private String recentSearchQuery; + private InsertRecentSearchQueryListener insertRecentSearchQueryListener; + + public InsertRecentSearchQueryAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, String username, + String recentSearchQuery, InsertRecentSearchQueryListener insertRecentSearchQueryListener) { + this.recentSearchQueryDao = redditDataRoomDatabase.recentSearchQueryDao(); + this.username = username; + this.recentSearchQuery = recentSearchQuery; + this.insertRecentSearchQueryListener = insertRecentSearchQueryListener; + } + + @Override + protected Void doInBackground(Void... voids) { + List<RecentSearchQuery> recentSearchQueries = recentSearchQueryDao.getAllRecentSearchQueries(username); + if (recentSearchQueries.size() >= 5) { + for (int i = 4; i < recentSearchQueries.size(); i++) { + recentSearchQueryDao.deleteRecentSearchQueries(recentSearchQueries.get(i)); + } + } + + recentSearchQueryDao.insert(new RecentSearchQuery(username, recentSearchQuery)); + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + insertRecentSearchQueryListener.success(); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RecentSearchQuery/RecentSearchQuery.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RecentSearchQuery/RecentSearchQuery.java new file mode 100644 index 00000000..70c9ed5a --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RecentSearchQuery/RecentSearchQuery.java @@ -0,0 +1,43 @@ +package ml.docilealligator.infinityforreddit.RecentSearchQuery; + +import androidx.annotation.NonNull; +import androidx.room.ColumnInfo; +import androidx.room.Entity; +import androidx.room.ForeignKey; + +import ml.docilealligator.infinityforreddit.Account.Account; + +@Entity(tableName = "recent_search_queries", primaryKeys = {"username", "search_query"}, + foreignKeys = @ForeignKey(entity = Account.class, parentColumns = "username", + childColumns = "username", onDelete = ForeignKey.CASCADE)) +public class RecentSearchQuery { + @NonNull + @ColumnInfo(name = "username") + private String username; + @NonNull + @ColumnInfo(name = "search_query") + private String searchQuery; + + public RecentSearchQuery(@NonNull String username, @NonNull String searchQuery) { + this.username = username; + this.searchQuery = searchQuery; + } + + @NonNull + public String getUsername() { + return username; + } + + public void setUsername(@NonNull String username) { + this.username = username; + } + + @NonNull + public String getSearchQuery() { + return searchQuery; + } + + public void setSearchQuery(@NonNull String searchQuery) { + this.searchQuery = searchQuery; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RecentSearchQuery/RecentSearchQueryDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RecentSearchQuery/RecentSearchQueryDao.java new file mode 100644 index 00000000..5db9718e --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RecentSearchQuery/RecentSearchQueryDao.java @@ -0,0 +1,25 @@ +package ml.docilealligator.infinityforreddit.RecentSearchQuery; + +import androidx.lifecycle.LiveData; +import androidx.room.Dao; +import androidx.room.Delete; +import androidx.room.Insert; +import androidx.room.OnConflictStrategy; +import androidx.room.Query; + +import java.util.List; + +@Dao +public interface RecentSearchQueryDao { + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insert(RecentSearchQuery recentSearchQuery); + + @Query("SELECT * FROM recent_search_queries WHERE username = :username") + LiveData<List<RecentSearchQuery>> getAllRecentSearchQueriesLiveData(String username); + + @Query("SELECT * FROM recent_search_queries WHERE username = :username") + List<RecentSearchQuery> getAllRecentSearchQueries(String username); + + @Delete + void deleteRecentSearchQueries(RecentSearchQuery recentSearchQuery); +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RecentSearchQuery/RecentSearchQueryRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RecentSearchQuery/RecentSearchQueryRepository.java new file mode 100644 index 00000000..259ba28d --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RecentSearchQuery/RecentSearchQueryRepository.java @@ -0,0 +1,19 @@ +package ml.docilealligator.infinityforreddit.RecentSearchQuery; + +import androidx.lifecycle.LiveData; + +import java.util.List; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class RecentSearchQueryRepository { + private LiveData<List<RecentSearchQuery>> mAllRecentSearchQueries; + + RecentSearchQueryRepository(RedditDataRoomDatabase redditDataRoomDatabase, String username) { + mAllRecentSearchQueries = redditDataRoomDatabase.recentSearchQueryDao().getAllRecentSearchQueriesLiveData(username); + } + + LiveData<List<RecentSearchQuery>> getAllRecentSearchQueries() { + return mAllRecentSearchQueries; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RecentSearchQuery/RecentSearchQueryViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RecentSearchQuery/RecentSearchQueryViewModel.java new file mode 100644 index 00000000..9b792117 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RecentSearchQuery/RecentSearchQueryViewModel.java @@ -0,0 +1,38 @@ +package ml.docilealligator.infinityforreddit.RecentSearchQuery; + +import androidx.annotation.NonNull; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.ViewModel; +import androidx.lifecycle.ViewModelProvider; + +import java.util.List; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class RecentSearchQueryViewModel extends ViewModel { + private LiveData<List<RecentSearchQuery>> mAllRecentSearchQueries; + + public RecentSearchQueryViewModel(RedditDataRoomDatabase redditDataRoomDatabase, String username) { + mAllRecentSearchQueries = new RecentSearchQueryRepository(redditDataRoomDatabase, username).getAllRecentSearchQueries(); + } + + public LiveData<List<RecentSearchQuery>> getAllRecentSearchQueries() { + return mAllRecentSearchQueries; + } + + public static class Factory extends ViewModelProvider.NewInstanceFactory { + private RedditDataRoomDatabase mRedditDataRoomDatabase; + private String mUsername; + + public Factory(RedditDataRoomDatabase redditDataRoomDatabase, String username) { + mRedditDataRoomDatabase = redditDataRoomDatabase; + mUsername = username; + } + + @NonNull + @Override + public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { + return (T) new RecentSearchQueryViewModel(mRedditDataRoomDatabase, mUsername); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java index 72ce475a..2556a173 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java @@ -16,6 +16,8 @@ import ml.docilealligator.infinityforreddit.CustomTheme.CustomTheme; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeDao; import ml.docilealligator.infinityforreddit.MultiReddit.MultiReddit; import ml.docilealligator.infinityforreddit.MultiReddit.MultiRedditDao; +import ml.docilealligator.infinityforreddit.RecentSearchQuery.RecentSearchQuery; +import ml.docilealligator.infinityforreddit.RecentSearchQuery.RecentSearchQueryDao; import ml.docilealligator.infinityforreddit.Subreddit.SubredditDao; import ml.docilealligator.infinityforreddit.Subreddit.SubredditData; import ml.docilealligator.infinityforreddit.SubscribedSubreddit.SubscribedSubredditDao; @@ -26,7 +28,7 @@ import ml.docilealligator.infinityforreddit.User.UserDao; import ml.docilealligator.infinityforreddit.User.UserData; @Database(entities = {Account.class, SubredditData.class, SubscribedSubredditData.class, UserData.class, - SubscribedUserData.class, MultiReddit.class, CustomTheme.class}, version = 9) + SubscribedUserData.class, MultiReddit.class, CustomTheme.class, RecentSearchQuery.class}, version = 10) public abstract class RedditDataRoomDatabase extends RoomDatabase { private static RedditDataRoomDatabase INSTANCE; @@ -37,7 +39,8 @@ public abstract class RedditDataRoomDatabase extends RoomDatabase { INSTANCE = Room.databaseBuilder(context.getApplicationContext(), RedditDataRoomDatabase.class, "reddit_data") .addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5, - MIGRATION_5_6, MIGRATION_6_7, MIGRATION_7_8, MIGRATION_8_9) + MIGRATION_5_6, MIGRATION_6_7, MIGRATION_7_8, MIGRATION_8_9, + MIGRATION_9_10) .build(); } } @@ -59,6 +62,8 @@ public abstract class RedditDataRoomDatabase extends RoomDatabase { public abstract CustomThemeDao customThemeDao(); + public abstract RecentSearchQueryDao recentSearchQueryDao(); + private static final Migration MIGRATION_1_2 = new Migration(1, 2) { @Override public void migrate(SupportSQLiteDatabase database) { @@ -209,4 +214,19 @@ public abstract class RedditDataRoomDatabase extends RoomDatabase { } }; + + private static final Migration MIGRATION_9_10 = new Migration(9, 10) { + @Override + public void migrate(@NonNull SupportSQLiteDatabase database) { + database.execSQL("CREATE TABLE recent_search_queries" + + "(username TEXT NOT NULL, search_query TEXT NOT NULL, PRIMARY KEY(username, search_query), " + + "FOREIGN KEY(username) REFERENCES accounts(username) ON DELETE CASCADE)"); + + database.execSQL("ALTER TABLE subreddits" + + " ADD COLUMN suggested_comment_sort TEXT"); + + database.execSQL("ALTER TABLE subreddits" + + " ADD COLUMN over18 INTEGER DEFAULT 0 NOT NULL"); + } + }; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/ParseSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/ParseSubredditData.java index 7c75b35d..f2545bd8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/ParseSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/ParseSubredditData.java @@ -8,7 +8,6 @@ import org.json.JSONObject; import java.util.ArrayList; -import ml.docilealligator.infinityforreddit.Subreddit.SubredditData; import ml.docilealligator.infinityforreddit.Utils.JSONUtils; import ml.docilealligator.infinityforreddit.Utils.Utils; @@ -27,6 +26,8 @@ class ParseSubredditData { String description = subredditDataJsonObject.getString(JSONUtils.PUBLIC_DESCRIPTION_KEY).trim(); String sidebarDescription = Utils.modifyMarkdown(subredditDataJsonObject.getString(JSONUtils.DESCRIPTION_KEY).trim()); long createdUTC = subredditDataJsonObject.getLong(JSONUtils.CREATED_UTC_KEY) * 1000; + String suggestedCommentSort = subredditDataJsonObject.getString(JSONUtils.SUGGESTED_COMMENT_SORT_KEY); + boolean isNSFW = subredditDataJsonObject.getBoolean(JSONUtils.OVER18_KEY); String bannerImageUrl; if (subredditDataJsonObject.isNull(JSONUtils.BANNER_BACKGROUND_IMAGE_KEY)) { @@ -54,7 +55,7 @@ class ParseSubredditData { } return new SubredditData(id, subredditFullName, iconUrl, bannerImageUrl, description, - sidebarDescription, nSubscribers, createdUTC); + sidebarDescription, nSubscribers, createdUTC, suggestedCommentSort, isNSFW); } interface ParseSubredditDataListener { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditData.java index 1fb628bf..f05e5912 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/SubredditData.java @@ -25,9 +25,14 @@ public class SubredditData { private int nSubscribers; @ColumnInfo(name = "created_utc") private long createdUTC; + @ColumnInfo(name = "suggested_comment_sort") + private String suggestedCommentSort; + @ColumnInfo(name = "over18") + private boolean isNSFW; public SubredditData(@NonNull String id, String name, String iconUrl, String bannerUrl, - String description, String sidebarDescription, int nSubscribers, long createdUTC) { + String description, String sidebarDescription, int nSubscribers, long createdUTC, + String suggestedCommentSort, boolean isNSFW) { this.id = id; this.name = name; this.iconUrl = iconUrl; @@ -36,6 +41,8 @@ public class SubredditData { this.sidebarDescription = sidebarDescription; this.nSubscribers = nSubscribers; this.createdUTC = createdUTC; + this.suggestedCommentSort = suggestedCommentSort; + this.isNSFW = isNSFW; } @NonNull @@ -70,4 +77,12 @@ public class SubredditData { public long getCreatedUTC() { return createdUTC; } + + public String getSuggestedCommentSort() { + return suggestedCommentSort; + } + + public boolean isNSFW() { + return isNSFW; + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/JSONUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/JSONUtils.java index 362d4b3f..51c9fbbc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/JSONUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/JSONUtils.java @@ -125,4 +125,6 @@ public class JSONUtils { public static final String GIF_KEY = "gif"; public static final String MAX_EMOJIS_KEY = "max_emojis"; public static final String RICHTEXT_KEY = "richtext"; + public static final String SUGGESTED_COMMENT_SORT_KEY = "suggested_comment_sort"; + public static final String OVER18_KEY = "over18"; } diff --git a/app/src/main/res/drawable/ic_header_hashtag_black_24dp.xml b/app/src/main/res/drawable/ic_header_hashtag_black_24dp.xml deleted file mode 100644 index 1cef8584..00000000 --- a/app/src/main/res/drawable/ic_header_hashtag_black_24dp.xml +++ /dev/null @@ -1,9 +0,0 @@ -<vector xmlns:android="http://schemas.android.com/apk/res/android" - android:width="24dp" - android:height="24dp" - android:viewportWidth="48" - android:viewportHeight="48"> - <path - android:pathData="M35.4182,18.3369c-0.2456,-0.3018 -0.6104,-0.4678 -1.0273,-0.4678H31.0766l0.9194,-4.4711c0.0864,-0.4199 -0.0063,-0.8359 -0.2549,-1.1406c-0.2461,-0.3008 -0.6108,-0.4668 -1.0278,-0.4668c-0.8237,0 -1.6011,0.6435 -1.77,1.4668l-0.2498,1.2147c-0.0082,0.0305 -0.0214,0.0594 -0.0277,0.0903l-0.6794,3.3066h-5.5789l0.9194,-4.4711c0.0864,-0.4199 -0.0063,-0.8359 -0.2549,-1.1406c-0.2461,-0.3008 -0.6108,-0.4668 -1.0278,-0.4668c-0.8237,0 -1.6011,0.6435 -1.77,1.4668l-0.2601,1.2647c-0.0032,0.0137 -0.0095,0.0266 -0.0124,0.0403l-0.6799,3.3066h-3.6087c-0.8237,0 -1.6016,0.6435 -1.7705,1.4658c-0.0864,0.4209 0.0068,0.8359 0.2554,1.1406c0.2461,0.3018 0.6108,0.4668 1.0273,0.4668h3.4649l-0.9841,4.7861h-3.6087c-0.8237,0 -1.6016,0.6435 -1.7705,1.4668c-0.0864,0.4189 0.0063,0.835 0.2544,1.1387c0.2461,0.3018 0.6113,0.4678 1.0283,0.4678h3.4649l-0.9244,4.4951c-0.0864,0.4199 0.0063,0.8359 0.2549,1.1406c0.2456,0.3008 0.6108,0.4668 1.0273,0.4668c0.8237,0 1.6016,-0.6435 1.7705,-1.4668l0.9533,-4.6357h5.5824l-0.9244,4.4951c-0.0864,0.4199 0.0063,0.8359 0.2549,1.1406c0.2461,0.3008 0.6108,0.4668 1.0278,0.4668c0.8237,0 1.6011,-0.6435 1.77,-1.4668l0.9533,-4.6357h3.4666c0.8237,0 1.6016,-0.6435 1.7705,-1.4658c0.0884,-0.4287 -0.001,-0.834 -0.2515,-1.1416c-0.2451,-0.3008 -0.6113,-0.4658 -1.0308,-0.4658h-3.3229l0.9837,-4.7861h3.4666c0.8237,0 1.6016,-0.6435 1.7705,-1.4668C35.7595,19.0566 35.6667,18.6406 35.4182,18.3369zM20.7882,25.7285l0.9837,-4.7861h5.5824l-0.9841,4.7861H20.7882z" - android:fillColor="#303030"/> -</vector> diff --git a/app/src/main/res/drawable/ic_history_24dp.xml b/app/src/main/res/drawable/ic_history_24dp.xml new file mode 100644 index 00000000..a3fbac1c --- /dev/null +++ b/app/src/main/res/drawable/ic_history_24dp.xml @@ -0,0 +1,9 @@ +<vector xmlns:android="http://schemas.android.com/apk/res/android" + android:width="24dp" + android:height="24dp" + android:viewportWidth="24" + android:viewportHeight="24"> + <path + android:pathData="M13,3c-4.97,0 -9,4.03 -9,9L1,12l3.89,3.89 0.07,0.14L9,12L6,12c0,-3.87 3.13,-7 7,-7s7,3.13 7,7 -3.13,7 -7,7c-1.93,0 -3.68,-0.79 -4.94,-2.06l-1.42,1.42C8.27,19.99 10.51,21 13,21c4.97,0 9,-4.03 9,-9s-4.03,-9 -9,-9zM12,8v5l4.28,2.54 0.72,-1.21 -3.5,-2.08L13.5,8L12,8z" + android:fillColor="#000000"/> +</vector> diff --git a/app/src/main/res/drawable/ic_title_24dp.xml b/app/src/main/res/drawable/ic_title_24dp.xml new file mode 100644 index 00000000..3e622fe7 --- /dev/null +++ b/app/src/main/res/drawable/ic_title_24dp.xml @@ -0,0 +1,9 @@ +<vector xmlns:android="http://schemas.android.com/apk/res/android" + android:width="24dp" + android:height="24dp" + android:viewportWidth="24" + android:viewportHeight="24"> + <path + android:pathData="M5,5.5C5,6.33 5.67,7 6.5,7h4v10.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5V7h4c0.83,0 1.5,-0.67 1.5,-1.5S18.33,4 17.5,4h-11C5.67,4 5,4.67 5,5.5z" + android:fillColor="#000000"/> +</vector> diff --git a/app/src/main/res/layout/activity_search.xml b/app/src/main/res/layout/activity_search.xml index 0048e1fc..972ba2af 100644 --- a/app/src/main/res/layout/activity_search.xml +++ b/app/src/main/res/layout/activity_search.xml @@ -48,38 +48,64 @@ android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> - <RelativeLayout - android:id="@+id/subreddit_name_relative_layout_search_activity" + <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" - android:background="?attr/selectableItemBackground" - android:clickable="true" - android:focusable="true" - android:padding="16dp"> + android:orientation="vertical"> - <TextView - android:id="@+id/search_in_text_view_search_activity" - android:layout_width="wrap_content" + <RelativeLayout + android:id="@+id/subreddit_name_relative_layout_search_activity" + android:layout_width="match_parent" android:layout_height="wrap_content" - android:text="@string/search_in" - android:textColor="?attr/colorAccent" - android:textSize="?attr/font_default" - android:fontFamily="?attr/font_family" /> + android:background="?attr/selectableItemBackground" + android:clickable="true" + android:focusable="true" + android:padding="16dp"> + + <TextView + android:id="@+id/search_in_text_view_search_activity" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="@string/search_in" + android:textColor="?attr/colorAccent" + android:textSize="?attr/font_default" + android:fontFamily="?attr/font_family" /> + + <TextView + android:id="@+id/subreddit_name_text_view_search_activity" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_centerVertical="true" + android:layout_marginStart="32dp" + android:layout_marginEnd="16dp" + android:layout_toEndOf="@id/search_in_text_view_search_activity" + android:text="@string/all_subreddits" + android:textColor="?attr/primaryTextColor" + android:textSize="?attr/font_default" + android:fontFamily="?attr/font_family" /> + + </RelativeLayout> + + <View + android:id="@+id/divider_search_activity" + android:layout_width="match_parent" + android:layout_height="1dp" /> <TextView - android:id="@+id/subreddit_name_text_view_search_activity" + android:id="@+id/recent_summary_text_view_search_activity" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_centerVertical="true" - android:layout_marginStart="32dp" - android:layout_marginEnd="16dp" - android:layout_toEndOf="@id/search_in_text_view_search_activity" - android:text="@string/all_subreddits" - android:textColor="?attr/primaryTextColor" - android:textSize="?attr/font_default" - android:fontFamily="?attr/font_family" /> - - </RelativeLayout> + android:padding="16dp" + android:text="@string/recent_searches" + android:fontFamily="?attr/font_family" + android:visibility="gone" /> + + <androidx.recyclerview.widget.RecyclerView + android:id="@+id/recycler_view_search_activity" + android:layout_width="match_parent" + android:layout_height="wrap_content" /> + + </LinearLayout> </androidx.core.widget.NestedScrollView> diff --git a/app/src/main/res/layout/item_recent_search_query.xml b/app/src/main/res/layout/item_recent_search_query.xml new file mode 100644 index 00000000..f014e2fe --- /dev/null +++ b/app/src/main/res/layout/item_recent_search_query.xml @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="utf-8"?> +<LinearLayout + xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:padding="16dp" + android:clickable="true" + android:focusable="true" + android:background="?attr/selectableItemBackground"> + + <TextView + android:id="@+id/recent_search_query_text_view_item_recent_search_query" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:fontFamily="?attr/font_family" + android:textSize="?attr/font_default" + android:drawablePadding="16dp" /> + +</LinearLayout>
\ 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 bd3f0681..5b85708a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -912,4 +912,6 @@ <string name="give_award_success">Award given</string> <string name="give_award_failed">Failed</string> + <string name="recent_searches">Recent searches</string> + </resources> |