mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2025-10-06 05:49:49 +02:00
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.
This commit is contained in:
@@ -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();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
39
app/src/main/java/User/UserRepository.java
Normal file
39
app/src/main/java/User/UserRepository.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@@ -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;
|
||||
|
||||
|
48
app/src/main/java/User/UserViewModel.java
Normal file
48
app/src/main/java/User/UserViewModel.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user