Display the Follow Chip only if the user can be followed. Change schemes of database users.

This commit is contained in:
Alex Ning 2019-01-11 18:44:54 +08:00
parent 0bd0639a4e
commit 047e31936f
7 changed files with 96 additions and 89 deletions

View File

@ -16,13 +16,16 @@ public class UserData extends SubscribedUserData {
private boolean isGold;
@ColumnInfo(name = "is_friend")
private boolean isFriend;
@ColumnInfo(name = "can_be_followed")
private boolean canBeFollowed;
public UserData(@NonNull String name, String iconUrl, String banner, int karma, boolean isGold, boolean isFriend) {
public UserData(@NonNull String name, String iconUrl, String banner, int karma, boolean isGold, boolean isFriend, boolean canBeFollowed) {
super(name, iconUrl);
this.banner = banner;
this.karma = karma;
this.isGold = isGold;
this.isFriend = isFriend;
this.canBeFollowed = canBeFollowed;
}
public String getBanner() {
@ -40,4 +43,8 @@ public class UserData extends SubscribedUserData {
public boolean isFriend() {
return isFriend;
}
public boolean isCanBeFollowed() {
return canBeFollowed;
}
}

View File

@ -1,13 +1,11 @@
package ml.docilealligator.infinityforreddit;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
@ -166,9 +164,7 @@ public class MainActivity extends AppCompatActivity {
subscribedSubredditRecyclerView.setAdapter(subredditadapter);
mSubscribedSubredditViewModel = ViewModelProviders.of(this).get(SubscribedSubredditViewModel.class);
mSubscribedSubredditViewModel.getAllSubscribedSubreddits().observe(this, new Observer<List<SubscribedSubredditData>>() {
@Override
public void onChanged(@Nullable final List<SubscribedSubredditData> subscribedSubredditData) {
mSubscribedSubredditViewModel.getAllSubscribedSubreddits().observe(this, subscribedSubredditData -> {
if (subscribedSubredditData == null || subscribedSubredditData.size() == 0) {
subscriptionsLabelTextView.setVisibility(View.GONE);
} else {
@ -176,23 +172,19 @@ public class MainActivity extends AppCompatActivity {
}
subredditadapter.setSubscribedSubreddits(subscribedSubredditData);
}
});
final SubscribedUserRecyclerViewAdapter userAdapter =
new SubscribedUserRecyclerViewAdapter(this, drawer::closeDrawers);
subscribedUserRecyclerView.setAdapter(userAdapter);
mSubscribedUserViewModel = ViewModelProviders.of(this).get(SubscribedUserViewModel.class);
mSubscribedUserViewModel.getAllSubscribedUsers().observe(this, new Observer<List<SubscribedUserData>>() {
@Override
public void onChanged(@Nullable final List<SubscribedUserData> subscribedUserData) {
mSubscribedUserViewModel.getAllSubscribedUsers().observe(this, subscribedUserData -> {
if (subscribedUserData == null || subscribedUserData.size() == 0) {
followingLabelTextView.setVisibility(View.GONE);
} else {
followingLabelTextView.setVisibility(View.VISIBLE);
}
userAdapter.setSubscribedUsers(subscribedUserData);
}
});
}
}

View File

@ -43,8 +43,12 @@ public class ParseUserData {
String userName = jsonResponse.getString(JSONUtils.NAME_KEY);
String iconImageUrl = jsonResponse.getString(JSONUtils.ICON_IMG_KEY);
String bannerImageUrl = "";
boolean canBeFollowed;
if(jsonResponse.has(JSONUtils.SUBREDDIT_KEY) && !jsonResponse.isNull(JSONUtils.SUBREDDIT_KEY)) {
bannerImageUrl = jsonResponse.getJSONObject(JSONUtils.SUBREDDIT_KEY).getString(JSONUtils.BANNER_IMG_KEY);
canBeFollowed = true;
} else {
canBeFollowed = false;
}
int linkKarma = jsonResponse.getInt(JSONUtils.LINK_KARMA_KEY);
int commentKarma = jsonResponse.getInt(JSONUtils.COMMENT_KARMA_KEY);
@ -52,7 +56,7 @@ public class ParseUserData {
boolean isGold = jsonResponse.getBoolean(JSONUtils.IS_GOLD_KEY);
boolean isFriend = jsonResponse.getBoolean(JSONUtils.IS_FRIEND_KEY);
userData = new UserData(userName, iconImageUrl, bannerImageUrl, karma, isGold, isFriend);
userData = new UserData(userName, iconImageUrl, bannerImageUrl, karma, isGold, isFriend, canBeFollowed);
} catch (JSONException e) {
parseFailed = true;
Log.i("parse user data error", e.getMessage());

View File

@ -666,11 +666,8 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
DataViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
scoreTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
scoreTextView.setOnClickListener(view -> {
//Do nothing in order to prevent clicking this to start ViewPostDetailActivity
}
});
}
}

View File

@ -70,7 +70,7 @@ class UserFollowing {
}
userFollowingListener.onUserFollowingSuccess();
} else {
Log.i("call failed", response.message());
Log.i("call failed", Integer.toString(response.code()));
userFollowingListener.onUserFollowingFail();
}
}

View File

@ -16,6 +16,7 @@ import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
@ -98,6 +99,7 @@ public class ViewUserDetailActivity extends AppCompatActivity {
String userName = getIntent().getExtras().getString(EXTRA_USER_NAME_KEY);
String title = "u/" + userName;
userNameTextView.setText(title);
CollapsingToolbarLayout collapsingToolbarLayout = findViewById(R.id.collapsing_toolbar_layout_view_user_detail_activity);
AppBarLayout appBarLayout = findViewById(R.id.app_bar_layout_view_user_detail_activity);
@ -127,65 +129,7 @@ public class ViewUserDetailActivity extends AppCompatActivity {
}
});
subscribeUserChip.setOnClickListener(view -> {
if(subscriptionReady) {
subscriptionReady = false;
if(subscribeUserChip.getText().equals(getResources().getString(R.string.follow))) {
UserFollowing.followUser(mOauthRetrofit, mRetrofit, sharedPreferences,
userName, subscribedUserDao, new UserFollowing.UserFollowingListener() {
@Override
public void onUserFollowingSuccess() {
subscribeUserChip.setText(R.string.unfollow);
subscribeUserChip.setChipBackgroundColor(getResources().getColorStateList(R.color.colorAccent));
makeSnackbar(R.string.followed);
subscriptionReady = true;
}
@Override
public void onUserFollowingFail() {
makeSnackbar(R.string.follow_failed);
subscriptionReady = true;
}
});
} else {
UserFollowing.unfollowUser(mOauthRetrofit, mRetrofit, sharedPreferences,
userName, subscribedUserDao, new UserFollowing.UserFollowingListener() {
@Override
public void onUserFollowingSuccess() {
subscribeUserChip.setText(R.string.follow);
subscribeUserChip.setChipBackgroundColor(getResources().getColorStateList(R.color.colorPrimaryDark));
makeSnackbar(R.string.unfollowed);
subscriptionReady = true;
}
@Override
public void onUserFollowingFail() {
makeSnackbar(R.string.unfollow_failed);
subscriptionReady = true;
}
});
}
}
});
subscribedUserDao = SubscribedUserRoomDatabase.getDatabase(this).subscribedUserDao();
new CheckIsFollowingUserAsyncTask(subscribedUserDao, userName, new CheckIsFollowingUserAsyncTask.CheckIsFollowingUserListener() {
@Override
public void isSubscribed() {
subscribeUserChip.setText(R.string.unfollow);
subscribeUserChip.setChipBackgroundColor(getResources().getColorStateList(R.color.colorAccent));
subscriptionReady = true;
}
@Override
public void isNotSubscribed() {
subscribeUserChip.setText(R.string.follow);
subscribeUserChip.setChipBackgroundColor(getResources().getColorStateList(R.color.colorPrimaryDark));
subscriptionReady = true;
}
}).execute();
glide = Glide.with(this);
userViewModel = ViewModelProviders.of(this, new UserViewModel.Factory(getApplication(), userName))
@ -244,6 +188,68 @@ public class ViewUserDetailActivity extends AppCompatActivity {
});
}
if(userData.isCanBeFollowed()) {
subscribeUserChip.setVisibility(View.VISIBLE);
subscribeUserChip.setOnClickListener(view -> {
if(subscriptionReady) {
subscriptionReady = false;
if(subscribeUserChip.getText().equals(getResources().getString(R.string.follow))) {
UserFollowing.followUser(mOauthRetrofit, mRetrofit, sharedPreferences,
userName, subscribedUserDao, new UserFollowing.UserFollowingListener() {
@Override
public void onUserFollowingSuccess() {
subscribeUserChip.setText(R.string.unfollow);
subscribeUserChip.setChipBackgroundColor(getResources().getColorStateList(R.color.colorAccent));
makeSnackbar(R.string.followed);
subscriptionReady = true;
}
@Override
public void onUserFollowingFail() {
makeSnackbar(R.string.follow_failed);
subscriptionReady = true;
}
});
} else {
UserFollowing.unfollowUser(mOauthRetrofit, mRetrofit, sharedPreferences,
userName, subscribedUserDao, new UserFollowing.UserFollowingListener() {
@Override
public void onUserFollowingSuccess() {
subscribeUserChip.setText(R.string.follow);
subscribeUserChip.setChipBackgroundColor(getResources().getColorStateList(R.color.colorPrimaryDark));
makeSnackbar(R.string.unfollowed);
subscriptionReady = true;
}
@Override
public void onUserFollowingFail() {
makeSnackbar(R.string.unfollow_failed);
subscriptionReady = true;
}
});
}
}
});
new CheckIsFollowingUserAsyncTask(subscribedUserDao, userName, new CheckIsFollowingUserAsyncTask.CheckIsFollowingUserListener() {
@Override
public void isSubscribed() {
subscribeUserChip.setText(R.string.unfollow);
subscribeUserChip.setChipBackgroundColor(getResources().getColorStateList(R.color.colorAccent));
subscriptionReady = true;
}
@Override
public void isNotSubscribed() {
subscribeUserChip.setText(R.string.follow);
subscribeUserChip.setChipBackgroundColor(getResources().getColorStateList(R.color.colorPrimaryDark));
subscriptionReady = true;
}
}).execute();
} else {
subscribeUserChip.setVisibility(View.GONE);
}
String userFullName = "u/" + userData.getName();
userNameTextView.setText(userFullName);
String karma = getString(R.string.karma_info, userData.getKarma());

View File

@ -68,6 +68,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:textColor="@android:color/black"
android:layout_gravity="center_horizontal"/>
@ -75,10 +76,10 @@
android:id="@+id/subscribe_user_chip_view_user_detail_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:textColor="@android:color/white"
android:layout_gravity="center_horizontal"/>
android:layout_gravity="center_horizontal"
android:visibility="gone"/>
</LinearLayout>