diff options
13 files changed, 288 insertions, 58 deletions
diff --git a/.idea/assetWizardSettings.xml b/.idea/assetWizardSettings.xml deleted file mode 100644 index 6423ac9f..00000000 --- a/.idea/assetWizardSettings.xml +++ /dev/null @@ -1,52 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<project version="4"> - <component name="WizardSettings"> - <option name="children"> - <map> - <entry key="imageWizard"> - <value> - <PersistentState /> - </value> - </entry> - <entry key="vectorWizard"> - <value> - <PersistentState> - <option name="children"> - <map> - <entry key="vectorAssetStep"> - <value> - <PersistentState> - <option name="children"> - <map> - <entry key="clipartAsset"> - <value> - <PersistentState> - <option name="values"> - <map> - <entry key="url" value="jar:file:/home/alex/Android%20Studio/plugins/android/lib/android.jar!/images/material_design_icons/action/ic_account_circle_black_24dp.xml" /> - </map> - </option> - </PersistentState> - </value> - </entry> - </map> - </option> - <option name="values"> - <map> - <entry key="color" value="9e9e9e" /> - <entry key="outputName" value="ic_account_circle_grey_24dp" /> - <entry key="sourceFile" value="$USER_HOME$" /> - </map> - </option> - </PersistentState> - </value> - </entry> - </map> - </option> - </PersistentState> - </value> - </entry> - </map> - </option> - </component> -</project>
\ No newline at end of file diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser Binary files differindex 57044362..2ec6cab5 100644 --- a/.idea/caches/build_file_checksums.ser +++ b/.idea/caches/build_file_checksums.ser diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java index 5bba9261..5d89497f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java @@ -25,7 +25,7 @@ class FetchComment { void queryComment(FetchCommentListener fetchCommentListener) { mFetchCommentListener = fetchCommentListener; - StringRequest commentRequest = new StringRequest(Request.Method.GET, RedditUtils.getQueryCommentURI(subredditName, article), new Response.Listener<String>() { + StringRequest commentRequest = new StringRequest(Request.Method.GET, RedditUtils.getQueryCommentUri(subredditName, article), new Response.Listener<String>() { @Override public void onResponse(String response) { mFetchCommentListener.onFetchCommentSuccess(response); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java new file mode 100644 index 00000000..00d07c26 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java @@ -0,0 +1,40 @@ +package ml.docilealligator.infinityforreddit; + +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; + +class FetchSubredditData { + interface FetchSubredditDataListener { + void onFetchSubredditDataSuccess(String response); + void onFetchSubredditDataFail(); + } + + private RequestQueue requestQueue; + private String subredditName; + private FetchSubredditDataListener mFetchSubredditDataListener; + + FetchSubredditData(RequestQueue requestQueue, String subredditName) { + this.requestQueue = requestQueue; + this.subredditName = subredditName; + } + + void querySubredditData(FetchSubredditDataListener fetchSubredditDataListener) { + mFetchSubredditDataListener = fetchSubredditDataListener; + StringRequest commentRequest = new StringRequest(Request.Method.GET, RedditUtils.getQuerySubredditDataUri(subredditName), new Response.Listener<String>() { + @Override + public void onResponse(String response) { + mFetchSubredditDataListener.onFetchSubredditDataSuccess(response); + } + }, new Response.ErrorListener() { + @Override + public void onErrorResponse(VolleyError error) { + mFetchSubredditDataListener.onFetchSubredditDataFail(); + } + }) {}; + commentRequest.setTag(FetchSubredditData.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 35484961..c766f786 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java @@ -58,6 +58,8 @@ class JSONUtils { 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"; + static final String SUBSCRIBERS_KEY = "subscribers"; + static final String PUBLIC_DESCRIPTION = "public_description"; + static final String ACTIVE_USER_COUNT = "active_user_count"; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java new file mode 100644 index 00000000..36cbaeeb --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java @@ -0,0 +1,65 @@ +package ml.docilealligator.infinityforreddit; + +import android.os.AsyncTask; +import android.util.Log; + +import org.json.JSONException; +import org.json.JSONObject; + +class ParseSubredditData { + interface ParseSubredditDataListener { + void onParseSubredditDataSuccess(); + void onParseSubredditDataFail(); + } + + private ParseSubredditDataListener mParseSubredditDataListener; + + void parseComment(String response, ParseSubredditDataListener parseSubredditDataListener) { + mParseSubredditDataListener = parseSubredditDataListener; + new ParseSubredditDataAsyncTask(response, mParseSubredditDataListener).execute(); + } + + private static class ParseSubredditDataAsyncTask extends AsyncTask<Void, Void, Void> { + private JSONObject jsonResponse; + private boolean parseFailed; + private ParseSubredditDataListener parseSubredditDataListener; + + ParseSubredditDataAsyncTask(String response, ParseSubredditDataListener parseSubredditDataListener){ + this.parseSubredditDataListener = parseSubredditDataListener; + try { + jsonResponse = new JSONObject(response); + parseFailed = false; + } catch (JSONException e) { + Log.i("comment json error", e.getMessage()); + parseSubredditDataListener.onParseSubredditDataFail(); + } + } + + @Override + protected Void doInBackground(Void... voids) { + try { + JSONObject data = jsonResponse.getJSONObject(JSONUtils.DATA_KEY); + String id = data.getString(JSONUtils.NAME_KEY); + 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); + } catch (JSONException e) { + parseFailed = true; + Log.i("parse", "SubredditData error"); + parseSubredditDataListener.onParseSubredditDataFail(); + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + if(!parseFailed) { + parseSubredditDataListener.onParseSubredditDataSuccess(); + } else { + parseSubredditDataListener.onParseSubredditDataFail(); + } + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java index c235d653..654942b1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java @@ -69,7 +69,11 @@ class RedditUtils { return params; } - static String getQueryCommentURI(String subredditName, String article) { + static String getQueryCommentUri(String subredditName, String article) { return API_BASE_URI + "/" + subredditName + "/comments/" + article + ".json"; } + + static String getQuerySubredditDataUri(String subredditName) { + return API_BASE_URI + "/r/" + subredditName + "/about.json"; + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDao.java new file mode 100644 index 00000000..7fb49f62 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDao.java @@ -0,0 +1,19 @@ +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; + +@Dao +public interface SubredditDao { + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insert(SubredditData SubredditData); + + @Query("DELETE FROM subreddits") + void deleteAllSubreddits(); + + @Query("SELECT * from subreddits WHERE id = :id") + LiveData<SubredditData> getSubreddit(String id); +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditData.java new file mode 100644 index 00000000..7f2095f7 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditData.java @@ -0,0 +1,63 @@ +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 = "subreddits") +class SubredditData { + @PrimaryKey + @NonNull + @ColumnInfo(name = "id") + private String id; + + @ColumnInfo(name = "name") + private String name; + + @ColumnInfo(name = "icon_url") + private String iconUrl; + + @ColumnInfo(name = "banner_url") + private String bannerUrl; + + @ColumnInfo(name = "description") + private String description; + + @ColumnInfo(name = "subscribers_count") + private int nSubscribers; + + SubredditData(@NonNull String id, String name, String iconUrl, String bannerUrl, String description, int nSubscribers) { + this.id = id; + this.name = name; + this.iconUrl = iconUrl; + this.bannerUrl = bannerUrl; + this.description = description; + this.nSubscribers = nSubscribers; + } + + @NonNull + public String getId() { + return id; + } + + public String getName() { + return name; + } + + public String getIconUrl() { + return iconUrl; + } + + public String getBannerUrl() { + return bannerUrl; + } + + public String getDescription() { + return description; + } + + public int getnSubscribers() { + return nSubscribers; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditRepository.java new file mode 100644 index 00000000..01320b3d --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditRepository.java @@ -0,0 +1,39 @@ +package ml.docilealligator.infinityforreddit; + +import android.app.Application; +import android.arch.lifecycle.LiveData; +import android.os.AsyncTask; + +public class SubredditRepository { + private SubredditDao mSubredditDao; + private LiveData<SubredditData> mSubredditData; + + SubredditRepository(Application application, String id) { + SubredditRoomDatabase db = SubredditRoomDatabase.getDatabase(application); + mSubredditDao = db.subredditDao(); + mSubredditData = mSubredditDao.getSubreddit(id); + } + + LiveData<SubredditData> getSubredditData() { + return mSubredditData; + } + + public void insert(SubredditData subredditData) { + new SubredditRepository.insertAsyncTask(mSubredditDao).execute(subredditData); + } + + private static class insertAsyncTask extends AsyncTask<SubredditData, Void, Void> { + + private SubredditDao mAsyncTaskDao; + + insertAsyncTask(SubredditDao dao) { + mAsyncTaskDao = dao; + } + + @Override + protected Void doInBackground(final SubredditData... params) { + mAsyncTaskDao.insert(params[0]); + return null; + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditRoomDatabase.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditRoomDatabase.java new file mode 100644 index 00000000..16affad3 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditRoomDatabase.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 = {SubredditData.class}, version = 1) +public abstract class SubredditRoomDatabase extends RoomDatabase{ + private static SubredditRoomDatabase INSTANCE; + + public abstract SubredditDao subredditDao(); + + public static SubredditRoomDatabase getDatabase(final Context context) { + if(INSTANCE == null) { + synchronized (SubredditRoomDatabase.class) { + if(INSTANCE == null) { + INSTANCE = Room.databaseBuilder(context.getApplicationContext(), + SubredditRoomDatabase.class, "subreddits") + .build(); + } + } + } + return INSTANCE; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditViewModel.java new file mode 100644 index 00000000..b96aa0db --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditViewModel.java @@ -0,0 +1,24 @@ +package ml.docilealligator.infinityforreddit; + +import android.app.Application; +import android.arch.lifecycle.AndroidViewModel; +import android.arch.lifecycle.LiveData; + +public class SubredditViewModel extends AndroidViewModel { + private SubredditRepository mSubredditRepository; + private LiveData<SubredditData> mSubredditData; + + public SubredditViewModel(Application application, String id) { + super(application); + mSubredditRepository = new SubredditRepository(application, id); + mSubredditData = mSubredditRepository.getSubredditData(); + } + + public LiveData<SubredditData> getSubredditData() { + return mSubredditData; + } + + public void insert(SubredditData subredditData) { + mSubredditRepository.insert(subredditData); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java index 3346aaea..6dc5b323 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java @@ -18,8 +18,8 @@ public class ViewSubredditDetailActivity extends AppCompatActivity { Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); - final CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout_view_subreddit_detail_activity); - AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.app_bar_layout_view_subreddit_detail_activity); + final CollapsingToolbarLayout collapsingToolbarLayout = findViewById(R.id.collapsing_toolbar_layout_view_subreddit_detail_activity); + AppBarLayout appBarLayout = findViewById(R.id.app_bar_layout_view_subreddit_detail_activity); appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { boolean isShow = true; int scrollRange = -1; @@ -39,7 +39,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity { } }); - FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); + FloatingActionButton fab = findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { |