Skip to content

Commit

Permalink
Merge branch 'feat/view-poll-results'
Browse files Browse the repository at this point in the history
# Conflicts:
#	mastodon/src/main/java/org/joinmastodon/android/ui/displayitems/PollFooterStatusDisplayItem.java
  • Loading branch information
LucasGGamerM committed Nov 17, 2023
2 parents 1ee96f3 + dca68de commit 43fe03c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,14 @@ public void onPollVoteButtonClick(PollFooterStatusDisplayItem.Holder holder){
submitPollVote(holder.getItemID(), poll.id, poll.selectedOptions.stream().map(opt->poll.options.indexOf(opt)).collect(Collectors.toList()));
}

public void onPollViewResultsButtonClick(PollFooterStatusDisplayItem.Holder holder, boolean shown){
for(int i=0;i<list.getChildCount();i++){
if(list.getChildViewHolder(list.getChildAt(i)) instanceof PollOptionStatusDisplayItem.Holder item && item.getItemID().equals(holder.getItemID())){
item.showResults(shown);
}
}
}

protected void submitPollVote(String parentID, String pollID, List<Integer> choices){
if(refreshing)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

public class PollFooterStatusDisplayItem extends StatusDisplayItem{
public final Poll poll;
public boolean resultsVisible=false;
public final Status status;

public PollFooterStatusDisplayItem(String parentID, BaseStatusListFragment parentFragment, Poll poll, Status status){
Expand All @@ -30,13 +31,19 @@ public Type getType(){

public static class Holder extends StatusDisplayItem.Holder<PollFooterStatusDisplayItem>{
private TextView text;
private Button button;
private Button voteButton, resultsButton;

public Holder(Activity activity, ViewGroup parent){
super(activity, R.layout.display_item_poll_footer, parent);
text=findViewById(R.id.text);
button=findViewById(R.id.vote_btn);
button.setOnClickListener(v->item.parentFragment.onPollVoteButtonClick(this));
voteButton=findViewById(R.id.vote_btn);
voteButton.setOnClickListener(v->item.parentFragment.onPollVoteButtonClick(this));
resultsButton=findViewById(R.id.results_btn);
resultsButton.setOnClickListener(v-> {
item.resultsVisible = !item.resultsVisible;
item.parentFragment.onPollViewResultsButtonClick(this, item.resultsVisible);
rebind();
});
}

@Override
Expand All @@ -51,8 +58,9 @@ public void onBind(PollFooterStatusDisplayItem item){
text+=" "+sep+" "+item.parentFragment.getString(R.string.poll_closed);
}
this.text.setText(text);
button.setVisibility(item.poll.isExpired() || item.poll.voted || (!item.poll.multiple && !GlobalUserPreferences.voteButtonForSingleChoice) ? View.GONE : View.VISIBLE);
button.setEnabled(item.poll.selectedOptions!=null && !item.poll.selectedOptions.isEmpty());
resultsButton.setText(item.resultsVisible ? R.string.sk_poll_view : R.string.sk_poll_results);
voteButton.setVisibility(item.poll.isExpired() || item.poll.voted || (!item.poll.multiple && !GlobalUserPreferences.voteButtonForSingleChoice) ? View.GONE : View.VISIBLE);
voteButton.setEnabled(item.poll.selectedOptions!=null && !item.poll.selectedOptions.isEmpty() && !item.resultsVisible);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.joinmastodon.android.ui.utils.CustomEmojiHelper;
import org.joinmastodon.android.ui.utils.UiUtils;

import java.util.Collections;
import java.util.Locale;

import me.grishka.appkit.imageloader.ImageLoaderViewHolder;
Expand Down Expand Up @@ -45,6 +46,10 @@ public PollOptionStatusDisplayItem(String parentID, Poll poll, int optionIndex,
text=HtmlParser.parseCustomEmoji(option.title, poll.emojis);
emojiHelper.setText(text);
showResults=poll.isExpired() || poll.voted;
calculateResults();
}

private void calculateResults() {
int total=poll.votersCount>0 ? poll.votersCount : poll.votesCount;
if(showResults && option.votesCount!=null && total>0){
votesFraction=(float)option.votesCount/(float)total;
Expand Down Expand Up @@ -136,5 +141,11 @@ public void clearImage(int index){
private void onButtonClick(View v){
item.parentFragment.onPollOptionClick(this);
}

public void showResults(boolean shown) {
item.showResults = shown;
item.calculateResults();
rebind();
}
}
}
10 changes: 10 additions & 0 deletions mastodon/src/main/res/layout/display_item_poll_footer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
android:textColor="?colorM3OnSurfaceVariant"
tools:text="fdsafdsafsdafds"/>

<Button
android:id="@+id/results_btn"
style="@style/Widget.Mastodon.M3.Button.Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:minHeight="48dp"
android:textAppearance="@style/m3_label_small"
android:textAllCaps="true"
android:text="@string/sk_poll_results"/>
<Button
android:id="@+id/vote_btn"
android:layout_width="match_parent"
Expand Down
2 changes: 2 additions & 0 deletions mastodon/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -656,4 +656,6 @@
<string name="manage_list_members">Manage list members</string>
<string name="list_no_members">No members yet</string>
<string name="list_find_users">Find users to add</string>
<string name="sk_poll_results">View results</string>
<string name="sk_poll_view">Hide results</string>
</resources>

0 comments on commit 43fe03c

Please sign in to comment.