From ddc7b36e72dee19b87204beacc8322ca221b5524 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 29 Jul 2018 20:25:55 +0800 Subject: Save subscribed subreddits and users to the SQLite database by using Room Persistence Library. Add a following section in the navigation drawer to display all the followed users. Add a NestedScrollView in the navigation drawer to wrap all the elements. Disable nested scrolling feature in all the RecyclerViews in the navigation drawer. --- .../infinityforreddit/AcquireAccessToken.java | 2 +- .../FetchSubscribedSubreddits.java | 97 --------------- .../infinityforreddit/FetchSubscribedThing.java | 105 +++++++++++++++++ .../infinityforreddit/JSONUtils.java | 3 + .../infinityforreddit/MainActivity.java | 130 ++++++++++++++++++--- .../infinityforreddit/ParseBestPost.java | 15 ++- .../ParseSubscribedSubreddits.java | 72 ------------ .../infinityforreddit/ParseSubscribedThing.java | 93 +++++++++++++++ .../infinityforreddit/SubredditData.java | 50 -------- .../infinityforreddit/SubscribedSubredditDao.java | 21 ++++ .../infinityforreddit/SubscribedSubredditData.java | 38 ++++++ .../SubscribedSubredditRecyclerViewAdapter.java | 32 +++-- .../SubscribedSubredditRepository.java | 41 +++++++ .../SubscribedSubredditRoomDatabase.java | 26 +++++ .../SubscribedSubredditViewModel.java | 26 +++++ .../infinityforreddit/SubscribedUserDao.java | 21 ++++ .../infinityforreddit/SubscribedUserData.java | 37 ++++++ .../SubscribedUserRecyclerViewAdapter.java | 73 ++++++++++++ .../SubscribedUserRepository.java | 41 +++++++ .../SubscribedUserRoomDatabase.java | 26 +++++ .../infinityforreddit/SubscribedUserViewModel.java | 26 +++++ 21 files changed, 724 insertions(+), 251 deletions(-) delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedSubreddits.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedSubreddits.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubredditData.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDao.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditData.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRepository.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRoomDatabase.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditViewModel.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDao.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserData.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserRecyclerViewAdapter.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserRepository.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserRoomDatabase.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserViewModel.java (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AcquireAccessToken.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AcquireAccessToken.java index 7895ce0d..aeefa753 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AcquireAccessToken.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AcquireAccessToken.java @@ -64,7 +64,7 @@ class AcquireAccessToken { public void onErrorResponse(VolleyError error) { Toast.makeText(mContext, "Error getting the new access token", Toast.LENGTH_SHORT).show(); mAcquireAccessTokenListener.onAcquireAccessTokenFail(); - Log.i("error get access token", error.getMessage()); + Log.i("Error", "getting access token"); } }) { @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedSubreddits.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedSubreddits.java deleted file mode 100644 index 999a6dc1..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedSubreddits.java +++ /dev/null @@ -1,97 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import android.content.Context; -import android.net.Uri; -import android.util.Log; - -import com.android.volley.AuthFailureError; -import com.android.volley.Request; -import com.android.volley.RequestQueue; -import com.android.volley.Response; -import com.android.volley.VolleyError; -import com.android.volley.toolbox.StringRequest; - -import java.util.ArrayList; -import java.util.Map; - -class FetchSubscribedSubreddits { - interface FetchSubscribedSubredditsListener { - void onFetchSubscribedSubredditsSuccess(ArrayList subredditData); - void onFetchSubscribedSubredditsFail(); - } - - private Context context; - private RequestQueue requestQueue; - private FetchSubscribedSubredditsListener mFetchSubscribedSubredditsListener; - private ArrayList mSubredditData; - - private String mLastItem; - - FetchSubscribedSubreddits(Context context, RequestQueue requestQueue, ArrayList subredditData) { - this.context = context; - this.requestQueue = requestQueue; - mSubredditData = subredditData; - } - - void fetchSubscribedSubreddits(FetchSubscribedSubredditsListener fetchUserInfoListener, final int refreshTime) { - if(refreshTime < 0) { - mFetchSubscribedSubredditsListener.onFetchSubscribedSubredditsFail(); - return; - } - - Uri uri = Uri.parse(RedditUtils.OAUTH_API_BASE_URI + RedditUtils.SUBSCRIBED_SUBREDDITS) - .buildUpon().appendQueryParameter(RedditUtils.AFTER_KEY, mLastItem) - .appendQueryParameter(RedditUtils.RAW_JSON_KEY, RedditUtils.RAW_JSON_VALUE).build(); - - mFetchSubscribedSubredditsListener = fetchUserInfoListener; - StringRequest commentRequest = new StringRequest(Request.Method.GET, uri.toString(), new Response.Listener() { - @Override - public void onResponse(String response) { - new ParseSubscribedSubreddits().parseSubscribedSubreddits(response, mSubredditData, - new ParseSubscribedSubreddits.ParseSubscribedSubredditsListener() { - @Override - public void onParseSubscribedSubredditsSuccess(ArrayList subredditData, String lastItem) { - mSubredditData = subredditData; - mLastItem = lastItem; - Log.i("last item", lastItem); - if(mLastItem.equals("null")) { - mFetchSubscribedSubredditsListener.onFetchSubscribedSubredditsSuccess(mSubredditData); - } else { - fetchSubscribedSubreddits(mFetchSubscribedSubredditsListener, refreshTime); - } - } - - @Override - public void onParseSubscribedSubredditsFail() { - - } - }); - } - }, new Response.ErrorListener() { - @Override - public void onErrorResponse(VolleyError error) { - if(error instanceof AuthFailureError) { - new AcquireAccessToken(context).refreshAccessToken(requestQueue, new AcquireAccessToken.AcquireAccessTokenListener() { - @Override - public void onAcquireAccessTokenSuccess() { - fetchSubscribedSubreddits(mFetchSubscribedSubredditsListener, refreshTime - 1); - } - - @Override - public void onAcquireAccessTokenFail() {} - }); - } else { - mFetchSubscribedSubredditsListener.onFetchSubscribedSubredditsFail(); - } - } - }) { - @Override - public Map getHeaders() { - String accessToken = context.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); - return RedditUtils.getOAuthHeader(accessToken); - } - }; - commentRequest.setTag(FetchComment.class); - requestQueue.add(commentRequest); - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java new file mode 100644 index 00000000..4a902c16 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java @@ -0,0 +1,105 @@ +package ml.docilealligator.infinityforreddit; + +import android.content.Context; +import android.net.Uri; + +import com.android.volley.AuthFailureError; +import com.android.volley.Request; +import com.android.volley.RequestQueue; +import com.android.volley.Response; +import com.android.volley.VolleyError; +import com.android.volley.toolbox.StringRequest; + +import java.util.ArrayList; +import java.util.Map; + +class FetchSubscribedThing { + interface FetchSubscribedSubredditsListener { + void onFetchSubscribedSubredditsSuccess(ArrayList subscribedSubredditData, + ArrayList subscribedUserData); + void onFetchSubscribedSubredditsFail(); + } + + private Context context; + private RequestQueue requestQueue; + private FetchSubscribedSubredditsListener mFetchSubscribedSubredditsListener; + private ArrayList mSubscribedSubredditData; + private ArrayList mSubscribedUserData; + + private String mLastItem; + + FetchSubscribedThing(Context context, RequestQueue requestQueue, ArrayList subscribedSubredditData, + ArrayList subscribedUserData) { + this.context = context; + this.requestQueue = requestQueue; + mSubscribedSubredditData = subscribedSubredditData; + mSubscribedUserData = subscribedUserData; + } + + void fetchSubscribedSubreddits(FetchSubscribedSubredditsListener fetchUserInfoListener, final int refreshTime) { + if(refreshTime < 0) { + mFetchSubscribedSubredditsListener.onFetchSubscribedSubredditsFail(); + return; + } + + Uri uri = Uri.parse(RedditUtils.OAUTH_API_BASE_URI + RedditUtils.SUBSCRIBED_SUBREDDITS) + .buildUpon().appendQueryParameter(RedditUtils.AFTER_KEY, mLastItem) + .appendQueryParameter(RedditUtils.RAW_JSON_KEY, RedditUtils.RAW_JSON_VALUE).build(); + + mFetchSubscribedSubredditsListener = fetchUserInfoListener; + StringRequest commentRequest = new StringRequest(Request.Method.GET, uri.toString(), new Response.Listener() { + @Override + public void onResponse(String response) { + new ParseSubscribedThing().parseSubscribedSubreddits(response, mSubscribedSubredditData, + mSubscribedUserData, + new ParseSubscribedThing.ParseSubscribedSubredditsListener() { + + @Override + public void onParseSubscribedSubredditsSuccess(ArrayList subscribedSubredditData, + ArrayList subscribedUserData, + String lastItem) { + mSubscribedSubredditData = subscribedSubredditData; + mSubscribedUserData = subscribedUserData; + mLastItem = lastItem; + if(mLastItem.equals("null")) { + mFetchSubscribedSubredditsListener.onFetchSubscribedSubredditsSuccess(mSubscribedSubredditData, + mSubscribedUserData); + } else { + fetchSubscribedSubreddits(mFetchSubscribedSubredditsListener, refreshTime); + } + } + + @Override + public void onParseSubscribedSubredditsFail() { + + } + }); + } + }, new Response.ErrorListener() { + @Override + public void onErrorResponse(VolleyError error) { + if(error instanceof AuthFailureError) { + new AcquireAccessToken(context).refreshAccessToken(requestQueue, new AcquireAccessToken.AcquireAccessTokenListener() { + @Override + public void onAcquireAccessTokenSuccess() { + fetchSubscribedSubreddits(mFetchSubscribedSubredditsListener, refreshTime - 1); + } + + @Override + public void onAcquireAccessTokenFail() {} + }); + } else { + mFetchSubscribedSubredditsListener.onFetchSubscribedSubredditsFail(); + } + } + }) { + @Override + public Map getHeaders() { + String accessToken = context.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); + return RedditUtils.getOAuthHeader(accessToken); + } + }; + commentRequest.setTag(FetchComment.class); + requestQueue.add(commentRequest); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java index 9f5ae9fc..35484961 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java @@ -57,4 +57,7 @@ class JSONUtils { static final String LINK_KARMA_KEY = "link_karma"; static final String COMMENT_KARMA_KEY = "comment_karma"; static final String DISPLAY_NAME = "display_name"; + static final String SUBREDDIT_TYPE_KEY = "subreddit_type"; + + static final String SUBREDDIT_TYPE_VALUE_USER = "user"; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index 92b73aae..a7f07ed5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -2,10 +2,14 @@ package ml.docilealligator.infinityforreddit; import android.app.Fragment; import android.app.FragmentTransaction; +import android.arch.lifecycle.Observer; +import android.arch.lifecycle.ViewModelProviders; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.os.AsyncTask; import android.os.Bundle; +import android.support.annotation.Nullable; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; @@ -22,8 +26,7 @@ import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; +import java.util.List; import de.hdodenhof.circleimageview.CircleImageView; @@ -38,7 +41,6 @@ public class MainActivity extends AppCompatActivity { private TextView mKarmaTextView; private CircleImageView mProfileImageView; private ImageView mBannerImageView; - private RecyclerView mSubscribedSubredditRecyclerView; private Fragment mFragment; private RequestManager glide; @@ -49,7 +51,8 @@ public class MainActivity extends AppCompatActivity { private String mKarma; private boolean mFetchUserInfoSuccess; - private ArrayList mSubredditData; + private SubscribedSubredditViewModel mSubscribedSubredditViewModel; + private SubscribedUserViewModel mSubscribedUserViewModel; @Override protected void onCreate(Bundle savedInstanceState) { @@ -70,8 +73,15 @@ public class MainActivity extends AppCompatActivity { mProfileImageView = header.findViewById(R.id.profile_image_view_nav_header_main); mBannerImageView = header.findViewById(R.id.banner_image_view_nav_header_main); - mSubscribedSubredditRecyclerView = findViewById(R.id.subscribed_subreddit_recycler_view_main_activity); - mSubscribedSubredditRecyclerView.setLayoutManager(new LinearLayoutManager(this)); + RecyclerView subscribedSubredditRecyclerView = findViewById(R.id.subscribed_subreddit_recycler_view_main_activity); + subscribedSubredditRecyclerView.setLayoutManager(new LinearLayoutManager(this)); + subscribedSubredditRecyclerView.setNestedScrollingEnabled(false); + final TextView subscriptionsLabelTextView = findViewById(R.id.subscriptions_label_main_activity); + + RecyclerView subscribedUserRecyclerView = findViewById(R.id.subscribed_user_recycler_view_main_activity); + subscribedUserRecyclerView.setLayoutManager(new LinearLayoutManager(this)); + subscribedUserRecyclerView.setNestedScrollingEnabled(false); + final TextView followingLabelTextView = findViewById(R.id.following_label_main_activity); mName = getSharedPreferences(SharedPreferencesUtils.USER_INFO_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.USER_KEY, ""); mProfileImageUrl = getSharedPreferences(SharedPreferencesUtils.USER_INFO_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.PROFILE_IMAGE_URL_KEY, ""); @@ -147,19 +157,55 @@ public class MainActivity extends AppCompatActivity { } }, 1); } - new FetchSubscribedSubreddits(this, Volley.newRequestQueue(this), new ArrayList()) - .fetchSubscribedSubreddits(new FetchSubscribedSubreddits.FetchSubscribedSubredditsListener() { + + final SubscribedSubredditRecyclerViewAdapter subredditadapter = new SubscribedSubredditRecyclerViewAdapter(this); + subscribedSubredditRecyclerView.setAdapter(subredditadapter); + mSubscribedSubredditViewModel = ViewModelProviders.of(this).get(SubscribedSubredditViewModel.class); + mSubscribedSubredditViewModel.getAllSubscribedSubreddits().observe(this, new Observer>() { + @Override + public void onChanged(@Nullable final List subscribedSubredditData) { + // Update the cached copy of the words in the adapter. + if(subscribedSubredditData == null || subscribedSubredditData.size() == 0) { + subscriptionsLabelTextView.setVisibility(View.GONE); + } else { + subscriptionsLabelTextView.setVisibility(View.VISIBLE); + } + + subredditadapter.setSubscribedSubreddits(subscribedSubredditData); + } + }); + + final SubscribedUserRecyclerViewAdapter userAdapter = new SubscribedUserRecyclerViewAdapter(this); + subscribedUserRecyclerView.setAdapter(userAdapter); + mSubscribedUserViewModel = ViewModelProviders.of(this).get(SubscribedUserViewModel.class); + mSubscribedUserViewModel.getAllSubscribedUsers().observe(this, new Observer>() { + @Override + public void onChanged(@Nullable final List subscribedUserData) { + // Update the cached copy of the words in the adapter. + if(subscribedUserData == null || subscribedUserData.size() == 0) { + followingLabelTextView.setVisibility(View.GONE); + } else { + followingLabelTextView.setVisibility(View.VISIBLE); + } + userAdapter.setSubscribedUsers(subscribedUserData); + } + }); + + new FetchSubscribedThing(this, Volley.newRequestQueue(this), new ArrayList(), + new ArrayList()) + .fetchSubscribedSubreddits(new FetchSubscribedThing.FetchSubscribedSubredditsListener() { @Override - public void onFetchSubscribedSubredditsSuccess(ArrayList subredditData) { - Collections.sort(subredditData, new Comparator() { - @Override - public int compare(SubredditData subredditData, SubredditData t1) { - return subredditData.getName().toLowerCase().compareTo(t1.getName().toLowerCase()); - } - }); - mSubredditData = subredditData; - mSubscribedSubredditRecyclerView.setAdapter(new SubscribedSubredditRecyclerViewAdapter( - MainActivity.this, mSubredditData)); + public void onFetchSubscribedSubredditsSuccess(ArrayList subscribedSubredditData, + ArrayList subscribedUserData) { + new InsertSubscribedThingsAsyncTask( + SubscribedSubredditRoomDatabase.getDatabase(MainActivity.this), + SubscribedUserRoomDatabase.getDatabase(MainActivity.this), + subscribedSubredditData, + subscribedUserData).execute(); + + /*new InsertSubscribedUsersAsyncTask( + SubscribedUserRoomDatabase.getDatabase(MainActivity.this), + subscribedUserData).execute();*/ } @Override @@ -207,4 +253,52 @@ public class MainActivity extends AppCompatActivity { glide.load(mBannerImageUrl).into(mBannerImageView); } } + + private static class InsertSubscribedThingsAsyncTask extends AsyncTask { + + private final SubscribedSubredditDao mSubredditDao; + private final SubscribedUserDao mUserDao; + private List subscribedSubredditData; + private List subscribedUserData; + + InsertSubscribedThingsAsyncTask(SubscribedSubredditRoomDatabase subredditDb, + SubscribedUserRoomDatabase userDb, + List subscribedSubredditData, + List subscribedUserData) { + mSubredditDao = subredditDb.subscribedSubredditDao(); + mUserDao = userDb.subscribedUserDao(); + this.subscribedSubredditData = subscribedSubredditData; + this.subscribedUserData = subscribedUserData; + } + + @Override + protected Void doInBackground(final Void... params) { + for(SubscribedSubredditData s : subscribedSubredditData) { + mSubredditDao.insert(s); + } + for(SubscribedUserData s : subscribedUserData) { + mUserDao.insert(s); + } + 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/ParseBestPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseBestPost.java index a0e93404..e8168e0f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseBestPost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseBestPost.java @@ -152,7 +152,7 @@ class ParseBestPost { videoPostData.setDownloadableGifOrVideo(false); bestPostData.add(videoPostData); - } else { + } else if(data.has(JSONUtils.PREVIEW_KEY)){ JSONObject variations = data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0); if (variations.has(JSONUtils.VARIANTS_KEY) && variations.getJSONObject(JSONUtils.VARIANTS_KEY).has(JSONUtils.MP4_KEY)) { //Gif video post (MP4) @@ -194,6 +194,19 @@ class ParseBestPost { bestPostData.add(linkPostData); } } + } else { + if (url.endsWith("jpg") || url.endsWith("png")) { + //Image post + Log.i("CP no preview image", Integer.toString(i)); + int postType = BestPostData.IMAGE_TYPE; + bestPostData.add(new BestPostData(id, fullName, subredditName, formattedPostTime, title, url, url, permalink, score, postType, voteType, nsfw)); + } else { + //Link post + Log.i("CP no preview link", Integer.toString(i)); + int postType = BestPostData.LINK_TYPE; + BestPostData linkPostData = new BestPostData(id, fullName, subredditName, formattedPostTime, title, previewUrl, url, permalink, score, postType, voteType, nsfw); + bestPostData.add(linkPostData); + } } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedSubreddits.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedSubreddits.java deleted file mode 100644 index 24d83b40..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedSubreddits.java +++ /dev/null @@ -1,72 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import android.os.AsyncTask; -import android.util.Log; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import java.util.ArrayList; - -class ParseSubscribedSubreddits { - interface ParseSubscribedSubredditsListener { - void onParseSubscribedSubredditsSuccess(ArrayList subredditData, String lastItem); - void onParseSubscribedSubredditsFail(); - } - - private ParseSubscribedSubredditsListener mParseSubscribedSubredditsListener; - - void parseSubscribedSubreddits(String response, ArrayList subredditData, - ParseSubscribedSubredditsListener parseSubscribedSubredditsListener) { - mParseSubscribedSubredditsListener = parseSubscribedSubredditsListener; - new ParseSubscribedSubredditsAsyncTask(response, subredditData).execute(); - } - - private class ParseSubscribedSubredditsAsyncTask extends AsyncTask { - private JSONObject jsonResponse; - private boolean parseFailed; - private String lastItem; - private ArrayList subredditData; - private ArrayList newSubredditData; - - ParseSubscribedSubredditsAsyncTask(String response, ArrayList subredditData){ - try { - jsonResponse = new JSONObject(response); - parseFailed = false; - this.subredditData = subredditData; - newSubredditData = new ArrayList<>(); - } catch (JSONException e) { - Log.i("user info json error", e.getMessage()); - mParseSubscribedSubredditsListener.onParseSubscribedSubredditsFail(); - } - } - - @Override - protected Void doInBackground(Void... voids) { - 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); - newSubredditData.add(new SubredditData(name, iconUrl)); - } - lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); - } catch (JSONException e) { - parseFailed = true; - Log.i("parse comment error", e.getMessage()); - } - return null; - } - - @Override - protected void onPostExecute(Void aVoid) { - if(!parseFailed) { - subredditData.addAll(newSubredditData); - mParseSubscribedSubredditsListener.onParseSubscribedSubredditsSuccess(subredditData, lastItem); - } else { - mParseSubscribedSubredditsListener.onParseSubscribedSubredditsFail(); - } - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java new file mode 100644 index 00000000..4aed5743 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java @@ -0,0 +1,93 @@ +package ml.docilealligator.infinityforreddit; + +import android.os.AsyncTask; +import android.util.Log; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.ArrayList; + +class ParseSubscribedThing { + interface ParseSubscribedSubredditsListener { + void onParseSubscribedSubredditsSuccess(ArrayList subscribedSubredditData, + ArrayList subscribedUserData, + String lastItem); + void onParseSubscribedSubredditsFail(); + } + + private ParseSubscribedSubredditsListener mParseSubscribedSubredditsListener; + + void parseSubscribedSubreddits(String response, ArrayList subscribedSubredditData, + ArrayList subscribedUserData, + ParseSubscribedSubredditsListener parseSubscribedSubredditsListener) { + mParseSubscribedSubredditsListener = parseSubscribedSubredditsListener; + new ParseSubscribedSubredditsAsyncTask(response, subscribedSubredditData, subscribedUserData).execute(); + } + + private class ParseSubscribedSubredditsAsyncTask extends AsyncTask { + private JSONObject jsonResponse; + private boolean parseFailed; + private String lastItem; + private ArrayList subscribedSubredditData; + private ArrayList subscribedUserData; + private ArrayList newSubscribedSubredditData; + private ArrayList newSubscribedUserData; + + ParseSubscribedSubredditsAsyncTask(String response, ArrayList subscribedSubredditData, + ArrayList subscribedUserData){ + try { + jsonResponse = new JSONObject(response); + parseFailed = false; + this.subscribedSubredditData = subscribedSubredditData; + this.subscribedUserData = subscribedUserData; + newSubscribedSubredditData = new ArrayList<>(); + newSubscribedUserData = new ArrayList<>(); + } catch (JSONException e) { + Log.i("user info json error", e.getMessage()); + mParseSubscribedSubredditsListener.onParseSubscribedSubredditsFail(); + } + } + + @Override + protected Void doInBackground(Void... voids) { + 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); + String id = children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.NAME_KEY); + if(iconUrl.equals("null")) { + iconUrl = ""; + } + + if(children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.SUBREDDIT_TYPE_KEY) + .equals(JSONUtils.SUBREDDIT_TYPE_VALUE_USER)) { + //It's a user + newSubscribedUserData.add(new SubscribedUserData(id, name.substring(2), iconUrl)); + } else { + newSubscribedSubredditData.add(new SubscribedSubredditData(id, name, iconUrl)); + } + } + lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); + } catch (JSONException e) { + parseFailed = true; + Log.i("parse comment error", e.getMessage()); + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + if(!parseFailed) { + subscribedSubredditData.addAll(newSubscribedSubredditData); + subscribedUserData.addAll(newSubscribedUserData); + mParseSubscribedSubredditsListener.onParseSubscribedSubredditsSuccess(subscribedSubredditData, + subscribedUserData, lastItem); + } else { + mParseSubscribedSubredditsListener.onParseSubscribedSubredditsFail(); + } + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditData.java deleted file mode 100644 index dcf0cdaf..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditData.java +++ /dev/null @@ -1,50 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import android.os.Parcel; -import android.os.Parcelable; - -class SubredditData implements Parcelable { - private String name; - private String iconUrl; - - SubredditData(String name, String iconUrl) { - this.name = name; - this.iconUrl = iconUrl; - } - - protected SubredditData(Parcel in) { - name = in.readString(); - iconUrl = in.readString(); - } - - public static final Creator CREATOR = new Creator() { - @Override - public SubredditData createFromParcel(Parcel in) { - return new SubredditData(in); - } - - @Override - public SubredditData[] newArray(int size) { - return new SubredditData[size]; - } - }; - - public String getName() { - return name; - } - - public String getIconUrl() { - return iconUrl; - } - - @Override - public int describeContents() { - return 0; - } - - @Override - public void writeToParcel(Parcel parcel, int i) { - parcel.writeString(name); - parcel.writeString(iconUrl); - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDao.java new file mode 100644 index 00000000..9ced85c1 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDao.java @@ -0,0 +1,21 @@ +package ml.docilealligator.infinityforreddit; + +import android.arch.lifecycle.LiveData; +import android.arch.persistence.room.Dao; +import android.arch.persistence.room.Insert; +import android.arch.persistence.room.OnConflictStrategy; +import android.arch.persistence.room.Query; + +import java.util.List; + +@Dao +public interface SubscribedSubredditDao { + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insert(SubscribedSubredditData subscribedSubredditData); + + @Query("DELETE FROM subscribed_subreddits") + void deleteAllSubscribedSubreddits(); + + @Query("SELECT * from subscribed_subreddits ORDER BY name COLLATE NOCASE ASC") + LiveData> getAllSubscribedSubreddits(); +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditData.java new file mode 100644 index 00000000..10b2c8e0 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditData.java @@ -0,0 +1,38 @@ +package ml.docilealligator.infinityforreddit; + +import android.arch.persistence.room.ColumnInfo; +import android.arch.persistence.room.Entity; +import android.arch.persistence.room.PrimaryKey; +import android.support.annotation.NonNull; + +@Entity(tableName = "subscribed_subreddits") +class SubscribedSubredditData { + + @PrimaryKey + @NonNull + @ColumnInfo(name = "id") + private String id; + @ColumnInfo(name = "name") + private String name; + @ColumnInfo(name = "icon") + private String iconUrl; + + SubscribedSubredditData(@NonNull String id, String name, String iconUrl) { + this.id = id; + this.name = name; + this.iconUrl = iconUrl; + } + + @NonNull + public String getId() { + return id; + } + + public String getName() { + return name; + } + + public String getIconUrl() { + return iconUrl; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRecyclerViewAdapter.java index 73f40a40..88d194d7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRecyclerViewAdapter.java @@ -11,18 +11,17 @@ import android.widget.TextView; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; -import java.util.ArrayList; +import java.util.List; import de.hdodenhof.circleimageview.CircleImageView; class SubscribedSubredditRecyclerViewAdapter extends RecyclerView.Adapter { - Context mContext; - ArrayList mSubredditData; - RequestManager glide; + private Context mContext; + private List mSubscribedSubredditData; + private RequestManager glide; - SubscribedSubredditRecyclerViewAdapter(Context context, ArrayList subredditData) { + SubscribedSubredditRecyclerViewAdapter(Context context) { mContext = context; - mSubredditData = subredditData; glide = Glide.with(context); } @@ -34,17 +33,20 @@ class SubscribedSubredditRecyclerViewAdapter extends RecyclerView.Adapter subscribedSubreddits){ + mSubscribedSubredditData = subscribedSubreddits; + notifyDataSetChanged(); + } + + private class SubredditViewHolder extends RecyclerView.ViewHolder { - private CircleImageView iconCircleImageView; - private TextView subredditNameTextView; + private final CircleImageView iconCircleImageView; + private final TextView subredditNameTextView; public SubredditViewHolder(View itemView) { super(itemView); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRepository.java new file mode 100644 index 00000000..3173bba4 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRepository.java @@ -0,0 +1,41 @@ +package ml.docilealligator.infinityforreddit; + +import android.app.Application; +import android.arch.lifecycle.LiveData; +import android.os.AsyncTask; + +import java.util.List; + +public class SubscribedSubredditRepository { + private SubscribedSubredditDao mSubscribedSubredditDao; + private LiveData> mAllSubscribedSubreddits; + + SubscribedSubredditRepository(Application application) { + SubscribedSubredditRoomDatabase db = SubscribedSubredditRoomDatabase.getDatabase(application); + mSubscribedSubredditDao = db.subscribedSubredditDao(); + mAllSubscribedSubreddits = mSubscribedSubredditDao.getAllSubscribedSubreddits(); + } + + LiveData> getAllSubscribedSubreddits() { + return mAllSubscribedSubreddits; + } + + public void insert(SubscribedSubredditData subscribedSubredditData) { + new insertAsyncTask(mSubscribedSubredditDao).execute(subscribedSubredditData); + } + + private static class insertAsyncTask extends AsyncTask { + + private SubscribedSubredditDao mAsyncTaskDao; + + insertAsyncTask(SubscribedSubredditDao dao) { + mAsyncTaskDao = dao; + } + + @Override + protected Void doInBackground(final SubscribedSubredditData... params) { + mAsyncTaskDao.insert(params[0]); + return null; + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRoomDatabase.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRoomDatabase.java new file mode 100644 index 00000000..d4b1f710 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRoomDatabase.java @@ -0,0 +1,26 @@ +package ml.docilealligator.infinityforreddit; + +import android.arch.persistence.room.Database; +import android.arch.persistence.room.Room; +import android.arch.persistence.room.RoomDatabase; +import android.content.Context; + +@Database(entities = {SubscribedSubredditData.class}, version = 1) +public abstract class SubscribedSubredditRoomDatabase extends RoomDatabase { + private static SubscribedSubredditRoomDatabase INSTANCE; + + public abstract SubscribedSubredditDao subscribedSubredditDao(); + + public static SubscribedSubredditRoomDatabase getDatabase(final Context context) { + if(INSTANCE == null) { + synchronized (SubscribedSubredditRoomDatabase.class) { + if(INSTANCE == null) { + INSTANCE = Room.databaseBuilder(context.getApplicationContext(), + SubscribedSubredditRoomDatabase.class, "subscribed_subreddits") + .build(); + } + } + } + return INSTANCE; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditViewModel.java new file mode 100644 index 00000000..bd092640 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditViewModel.java @@ -0,0 +1,26 @@ +package ml.docilealligator.infinityforreddit; + +import android.app.Application; +import android.arch.lifecycle.AndroidViewModel; +import android.arch.lifecycle.LiveData; + +import java.util.List; + +public class SubscribedSubredditViewModel extends AndroidViewModel { + private SubscribedSubredditRepository mSubscribedSubredditRepository; + private LiveData> mAllSubscribedSubreddits; + + public SubscribedSubredditViewModel(Application application) { + super(application); + mSubscribedSubredditRepository = new SubscribedSubredditRepository(application); + mAllSubscribedSubreddits = mSubscribedSubredditRepository.getAllSubscribedSubreddits(); + } + + public LiveData> getAllSubscribedSubreddits() { + return mAllSubscribedSubreddits; + } + + public void insert(SubscribedSubredditData subscribedSubredditData) { + mSubscribedSubredditRepository.insert(subscribedSubredditData); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDao.java new file mode 100644 index 00000000..a2979dcd --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDao.java @@ -0,0 +1,21 @@ +package ml.docilealligator.infinityforreddit; + +import android.arch.lifecycle.LiveData; +import android.arch.persistence.room.Dao; +import android.arch.persistence.room.Insert; +import android.arch.persistence.room.OnConflictStrategy; +import android.arch.persistence.room.Query; + +import java.util.List; + +@Dao +public interface SubscribedUserDao { + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insert(SubscribedUserData subscribedUserData); + + @Query("DELETE FROM subscribed_users") + void deleteAllSubscribedUsers(); + + @Query("SELECT * from subscribed_users ORDER BY name COLLATE NOCASE ASC") + LiveData> getAllSubscribedUsers(); +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserData.java new file mode 100644 index 00000000..9bf58700 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserData.java @@ -0,0 +1,37 @@ +package ml.docilealligator.infinityforreddit; + +import android.arch.persistence.room.ColumnInfo; +import android.arch.persistence.room.Entity; +import android.arch.persistence.room.PrimaryKey; +import android.support.annotation.NonNull; + +@Entity(tableName = "subscribed_users") +class SubscribedUserData { + @PrimaryKey + @NonNull + @ColumnInfo(name = "id") + private String id; + @ColumnInfo(name = "name") + private String name; + @ColumnInfo(name = "icon") + private String iconUrl; + + SubscribedUserData(@NonNull String id, String name, String iconUrl) { + this.id = id; + this.name = name; + this.iconUrl = iconUrl; + } + + @NonNull + public String getId() { + return id; + } + + public String getName() { + return name; + } + + public String getIconUrl() { + return iconUrl; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserRecyclerViewAdapter.java new file mode 100644 index 00000000..cb779a2c --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserRecyclerViewAdapter.java @@ -0,0 +1,73 @@ +package ml.docilealligator.infinityforreddit; + +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import com.bumptech.glide.Glide; +import com.bumptech.glide.RequestManager; + +import java.util.List; + +import de.hdodenhof.circleimageview.CircleImageView; + +class SubscribedUserRecyclerViewAdapter extends RecyclerView.Adapter { + private Context mContext; + private List mSubscribedUserData; + private RequestManager glide; + + SubscribedUserRecyclerViewAdapter(Context context) { + mContext = context; + glide = Glide.with(context); + } + + @NonNull + @Override + public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { + return new UserViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_subscribed_subreddit, viewGroup, false)); + } + + @Override + public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) { + if(!mSubscribedUserData.get(i).getIconUrl().equals("")) { + glide.load(mSubscribedUserData.get(i).getIconUrl()).into(((UserViewHolder) viewHolder).iconCircleImageView); + } else { + glide.load(R.drawable.subreddit_default_icon).into(((UserViewHolder) viewHolder).iconCircleImageView); + } + ((UserViewHolder) viewHolder).subredditNameTextView.setText(mSubscribedUserData.get(i).getName()); + } + + @Override + public int getItemCount() { + if(mSubscribedUserData != null) { + return mSubscribedUserData.size(); + } + return 0; + } + + @Override + public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) { + glide.clear(((UserViewHolder) holder).iconCircleImageView); + } + + void setSubscribedUsers(List subscribedUsers){ + mSubscribedUserData = subscribedUsers; + notifyDataSetChanged(); + } + + + private class UserViewHolder extends RecyclerView.ViewHolder { + private final CircleImageView iconCircleImageView; + private final TextView subredditNameTextView; + + public UserViewHolder(View itemView) { + super(itemView); + iconCircleImageView = itemView.findViewById(R.id.subreddit_icon_circle_image_view_item_subscribed_subreddit); + subredditNameTextView = itemView.findViewById(R.id.subreddit_name_text_view_item_subscribed_subreddit); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserRepository.java new file mode 100644 index 00000000..8dfc4204 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserRepository.java @@ -0,0 +1,41 @@ +package ml.docilealligator.infinityforreddit; + +import android.app.Application; +import android.arch.lifecycle.LiveData; +import android.os.AsyncTask; + +import java.util.List; + +public class SubscribedUserRepository { + private SubscribedUserDao mSubscribedUserDao; + private LiveData> mAllSubscribedUsers; + + SubscribedUserRepository(Application application) { + SubscribedUserRoomDatabase db = SubscribedUserRoomDatabase.getDatabase(application); + mSubscribedUserDao = db.subscribedUserDao(); + mAllSubscribedUsers = mSubscribedUserDao.getAllSubscribedUsers(); + } + + LiveData> getAllSubscribedSubreddits() { + return mAllSubscribedUsers; + } + + public void insert(SubscribedUserData subscribedUserData) { + new SubscribedUserRepository.insertAsyncTask(mSubscribedUserDao).execute(subscribedUserData); + } + + private static class insertAsyncTask extends AsyncTask { + + private SubscribedUserDao mAsyncTaskDao; + + insertAsyncTask(SubscribedUserDao dao) { + mAsyncTaskDao = dao; + } + + @Override + protected Void doInBackground(final SubscribedUserData... params) { + mAsyncTaskDao.insert(params[0]); + return null; + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserRoomDatabase.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserRoomDatabase.java new file mode 100644 index 00000000..0f6575d7 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserRoomDatabase.java @@ -0,0 +1,26 @@ +package ml.docilealligator.infinityforreddit; + +import android.arch.persistence.room.Database; +import android.arch.persistence.room.Room; +import android.arch.persistence.room.RoomDatabase; +import android.content.Context; + +@Database(entities = {SubscribedUserData.class}, version = 1) +public abstract class SubscribedUserRoomDatabase extends RoomDatabase { + private static SubscribedUserRoomDatabase INSTANCE; + + public abstract SubscribedUserDao subscribedUserDao(); + + public static SubscribedUserRoomDatabase getDatabase(final Context context) { + if(INSTANCE == null) { + synchronized (SubscribedUserRoomDatabase.class) { + if(INSTANCE == null) { + INSTANCE = Room.databaseBuilder(context.getApplicationContext(), + SubscribedUserRoomDatabase.class, "subscribed_users") + .build(); + } + } + } + return INSTANCE; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserViewModel.java new file mode 100644 index 00000000..44e40fe5 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserViewModel.java @@ -0,0 +1,26 @@ +package ml.docilealligator.infinityforreddit; + +import android.app.Application; +import android.arch.lifecycle.AndroidViewModel; +import android.arch.lifecycle.LiveData; + +import java.util.List; + +public class SubscribedUserViewModel extends AndroidViewModel { + private SubscribedUserRepository mSubscribedUserRepository; + private LiveData> mAllSubscribedUsers; + + public SubscribedUserViewModel(Application application) { + super(application); + mSubscribedUserRepository = new SubscribedUserRepository(application); + mAllSubscribedUsers = mSubscribedUserRepository.getAllSubscribedSubreddits(); + } + + public LiveData> getAllSubscribedUsers() { + return mAllSubscribedUsers; + } + + public void insert(SubscribedUserData subscribedUserData) { + mSubscribedUserRepository.insert(subscribedUserData); + } +} -- cgit v1.2.3