From 58efaf1d00fed5bd47b233ea50a1b12e89939bca Mon Sep 17 00:00:00 2001 From: tom79 Date: Sun, 24 Mar 2019 17:21:13 +0100 Subject: [PATCH] Fix last issues --- .../asynctasks/ManagePollAsyncTask.java | 71 +++++++++++++++++++ .../fr/gouv/etalab/mastodon/client/API.java | 26 ++++++- .../mastodon/client/HttpsConnection.java | 3 - .../mastodon/drawers/StatusListAdapter.java | 61 ++++++++++++++-- .../mastodon/interfaces/OnPollInterface.java | 27 +++++++ app/src/main/res/layout/popup_poll.xml | 4 ++ 6 files changed, 183 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/ManagePollAsyncTask.java create mode 100644 app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnPollInterface.java diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/ManagePollAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/ManagePollAsyncTask.java new file mode 100644 index 000000000..209da409f --- /dev/null +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/ManagePollAsyncTask.java @@ -0,0 +1,71 @@ +/* Copyright 2019 Thomas Schneider + * + * This file is a part of Mastalab + * + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; either version 3 of the + * License, or (at your option) any later version. + * + * Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even + * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Mastalab; if not, + * see . */ +package fr.gouv.etalab.mastodon.asynctasks; + +import android.content.Context; +import android.os.AsyncTask; +import java.lang.ref.WeakReference; + +import fr.gouv.etalab.mastodon.client.API; +import fr.gouv.etalab.mastodon.client.APIResponse; +import fr.gouv.etalab.mastodon.client.Entities.Poll; +import fr.gouv.etalab.mastodon.interfaces.OnPollInterface; + +/** + * Created by Thomas on 23/03/2019. + * Manage Poll + */ + +public class ManagePollAsyncTask extends AsyncTask { + + private OnPollInterface listener; + private APIResponse apiResponse; + private fr.gouv.etalab.mastodon.client.Entities.Status status; + private int[] choices; + private WeakReference contextReference; + private Poll poll; + private type_s type; + + public enum type_s{ + SUBMIT, + REFRESH + } + + public ManagePollAsyncTask(Context context, type_s type, fr.gouv.etalab.mastodon.client.Entities.Status status, int[] choices, OnPollInterface onPollInterface){ + this.contextReference = new WeakReference<>(context); + this.listener = onPollInterface; + this.status = status; + this.choices = choices; + this.type = type; + } + + @Override + protected Void doInBackground(Void... params) { + if( status.getPoll().getId() == null) + return null; + if (type == type_s.SUBMIT){ + poll = new API(contextReference.get()).submiteVote(status.getPoll().getId(),choices); + }else if( type == type_s.REFRESH){ + poll = new API(contextReference.get()).getPoll(status.getPoll().getId()); + } + return null; + } + + @Override + protected void onPostExecute(Void result) { + listener.onPoll(status, poll); + } + +} diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java index a60827b08..1c53bca1e 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java @@ -20,7 +20,6 @@ import android.content.SharedPreferences; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.support.v4.content.LocalBroadcastManager; -import android.util.Log; import com.google.gson.JsonArray; import com.google.gson.JsonObject; @@ -2128,6 +2127,31 @@ public class API { return null; } + /** + * Public api call to refresh a poll + * @param pollId + * @return + */ + public Poll getPoll(String pollId){ + try { + HttpsConnection httpsConnection = new HttpsConnection(context); + String response = httpsConnection.get(getAbsoluteUrl(String.format("/polls/%s", pollId)), 60, null, prefKeyOauthTokenT); + return parsePoll(context, new JSONObject(response)); + } catch (HttpsConnection.HttpsConnectionException e) { + setError(e.getStatusCode(), e); + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (KeyManagementException e) { + e.printStackTrace(); + } catch (JSONException e) { + e.printStackTrace(); + } + return null; + } + /** * Posts a status diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/HttpsConnection.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/HttpsConnection.java index d24f0a966..5d068bfe5 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/HttpsConnection.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/HttpsConnection.java @@ -20,7 +20,6 @@ import android.database.sqlite.SQLiteDatabase; import android.os.Build; import android.text.Html; import android.text.SpannableString; -import android.util.Log; import com.google.common.io.ByteStreams; import com.google.gson.JsonObject; @@ -328,7 +327,6 @@ public class HttpsConnection { postData.append(String.valueOf(param.getValue())); } byte[] postDataBytes = postData.toString().getBytes("UTF-8"); - Log.v(Helper.TAG,"postData! " + postData); if (proxy != null) httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy); else @@ -433,7 +431,6 @@ public class HttpsConnection { public String postJson(String urlConnection, int timeout, JsonObject jsonObject, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException { if( urlConnection.startsWith("https://")) { URL url = new URL(urlConnection); - Log.v(Helper.TAG,"--> " +jsonObject); byte[] postDataBytes = new byte[0]; postDataBytes = jsonObject.toString().getBytes("UTF-8"); if (proxy != null) 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 ae810e836..75e6f94ef 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 @@ -25,6 +25,7 @@ import android.content.SharedPreferences; import android.database.sqlite.SQLiteDatabase; import android.graphics.Bitmap; import android.graphics.Color; +import android.graphics.Paint; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.AsyncTask; @@ -47,7 +48,6 @@ import android.text.method.LinkMovementMethod; import android.text.style.ClickableSpan; import android.text.style.ForegroundColorSpan; import android.text.style.URLSpan; -import android.util.Log; import android.util.TypedValue; import android.view.LayoutInflater; import android.view.MenuItem; @@ -91,6 +91,7 @@ import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; +import java.util.Iterator; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -108,6 +109,7 @@ import fr.gouv.etalab.mastodon.activities.ShowAccountActivity; import fr.gouv.etalab.mastodon.activities.ShowConversationActivity; import fr.gouv.etalab.mastodon.activities.TootActivity; import fr.gouv.etalab.mastodon.activities.TootInfoActivity; +import fr.gouv.etalab.mastodon.asynctasks.ManagePollAsyncTask; import fr.gouv.etalab.mastodon.asynctasks.PostActionAsyncTask; import fr.gouv.etalab.mastodon.asynctasks.RetrieveFeedsAsyncTask; import fr.gouv.etalab.mastodon.asynctasks.UpdateAccountInfoAsyncTask; @@ -128,6 +130,7 @@ import fr.gouv.etalab.mastodon.fragments.DisplayStatusFragment; import fr.gouv.etalab.mastodon.helper.CrossActions; import fr.gouv.etalab.mastodon.helper.CustomTextView; import fr.gouv.etalab.mastodon.helper.Helper; +import fr.gouv.etalab.mastodon.interfaces.OnPollInterface; import fr.gouv.etalab.mastodon.interfaces.OnPostActionInterface; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveCardInterface; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveEmojiInterface; @@ -153,7 +156,7 @@ import static fr.gouv.etalab.mastodon.helper.Helper.getLiveInstance; * Created by Thomas on 24/04/2017. * Adapter for Status */ -public class StatusListAdapter extends RecyclerView.Adapter implements OnPostActionInterface, OnRetrieveFeedsInterface, OnRetrieveEmojiInterface, OnRetrieveRepliesInterface, OnRetrieveCardInterface { +public class StatusListAdapter extends RecyclerView.Adapter implements OnPostActionInterface, OnRetrieveFeedsInterface, OnRetrieveEmojiInterface, OnRetrieveRepliesInterface, OnRetrieveCardInterface, OnPollInterface { private Context context; private List statuses; @@ -240,6 +243,12 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct notifyStatusChanged(modifiedStatus.get(0)); } + @Override + public void onPoll(Status status, Poll poll) { + status.setPoll(poll); + notifyStatusChanged(status); + } + private class ViewHolderEmpty extends RecyclerView.ViewHolder{ ViewHolderEmpty(View itemView) { @@ -344,7 +353,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct 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; + TextView number_votes, remaining_time, refresh_poll; Button submit_vote; public View getView(){ @@ -452,6 +461,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct number_votes = itemView.findViewById(R.id.number_votes); remaining_time = itemView.findViewById(R.id.remaining_time); submit_vote = itemView.findViewById(R.id.submit_vote); + refresh_poll = itemView.findViewById(R.id.refresh_poll); } } @@ -572,10 +582,10 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct items.add(bar); } } + holder.choices.init(context).hasAnimation(true).removeAll(); holder.choices.init(context).hasAnimation(true).addAll(items).build(); }else { if( poll.isMultiple()){ - Log.v(Helper.TAG,"count: " + choiceCount); holder.multiple_choice.setVisibility(View.VISIBLE); holder.c_choice_3.setVisibility(View.GONE); holder.c_choice_4.setVisibility(View.GONE); @@ -597,7 +607,6 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct } }else { - Log.v(Helper.TAG,"count: " + choiceCount); holder.single_choice.setVisibility(View.VISIBLE); holder.r_choice_3.setVisibility(View.GONE); holder.r_choice_4.setVisibility(View.GONE); @@ -619,8 +628,50 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct } } holder.submit_vote.setVisibility(View.VISIBLE); + holder.submit_vote.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + 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); + choice = new int[choices.size()]; + Iterator iterator = choices.iterator(); + for (int i = 0; i < choice.length; i++) { + choice[i] = iterator.next().intValue(); + } + }else{ + choice = new int[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; + } + new ManagePollAsyncTask(context, ManagePollAsyncTask.type_s.SUBMIT, status, choice, StatusListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } + }); } + + holder.refresh_poll.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + new ManagePollAsyncTask(context, ManagePollAsyncTask.type_s.REFRESH, status, null, StatusListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } + }); holder.poll_container.setVisibility(View.VISIBLE); + holder.refresh_poll.setPaintFlags(holder.refresh_poll.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); holder.number_votes.setText(context.getResources().getQuantityString(R.plurals.number_of_vote,status.getPoll().getVotes_count(),status.getPoll().getVotes_count())); holder.remaining_time.setText(context.getString(R.string.poll_finish_at, Helper.dateToStringPoll(poll.getExpires_at()))); }else { diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnPollInterface.java b/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnPollInterface.java new file mode 100644 index 000000000..d195df513 --- /dev/null +++ b/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnPollInterface.java @@ -0,0 +1,27 @@ +/* Copyright 2019 Thomas Schneider + * + * This file is a part of Mastalab + * + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; either version 3 of the + * License, or (at your option) any later version. + * + * Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even + * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Mastalab; if not, + * see . */ +package fr.gouv.etalab.mastodon.interfaces; + + +import fr.gouv.etalab.mastodon.client.Entities.Poll; +import fr.gouv.etalab.mastodon.client.Entities.Status; + +/** + * Created by Thomas on 23/03/2019. + * Interface when for polls + */ +public interface OnPollInterface { + void onPoll(Status status, Poll poll); +} diff --git a/app/src/main/res/layout/popup_poll.xml b/app/src/main/res/layout/popup_poll.xml index 8d4e54cf6..5782121fa 100644 --- a/app/src/main/res/layout/popup_poll.xml +++ b/app/src/main/res/layout/popup_poll.xml @@ -25,6 +25,7 @@ android:id="@+id/choice_1" android:hint="@string/poll_choice_1" android:singleLine="true" + android:maxLength="25" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" @@ -32,6 +33,7 @@