From c6026e5659a16ae1d2b3b72a1bafafd0ba467f12 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 5 Aug 2018 12:26:50 +0800 Subject: Fetch the subreddit info and save it to the database. Solve a bug that cannot create a instance of SubredditViewModel. --- .idea/caches/build_file_checksums.ser | Bin 533 -> 533 bytes .../infinityforreddit/JSONUtils.java | 1 + .../infinityforreddit/MainActivity.java | 23 ----- .../infinityforreddit/ParseSubredditData.java | 9 +- .../infinityforreddit/SubredditData.java | 2 +- .../infinityforreddit/SubredditViewModel.java | 22 +++++ .../SubscribedSubredditRecyclerViewAdapter.java | 4 +- .../ViewSubredditDetailActivity.java | 100 +++++++++++++++++++-- .../res/layout/activity_view_subreddit_detail.xml | 48 ++++++---- app/src/main/res/values/strings.xml | 2 + 10 files changed, 158 insertions(+), 53 deletions(-) diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 2ec6cab5..9b6f04a5 100644 Binary files a/.idea/caches/build_file_checksums.ser and b/.idea/caches/build_file_checksums.ser differ diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java index c766f786..90518d74 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java @@ -62,4 +62,5 @@ class JSONUtils { static final String SUBSCRIBERS_KEY = "subscribers"; static final String PUBLIC_DESCRIPTION = "public_description"; static final String ACTIVE_USER_COUNT = "active_user_count"; + static final String DISPLAY_NAME_PREFIXED = "display_name_prefixed"; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index a7f07ed5..8ebd61b4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -202,10 +202,6 @@ public class MainActivity extends AppCompatActivity { SubscribedUserRoomDatabase.getDatabase(MainActivity.this), subscribedSubredditData, subscribedUserData).execute(); - - /*new InsertSubscribedUsersAsyncTask( - SubscribedUserRoomDatabase.getDatabase(MainActivity.this), - subscribedUserData).execute();*/ } @Override @@ -282,23 +278,4 @@ public class MainActivity extends AppCompatActivity { return null; } } - - /*private static class InsertSubscribedUsersAsyncTask extends AsyncTask { - - private final SubscribedUserDao mDao; - private List subscribedUserData; - - InsertSubscribedUsersAsyncTask(SubscribedUserRoomDatabase db, List subscribedUserData) { - mDao = db.subscribedUserDao(); - this.subscribedUserData = subscribedUserData; - } - - @Override - protected Void doInBackground(final Void... params) { - for(SubscribedUserData s : subscribedUserData) { - mDao.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 36cbaeeb..8f89cc0e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java @@ -8,7 +8,7 @@ import org.json.JSONObject; class ParseSubredditData { interface ParseSubredditDataListener { - void onParseSubredditDataSuccess(); + void onParseSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers); void onParseSubredditDataFail(); } @@ -23,6 +23,8 @@ class ParseSubredditData { private JSONObject jsonResponse; private boolean parseFailed; private ParseSubredditDataListener parseSubredditDataListener; + private SubredditData subredditData; + private int mNCurrentOnlineSubscribers; ParseSubredditDataAsyncTask(String response, ParseSubredditDataListener parseSubredditDataListener){ this.parseSubredditDataListener = parseSubredditDataListener; @@ -40,11 +42,14 @@ class ParseSubredditData { try { 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 bannerImageUrl = data.getString(JSONUtils.BANNER_IMG_KEY); String iconImageUrl = data.getString(JSONUtils.ICON_IMG_KEY); int nSubscribers = data.getInt(JSONUtils.SUBSCRIBERS_KEY); int nCurrentOnlineSubscribers = data.getInt(JSONUtils.ACTIVE_USER_COUNT); + subredditData = new SubredditData(id, subredditFullName, iconImageUrl, bannerImageUrl, description, nSubscribers); + mNCurrentOnlineSubscribers = nCurrentOnlineSubscribers; } catch (JSONException e) { parseFailed = true; Log.i("parse", "SubredditData error"); @@ -56,7 +61,7 @@ class ParseSubredditData { @Override protected void onPostExecute(Void aVoid) { if(!parseFailed) { - parseSubredditDataListener.onParseSubredditDataSuccess(); + parseSubredditDataListener.onParseSubredditDataSuccess(subredditData, mNCurrentOnlineSubscribers); } else { parseSubredditDataListener.onParseSubredditDataFail(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditData.java index 7f2095f7..59b8e6b8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditData.java @@ -57,7 +57,7 @@ class SubredditData { return description; } - public int getnSubscribers() { + public int getNSubscribers() { return nSubscribers; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditViewModel.java index b96aa0db..b3110347 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditViewModel.java @@ -3,6 +3,9 @@ package ml.docilealligator.infinityforreddit; import android.app.Application; import android.arch.lifecycle.AndroidViewModel; import android.arch.lifecycle.LiveData; +import android.arch.lifecycle.ViewModel; +import android.arch.lifecycle.ViewModelProvider; +import android.support.annotation.NonNull; public class SubredditViewModel extends AndroidViewModel { private SubredditRepository mSubredditRepository; @@ -21,4 +24,23 @@ public class SubredditViewModel extends AndroidViewModel { public void insert(SubredditData subredditData) { mSubredditRepository.insert(subredditData); } + + public static class Factory extends ViewModelProvider.NewInstanceFactory { + + @NonNull + private final Application mApplication; + + private final String id; + + public Factory(@NonNull Application application, String id) { + mApplication = application; + this.id = id; + } + + @Override + public T create(Class modelClass) { + //noinspection unchecked + return (T) new SubredditViewModel(mApplication, id); + } + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRecyclerViewAdapter.java index 67a569cd..141a3fef 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRecyclerViewAdapter.java @@ -33,11 +33,13 @@ class SubscribedSubredditRecyclerViewAdapter extends RecyclerView.Adapter() { + @Override + public void onChanged(@Nullable SubredditData subredditData) { + if(subredditData != null) { + if(!subredditData.getBannerUrl().equals("") && !subredditData.getBannerUrl().equals("null")) { + glide.load(subredditData.getBannerUrl()).into(bannerImageView); + } + + if(subredditData.getIconUrl().equals("") || subredditData.getIconUrl().equals("null")) { + glide.load(getDrawable(R.drawable.subreddit_default_icon)).into(iconCircleImageView); + } else { + glide.load(subredditData.getIconUrl()).into(iconCircleImageView); + } + + subredditNameTextView.setText(subredditData.getName()); + String nSubscribers = getString(R.string.subscribers_number_detail, subredditData.getNSubscribers()); + nSubscribersTextView.setText(nSubscribers); + descriptionTextView.setText(subredditData.getDescription()); + } + } + }); + + new FetchSubredditData(Volley.newRequestQueue(this), subredditName).querySubredditData(new FetchSubredditData.FetchSubredditDataListener() { + @Override + public void onFetchSubredditDataSuccess(String response) { + new ParseSubredditData().parseComment(response, new ParseSubredditData.ParseSubredditDataListener() { + @Override + public void onParseSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { + new InsertSubredditDataAsyncTask(SubredditRoomDatabase.getDatabase(ViewSubredditDetailActivity.this), subredditData) + .execute(); + String nOnlineSubscribers = getString(R.string.online_subscribers_number_detail, nCurrentOnlineSubscribers); + nOnlineSubscribersTextView.setText(nOnlineSubscribers); + } + + @Override + public void onParseSubredditDataFail() { + Toast.makeText(ViewSubredditDetailActivity.this, "Cannot fetch subreddit info", Toast.LENGTH_SHORT).show(); + } + }); + } + @Override - public void onClick(View view) { - Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) - .setAction("Action", null).show(); + public void onFetchSubredditDataFail() { + } }); } + + private static class InsertSubredditDataAsyncTask extends AsyncTask { + + private final SubredditDao mSubredditDao; + private SubredditData subredditData; + + InsertSubredditDataAsyncTask(SubredditRoomDatabase subredditDb, SubredditData subredditData) { + mSubredditDao = subredditDb.subredditDao(); + this.subredditData = subredditData; + } + + @Override + protected Void doInBackground(final Void... params) { + mSubredditDao.insert(subredditData); + return null; + } + } } diff --git a/app/src/main/res/layout/activity_view_subreddit_detail.xml b/app/src/main/res/layout/activity_view_subreddit_detail.xml index 58b808f9..a840154b 100644 --- a/app/src/main/res/layout/activity_view_subreddit_detail.xml +++ b/app/src/main/res/layout/activity_view_subreddit_detail.xml @@ -29,13 +29,12 @@ + android:textSize="18sp" + android:textColor="@color/colorAccent"/> + + + + + + + + + android:textColor="@android:color/black" /> @@ -86,13 +109,4 @@ - - \ 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 00eb8251..f1a80eb3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -20,6 +20,8 @@ Following Subscriptions ViewSubredditDetailActivity + Subscribers: %1$d + Online: %1$d "Material is the metaphor.\n\n" -- cgit v1.2.3