diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-01-11 10:44:54 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-01-11 10:44:54 +0000 |
commit | 047e31936fa87830b38d4f9a52c7c4dabb84467e (patch) | |
tree | 0f35a8f3946dabab097bb56d7afd107b467e7ed0 /app/src/main | |
parent | 0bd0639a4ed270524443c637be9320eb822cc0cf (diff) | |
download | infinity-for-reddit-047e31936fa87830b38d4f9a52c7c4dabb84467e.tar infinity-for-reddit-047e31936fa87830b38d4f9a52c7c4dabb84467e.tar.gz infinity-for-reddit-047e31936fa87830b38d4f9a52c7c4dabb84467e.tar.bz2 infinity-for-reddit-047e31936fa87830b38d4f9a52c7c4dabb84467e.tar.lz infinity-for-reddit-047e31936fa87830b38d4f9a52c7c4dabb84467e.tar.xz infinity-for-reddit-047e31936fa87830b38d4f9a52c7c4dabb84467e.tar.zst infinity-for-reddit-047e31936fa87830b38d4f9a52c7c4dabb84467e.zip |
Display the Follow Chip only if the user can be followed. Change schemes of database users.
Diffstat (limited to 'app/src/main')
7 files changed, 96 insertions, 89 deletions
diff --git a/app/src/main/java/User/UserData.java b/app/src/main/java/User/UserData.java index 89eafb96..a3b15b0e 100644 --- a/app/src/main/java/User/UserData.java +++ b/app/src/main/java/User/UserData.java @@ -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; + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index 7254bfcf..3b814e94 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -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,33 +164,27 @@ 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) { - if (subscribedSubredditData == null || subscribedSubredditData.size() == 0) { - subscriptionsLabelTextView.setVisibility(View.GONE); - } else { - subscriptionsLabelTextView.setVisibility(View.VISIBLE); - } - - subredditadapter.setSubscribedSubreddits(subscribedSubredditData); + mSubscribedSubredditViewModel.getAllSubscribedSubreddits().observe(this, subscribedSubredditData -> { + if (subscribedSubredditData == null || subscribedSubredditData.size() == 0) { + subscriptionsLabelTextView.setVisibility(View.GONE); + } else { + subscriptionsLabelTextView.setVisibility(View.VISIBLE); } + + 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) { - if (subscribedUserData == null || subscribedUserData.size() == 0) { - followingLabelTextView.setVisibility(View.GONE); - } else { - followingLabelTextView.setVisibility(View.VISIBLE); - } - userAdapter.setSubscribedUsers(subscribedUserData); + mSubscribedUserViewModel.getAllSubscribedUsers().observe(this, subscribedUserData -> { + if (subscribedUserData == null || subscribedUserData.size() == 0) { + followingLabelTextView.setVisibility(View.GONE); + } else { + followingLabelTextView.setVisibility(View.VISIBLE); } + userAdapter.setSubscribedUsers(subscribedUserData); }); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseUserData.java index 4666ffef..10d118cf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseUserData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseUserData.java @@ -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()); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java index 35e95b75..46a0ac66 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java @@ -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) { - //Do nothing in order to prevent clicking this to start ViewPostDetailActivity - } + scoreTextView.setOnClickListener(view -> { + //Do nothing in order to prevent clicking this to start ViewPostDetailActivity }); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/UserFollowing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/UserFollowing.java index 56040fa8..92d5012c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/UserFollowing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/UserFollowing.java @@ -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(); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java index 636bb98a..97a6830a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java @@ -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()); diff --git a/app/src/main/res/layout/activity_view_user_detail.xml b/app/src/main/res/layout/activity_view_user_detail.xml index a292d390..4d82dc6a 100644 --- a/app/src/main/res/layout/activity_view_user_detail.xml +++ b/app/src/main/res/layout/activity_view_user_detail.xml @@ -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> |