Prepares asynctask for lists
This commit is contained in:
parent
541177a438
commit
b6d50ee87c
|
@ -0,0 +1,91 @@
|
|||
/* 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.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.interfaces.OnListActionInterface;
|
||||
|
||||
|
||||
/**
|
||||
* Created by Thomas on 13/12/2017.
|
||||
* Async works to manage Lists
|
||||
*/
|
||||
|
||||
public class ManageListsAsyncTask extends AsyncTask<Void, Void, Void> {
|
||||
|
||||
public enum action{
|
||||
GET_LIST,
|
||||
GET_LIST_ACCOUNT,
|
||||
CREATE_LIST,
|
||||
DELETE_LIST,
|
||||
UPDATE_LIST,
|
||||
ADD_USERS,
|
||||
DELETE_USERS
|
||||
}
|
||||
|
||||
private OnListActionInterface listener;
|
||||
private APIResponse apiResponse;
|
||||
private int statusCode;
|
||||
private String targetedId;
|
||||
private String listId;
|
||||
private String title;
|
||||
private String[] accountsId;
|
||||
private action apiAction;
|
||||
private WeakReference<Context> contextReference;
|
||||
|
||||
|
||||
public ManageListsAsyncTask(Context context, action apiAction, String[] accountsId, String targetedId, String listId, String title, OnListActionInterface onListActionInterface){
|
||||
contextReference = new WeakReference<>(context);
|
||||
this.listener = onListActionInterface;
|
||||
this.listId = listId;
|
||||
this.title = title;
|
||||
this.accountsId = accountsId;
|
||||
this.apiAction = apiAction;
|
||||
this.targetedId = targetedId;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(Void... params) {
|
||||
|
||||
if(apiAction == action.GET_LIST){
|
||||
apiResponse = new API(contextReference.get()).getLists();
|
||||
}else if(apiAction == action.GET_LIST_ACCOUNT){
|
||||
apiResponse = new API(contextReference.get()).getLists(this.targetedId);
|
||||
}else if( apiAction == action.CREATE_LIST){
|
||||
apiResponse = new API(contextReference.get()).createList(this.title);
|
||||
}else if(apiAction == action.DELETE_LIST){
|
||||
statusCode = new API(contextReference.get()).deleteList(this.listId);
|
||||
}else if(apiAction == action.UPDATE_LIST){
|
||||
apiResponse = new API(contextReference.get()).updateList(this.listId, this.title);
|
||||
}else if(apiAction == action.ADD_USERS){
|
||||
apiResponse = new API(contextReference.get()).addAccountToList(this.listId, this.accountsId);
|
||||
}else if(apiAction == action.DELETE_USERS){
|
||||
statusCode = new API(contextReference.get()).deleteAccountFromList(this.listId, this.accountsId);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void result) {
|
||||
listener.onActionDone(apiResponse, statusCode);
|
||||
}
|
||||
|
||||
}
|
|
@ -1240,10 +1240,16 @@ public class API {
|
|||
*/
|
||||
//TODO: it is unclear what is returned here
|
||||
//TODO: improves doc https://github.com/tootsuite/documentation/blob/4bb149c73f40fa58fd7265a336703dd2d83efb1c/Using-the-API/API.md#addingremoving-accounts-tofrom-a-list
|
||||
public APIResponse addAccountToList(String id, String account_ids){
|
||||
public APIResponse addAccountToList(String id, String[] account_ids){
|
||||
|
||||
HashMap<String, String> params = new HashMap<>();
|
||||
params.put("account_ids",account_ids);
|
||||
StringBuilder parameters = new StringBuilder();
|
||||
for(String val: account_ids)
|
||||
parameters.append("account_ids[]=").append(val).append("&");
|
||||
if( parameters.length() > 0) {
|
||||
parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(16));
|
||||
params.put("account_ids[]", parameters.toString());
|
||||
}
|
||||
List<fr.gouv.etalab.mastodon.client.Entities.List> lists = new ArrayList<>();
|
||||
fr.gouv.etalab.mastodon.client.Entities.List list;
|
||||
try {
|
||||
|
@ -1262,10 +1268,18 @@ public class API {
|
|||
* @param id String, the id of the list
|
||||
* @return APIResponse
|
||||
*/
|
||||
public int deleteAccountFromList(String id, String account_ids){
|
||||
public int deleteAccountFromList(String id, String[] account_ids){
|
||||
try {
|
||||
HttpsConnection httpsConnection = new HttpsConnection();
|
||||
httpsConnection.delete(getAbsoluteUrl(String.format("/lists/%s/accounts", id)), 60, null, prefKeyOauthTokenT);
|
||||
StringBuilder parameters = new StringBuilder();
|
||||
HashMap<String, String> params = new HashMap<>();
|
||||
for(String val: account_ids)
|
||||
parameters.append("account_ids[]=").append(val).append("&");
|
||||
if( parameters.length() > 0) {
|
||||
parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(16));
|
||||
params.put("account_ids[]", parameters.toString());
|
||||
}
|
||||
httpsConnection.delete(getAbsoluteUrl(String.format("/lists/%s/accounts", id)), 60, params, prefKeyOauthTokenT);
|
||||
actionCode = httpsConnection.getActionCode();
|
||||
} catch (HttpsConnection.HttpsConnectionException e) {
|
||||
setError(e.getStatusCode(), e);
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
/* 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.interfaces;
|
||||
|
||||
|
||||
import fr.gouv.etalab.mastodon.client.APIResponse;
|
||||
|
||||
/**
|
||||
* Created by Thomas on 13/12/2017.
|
||||
* Interface when actions have been done with lists
|
||||
*/
|
||||
public interface OnListActionInterface {
|
||||
void onActionDone(APIResponse apiResponse, int statusCode);
|
||||
}
|
Loading…
Reference in New Issue