Scheduled view for server side

This commit is contained in:
stom79 2019-01-20 09:45:48 +01:00
parent 0411b878c2
commit 284c118daf
4 changed files with 166 additions and 25 deletions

View File

@ -1227,8 +1227,9 @@ public class TootActivity extends BaseActivity implements OnRetrieveSearcAccount
datePicker.getDayOfMonth(),
hour,
minute);
long time = calendar.getTimeInMillis();
if( (time - new Date().getTime()) < 60000 ){
final long[] time = {calendar.getTimeInMillis()};
final String date = Helper.dateToString(new Date(calendar.getTimeInMillis()));
if( (time[0] - new Date().getTime()) < 60000 ){
Toasty.warning(getApplicationContext(), getString(R.string.toot_scheduled_date), Toast.LENGTH_LONG).show();
}else {
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
@ -1241,21 +1242,20 @@ public class TootActivity extends BaseActivity implements OnRetrieveSearcAccount
builderSingle.setNegativeButton(R.string.device_schedule, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
deviceSchedule(time);
deviceSchedule(time[0]);
dialog.dismiss();
}
});
builderSingle.setPositiveButton(R.string.server_schedule, new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, int which) {
serverSchedule(time);
serverSchedule(date);
}
});
builderSingle.show();
} else {
deviceSchedule(time);
deviceSchedule(time[0]);
}
alertDialog.dismiss();
@ -1299,16 +1299,19 @@ public class TootActivity extends BaseActivity implements OnRetrieveSearcAccount
if( tootReply != null)
toot.setIn_reply_to_id(tootReply.getId());
toot.setContent(tootContent);
if( timestamp != null ){
if( timestamp == null)
new PostStatusAsyncTask(getApplicationContext(), accountReply, toot, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else {
toot.setScheduled_at(timestamp);
new PostStatusAsyncTask(getApplicationContext(), accountReply, toot, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
new PostStatusAsyncTask(getApplicationContext(), accountReply, toot, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
private void serverSchedule(long time){
sendToot(String.valueOf(time));
private void serverSchedule(String time){
sendToot(time);
resetForNextToot();
}

View File

@ -57,6 +57,7 @@ import fr.gouv.etalab.mastodon.client.Entities.Notification;
import fr.gouv.etalab.mastodon.client.Entities.Peertube;
import fr.gouv.etalab.mastodon.client.Entities.Relationship;
import fr.gouv.etalab.mastodon.client.Entities.Results;
import fr.gouv.etalab.mastodon.client.Entities.Schedule;
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.client.Entities.StoredStatus;
import fr.gouv.etalab.mastodon.client.Entities.Tag;
@ -1800,7 +1801,7 @@ public class API {
params.put("spoiler_text", status.getSpoiler_text());
}
params.put("visibility", status.getVisibility());
break;
break;
default:
return -1;
}
@ -1897,7 +1898,6 @@ public class API {
response = httpsConnection.get(getAbsoluteUrl(String.format("/scheduled_statuses/%s", status.getId())), 60, params, prefKeyOauthTokenT);
else if( call.equals("DELETE"))
responseCode = httpsConnection.delete(getAbsoluteUrl(String.format("/scheduled_statuses/%s", status.getId())), 60, params, prefKeyOauthTokenT);
if(call.equals("GET")) {
apiResponse.setSince_id(httpsConnection.getSince_id());
apiResponse.setMax_id(httpsConnection.getMax_id());
@ -1917,22 +1917,21 @@ public class API {
st.setStatus(statusreturned);
storedStatus.add(st);
}else if (response != null && call.equals("GET")) {
List<Status> statusreturned = parseStatuses(context, new JSONArray(response));
for(Status status1: statusreturned){
List<Schedule> scheduleList = parseSchedule(context, new JSONArray(response));
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
for(Schedule schedule: scheduleList){
StoredStatus st = new StoredStatus();
st.setCreation_date(status.getCreated_at());
st.setCreation_date(null);
st.setId(-1);
st.setJobId(-1);
st.setScheduled_date(new Date(Long.parseLong(status.getScheduled_at())));
st.setScheduled_date(schedule.getScheduled_at());
st.setStatusReply(null);
st.setSent_date(null);
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
st.setUserId(userId);
st.setStatus(status1);
st.setStatus(schedule.getStatus());
storedStatus.add(st);
}
storedStatus.addAll(storedStatus);
}
} catch (HttpsConnection.HttpsConnectionException e) {
@ -1975,6 +1974,8 @@ public class API {
parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(12));
params.put("media_ids[]", parameters.toString());
}
if( status.getScheduled_at() != null)
params.put("scheduled_at", status.getScheduled_at());
if( status.isSensitive())
params.put("sensitive", Boolean.toString(status.isSensitive()));
if( status.getSpoiler_text() != null)
@ -1985,7 +1986,6 @@ public class API {
}
params.put("visibility", status.getVisibility());
statuses = new ArrayList<>();
try {
HttpsConnection httpsConnection = new HttpsConnection(context);
String response = httpsConnection.post(getAbsoluteUrl("/statuses"), 60, params, prefKeyOauthTokenT);
@ -3195,12 +3195,43 @@ public class API {
}catch (JSONException ignored) {}
return conversation;
}
/**
* Parse json response for several scheduled toots
* @param jsonArray JSONArray
* @return List<Status>
*/
private static List<Schedule> parseSchedule(Context context, JSONArray jsonArray){
List<Schedule> schedules = new ArrayList<>();
try {
int i = 0;
while (i < jsonArray.length() ){
Schedule schedule = new Schedule();
JSONObject resobj = jsonArray.getJSONObject(i).getJSONObject("params");
Status status = parseSchedule(context, resobj);
List<Attachment> attachements = parseAttachmentResponse(jsonArray.getJSONObject(i).getJSONArray("media_attachments"));
status.setMedia_attachments((ArrayList<Attachment>) attachements);
schedule.setStatus(status);
schedule.setAttachmentList(attachements);
schedules.add(schedule);
schedule.setId(jsonArray.getJSONObject(i).get("id").toString());
schedule.setScheduled_at(Helper.stringToDate(context, resobj.get("scheduled_at").toString()));
i++;
}
} catch (JSONException e) {
e.printStackTrace();
}
return schedules;
}
/**
* Parse json response for several status
* @param jsonArray JSONArray
* @return List<Status>
*/
public static List<Status> parseStatuses(Context context, JSONArray jsonArray){
private static List<Status> parseStatuses(Context context, JSONArray jsonArray){
List<Status> statuses = new ArrayList<>();
try {
@ -3362,6 +3393,26 @@ public class API {
}
/**
* Parse json response for unique schedule
* @param resobj JSONObject
* @return Status
*/
@SuppressWarnings("InfiniteRecursion")
private static Status parseSchedule(Context context, JSONObject resobj){
Status status = new Status();
try {
status.setIn_reply_to_id(resobj.get("in_reply_to_id").toString());
status.setSensitive(Boolean.parseBoolean(resobj.get("sensitive").toString()));
status.setSpoiler_text(resobj.get("spoiler_text").toString());
try {
status.setVisibility(resobj.get("visibility").toString());
}catch (Exception e){status.setVisibility("public");}
status.setContent(resobj.get("text").toString());
} catch (JSONException ignored) {}
return status;
}
/**
* Parse json response for several notes (Misskey)
* @param jsonArray JSONArray
@ -3985,6 +4036,27 @@ public class API {
return context;
}
/**
* Parse json response for list of relationship
* @param jsonArray JSONArray
* @return List<Relationship>
*/
private static List<Attachment> parseAttachmentResponse(JSONArray jsonArray){
List<Attachment> attachments = new ArrayList<>();
try {
int i = 0;
while (i < jsonArray.length() ) {
JSONObject resobj = jsonArray.getJSONObject(i);
Attachment attachment = parseAttachmentResponse(resobj);
attachments.add(attachment);
i++;
}
} catch (JSONException ignored) { }
return attachments;
}
/**
* Parse json response an unique attachment
* @param resobj JSONObject

View File

@ -0,0 +1,67 @@
/* Copyright 2017 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.client.Entities;
import java.util.Date;
import java.util.List;
/**
* Created by Thomas on 18/01/2019.
* Manages scheduled toots
*/
public class Schedule {
private String id;
private Date scheduled_at;
private Status status;
private List<Attachment> attachmentList;
public Schedule(){}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Date getScheduled_at() {
return scheduled_at;
}
public void setScheduled_at(Date scheduled_at) {
this.scheduled_at = scheduled_at;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public List<Attachment> getAttachmentList() {
return attachmentList;
}
public void setAttachmentList(List<Attachment> attachmentList) {
this.attachmentList = attachmentList;
}
}

View File

@ -93,7 +93,7 @@ public class ScheduledTootsListAdapter extends BaseAdapter {
}
@Override
public Object getItem(int position) {
public StoredStatus getItem(int position) {
return storedStatuses.get(position);
}
@ -107,6 +107,7 @@ public class ScheduledTootsListAdapter extends BaseAdapter {
public View getView(final int position, View convertView, ViewGroup parent) {
final StoredStatus storedStatus = storedStatuses.get(position);
final Status status = storedStatuses.get(position).getStatus();
final ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.drawer_scheduled_toot, parent, false);
@ -148,8 +149,6 @@ public class ScheduledTootsListAdapter extends BaseAdapter {
changeDrawableColor(context, R.drawable.ic_mail_outline,R.color.action_light);
}
final Status status = storedStatus.getStatus();
switch (status.getVisibility()) {
case "public":
holder.scheduled_toot_privacy.setImageResource(R.drawable.ic_public);