aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/User
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2019-01-11 03:33:32 +0000
committerAlex Ning <chineseperson5@gmail.com>2019-01-11 03:33:32 +0000
commite48bb565a5aa871be18cae4003cb781ef6e2e154 (patch)
tree92efb6bf6b5d9db8f7411c1c6df83e18ccde06b5 /app/src/main/java/User
parentf0b149ce82cf11df1f5cf84c7d644e5e670ad0b2 (diff)
downloadinfinity-for-reddit-e48bb565a5aa871be18cae4003cb781ef6e2e154.tar
infinity-for-reddit-e48bb565a5aa871be18cae4003cb781ef6e2e154.tar.gz
infinity-for-reddit-e48bb565a5aa871be18cae4003cb781ef6e2e154.tar.bz2
infinity-for-reddit-e48bb565a5aa871be18cae4003cb781ef6e2e154.tar.lz
infinity-for-reddit-e48bb565a5aa871be18cae4003cb781ef6e2e154.tar.xz
infinity-for-reddit-e48bb565a5aa871be18cae4003cb781ef6e2e154.tar.zst
infinity-for-reddit-e48bb565a5aa871be18cae4003cb781ef6e2e154.zip
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.
Diffstat (limited to 'app/src/main/java/User')
-rw-r--r--app/src/main/java/User/FetchUserData.java50
-rw-r--r--app/src/main/java/User/ParseUserData.java72
-rw-r--r--app/src/main/java/User/UserDao.java10
-rw-r--r--app/src/main/java/User/UserData.java (renamed from app/src/main/java/User/User.java)25
-rw-r--r--app/src/main/java/User/UserRepository.java39
-rw-r--r--app/src/main/java/User/UserRoomDatabase.java2
-rw-r--r--app/src/main/java/User/UserViewModel.java48
7 files changed, 100 insertions, 146 deletions
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<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
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<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/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<UserData> getUserLiveData(String userName);
+
+ @Query("SELECT * FROM users WHERE name = :userName LIMIT 1")
+ UserData getUserData(String userName);
}
diff --git a/app/src/main/java/User/User.java b/app/src/main/java/User/UserData.java
index 3fd8dcb3..89eafb96 100644
--- a/app/src/main/java/User/User.java
+++ b/app/src/main/java/User/UserData.java
@@ -2,17 +2,12 @@ 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;
+import SubscribedUserDatabase.SubscribedUserData;
+
@Entity(tableName = "users")
-public class User {
- @PrimaryKey
- @NonNull
- @ColumnInfo(name = "user_name")
- private String userName;
- @ColumnInfo(name = "icon")
- private String icon;
+public class UserData extends SubscribedUserData {
@ColumnInfo(name = "banner")
private String banner;
@ColumnInfo(name = "karma")
@@ -22,24 +17,14 @@ public class User {
@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;
+ 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;
}
- @NonNull
- public String getUserName() {
- return userName;
- }
-
- public String getIcon() {
- return icon;
- }
-
public String getBanner() {
return banner;
}
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<UserData> mUserLiveData;
+
+ UserRepository(Application application, String userName) {
+ mUserDao = UserRoomDatabase.getDatabase(application).userDao();
+
+ mUserLiveData = mUserDao.getUserLiveData(userName);
+ }
+
+ LiveData<UserData> getUserLiveData() {
+ return mUserLiveData;
+ }
+
+ public void insert(UserData userData) {
+ new InsertAsyncTask(mUserDao).execute(userData);
+ }
+
+ private static class InsertAsyncTask extends AsyncTask<UserData, Void, Void> {
+
+ 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<UserData> mUserLiveData;
+
+ public UserViewModel(Application application, String id) {
+ super(application);
+ mSubredditRepository = new UserRepository(application, id);
+ mUserLiveData = mSubredditRepository.getUserLiveData();
+ }
+
+ public LiveData<UserData> 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 extends ViewModel> T create(Class<T> modelClass) {
+ //noinspection unchecked
+ return (T) new UserViewModel(mApplication, userName);
+ }
+ }
+}