Skip to content

Commit

Permalink
feat: add option to view poll results (#935)
Browse files Browse the repository at this point in the history
* feat: add option to view poll results

* fix: button showing wrong text when refreshing

* fix: hide results button when voted

* remove unused string

* change view results layout, remove unused setting

---------

Co-authored-by: sk <sk22@mailbox.org>
  • Loading branch information
FineFindus and sk22 committed Nov 27, 2023
1 parent 70b9d08 commit 79d5067
Show file tree
Hide file tree
Showing 39 changed files with 91 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public class GlobalUserPreferences{
public static boolean showNewPostsButton;
public static boolean toolbarMarquee;
public static boolean disableSwipe;
public static boolean voteButtonForSingleChoice;
public static boolean enableDeleteNotifications;
public static boolean translateButtonOpenedOnly;
public static boolean uniformNotificationIcon;
Expand Down Expand Up @@ -99,7 +98,6 @@ public static void load(){
showNewPostsButton=prefs.getBoolean("showNewPostsButton", true);
toolbarMarquee=prefs.getBoolean("toolbarMarquee", true);
disableSwipe=prefs.getBoolean("disableSwipe", false);
voteButtonForSingleChoice=prefs.getBoolean("voteButtonForSingleChoice", true);
enableDeleteNotifications=prefs.getBoolean("enableDeleteNotifications", false);
translateButtonOpenedOnly=prefs.getBoolean("translateButtonOpenedOnly", false);
uniformNotificationIcon=prefs.getBoolean("uniformNotificationIcon", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,8 @@ protected void updatePoll(String itemID, Status status, Poll poll){
public void onPollOptionClick(PollOptionStatusDisplayItem.Holder holder){
Poll poll=holder.getItem().poll;
Poll.Option option=holder.getItem().option;
if(poll.multiple || GlobalUserPreferences.voteButtonForSingleChoice){
// MEGALODON: always show vote button
// if(poll.multiple){
if(poll.selectedOptions==null)
poll.selectedOptions=new ArrayList<>();
boolean optionContained=poll.selectedOptions.contains(option);
Expand All @@ -531,7 +532,7 @@ public void onPollOptionClick(PollOptionStatusDisplayItem.Holder holder){
for(int i=0;i<list.getChildCount();i++){
RecyclerView.ViewHolder vh=list.getChildViewHolder(list.getChildAt(i));
if(!poll.multiple && vh instanceof PollOptionStatusDisplayItem.Holder item){
if (item != holder) item.itemView.setSelected(false);
if(item!=holder) item.itemView.setSelected(false);
}
if(vh instanceof PollFooterStatusDisplayItem.Holder footer){
if(footer.getItemID().equals(holder.getItemID())){
Expand All @@ -540,16 +541,24 @@ public void onPollOptionClick(PollOptionStatusDisplayItem.Holder holder){
}
}
}
}else{
submitPollVote(holder.getItemID(), poll.id, Collections.singletonList(poll.options.indexOf(option)));
}
// }else{
// submitPollVote(holder.getItemID(), poll.id, Collections.singletonList(poll.options.indexOf(option)));
// }
}

public void onPollVoteButtonClick(PollFooterStatusDisplayItem.Holder holder){
Poll poll=holder.getItem().poll;
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 @@ -6,14 +6,14 @@
import android.widget.Button;
import android.widget.TextView;

import org.joinmastodon.android.GlobalUserPreferences;
import org.joinmastodon.android.R;
import org.joinmastodon.android.fragments.BaseStatusListFragment;
import org.joinmastodon.android.model.Poll;
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);
Expand All @@ -27,29 +27,40 @@ public Type getType(){

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

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);
wrapper=findViewById(R.id.wrapper);
resultsButton.setOnClickListener(v-> {
item.resultsVisible = !item.resultsVisible;
item.parentFragment.onPollViewResultsButtonClick(this, item.resultsVisible);
rebind();
UiUtils.beginLayoutTransition(wrapper);
});
}

@Override
public void onBind(PollFooterStatusDisplayItem item){
String text=item.parentFragment.getResources().getQuantityString(R.plurals.x_votes, item.poll.votesCount, item.poll.votesCount);
String sep=item.parentFragment.getString(R.string.sk_separator);
if(item.poll.expiresAt!=null && !item.poll.isExpired()){
text+=" "+sep+" "+UiUtils.formatTimeLeft(itemView.getContext(), item.poll.expiresAt);
if(item.poll.multiple)
text+=" "+sep+" "+item.parentFragment.getString(R.string.poll_multiple_choice);
}else if(item.poll.isExpired()){
text+=" "+sep+" "+item.parentFragment.getString(R.string.poll_closed);
}
String sep=" "+item.parentFragment.getString(R.string.sk_separator)+" ";
if(item.poll.expiresAt!=null && !item.poll.isExpired())
text+=sep+UiUtils.formatTimeLeft(itemView.getContext(), item.poll.expiresAt).replaceAll(" ", " ");
else if(item.poll.isExpired())
text+=sep+item.parentFragment.getString(R.string.poll_closed).replaceAll(" ", " ");
if(item.poll.multiple)
text+=sep+item.parentFragment.getString(R.string.sk_poll_multiple_choice).replaceAll(" ", " ");
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.setVisibility(item.poll.isExpired() || item.poll.voted ? View.GONE : View.VISIBLE);
resultsButton.setText(item.resultsVisible ? R.string.sk_poll_hide_results : R.string.sk_poll_show_results);
resultsButton.setSelected(item.resultsVisible);
voteButton.setVisibility(item.poll.isExpired() || item.poll.voted ? 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 @@ -17,6 +17,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 @@ -44,6 +45,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 @@ -135,5 +140,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();
}
}
}
43 changes: 31 additions & 12 deletions mastodon/src/main/res/layout/display_item_poll_footer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,45 @@
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/text"
<LinearLayout
android:id="@+id/wrapper"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginBottom="12dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textAppearance="@style/m3_body_medium"
android:gravity="center_vertical"
android:textColor="?colorM3OnSurfaceVariant"
tools:text="fdsafdsafsdafds"/>
android:layout_height="wrap_content">

<TextView
android:id="@+id/text"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginVertical="6dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textAppearance="@style/m3_body_medium"
android:layout_gravity="center_vertical"
android:textColor="?colorM3OnSurfaceVariant"
tools:text="fdsafdsafsdafds"/>

<Button
android:id="@+id/results_btn"
style="@style/Widget.Mastodon.M3.Button.Tonal"
android:background="@drawable/bg_button_m3_tonal_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginEnd="16dp"
android:layout_marginVertical="4dp"
android:textAppearance="@style/m3_label_small"
android:text="@string/sk_poll_show_results"/>

</LinearLayout>

<Button
android:id="@+id/vote_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginBottom="4dp"
android:layout_marginTop="-4dp"
android:enabled="false"
style="@style/Widget.Mastodon.M3.Button.Filled.Elevated"
android:text="@string/action_vote"/>
Expand Down
1 change: 0 additions & 1 deletion mastodon/src/main/res/values-ar/strings_sk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
<string name="sk_color_palette_brown">قهوي</string>
<string name="sk_color_palette_red">أحمر</string>
<string name="sk_color_palette_yellow">أصفر</string>
<string name="sk_poll_allow_multiple">السماح بخيارات متعددة</string>
<string name="sk_translate_post">ترجمة</string>
<string name="sk_translate_show_original">إظهار الأصلي</string>
<string name="sk_translated_using">تُرجِم بواسطة %s</string>
Expand Down
1 change: 0 additions & 1 deletion mastodon/src/main/res/values-ca-rES/strings_sk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
<string name="sk_color_palette_yellow">Groc</string>
<string name="sk_timeline_local">Local</string>
<string name="sk_timeline_federated">Federació</string>
<string name="sk_poll_allow_multiple">Permet resposta múltiple</string>
<string name="sk_translate_post">Tradueix</string>
<string name="sk_example_domain">exemple.social</string>
<string name="sk_welcome_title">Et donem la benvinguda!</string>
Expand Down
1 change: 0 additions & 1 deletion mastodon/src/main/res/values-cy/strings_sk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
<string name="sk_settings_color_palette">Palet lliwiau</string>
<string name="sk_no_update_available">Dim diweddariad ar gael</string>
<string name="sk_check_for_update">Gwirio am ddiweddariad</string>
<string name="sk_poll_allow_multiple">Caniatáu mwy nag un dewis</string>
<string name="sk_language_name">%1$s (%2$s)</string>
<string name="sk_settings_tabs_disable_swipe">Analluogi llusgo rhwng tabiau</string>
<string name="sk_settings_posting">Dewisiadau postio</string>
Expand Down
2 changes: 1 addition & 1 deletion mastodon/src/main/res/values-de-rDE/strings_sk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
<string name="sk_color_palette_yellow">Gelb</string>
<string name="sk_notification_type_status">Beiträge</string>
<string name="sk_color_palette_blue">Blau</string>
<string name="sk_poll_allow_multiple">Mehrfachantworten erlauben</string>
<string name="sk_translated_using">Übersetzt mit %s</string>
<string name="sk_post_language">Sprache: %s</string>
<string name="sk_language_name">%1$s (%2$s)</string>
Expand Down Expand Up @@ -434,4 +433,5 @@
<string name="sk_open_post_preview">Vorschau öffnen</string>
<string name="sk_post_preview">Vorschau</string>
<string name="sk_private_note_confirm_delete">Private Notiz über %s löschen\?</string>
<string name="sk_poll_multiple_choice">Mehrfachauswahl</string>
</resources>
1 change: 0 additions & 1 deletion mastodon/src/main/res/values-es-rES/strings_sk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
<string name="sk_color_palette_blue">Azul</string>
<string name="sk_color_palette_brown">Marrón</string>
<string name="sk_color_palette_yellow">Amarillo</string>
<string name="sk_poll_allow_multiple">Permitir varias respuestas</string>
<string name="sk_translate_post">Traducir</string>
<string name="sk_translate_show_original">Mostrar original</string>
<string name="sk_translated_using">Traducido mediante %s</string>
Expand Down
1 change: 0 additions & 1 deletion mastodon/src/main/res/values-eu-rES/strings_sk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
<string name="sk_no_update_available">Ez dago eguneraketarik eskuragarri</string>
<string name="sk_list_timelines">Zerrendak</string>
<string name="sk_follow_requests">Jarraitzeko eskaerak</string>
<string name="sk_poll_allow_multiple">Onartu aukera anitzak</string>
<string name="sk_clear_recent_languages">Ezabatu berriki erabili diren hizkuntzak</string>
<string name="sk_welcome_title">Ongi etorri!</string>
<string name="sk_example_domain">adibidea.eus</string>
Expand Down
1 change: 0 additions & 1 deletion mastodon/src/main/res/values-fa/strings_sk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
<string name="sk_settings_show_boosts">نمایش تقویت‌ها</string>
<string name="sk_notification_type_update">فرسته‌های ویرایش شده</string>
<string name="sk_update_ready">مگالودون %s بارگیری شده و آماده نصب است.</string>
<string name="sk_poll_allow_multiple">اجازه انتخاب های متعدد</string>
<string name="sk_open_with_account">باز کردن با حساب دیگر</string>
<string name="sk_confirm_delete_draft_title">حذف پیش نویس</string>
<string name="sk_draft">پیش‌نویس</string>
Expand Down
1 change: 0 additions & 1 deletion mastodon/src/main/res/values-fi-rFI/strings_sk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@
<string name="sk_color_palette_brown">Ruskea</string>
<string name="sk_color_palette_red">Punainen</string>
<string name="sk_color_palette_yellow">Keltainen</string>
<string name="sk_poll_allow_multiple">Salli useita valintoja</string>
<string name="sk_translate_post">Käännä</string>
<string name="sk_welcome_text">Hai tervehtii sinua! Päästäksesi alkuun kirjoita koti-instanssisi osoite alla olevaan ruutuun.</string>
<string name="sk_example_domain">example.social</string>
Expand Down
1 change: 0 additions & 1 deletion mastodon/src/main/res/values-fr-rFR/strings_sk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
<string name="sk_color_palette_brown">Marron</string>
<string name="sk_color_palette_yellow">Jaune</string>
<string name="sk_settings_color_palette">Couleur d\'accentuation</string>
<string name="sk_poll_allow_multiple">Autoriser plusieurs choix</string>
<string name="sk_translate_post">Traduire</string>
<string name="sk_translate_show_original">Afficher l\'original</string>
<string name="sk_translated_using">Traduit en utilisant %s</string>
Expand Down
1 change: 0 additions & 1 deletion mastodon/src/main/res/values-gd-rGB/strings_sk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<resources>
<string name="sk_filtered">Criathraichte: %s</string>
<string name="sk_collapse">Co-theannaich</string>
<string name="sk_poll_allow_multiple">Ceadaich iomadh roghainn</string>
<string name="sk_pinned_posts">Prìnichte</string>
<string name="sk_clear_recent_languages">Falamhaich na cànain o chionn goirid</string>
<string name="sk_app_name">Megalodon</string>
Expand Down
1 change: 0 additions & 1 deletion mastodon/src/main/res/values-gl-rES/strings_sk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
<string name="sk_reject_follow_request">Rexeitar solicitude de seguimento</string>
<string name="sk_settings_always_reveal_content_warnings">Mostrar sempre o contido con avisos</string>
<string name="sk_notification_type_status">Publicacións</string>
<string name="sk_poll_allow_multiple">Permitir selección múltiple</string>
<string name="sk_translate_post">Traducir</string>
<string name="sk_translate_show_original">Mostrar orixinal</string>
<string name="sk_translated_using">Traducido empregando %s</string>
Expand Down
1 change: 0 additions & 1 deletion mastodon/src/main/res/values-hr-rHR/strings_sk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<string name="sk_timeline_home">Home</string>
<string name="sk_timeline_local">Lokalno</string>
<string name="sk_timeline_federated">Federacija</string>
<string name="sk_poll_allow_multiple">Omogući više izbora</string>
<string name="sk_clear_recent_languages">Očisti listu nedavno korištenih jezika</string>
<string name="sk_app_name">Megalodon</string>
<string name="sk_pinned_posts">Prikvačeno</string>
Expand Down
1 change: 0 additions & 1 deletion mastodon/src/main/res/values-hu-rHU/strings_sk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@
<string name="sk_settings_show_boosts">Show boostok</string>
<string name="sk_settings_show_interaction_counts">Interakciószámok megjelenítése</string>
<string name="sk_reject_follow_request">Követési kérelem elutasítása</string>
<string name="sk_poll_allow_multiple">Több választási lehetőség engedélyezése</string>
<string name="sk_settings_tabs_disable_swipe">Lapok közötti lapozás letiltása</string>
<string name="sk_settings_about">Az alkalmazásról</string>
<string name="sk_quoting_user">%s említése</string>
Expand Down
1 change: 0 additions & 1 deletion mastodon/src/main/res/values-in-rID/strings_sk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
<string name="sk_color_palette_blue">Biru</string>
<string name="sk_color_palette_brown">Cokelat</string>
<string name="sk_color_palette_yellow">Kuning</string>
<string name="sk_poll_allow_multiple">Perbolehkan beberapa pilihan</string>
<string name="sk_clear_recent_languages">Hapus bahasa terkini yang digunakan</string>
<string name="sk_welcome_title">Selamat datang!</string>
<string name="sk_confirm_clear_recent_languages">Apakah Anda yakin ingin menghapus bahasa terkini yang Anda gunakan\?</string>
Expand Down
1 change: 0 additions & 1 deletion mastodon/src/main/res/values-it-rIT/strings_sk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
<string name="sk_clear_recent_languages">Cancella le lingue usate di recente</string>
<string name="sk_welcome_title">Benvenuto!</string>
<string name="sk_example_domain">example.social</string>
<string name="sk_poll_allow_multiple">Consenti scelte multiple</string>
<string name="sk_available_languages">Lingue disponibili</string>
<string name="sk_welcome_text">Lo squalo ti saluta! Per iniziare inserisci qui sotto l\'indirizzo dell\'istanza a cui sei iscritto.</string>
<string name="sk_color_palette_material3">Sistema</string>
Expand Down
1 change: 0 additions & 1 deletion mastodon/src/main/res/values-ja-rJP/strings_sk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
<string name="sk_timeline_home">ホーム</string>
<string name="sk_translate_post">翻訳</string>
<string name="sk_translate_show_original">オリジナルを表示</string>
<string name="sk_poll_allow_multiple">複数回答を許可</string>
<string name="sk_clear_recent_languages">最近使用した言語を消去</string>
<string name="sk_settings_show_boosts">ブーストを表示</string>
<string name="sk_app_name">Megalodon</string>
Expand Down
1 change: 0 additions & 1 deletion mastodon/src/main/res/values-ko-rKR/strings_sk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
<string name="sk_color_palette_brown">갈색</string>
<string name="sk_color_palette_yellow">노란색</string>
<string name="sk_settings_contribute">Megalodon에 기여</string>
<string name="sk_poll_allow_multiple">다중 선택 허용</string>
<string name="sk_translate_post">번역하기</string>
<string name="sk_translate_show_original">원본 보기</string>
<string name="sk_post_language">언어: %s</string>
Expand Down
1 change: 0 additions & 1 deletion mastodon/src/main/res/values-my/strings_sk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
<string name="sk_federated_timeline_info_banner">သင့်ဖက်ဒရေးရှင်းကလူတွေရဲ့ လတ်တလောပို့စ်တွေ ဖြစ်ပါတယ်။</string>
<string name="sk_check_for_update">အပ်ဒိတ်ရှိလားစစ်ဆေးမည်</string>
<string name="sk_settings_enable_marquee">ခေါင်းစီးဘားများတွင် စာများရွေ့လျားမှု ပိတ်မည်</string>
<string name="sk_poll_allow_multiple">တစ်ခုထက်ပိုသောရွေးချယ်မှုများကို ခွင့်ပြုမည်</string>
<string name="sk_clear_recent_languages">လတ်တလောသုံးထားသည့် ဘာသာစကားများကို ရှင်းလင်းမည်</string>
<string name="sk_welcome_title">မင်္ဂလာပါ</string>
<string name="sk_pinned_posts">ပင်တွဲထားသည်များ</string>
Expand Down
1 change: 0 additions & 1 deletion mastodon/src/main/res/values-nl-rNL/strings_sk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
<string name="sk_timeline_home">Home</string>
<string name="sk_timeline_federated">Federatie</string>
<string name="sk_translate_post">Vertaal</string>
<string name="sk_poll_allow_multiple">Meerdere keuzes toestaan</string>
<string name="sk_clear_recent_languages">Wis recent gebruikte talen</string>
<string name="sk_welcome_text">De haai groet je! Graag je instance naam (Mastodon server) hieronder invullen om te beginnen.</string>
<string name="sk_welcome_title">Welkom!</string>
Expand Down

0 comments on commit 79d5067

Please sign in to comment.