From e44a172db0c2fb35f4d5657226c86ad4af0034dc Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 10 Aug 2018 08:22:56 +0800 Subject: Display subreddit icons in best posts, eventually. Rename some methods. --- .../infinityforreddit/BestPostFragment.java | 3 +- .../BestPostRecyclerViewAdapter.java | 149 +++++++++++++-------- .../infinityforreddit/FetchSubscribedThing.java | 16 ++- .../infinityforreddit/MainActivity.java | 36 +++-- .../infinityforreddit/ParseSubredditData.java | 2 +- .../infinityforreddit/ParseSubscribedThing.java | 24 +++- .../infinityforreddit/SubredditDao.java | 5 +- .../infinityforreddit/SubredditRepository.java | 8 +- .../infinityforreddit/SubredditViewModel.java | 10 +- .../ViewSubredditDetailActivity.java | 4 +- 10 files changed, 164 insertions(+), 93 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostFragment.java index de4e6eb0..ab3118c7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostFragment.java @@ -1,7 +1,7 @@ package ml.docilealligator.infinityforreddit; -import android.app.Fragment; +import android.support.v4.app.Fragment; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; @@ -158,7 +158,6 @@ public class BestPostFragment extends Fragment { }; mPaginationSynchronizer.setPaginationRequestQueueSynchronizer(paginationRequestQueueSynchronizer); - return rootView; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostRecyclerViewAdapter.java index 94c9efc5..7b70ca08 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostRecyclerViewAdapter.java @@ -4,6 +4,7 @@ import android.content.Context; import android.content.Intent; import android.graphics.drawable.Drawable; import android.net.Uri; +import android.os.AsyncTask; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.customtabs.CustomTabsIntent; @@ -39,12 +40,13 @@ import de.hdodenhof.circleimageview.CircleImageView; */ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter { - private ArrayList bestPostData; + private ArrayList mBestPostData; private Context mContext; private PaginationSynchronizer mPaginationSynchronizer; private RequestQueue mVoteThingRequestQueue; private RequestQueue mAcquireAccessTokenRequestQueue; private RequestManager glide; + private SubredditDao subredditDao; private boolean isLoadingMorePostSuccess; private boolean canStartActivity; @@ -56,13 +58,14 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { @@ -166,7 +172,7 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { @@ -179,7 +185,7 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { @@ -208,7 +214,7 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { @@ -244,7 +250,7 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter= bestPostData.size() ? VIEW_TYPE_LOADING : VIEW_TYPE_DATA); + return (position >= mBestPostData.size() ? VIEW_TYPE_LOADING : VIEW_TYPE_DATA); } class DataViewHolder extends RecyclerView.ViewHolder { @@ -524,4 +530,37 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter { + private Context context; + private CircleImageView iconImageView; + private SubredditDao subredditDao; + private String subredditName; + private String iconImageUrl; + + LoadSubredditIconAsyncTask(Context context, CircleImageView iconImageView, SubredditDao subredditDao, String subredditName) { + this.context = context; + this.iconImageView = iconImageView; + this.subredditDao = subredditDao; + this.subredditName = subredditName; + } + + @Override + protected Void doInBackground(Void... voids) { + if(subredditDao.getSubredditData(subredditName) != null) { + iconImageUrl = subredditDao.getSubredditData(subredditName).getIconUrl(); + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + if(iconImageUrl != null) { + Glide.with(context).load(iconImageUrl).into(iconImageView); + } else { + Glide.with(context).load(R.drawable.subreddit_default_icon).into(iconImageView); + } + } + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java index 4a902c16..d30bfa7f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java @@ -16,7 +16,8 @@ import java.util.Map; class FetchSubscribedThing { interface FetchSubscribedSubredditsListener { void onFetchSubscribedSubredditsSuccess(ArrayList subscribedSubredditData, - ArrayList subscribedUserData); + ArrayList subscribedUserData, + ArrayList subredditData); void onFetchSubscribedSubredditsFail(); } @@ -25,15 +26,19 @@ class FetchSubscribedThing { private FetchSubscribedSubredditsListener mFetchSubscribedSubredditsListener; private ArrayList mSubscribedSubredditData; private ArrayList mSubscribedUserData; + private ArrayList mSubredditData; private String mLastItem; - FetchSubscribedThing(Context context, RequestQueue requestQueue, ArrayList subscribedSubredditData, - ArrayList subscribedUserData) { + FetchSubscribedThing(Context context, RequestQueue requestQueue, + ArrayList subscribedSubredditData, + ArrayList subscribedUserData, + ArrayList subredditData) { this.context = context; this.requestQueue = requestQueue; mSubscribedSubredditData = subscribedSubredditData; mSubscribedUserData = subscribedUserData; + mSubredditData = subredditData; } void fetchSubscribedSubreddits(FetchSubscribedSubredditsListener fetchUserInfoListener, final int refreshTime) { @@ -51,19 +56,20 @@ class FetchSubscribedThing { @Override public void onResponse(String response) { new ParseSubscribedThing().parseSubscribedSubreddits(response, mSubscribedSubredditData, - mSubscribedUserData, + mSubscribedUserData, mSubredditData, new ParseSubscribedThing.ParseSubscribedSubredditsListener() { @Override public void onParseSubscribedSubredditsSuccess(ArrayList subscribedSubredditData, ArrayList subscribedUserData, + ArrayList subredditData, String lastItem) { mSubscribedSubredditData = subscribedSubredditData; mSubscribedUserData = subscribedUserData; mLastItem = lastItem; if(mLastItem.equals("null")) { mFetchSubscribedSubredditsListener.onFetchSubscribedSubredditsSuccess(mSubscribedSubredditData, - mSubscribedUserData); + mSubscribedUserData, mSubredditData); } else { fetchSubscribedSubreddits(mFetchSubscribedSubredditsListener, refreshTime); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index 664ccb63..87de57a7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -1,7 +1,7 @@ package ml.docilealligator.infinityforreddit; -import android.app.Fragment; -import android.app.FragmentTransaction; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentTransaction; import android.arch.lifecycle.Observer; import android.arch.lifecycle.ViewModelProviders; import android.content.Context; @@ -75,12 +75,12 @@ public class MainActivity extends AppCompatActivity { startActivity(loginIntent); } else { if(savedInstanceState == null) { - FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); + FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); mFragment = new BestPostFragment(); fragmentTransaction.replace(R.id.frame_layout_content_main, mFragment).commit(); } else { - mFragment = getFragmentManager().getFragment(savedInstanceState, "outStateFragment"); - getFragmentManager().beginTransaction().replace(R.id.frame_layout_content_main, mFragment).commit(); + mFragment = getSupportFragmentManager().getFragment(savedInstanceState, "outStateFragment"); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_content_main, mFragment).commit(); } Calendar now = Calendar.getInstance(); @@ -214,17 +214,20 @@ public class MainActivity extends AppCompatActivity { }); new FetchSubscribedThing(this, Volley.newRequestQueue(this), new ArrayList(), - new ArrayList()) + new ArrayList(), new ArrayList()) .fetchSubscribedSubreddits(new FetchSubscribedThing.FetchSubscribedSubredditsListener() { @Override public void onFetchSubscribedSubredditsSuccess(ArrayList subscribedSubredditData, - ArrayList subscribedUserData) { + ArrayList subscribedUserData, + ArrayList subredditData) { mIsInserting = true; new InsertSubscribedThingsAsyncTask( SubscribedSubredditRoomDatabase.getDatabase(MainActivity.this), SubscribedUserRoomDatabase.getDatabase(MainActivity.this), + SubredditRoomDatabase.getDatabase(MainActivity.this), subscribedSubredditData, subscribedUserData, + subredditData, new InsertSubscribedThingsAsyncTask.InsertSubscribedThingListener() { @Override public void insertSuccess() { @@ -255,7 +258,7 @@ public class MainActivity extends AppCompatActivity { protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if(mFragment != null) { - getFragmentManager().putFragment(outState, "outStateFragment", mFragment); + getSupportFragmentManager().putFragment(outState, "outStateFragment", mFragment); } outState.putString(nameState, mName); outState.putString(profileImageUrlState, mProfileImageUrl); @@ -286,32 +289,41 @@ public class MainActivity extends AppCompatActivity { void insertSuccess(); } - private final SubscribedSubredditDao mSubredditDao; + private final SubscribedSubredditDao mSubscribedSubredditDao; private final SubscribedUserDao mUserDao; + private final SubredditDao mSubredditDao; private List subscribedSubredditData; private List subscribedUserData; + private List subredditData; private InsertSubscribedThingListener insertSubscribedThingListener; - InsertSubscribedThingsAsyncTask(SubscribedSubredditRoomDatabase subredditDb, + InsertSubscribedThingsAsyncTask(SubscribedSubredditRoomDatabase subscribedSubredditDb, SubscribedUserRoomDatabase userDb, + SubredditRoomDatabase subredditDb, List subscribedSubredditData, List subscribedUserData, + List subredditData, InsertSubscribedThingListener insertSubscribedThingListener) { - mSubredditDao = subredditDb.subscribedSubredditDao(); + mSubscribedSubredditDao = subscribedSubredditDb.subscribedSubredditDao(); mUserDao = userDb.subscribedUserDao(); + mSubredditDao = subredditDb.subredditDao(); this.subscribedSubredditData = subscribedSubredditData; this.subscribedUserData = subscribedUserData; + this.subredditData = subredditData; this.insertSubscribedThingListener = insertSubscribedThingListener; } @Override protected Void doInBackground(final Void... params) { for(SubscribedSubredditData s : subscribedSubredditData) { - mSubredditDao.insert(s); + mSubscribedSubredditDao.insert(s); } for(SubscribedUserData s : subscribedUserData) { mUserDao.insert(s); } + for(SubredditData s : subredditData) { + mSubredditDao.insert(s); + } return null; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java index 8f89cc0e..3c31be30 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java @@ -43,7 +43,7 @@ class ParseSubredditData { JSONObject data = jsonResponse.getJSONObject(JSONUtils.DATA_KEY); String id = data.getString(JSONUtils.NAME_KEY); String subredditFullName = data.getString(JSONUtils.DISPLAY_NAME_PREFIXED); - String description = data.getString(JSONUtils.PUBLIC_DESCRIPTION); + String description = data.getString(JSONUtils.PUBLIC_DESCRIPTION).trim(); String bannerImageUrl = data.getString(JSONUtils.BANNER_IMG_KEY); String iconImageUrl = data.getString(JSONUtils.ICON_IMG_KEY); int nSubscribers = data.getInt(JSONUtils.SUBSCRIBERS_KEY); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java index 4aed5743..cc1754ac 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java @@ -13,6 +13,7 @@ class ParseSubscribedThing { interface ParseSubscribedSubredditsListener { void onParseSubscribedSubredditsSuccess(ArrayList subscribedSubredditData, ArrayList subscribedUserData, + ArrayList subredditData, String lastItem); void onParseSubscribedSubredditsFail(); } @@ -21,9 +22,10 @@ class ParseSubscribedThing { void parseSubscribedSubreddits(String response, ArrayList subscribedSubredditData, ArrayList subscribedUserData, + ArrayList subredditData, ParseSubscribedSubredditsListener parseSubscribedSubredditsListener) { mParseSubscribedSubredditsListener = parseSubscribedSubredditsListener; - new ParseSubscribedSubredditsAsyncTask(response, subscribedSubredditData, subscribedUserData).execute(); + new ParseSubscribedSubredditsAsyncTask(response, subscribedSubredditData, subscribedUserData, subredditData).execute(); } private class ParseSubscribedSubredditsAsyncTask extends AsyncTask { @@ -32,18 +34,23 @@ class ParseSubscribedThing { private String lastItem; private ArrayList subscribedSubredditData; private ArrayList subscribedUserData; + private ArrayList subredditData; private ArrayList newSubscribedSubredditData; private ArrayList newSubscribedUserData; + private ArrayList newSubredditData; ParseSubscribedSubredditsAsyncTask(String response, ArrayList subscribedSubredditData, - ArrayList subscribedUserData){ + ArrayList subscribedUserData, + ArrayList subredditData){ try { jsonResponse = new JSONObject(response); parseFailed = false; this.subscribedSubredditData = subscribedSubredditData; this.subscribedUserData = subscribedUserData; + this.subredditData = subredditData; newSubscribedSubredditData = new ArrayList<>(); newSubscribedUserData = new ArrayList<>(); + newSubredditData = new ArrayList<>(); } catch (JSONException e) { Log.i("user info json error", e.getMessage()); mParseSubscribedSubredditsListener.onParseSubscribedSubredditsFail(); @@ -55,8 +62,10 @@ class ParseSubscribedThing { try { JSONArray children = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); for(int i = 0; i < children.length(); i++) { - String name = children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.DISPLAY_NAME); - String iconUrl = children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.ICON_IMG_KEY); + JSONObject data = children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY); + String name = data.getString(JSONUtils.DISPLAY_NAME); + String bannerUrl = data.getString(JSONUtils.BANNER_IMG_KEY); + String iconUrl = data.getString(JSONUtils.ICON_IMG_KEY); String id = children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.NAME_KEY); if(iconUrl.equals("null")) { iconUrl = ""; @@ -67,7 +76,11 @@ class ParseSubscribedThing { //It's a user newSubscribedUserData.add(new SubscribedUserData(id, name.substring(2), iconUrl)); } else { + String subredditFullName = data.getString(JSONUtils.DISPLAY_NAME_PREFIXED); + String description = data.getString(JSONUtils.PUBLIC_DESCRIPTION).trim(); + int nSubscribers = data.getInt(JSONUtils.SUBSCRIBERS_KEY); newSubscribedSubredditData.add(new SubscribedSubredditData(id, name, iconUrl)); + newSubredditData.add(new SubredditData(id, subredditFullName, iconUrl, bannerUrl, description, nSubscribers)); } } lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); @@ -83,8 +96,9 @@ class ParseSubscribedThing { if(!parseFailed) { subscribedSubredditData.addAll(newSubscribedSubredditData); subscribedUserData.addAll(newSubscribedUserData); + subredditData.addAll(newSubredditData); mParseSubscribedSubredditsListener.onParseSubscribedSubredditsSuccess(subscribedSubredditData, - subscribedUserData, lastItem); + subscribedUserData, subredditData, lastItem); } else { mParseSubscribedSubredditsListener.onParseSubscribedSubredditsFail(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDao.java index 7fb49f62..8fb1593c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDao.java @@ -15,5 +15,8 @@ public interface SubredditDao { void deleteAllSubreddits(); @Query("SELECT * from subreddits WHERE id = :id") - LiveData getSubreddit(String id); + LiveData getSubredditLiveData(String id); + + @Query("SELECT * from subreddits WHERE name = :namePrefixed") + SubredditData getSubredditData(String namePrefixed); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditRepository.java index 01320b3d..a104fed7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditRepository.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditRepository.java @@ -6,16 +6,16 @@ import android.os.AsyncTask; public class SubredditRepository { private SubredditDao mSubredditDao; - private LiveData mSubredditData; + private LiveData mSubredditLiveData; SubredditRepository(Application application, String id) { SubredditRoomDatabase db = SubredditRoomDatabase.getDatabase(application); mSubredditDao = db.subredditDao(); - mSubredditData = mSubredditDao.getSubreddit(id); + mSubredditLiveData = mSubredditDao.getSubredditLiveData(id); } - LiveData getSubredditData() { - return mSubredditData; + LiveData getSubredditLiveData() { + return mSubredditLiveData; } public void insert(SubredditData subredditData) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditViewModel.java index b3110347..6d9e20fc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditViewModel.java @@ -9,16 +9,16 @@ import android.support.annotation.NonNull; public class SubredditViewModel extends AndroidViewModel { private SubredditRepository mSubredditRepository; - private LiveData mSubredditData; + private LiveData mSubredditLiveData; - public SubredditViewModel(Application application, String id) { + SubredditViewModel(Application application, String id) { super(application); mSubredditRepository = new SubredditRepository(application, id); - mSubredditData = mSubredditRepository.getSubredditData(); + mSubredditLiveData = mSubredditRepository.getSubredditLiveData(); } - public LiveData getSubredditData() { - return mSubredditData; + public LiveData getSubredditLiveData() { + return mSubredditLiveData; } public void insert(SubredditData subredditData) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java index c7edcb93..887beb01 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java @@ -9,7 +9,6 @@ import android.support.design.widget.AppBarLayout; import android.support.design.widget.CollapsingToolbarLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; -import android.util.Log; import android.view.MenuItem; import android.widget.ImageView; import android.widget.TextView; @@ -53,7 +52,6 @@ public class ViewSubredditDetailActivity extends AppCompatActivity { scrollRange = appBarLayout.getTotalScrollRange(); } if (scrollRange + verticalOffset == 0) { - Log.i("asfasdf", "asdfasdf"); collapsingToolbarLayout.setTitle(title); isShow = true; } else if(isShow) { @@ -73,7 +71,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity { SubredditViewModel.Factory factory = new SubredditViewModel.Factory(getApplication(), id); mSubredditViewModel = ViewModelProviders.of(this, factory).get(SubredditViewModel.class); - mSubredditViewModel.getSubredditData().observe(this, new Observer() { + mSubredditViewModel.getSubredditLiveData().observe(this, new Observer() { @Override public void onChanged(@Nullable SubredditData subredditData) { if(subredditData != null) { -- cgit v1.2.3