Skip to content

Commit

Permalink
Check dribbble user type as prospects cannot comment. Fix nickbutcher#20
Browse files Browse the repository at this point in the history
.
  • Loading branch information
nickbutcher committed Nov 13, 2015
1 parent 68837f8 commit feaa09e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 17 deletions.
15 changes: 15 additions & 0 deletions app/src/main/java/io/plaidapp/data/prefs/DribbblePrefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import io.plaidapp.BuildConfig;
Expand All @@ -44,6 +45,9 @@ public class DribbblePrefs {
private static final String KEY_USER_NAME = "KEY_USER_NAME";
private static final String KEY_USER_USERNAME = "KEY_USER_USERNAME";
private static final String KEY_USER_AVATAR = "KEY_USER_AVATAR";
private static final String KEY_USER_TYPE = "KEY_USER_TYPE";
private static final List<String> CREATIVE_TYPES
= Arrays.asList(new String[] { "Player", "Team" });

private static volatile DribbblePrefs singleton;
private final SharedPreferences prefs;
Expand All @@ -54,6 +58,7 @@ public class DribbblePrefs {
private String userName;
private String userUsername;
private String userAvatar;
private String userType;
private List<DribbbleLoginStatusListener> loginStatusListeners;

public static DribbblePrefs get(Context context) {
Expand All @@ -75,6 +80,7 @@ private DribbblePrefs(Context context) {
userName = prefs.getString(KEY_USER_NAME, null);
userUsername = prefs.getString(KEY_USER_USERNAME, null);
userAvatar = prefs.getString(KEY_USER_AVATAR, null);
userType = prefs.getString(KEY_USER_TYPE, null);
}
}

Expand Down Expand Up @@ -102,11 +108,13 @@ public void setLoggedInUser(User user) {
userUsername = user.username;
userId = user.id;
userAvatar = user.avatar_url;
userType = user.type;
SharedPreferences.Editor editor = prefs.edit();
editor.putLong(KEY_USER_ID, userId);
editor.putString(KEY_USER_NAME, userName);
editor.putString(KEY_USER_USERNAME, userUsername);
editor.putString(KEY_USER_AVATAR, userAvatar);
editor.putString(KEY_USER_TYPE, userType);
editor.apply();
}
}
Expand All @@ -127,12 +135,17 @@ public String getUserAvatar() {
return userAvatar;
}

public boolean userCanPost() {
return CREATIVE_TYPES.contains(userType);
}

public User getUser() {
return new User.Builder()
.setId(userId)
.setName(userName)
.setUsername(userUsername)
.setAvatarUrl(userAvatar)
.setType(userType)
.build();
}

Expand All @@ -143,11 +156,13 @@ public void logout() {
userName = null;
userUsername = null;
userAvatar = null;
userType = null;
SharedPreferences.Editor editor = prefs.edit();
editor.putString(KEY_ACCESS_TOKEN, null);
editor.putLong(KEY_USER_ID, 0l);
editor.putString(KEY_USER_NAME, null);
editor.putString(KEY_USER_AVATAR, null);
editor.putString(KEY_USER_TYPE, null);
editor.apply();
dispatchLogoutEvent();
}
Expand Down
57 changes: 40 additions & 17 deletions app/src/main/java/io/plaidapp/ui/DribbbleShot.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public class DribbbleShot extends Activity {
private TextView shotTimeAgo;
private ListView commentsList;
private DribbbleCommentsAdapter commentsAdapter;
private View commentFooter;
private ImageView userAvatar;
private EditText enterComment;
private ImageButton postComment;
Expand All @@ -137,6 +138,7 @@ public class DribbbleShot extends Activity {
private DribbblePrefs dribbblePrefs;
private DribbbleService dribbbleApi;
private boolean performingLike;
private boolean allowComment;
private CircleTransform circleTransform;
private ElasticDragDismissFrameLayout.SystemChromeFader chromeFader;

Expand All @@ -148,6 +150,7 @@ protected void onCreate(Bundle savedInstanceState) {
setupDribbble();
setExitSharedElementCallback(fabLoginSharedElementCallback);
getWindow().getSharedElementReturnTransition().addListener(shotReturnHomeListener);
circleTransform = new CircleTransform(this);
Resources res = getResources();

ButterKnife.bind(this);
Expand All @@ -165,13 +168,7 @@ protected void onCreate(Bundle savedInstanceState) {
shotTimeAgo = (TextView) shotDescription.findViewById(R.id.shot_time_ago);
commentsList = (ListView) findViewById(R.id.dribbble_comments);
commentsList.addHeaderView(shotDescription);
View enterCommentView = getLayoutInflater().inflate(R.layout.dribbble_enter_comment,
commentsList, false);
userAvatar = (ForegroundImageView) enterCommentView.findViewById(R.id.avatar);
enterComment = (EditText) enterCommentView.findViewById(R.id.comment);
postComment = (ImageButton) enterCommentView.findViewById(R.id.post_comment);
enterComment.setOnFocusChangeListener(enterCommentFocus);
commentsList.addFooterView(enterCommentView);
setupCommenting();
commentsList.setOnScrollListener(scrollListener);
back.setOnClickListener(new View.OnClickListener() {
@Override
Expand All @@ -186,7 +183,6 @@ public void onDragDismissed() {
expandImageAndFinish();
}
};
circleTransform = new CircleTransform(this);

// load the main image
Glide.with(this)
Expand Down Expand Up @@ -267,14 +263,6 @@ public void onClick(View v) {
} else {
commentsList.setAdapter(getNoCommentsAdapter());
}

if (dribbblePrefs.isLoggedIn() && !TextUtils.isEmpty(dribbblePrefs.getUserAvatar())) {
Glide.with(this)
.load(dribbblePrefs.getUserAvatar())
.transform(circleTransform)
.placeholder(R.drawable.ic_player)
.into(userAvatar);
}
}

@Override
Expand Down Expand Up @@ -302,8 +290,13 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// the user was trying to do when forced to login
fab.setChecked(true);
doLike();
setupCommenting();
}
break;
case RC_LOGIN_COMMENT:
if (resultCode == RESULT_OK) {
setupCommenting();
}
}
}

Expand All @@ -323,6 +316,35 @@ public void onProvideAssistContent(AssistContent outContent) {
outContent.setWebUri(Uri.parse(shot.url));
}

private void setupCommenting() {
allowComment = !dribbblePrefs.isLoggedIn()
|| (dribbblePrefs.isLoggedIn() && dribbblePrefs.userCanPost());
if (allowComment && commentFooter == null) {
commentFooter = getLayoutInflater().inflate(R.layout.dribbble_enter_comment,
commentsList, false);
userAvatar = (ForegroundImageView) commentFooter.findViewById(R.id.avatar);
enterComment = (EditText) commentFooter.findViewById(R.id.comment);
postComment = (ImageButton) commentFooter.findViewById(R.id.post_comment);
enterComment.setOnFocusChangeListener(enterCommentFocus);
commentsList.addFooterView(commentFooter);
} else if (!allowComment && commentFooter != null) {
commentsList.removeFooterView(commentFooter);
commentFooter = null;
Toast.makeText(getApplicationContext(),
R.string.prospects_cant_post, Toast.LENGTH_SHORT).show();
}

if (allowComment
&& dribbblePrefs.isLoggedIn()
&& !TextUtils.isEmpty(dribbblePrefs.getUserAvatar())) {
Glide.with(this)
.load(dribbblePrefs.getUserAvatar())
.transform(circleTransform)
.placeholder(R.drawable.ic_player)
.into(userAvatar);
}
}

private View.OnClickListener shotClick = new View.OnClickListener() {
@Override
public void onClick(View view) {
Expand Down Expand Up @@ -849,7 +871,8 @@ public void failure(RetrofitError error) {
}
});

reply.setVisibility(position == expandedCommentPosition ? View.VISIBLE : View.GONE);
reply.setVisibility((position == expandedCommentPosition && allowComment) ?
View.VISIBLE : View.GONE);
reply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
</plurals>
<string name="share">share</string>
<string name="hint_reply">Reply</string>
<string name="prospects_cant_post">Sorry but prospects cannot comment :(</string>

<!-- dribbble login -->

Expand Down

0 comments on commit feaa09e

Please sign in to comment.