diff options
Diffstat (limited to '')
-rw-r--r-- | app/src/main/java/User/FetchUserData.java | 50 | ||||
-rw-r--r-- | app/src/main/java/User/ParseUserData.java | 72 | ||||
-rw-r--r-- | app/src/main/java/User/User.java | 58 | ||||
-rw-r--r-- | app/src/main/java/User/UserDao.java | 18 | ||||
-rw-r--r-- | app/src/main/java/User/UserData.java | 4 | ||||
-rw-r--r-- | app/src/main/java/User/UserRoomDatabase.java | 26 |
6 files changed, 224 insertions, 4 deletions
diff --git a/app/src/main/java/User/FetchUserData.java b/app/src/main/java/User/FetchUserData.java new file mode 100644 index 00000000..edaa3b14 --- /dev/null +++ b/app/src/main/java/User/FetchUserData.java @@ -0,0 +1,50 @@ +package User; + +import android.support.annotation.NonNull; +import android.util.Log; + +import ml.docilealligator.infinityforreddit.RedditAPI; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Retrofit; + +public class FetchUserData { + public interface FetchUserDataListener { + void onFetchUserDataSuccess(User user); + void onFetchUserDataFail(); + } + + public static void fetchUserData(final Retrofit retrofit, String userName, + final FetchUserDataListener fetchUserDataListener) { + RedditAPI api = retrofit.create(RedditAPI.class); + + Call<String> userInfo = api.getUserData(userName); + userInfo.enqueue(new Callback<String>() { + @Override + public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { + if(response.isSuccessful()) { + ParseUserData.parseMyInfo(response.body(), new ParseUserData.ParseUserDataListener() { + @Override + public void onParseUserDataSuccess(User user) { + fetchUserDataListener.onFetchUserDataSuccess(user); + } + + @Override + public void onParseUserDataFail() { + fetchUserDataListener.onFetchUserDataFail(); + } + }); + } else { + Log.i("call failed", response.message()); + fetchUserDataListener.onFetchUserDataFail(); + } + } + + @Override + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + Log.i("call failed", t.getMessage()); + fetchUserDataListener.onFetchUserDataFail(); + } + }); + } +} 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(); + } + } + } +} diff --git a/app/src/main/java/User/User.java b/app/src/main/java/User/User.java new file mode 100644 index 00000000..3fd8dcb3 --- /dev/null +++ b/app/src/main/java/User/User.java @@ -0,0 +1,58 @@ +package User; + +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 = "users") +public class User { + @PrimaryKey + @NonNull + @ColumnInfo(name = "user_name") + private String userName; + @ColumnInfo(name = "icon") + private String icon; + @ColumnInfo(name = "banner") + private String banner; + @ColumnInfo(name = "karma") + private int karma; + @ColumnInfo(name = "is_gold") + private boolean isGold; + @ColumnInfo(name = "is_friend") + private boolean isFriend; + + User(@NonNull String userName, String icon, String banner, int karma, boolean isGold, boolean isFriend) { + this.userName = userName; + this.icon = icon; + this.banner = banner; + this.karma = karma; + this.isGold = isGold; + this.isFriend = isFriend; + } + + @NonNull + public String getUserName() { + return userName; + } + + public String getIcon() { + return icon; + } + + public String getBanner() { + return banner; + } + + public int getKarma() { + return karma; + } + + public boolean isGold() { + return isGold; + } + + public boolean isFriend() { + return isFriend; + } +} diff --git a/app/src/main/java/User/UserDao.java b/app/src/main/java/User/UserDao.java new file mode 100644 index 00000000..ec6bb7e2 --- /dev/null +++ b/app/src/main/java/User/UserDao.java @@ -0,0 +1,18 @@ +package User; + +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 UserDao { + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insert(User user); + + @Query("DELETE FROM users") + void deleteAllUsers(); + + @Query("SELECT * FROM users WHERE user_name = :userName LIMIT 1") + User getUserData(String userName); +} diff --git a/app/src/main/java/User/UserData.java b/app/src/main/java/User/UserData.java deleted file mode 100644 index 2846cfb6..00000000 --- a/app/src/main/java/User/UserData.java +++ /dev/null @@ -1,4 +0,0 @@ -package User; - -public class UserData { -} diff --git a/app/src/main/java/User/UserRoomDatabase.java b/app/src/main/java/User/UserRoomDatabase.java new file mode 100644 index 00000000..31ca1259 --- /dev/null +++ b/app/src/main/java/User/UserRoomDatabase.java @@ -0,0 +1,26 @@ +package User; + +import android.arch.persistence.room.Database; +import android.arch.persistence.room.Room; +import android.arch.persistence.room.RoomDatabase; +import android.content.Context; + +@Database(entities = {User.class}, version = 1) +public abstract class UserRoomDatabase extends RoomDatabase { + private static UserRoomDatabase INSTANCE; + + public abstract UserDao userDao(); + + public static UserRoomDatabase getDatabase(final Context context) { + if(INSTANCE == null) { + synchronized (UserRoomDatabase.class) { + if(INSTANCE == null) { + INSTANCE = Room.databaseBuilder(context.getApplicationContext(), + UserRoomDatabase.class, "users") + .build(); + } + } + } + return INSTANCE; + } +} |