diff options
Diffstat (limited to 'app/src/main/java')
15 files changed, 423 insertions, 38 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"; } |