diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-01-01 15:32:03 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-01-01 15:32:03 +0000 |
commit | 49d25314d561e8f01b4d3c5dad5fdf95babad287 (patch) | |
tree | 04c2dc23b04d9c574c823c6cef7cfe3c030b5fbf /app/src/main/java/User/ParseUserData.java | |
parent | f529bba550b63ff1ea0b254c308257f5fd0c25ed (diff) | |
download | infinity-for-reddit-49d25314d561e8f01b4d3c5dad5fdf95babad287.tar infinity-for-reddit-49d25314d561e8f01b4d3c5dad5fdf95babad287.tar.gz infinity-for-reddit-49d25314d561e8f01b4d3c5dad5fdf95babad287.tar.bz2 infinity-for-reddit-49d25314d561e8f01b4d3c5dad5fdf95babad287.tar.lz infinity-for-reddit-49d25314d561e8f01b4d3c5dad5fdf95babad287.tar.xz infinity-for-reddit-49d25314d561e8f01b4d3c5dad5fdf95babad287.tar.zst infinity-for-reddit-49d25314d561e8f01b4d3c5dad5fdf95babad287.zip |
Display user name and user icon in each post when loading posts in specific subreddits.
Diffstat (limited to '')
-rw-r--r-- | app/src/main/java/User/ParseUserData.java | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/app/src/main/java/User/ParseUserData.java b/app/src/main/java/User/ParseUserData.java new file mode 100644 index 00000000..b6a7a39c --- /dev/null +++ b/app/src/main/java/User/ParseUserData.java @@ -0,0 +1,72 @@ +package User; + +import android.os.AsyncTask; +import android.util.Log; + +import org.json.JSONException; +import org.json.JSONObject; + +import ml.docilealligator.infinityforreddit.JSONUtils; + +public class ParseUserData { + interface ParseUserDataListener { + void onParseUserDataSuccess(User user); + void onParseUserDataFail(); + } + + static void parseMyInfo(String response, ParseUserDataListener parseUserDataListener) { + new ParseUserDataAsyncTask(response, parseUserDataListener).execute(); + } + + private static class ParseUserDataAsyncTask extends AsyncTask<Void, Void, Void> { + private JSONObject jsonResponse; + private ParseUserDataListener parseUserDataListener; + private boolean parseFailed; + + private User user; + + ParseUserDataAsyncTask(String response, ParseUserDataListener parseUserDataListener){ + try { + jsonResponse = new JSONObject(response); + this.parseUserDataListener = parseUserDataListener; + parseFailed = false; + } catch (JSONException e) { + Log.i("user data json error", e.getMessage()); + parseUserDataListener.onParseUserDataFail(); + } + } + + @Override + protected Void doInBackground(Void... voids) { + try { + jsonResponse = jsonResponse.getJSONObject(JSONUtils.DATA_KEY); + String userName = jsonResponse.getString(JSONUtils.NAME_KEY); + String iconImageUrl = jsonResponse.getString(JSONUtils.ICON_IMG_KEY); + String bannerImageUrl = ""; + if(!jsonResponse.isNull(JSONUtils.SUBREDDIT_KEY)) { + bannerImageUrl = jsonResponse.getJSONObject(JSONUtils.SUBREDDIT_KEY).getString(JSONUtils.BANNER_IMG_KEY); + } + int linkKarma = jsonResponse.getInt(JSONUtils.LINK_KARMA_KEY); + int commentKarma = jsonResponse.getInt(JSONUtils.COMMENT_KARMA_KEY); + int karma = linkKarma + commentKarma; + boolean isGold = jsonResponse.getBoolean(JSONUtils.IS_GOLD_KEY); + boolean isFriend = jsonResponse.getBoolean(JSONUtils.IS_FRIEND_KEY); + + user = new User(userName, iconImageUrl, bannerImageUrl, karma, isGold, isFriend); + } catch (JSONException e) { + parseFailed = true; + Log.i("parse user data error", e.getMessage()); + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + if(!parseFailed) { + parseUserDataListener.onParseUserDataSuccess(user); + } else { + parseUserDataListener.onParseUserDataFail(); + } + } + } +} |