From e48bb565a5aa871be18cae4003cb781ef6e2e154 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 11 Jan 2019 11:33:32 +0800 Subject: View user details in ViewUserDetailActivity. Follow or unfollow user is not properly implemented right now. Change users and subscribed_users databases' schemes. Press Profile in navigation drawer to view my reddit info. Press the username in the post to view that account's info. --- app/src/main/java/User/FetchUserData.java | 50 ------------------- app/src/main/java/User/ParseUserData.java | 72 ---------------------------- app/src/main/java/User/User.java | 58 ---------------------- app/src/main/java/User/UserDao.java | 10 ++-- app/src/main/java/User/UserData.java | 43 +++++++++++++++++ app/src/main/java/User/UserRepository.java | 39 +++++++++++++++ app/src/main/java/User/UserRoomDatabase.java | 2 +- app/src/main/java/User/UserViewModel.java | 48 +++++++++++++++++++ 8 files changed, 138 insertions(+), 184 deletions(-) delete mode 100644 app/src/main/java/User/FetchUserData.java delete mode 100644 app/src/main/java/User/ParseUserData.java delete mode 100644 app/src/main/java/User/User.java create mode 100644 app/src/main/java/User/UserData.java create mode 100644 app/src/main/java/User/UserRepository.java create mode 100644 app/src/main/java/User/UserViewModel.java (limited to 'app/src/main/java/User') diff --git a/app/src/main/java/User/FetchUserData.java b/app/src/main/java/User/FetchUserData.java deleted file mode 100644 index edaa3b14..00000000 --- a/app/src/main/java/User/FetchUserData.java +++ /dev/null @@ -1,50 +0,0 @@ -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 userInfo = api.getUserData(userName); - userInfo.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response 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 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 deleted file mode 100644 index b6a7a39c..00000000 --- a/app/src/main/java/User/ParseUserData.java +++ /dev/null @@ -1,72 +0,0 @@ -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 { - 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 deleted file mode 100644 index 3fd8dcb3..00000000 --- a/app/src/main/java/User/User.java +++ /dev/null @@ -1,58 +0,0 @@ -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 index ec6bb7e2..7aba086d 100644 --- a/app/src/main/java/User/UserDao.java +++ b/app/src/main/java/User/UserDao.java @@ -1,5 +1,6 @@ package User; +import android.arch.lifecycle.LiveData; import android.arch.persistence.room.Dao; import android.arch.persistence.room.Insert; import android.arch.persistence.room.OnConflictStrategy; @@ -8,11 +9,14 @@ import android.arch.persistence.room.Query; @Dao public interface UserDao { @Insert(onConflict = OnConflictStrategy.REPLACE) - void insert(User user); + void insert(UserData userData); @Query("DELETE FROM users") void deleteAllUsers(); - @Query("SELECT * FROM users WHERE user_name = :userName LIMIT 1") - User getUserData(String userName); + @Query("SELECT * FROM users WHERE name = :userName LIMIT 1") + LiveData getUserLiveData(String userName); + + @Query("SELECT * FROM users WHERE name = :userName LIMIT 1") + UserData getUserData(String userName); } diff --git a/app/src/main/java/User/UserData.java b/app/src/main/java/User/UserData.java new file mode 100644 index 00000000..89eafb96 --- /dev/null +++ b/app/src/main/java/User/UserData.java @@ -0,0 +1,43 @@ +package User; + +import android.arch.persistence.room.ColumnInfo; +import android.arch.persistence.room.Entity; +import android.support.annotation.NonNull; + +import SubscribedUserDatabase.SubscribedUserData; + +@Entity(tableName = "users") +public class UserData extends SubscribedUserData { + @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; + + public UserData(@NonNull String name, String iconUrl, String banner, int karma, boolean isGold, boolean isFriend) { + super(name, iconUrl); + this.banner = banner; + this.karma = karma; + this.isGold = isGold; + this.isFriend = isFriend; + } + + 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/UserRepository.java b/app/src/main/java/User/UserRepository.java new file mode 100644 index 00000000..d505db2b --- /dev/null +++ b/app/src/main/java/User/UserRepository.java @@ -0,0 +1,39 @@ +package User; + +import android.app.Application; +import android.arch.lifecycle.LiveData; +import android.os.AsyncTask; + +public class UserRepository { + private UserDao mUserDao; + private LiveData mUserLiveData; + + UserRepository(Application application, String userName) { + mUserDao = UserRoomDatabase.getDatabase(application).userDao(); + + mUserLiveData = mUserDao.getUserLiveData(userName); + } + + LiveData getUserLiveData() { + return mUserLiveData; + } + + public void insert(UserData userData) { + new InsertAsyncTask(mUserDao).execute(userData); + } + + private static class InsertAsyncTask extends AsyncTask { + + private UserDao mAsyncTaskDao; + + InsertAsyncTask(UserDao dao) { + mAsyncTaskDao = dao; + } + + @Override + protected Void doInBackground(final UserData... params) { + mAsyncTaskDao.insert(params[0]); + return null; + } + } +} diff --git a/app/src/main/java/User/UserRoomDatabase.java b/app/src/main/java/User/UserRoomDatabase.java index 31ca1259..288400a6 100644 --- a/app/src/main/java/User/UserRoomDatabase.java +++ b/app/src/main/java/User/UserRoomDatabase.java @@ -5,7 +5,7 @@ import android.arch.persistence.room.Room; import android.arch.persistence.room.RoomDatabase; import android.content.Context; -@Database(entities = {User.class}, version = 1) +@Database(entities = {UserData.class}, version = 1) public abstract class UserRoomDatabase extends RoomDatabase { private static UserRoomDatabase INSTANCE; diff --git a/app/src/main/java/User/UserViewModel.java b/app/src/main/java/User/UserViewModel.java new file mode 100644 index 00000000..a006e253 --- /dev/null +++ b/app/src/main/java/User/UserViewModel.java @@ -0,0 +1,48 @@ +package User; + +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; + +import SubredditDatabase.SubredditViewModel; + +public class UserViewModel extends AndroidViewModel { + private UserRepository mSubredditRepository; + private LiveData mUserLiveData; + + public UserViewModel(Application application, String id) { + super(application); + mSubredditRepository = new UserRepository(application, id); + mUserLiveData = mSubredditRepository.getUserLiveData(); + } + + public LiveData getUserLiveData() { + return mUserLiveData; + } + + public void insert(UserData userData) { + mSubredditRepository.insert(userData); + } + + public static class Factory extends ViewModelProvider.NewInstanceFactory { + + @NonNull + private final Application mApplication; + + private final String userName; + + public Factory(@NonNull Application application, String userName) { + mApplication = application; + this.userName = userName; + } + + @Override + public T create(Class modelClass) { + //noinspection unchecked + return (T) new UserViewModel(mApplication, userName); + } + } +} -- cgit v1.2.3