From 9b102a87a689eda497c8059d70a62d05089ef731 Mon Sep 17 00:00:00 2001 From: tom79 Date: Sun, 28 Apr 2019 11:15:57 +0200 Subject: [PATCH 1/2] Improve polls by allowing more choices --- .../drawers/NotificationsListAdapter.java | 92 ++++++------------- .../mastodon/drawers/StatusListAdapter.java | 88 ++++++------------ app/src/main/res/layout/layout_poll.xml | 45 +-------- 3 files changed, 60 insertions(+), 165 deletions(-) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/NotificationsListAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/NotificationsListAdapter.java index c70d13bb8..6f569cde2 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/NotificationsListAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/NotificationsListAdapter.java @@ -574,46 +574,23 @@ public class NotificationsListAdapter extends RecyclerView.Adapter implements On holder.choices.init(context).hasAnimation(true).addAll(items).build(); }else { if( poll.isMultiple()){ + if((holder.multiple_choice).getChildCount() > 0) + (holder.multiple_choice).removeAllViews(); + for(PollOptions pollOption: poll.getOptionsList()){ + CheckBox cb = new CheckBox(context); + cb.setText(pollOption.getTitle()); + holder.multiple_choice.addView(cb); + } holder.multiple_choice.setVisibility(View.VISIBLE); - holder.c_choice_3.setVisibility(View.GONE); - holder.c_choice_4.setVisibility(View.GONE); - if( choiceCount > 2) - holder.c_choice_3.setVisibility(View.VISIBLE); - if( choiceCount > 3) - holder.c_choice_4.setVisibility(View.VISIBLE); - int j = 1; - for(PollOptions pollOption: status.getPoll().getOptionsList()){ - if( j == 1 ) - holder.c_choice_1.setText(pollOption.getTitle()); - else if( j == 2 ) - holder.c_choice_2.setText(pollOption.getTitle()); - else if( j == 3 ) - holder.c_choice_3.setText(pollOption.getTitle()); - else if( j == 4 ) - holder.c_choice_4.setText(pollOption.getTitle()); - j++; - } - }else { - holder.single_choice.setVisibility(View.VISIBLE); - holder.r_choice_3.setVisibility(View.GONE); - holder.r_choice_4.setVisibility(View.GONE); - if( choiceCount > 2) - holder.r_choice_3.setVisibility(View.VISIBLE); - if( choiceCount > 3) - holder.r_choice_4.setVisibility(View.VISIBLE); - int j = 1; - for(PollOptions pollOption: status.getPoll().getOptionsList()){ - if( j == 1 ) - holder.r_choice_1.setText(pollOption.getTitle()); - else if( j == 2 ) - holder.r_choice_2.setText(pollOption.getTitle()); - else if( j == 3 ) - holder.r_choice_3.setText(pollOption.getTitle()); - else if( j == 4 ) - holder.r_choice_4.setText(pollOption.getTitle()); - j++; + if((holder.radio_group).getChildCount() > 0) + (holder.radio_group).removeAllViews(); + for(PollOptions pollOption: poll.getOptionsList()){ + RadioButton rb = new RadioButton(context); + rb.setText(pollOption.getTitle()); + holder.radio_group.addView(rb); } + holder.single_choice.setVisibility(View.VISIBLE); } holder.submit_vote.setVisibility(View.VISIBLE); holder.submit_vote.setOnClickListener(new View.OnClickListener() { @@ -622,14 +599,14 @@ public class NotificationsListAdapter extends RecyclerView.Adapter implements On int [] choice; if( poll.isMultiple()){ ArrayList choices = new ArrayList<>(); - if( holder.c_choice_1.isChecked()) - choices.add(0); - if( holder.c_choice_2.isChecked()) - choices.add(1); - if( holder.c_choice_3.isChecked()) - choices.add(2); - if( holder.c_choice_4.isChecked()) - choices.add(3); + int choicesCount = holder.multiple_choice.getChildCount(); + for( int i = 0 ; i < choicesCount ; i++){ + if( holder.multiple_choice.getChildAt(i) != null && holder.multiple_choice.getChildAt(i) instanceof CheckBox){ + if(((CheckBox) holder.multiple_choice.getChildAt(i)).isChecked()){ + choices.add(i); + } + } + } choice = new int[choices.size()]; Iterator iterator = choices.iterator(); for (int i = 0; i < choice.length; i++) { @@ -640,15 +617,14 @@ public class NotificationsListAdapter extends RecyclerView.Adapter implements On }else{ choice = new int[1]; choice[0] = -1; - int checkedId = holder.radio_group.getCheckedRadioButtonId(); - if( checkedId == R.id.r_choice_1) - choice[0] = 0; - if( checkedId == R.id.r_choice_2) - choice[0] = 1; - if( checkedId == R.id.r_choice_3) - choice[0] = 2; - if( checkedId == R.id.r_choice_4) - choice[0] = 3; + int choicesCount = holder.radio_group.getChildCount(); + for( int i = 0 ; i < choicesCount ; i++){ + if( holder.radio_group.getChildAt(i) != null && holder.radio_group.getChildAt(i) instanceof RadioButton){ + if(((RadioButton) holder.radio_group.getChildAt(i)).isChecked()){ + choice[0] = i; + } + } + } if( choice[0] == -1) return; } @@ -1425,14 +1401,6 @@ public class NotificationsListAdapter extends RecyclerView.Adapter implements On multiple_choice = itemView.findViewById(R.id.multiple_choice); rated = itemView.findViewById(R.id.rated); radio_group = itemView.findViewById(R.id.radio_group); - r_choice_1 = itemView.findViewById(R.id.r_choice_1); - r_choice_2 = itemView.findViewById(R.id.r_choice_2); - r_choice_3 = itemView.findViewById(R.id.r_choice_3); - r_choice_4 = itemView.findViewById(R.id.r_choice_4); - c_choice_1 = itemView.findViewById(R.id.c_choice_1); - c_choice_2 = itemView.findViewById(R.id.c_choice_2); - c_choice_3 = itemView.findViewById(R.id.c_choice_3); - c_choice_4 = itemView.findViewById(R.id.c_choice_4); choices = itemView.findViewById(R.id.choices); number_votes = itemView.findViewById(R.id.number_votes); remaining_time = itemView.findViewById(R.id.remaining_time); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java index 363bc2f8a..cc1b98071 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java @@ -454,14 +454,6 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct multiple_choice = itemView.findViewById(R.id.multiple_choice); rated = itemView.findViewById(R.id.rated); radio_group = itemView.findViewById(R.id.radio_group); - r_choice_1 = itemView.findViewById(R.id.r_choice_1); - r_choice_2 = itemView.findViewById(R.id.r_choice_2); - r_choice_3 = itemView.findViewById(R.id.r_choice_3); - r_choice_4 = itemView.findViewById(R.id.r_choice_4); - c_choice_1 = itemView.findViewById(R.id.c_choice_1); - c_choice_2 = itemView.findViewById(R.id.c_choice_2); - c_choice_3 = itemView.findViewById(R.id.c_choice_3); - c_choice_4 = itemView.findViewById(R.id.c_choice_4); choices = itemView.findViewById(R.id.choices); number_votes = itemView.findViewById(R.id.number_votes); remaining_time = itemView.findViewById(R.id.remaining_time); @@ -605,46 +597,25 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct holder.choices.init(context).hasAnimation(true).addAll(items).build(); }else { if( poll.isMultiple()){ - holder.multiple_choice.setVisibility(View.VISIBLE); - holder.c_choice_3.setVisibility(View.GONE); - holder.c_choice_4.setVisibility(View.GONE); - if( choiceCount > 2) - holder.c_choice_3.setVisibility(View.VISIBLE); - if( choiceCount > 3) - holder.c_choice_4.setVisibility(View.VISIBLE); - int j = 1; + + if((holder.multiple_choice).getChildCount() > 0) + (holder.multiple_choice).removeAllViews(); for(PollOptions pollOption: poll.getOptionsList()){ - if( j == 1 ) - holder.c_choice_1.setText(pollOption.getTitle()); - else if( j == 2 ) - holder.c_choice_2.setText(pollOption.getTitle()); - else if( j == 3 ) - holder.c_choice_3.setText(pollOption.getTitle()); - else if( j == 4 ) - holder.c_choice_4.setText(pollOption.getTitle()); - j++; + CheckBox cb = new CheckBox(context); + cb.setText(pollOption.getTitle()); + holder.multiple_choice.addView(cb); } + holder.multiple_choice.setVisibility(View.VISIBLE); }else { - holder.single_choice.setVisibility(View.VISIBLE); - holder.r_choice_3.setVisibility(View.GONE); - holder.r_choice_4.setVisibility(View.GONE); - if( choiceCount > 2) - holder.r_choice_3.setVisibility(View.VISIBLE); - if( choiceCount > 3) - holder.r_choice_4.setVisibility(View.VISIBLE); - int j = 1; + if((holder.radio_group).getChildCount() > 0) + (holder.radio_group).removeAllViews(); for(PollOptions pollOption: poll.getOptionsList()){ - if( j == 1 ) - holder.r_choice_1.setText(pollOption.getTitle()); - else if( j == 2 ) - holder.r_choice_2.setText(pollOption.getTitle()); - else if( j == 3 ) - holder.r_choice_3.setText(pollOption.getTitle()); - else if( j == 4 ) - holder.r_choice_4.setText(pollOption.getTitle()); - j++; + RadioButton rb = new RadioButton(context); + rb.setText(pollOption.getTitle()); + holder.radio_group.addView(rb); } + holder.single_choice.setVisibility(View.VISIBLE); } holder.submit_vote.setVisibility(View.VISIBLE); holder.submit_vote.setOnClickListener(new View.OnClickListener() { @@ -653,14 +624,14 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct int [] choice; if( poll.isMultiple()){ ArrayList choices = new ArrayList<>(); - if( holder.c_choice_1.isChecked()) - choices.add(0); - if( holder.c_choice_2.isChecked()) - choices.add(1); - if( holder.c_choice_3.isChecked()) - choices.add(2); - if( holder.c_choice_4.isChecked()) - choices.add(3); + int choicesCount = holder.multiple_choice.getChildCount(); + for( int i = 0 ; i < choicesCount ; i++){ + if( holder.multiple_choice.getChildAt(i) != null && holder.multiple_choice.getChildAt(i) instanceof CheckBox){ + if(((CheckBox) holder.multiple_choice.getChildAt(i)).isChecked()){ + choices.add(i); + } + } + } choice = new int[choices.size()]; Iterator iterator = choices.iterator(); for (int i = 0; i < choice.length; i++) { @@ -670,16 +641,15 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct return; }else{ choice = new int[1]; - int checkedId = holder.radio_group.getCheckedRadioButtonId(); choice[0] = -1; - if( checkedId == R.id.r_choice_1) - choice[0] = 0; - if( checkedId == R.id.r_choice_2) - choice[0] = 1; - if( checkedId == R.id.r_choice_3) - choice[0] = 2; - if( checkedId == R.id.r_choice_4) - choice[0] = 3; + int choicesCount = holder.radio_group.getChildCount(); + for( int i = 0 ; i < choicesCount ; i++){ + if( holder.radio_group.getChildAt(i) != null && holder.radio_group.getChildAt(i) instanceof RadioButton){ + if(((RadioButton) holder.radio_group.getChildAt(i)).isChecked()){ + choice[0] = i; + } + } + } if( choice[0] == -1) return; } diff --git a/app/src/main/res/layout/layout_poll.xml b/app/src/main/res/layout/layout_poll.xml index 1fbf58c9e..a2d51e0da 100644 --- a/app/src/main/res/layout/layout_poll.xml +++ b/app/src/main/res/layout/layout_poll.xml @@ -37,32 +37,7 @@ android:layout_height="wrap_content" android:orientation="vertical" tools:ignore="UselessParent"> - - - - + - - - - Date: Sun, 28 Apr 2019 14:30:37 +0200 Subject: [PATCH 2/2] Allow polls for private messages --- .../java/fr/gouv/etalab/mastodon/activities/TootActivity.java | 4 ++-- .../etalab/mastodon/drawers/NotificationsListAdapter.java | 2 -- .../fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java index 1f296f8b2..ace06bd5e 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java @@ -1517,11 +1517,11 @@ public class TootActivity extends BaseActivity implements OnPostActionInterface, toot_it.setEnabled(true); return; } - if( poll != null && visibility.equals("direct")){ + /*if( poll != null && visibility.equals("direct")){ Toasty.error(getApplicationContext(),getString(R.string.poll_not_private),Toast.LENGTH_LONG).show(); toot_it.setEnabled(true); return; - } + }*/ SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE); boolean split_toot = sharedpreferences.getBoolean(Helper.SET_AUTOMATICALLY_SPLIT_TOOTS, false); int split_toot_size = sharedpreferences.getInt(Helper.SET_AUTOMATICALLY_SPLIT_TOOTS_SIZE, Helper.SPLIT_TOOT_SIZE); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/NotificationsListAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/NotificationsListAdapter.java index 6f569cde2..94e354192 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/NotificationsListAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/NotificationsListAdapter.java @@ -1347,8 +1347,6 @@ public class NotificationsListAdapter extends RecyclerView.Adapter implements On //Poll LinearLayout poll_container, single_choice, multiple_choice, rated; RadioGroup radio_group; - RadioButton r_choice_1, r_choice_2, r_choice_3, r_choice_4; - CheckBox c_choice_1, c_choice_2, c_choice_3, c_choice_4; HorizontalBar choices; TextView number_votes, remaining_time; Button submit_vote, refresh_poll; diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java index cc1b98071..c9200e7b4 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java @@ -355,8 +355,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct //Poll LinearLayout poll_container, single_choice, multiple_choice, rated; RadioGroup radio_group; - RadioButton r_choice_1, r_choice_2, r_choice_3, r_choice_4; - CheckBox c_choice_1, c_choice_2, c_choice_3, c_choice_4; + HorizontalBar choices; TextView number_votes, remaining_time; Button submit_vote, refresh_poll;