Fix last issues

This commit is contained in:
tom79 2019-03-24 17:21:13 +01:00
parent 4c54271a70
commit 58efaf1d00
6 changed files with 183 additions and 9 deletions

View File

@ -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 <http://www.gnu.org/licenses>. */
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<Void, Void, Void> {
private OnPollInterface listener;
private APIResponse apiResponse;
private fr.gouv.etalab.mastodon.client.Entities.Status status;
private int[] choices;
private WeakReference<Context> 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);
}
}

View File

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

View File

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

View File

@ -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<Status> 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<Integer> 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<Integer> 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 {

View File

@ -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 <http://www.gnu.org/licenses>. */
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);
}

View File

@ -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 @@
<EditText
android:id="@+id/choice_2"
android:hint="@string/poll_choice_2"
android:maxLength="25"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -40,6 +42,7 @@
<EditText
android:id="@+id/choice_3"
android:hint="@string/poll_choice_3"
android:maxLength="25"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -48,6 +51,7 @@
<EditText
android:id="@+id/choice_4"
android:hint="@string/poll_choice_4"
android:maxLength="25"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"