mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-05-19 04:09:03 +00:00
Merge pull request #529 from Jawnnypoo/update-guild-state
Update guild state
This commit is contained in:
commit
5974914f7f
6 changed files with 109 additions and 64 deletions
|
|
@ -1,44 +1,47 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical" android:layout_width="match_parent"
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:padding="@dimen/row_padding">
|
||||
android:orientation="vertical"
|
||||
android:padding="@dimen/row_padding"
|
||||
android:background="?attr/selectableItemBackground">
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/nameTextView"
|
||||
style="@style/RowTitle"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
style="@style/RowTitle"
|
||||
android:id="@+id/nameTextView"
|
||||
android:layout_weight="1" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/memberCountTextView"
|
||||
style="@style/RowText"
|
||||
android:id="@+id/memberCountTextView" />
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/descriptionTextView"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="New Text"
|
||||
android:id="@+id/descriptionTextView"
|
||||
android:layout_weight="1" />
|
||||
android:layout_weight="1"
|
||||
android:text="New Text" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/joinleaveButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="New Button"
|
||||
android:id="@+id/joinleaveButton" />
|
||||
android:text="New Button" />
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
|
|
@ -6,6 +6,7 @@ import android.view.View;
|
|||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.habitrpg.android.habitica.APIHelper;
|
||||
import com.habitrpg.android.habitica.R;
|
||||
|
|
@ -40,16 +41,64 @@ public class PublicGuildsRecyclerViewAdapter extends RecyclerView.Adapter<Public
|
|||
|
||||
@Override
|
||||
public GuildViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
|
||||
View view = LayoutInflater.from(parent.getContext())
|
||||
.inflate(R.layout.item_public_guild, parent, false);
|
||||
GuildViewHolder guildViewHolder = new GuildViewHolder(view);
|
||||
guildViewHolder.itemView.setOnClickListener(v -> {
|
||||
Group guild = (Group) v.getTag();
|
||||
GuildFragment guildFragment = new GuildFragment();
|
||||
guildFragment.setGuild(guild);
|
||||
guildFragment.isMember = isInGroup(guild);
|
||||
DisplayFragmentEvent event = new DisplayFragmentEvent();
|
||||
event.fragment = guildFragment;
|
||||
EventBus.getDefault().post(event);
|
||||
});
|
||||
guildViewHolder.joinLeaveButton.setOnClickListener(v -> {
|
||||
Group guild = (Group) v.getTag();
|
||||
boolean isMember = this.memberGuildIDs != null && this.memberGuildIDs.contains(guild.id);
|
||||
if (isMember) {
|
||||
PublicGuildsRecyclerViewAdapter.this.apiHelper.apiService.leaveGroup(guild.id, new Callback<Void>() {
|
||||
@Override
|
||||
public void success(Void nope, Response response) {
|
||||
memberGuildIDs.remove(guild.id);
|
||||
int indexOfGroup = publicGuildList.indexOf(guild);
|
||||
notifyItemChanged(indexOfGroup);
|
||||
}
|
||||
|
||||
return new GuildViewHolder(view);
|
||||
@Override
|
||||
public void failure(RetrofitError error) {
|
||||
Toast.makeText(guildViewHolder.itemView.getContext(), R.string.unknown_error, Toast.LENGTH_SHORT)
|
||||
.show();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
PublicGuildsRecyclerViewAdapter.this.apiHelper.apiService.joinGroup(guild.id, new Callback<Group>() {
|
||||
@Override
|
||||
public void success(Group group, Response response) {
|
||||
memberGuildIDs.add(group.id);
|
||||
int indexOfGroup = publicGuildList.indexOf(group);
|
||||
notifyItemChanged(indexOfGroup);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failure(RetrofitError error) {
|
||||
Toast.makeText(guildViewHolder.itemView.getContext(), R.string.unknown_error, Toast.LENGTH_SHORT)
|
||||
.show();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
return guildViewHolder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(GuildViewHolder holder, int position) {
|
||||
holder.bind(publicGuildList.get(position));
|
||||
Group guild = publicGuildList.get(position);
|
||||
boolean isInGroup = isInGroup(guild);
|
||||
holder.bind(guild, isInGroup);
|
||||
holder.itemView.setTag(guild);
|
||||
holder.joinLeaveButton.setTag(guild);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -57,7 +106,11 @@ public class PublicGuildsRecyclerViewAdapter extends RecyclerView.Adapter<Public
|
|||
return this.publicGuildList == null ? 0 : this.publicGuildList.size();
|
||||
}
|
||||
|
||||
class GuildViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, Callback<Group> {
|
||||
private boolean isInGroup(Group guild) {
|
||||
return this.memberGuildIDs != null && this.memberGuildIDs.contains(guild.id);
|
||||
}
|
||||
|
||||
static class GuildViewHolder extends RecyclerView.ViewHolder {
|
||||
|
||||
@BindView(R.id.nameTextView)
|
||||
TextView nameTextView;
|
||||
|
|
@ -71,57 +124,20 @@ public class PublicGuildsRecyclerViewAdapter extends RecyclerView.Adapter<Public
|
|||
@BindView(R.id.joinleaveButton)
|
||||
Button joinLeaveButton;
|
||||
|
||||
Group guild;
|
||||
Boolean isMember;
|
||||
|
||||
public GuildViewHolder(View itemView) {
|
||||
super(itemView);
|
||||
|
||||
ButterKnife.bind(this, itemView);
|
||||
itemView.setOnClickListener(this);
|
||||
joinLeaveButton.setOnClickListener(this);
|
||||
}
|
||||
|
||||
public void bind(Group guild) {
|
||||
this.guild = guild;
|
||||
public void bind(Group guild, boolean isInGroup) {
|
||||
this.nameTextView.setText(guild.name);
|
||||
this.memberCountTextView.setText(String.valueOf(guild.memberCount));
|
||||
this.descriptionTextView.setText(guild.description);
|
||||
if (PublicGuildsRecyclerViewAdapter.this.memberGuildIDs != null && PublicGuildsRecyclerViewAdapter.this.memberGuildIDs.contains(guild.id)) {
|
||||
this.isMember = true;
|
||||
if (isInGroup) {
|
||||
this.joinLeaveButton.setText(R.string.leave);
|
||||
} else {
|
||||
this.isMember = false;
|
||||
this.joinLeaveButton.setText(R.string.join);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (v == this.joinLeaveButton) {
|
||||
if (this.isMember) {
|
||||
PublicGuildsRecyclerViewAdapter.this.apiHelper.apiService.leaveGroup(this.guild.id, this);
|
||||
} else {
|
||||
PublicGuildsRecyclerViewAdapter.this.apiHelper.apiService.joinGroup(this.guild.id, this);
|
||||
}
|
||||
} else {
|
||||
GuildFragment guildFragment = new GuildFragment();
|
||||
guildFragment.setGuild(this.guild);
|
||||
guildFragment.isMember = this.isMember;
|
||||
DisplayFragmentEvent event = new DisplayFragmentEvent();
|
||||
event.fragment = guildFragment;
|
||||
EventBus.getDefault().post(event);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void success(Group group, Response response) {
|
||||
this.bind(guild);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failure(RetrofitError error) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -93,7 +93,16 @@ public class GuildFragment extends BaseMainFragment implements Callback<Group> {
|
|||
this.isMember = true;
|
||||
return true;
|
||||
case R.id.menu_guild_leave:
|
||||
this.mAPIHelper.apiService.leaveGroup(this.guild.id, this);
|
||||
this.mAPIHelper.apiService.leaveGroup(this.guild.id, new Callback<Void>() {
|
||||
@Override
|
||||
public void success(Void aVoid, Response response) {
|
||||
getActivity().supportInvalidateOptionsMenu();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failure(RetrofitError error) {
|
||||
}
|
||||
});
|
||||
this.isMember = false;
|
||||
return true;
|
||||
case R.id.menu_guild_edit:
|
||||
|
|
|
|||
|
|
@ -135,9 +135,9 @@ public class PartyFragment extends BaseMainFragment {
|
|||
this.displayEditForm();
|
||||
return true;
|
||||
case R.id.menu_guild_leave:
|
||||
this.mAPIHelper.apiService.leaveGroup(this.group.id, new Callback<Group>() {
|
||||
this.mAPIHelper.apiService.leaveGroup(this.group.id, new Callback<Void>() {
|
||||
@Override
|
||||
public void success(Group group, Response response) {
|
||||
public void success(Void aVoid, Response response) {
|
||||
getActivity().getSupportFragmentManager().beginTransaction().remove(PartyFragment.this).commit();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -145,7 +145,7 @@ public interface ApiService {
|
|||
void joinGroup(@Path("gid") String groupId, Callback<Group> cb);
|
||||
|
||||
@POST("/groups/{gid}/leave")
|
||||
void leaveGroup(@Path("gid") String groupId, Callback<Group> cb);
|
||||
void leaveGroup(@Path("gid") String groupId, Callback<Void> cb);
|
||||
|
||||
@POST("/groups/{gid}/chat")
|
||||
void postGroupChat(@Path("gid") String groupId, @Query("message") String message, Callback<PostChatMessageResult> cb);
|
||||
|
|
|
|||
|
|
@ -40,4 +40,21 @@ public class Group extends BaseModel {
|
|||
public int challengeCount;
|
||||
|
||||
// TODO Challenges
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Group group = (Group) o;
|
||||
|
||||
return id != null ? id.equals(group.id) : group.id == null;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return id != null ? id.hashCode() : 0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue