feat: add option to view poll results

This commit is contained in:
FineFindus 2023-11-16 22:16:28 +01:00
parent d96b9558b4
commit 0ef879ebd3
No known key found for this signature in database
GPG Key ID: 64873EE210FF8E6B
5 changed files with 48 additions and 5 deletions

View File

@ -550,6 +550,14 @@ public abstract class BaseStatusListFragment<T extends DisplayItemsParent> exten
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;

View File

@ -14,6 +14,7 @@ import org.joinmastodon.android.ui.utils.UiUtils;
public class PollFooterStatusDisplayItem extends StatusDisplayItem{
public final Poll poll;
public boolean resultsVisible = false;
public PollFooterStatusDisplayItem(String parentID, BaseStatusListFragment parentFragment, Poll poll){
super(parentID, parentFragment);
@ -27,13 +28,20 @@ public class PollFooterStatusDisplayItem extends StatusDisplayItem{
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);
resultsButton.setText(item.resultsVisible ? R.string.sk_poll_view : R.string.sk_poll_results);
setVoteButtonEnabled();
});
}
@Override
@ -48,8 +56,12 @@ public class PollFooterStatusDisplayItem extends StatusDisplayItem{
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());
setVoteButtonEnabled();
}
private void setVoteButtonEnabled() {
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);
}
}
}

View File

@ -17,6 +17,7 @@ import org.joinmastodon.android.ui.text.HtmlParser;
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;
@ -44,6 +45,10 @@ public class PollOptionStatusDisplayItem extends StatusDisplayItem{
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;
@ -135,5 +140,11 @@ public class PollOptionStatusDisplayItem extends StatusDisplayItem{
private void onButtonClick(View v){
item.parentFragment.onPollOptionClick(this);
}
public void showResults(boolean shown) {
item.showResults = shown;
item.calculateResults();
rebind();
}
}
}

View File

@ -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"

View File

@ -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>