Implements async task
This commit is contained in:
parent
13e1a97829
commit
288d737393
|
@ -0,0 +1,54 @@
|
|||
/* 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.util.List;
|
||||
import fr.gouv.etalab.mastodon.client.API;
|
||||
import fr.gouv.etalab.mastodon.client.APIResponse;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
||||
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveManyRelationshipsInterface;
|
||||
|
||||
/**
|
||||
* Created by Thomas on 16/09/2017.
|
||||
* Retrieves many relationship between the authenticated user and other accounts
|
||||
*/
|
||||
|
||||
public class RetrieveManyRelationshipsAsyncTask extends AsyncTask<Void, Void, Void> {
|
||||
|
||||
private Context context;
|
||||
private List<Account> accounts;
|
||||
private OnRetrieveManyRelationshipsInterface listener;
|
||||
private APIResponse apiResponse;
|
||||
|
||||
public RetrieveManyRelationshipsAsyncTask(Context context, List<Account> accounts, OnRetrieveManyRelationshipsInterface onRetrieveManyRelationshipsInterface){
|
||||
this.context = context;
|
||||
this.listener = onRetrieveManyRelationshipsInterface;
|
||||
this.accounts = accounts;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(Void... params) {
|
||||
apiResponse = new API(context).getRelationship(accounts);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void result) {
|
||||
listener.onRetrieveRelationship(apiResponse);
|
||||
}
|
||||
|
||||
}
|
|
@ -69,6 +69,7 @@ public class API {
|
|||
private Attachment attachment;
|
||||
private List<Account> accounts;
|
||||
private List<Status> statuses;
|
||||
private List<Relationship> relationships;
|
||||
private List<Notification> notifications;
|
||||
private int tootPerPage, accountPerPage, notificationPerPage;
|
||||
private int actionCode;
|
||||
|
@ -270,6 +271,42 @@ public class API {
|
|||
return relationship;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a relationship between the authenticated account and an account
|
||||
* @param accounts ArrayList<Account> accounts fetched
|
||||
* @return Relationship entity
|
||||
*/
|
||||
public APIResponse getRelationship(List<Account> accounts) {
|
||||
|
||||
relationship = new Relationship();
|
||||
RequestParams params = new RequestParams();
|
||||
if( accounts != null && accounts.size() > 0 ) {
|
||||
for(Account account: accounts) {
|
||||
params.add("id[]", account.getId());
|
||||
}
|
||||
}
|
||||
get("/accounts/relationships", params, new JsonHttpResponseHandler() {
|
||||
@Override
|
||||
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
||||
Relationship relationship = parseRelationshipResponse(response);
|
||||
relationships.add(relationship);
|
||||
apiResponse.setSince_id(findSinceId(headers));
|
||||
apiResponse.setMax_id(findMaxId(headers));
|
||||
}
|
||||
@Override
|
||||
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
|
||||
apiResponse.setSince_id(findSinceId(headers));
|
||||
apiResponse.setMax_id(findMaxId(headers));
|
||||
relationships = parseRelationshipResponse(response);
|
||||
}
|
||||
@Override
|
||||
public void onFailure(int statusCode, Header[] headers, Throwable error, JSONObject response){
|
||||
setError(statusCode, error);
|
||||
}
|
||||
});
|
||||
return apiResponse;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves status for the account *synchronously*
|
||||
*
|
||||
|
@ -1399,29 +1436,47 @@ public class API {
|
|||
|
||||
|
||||
/**
|
||||
* Parse json response for list of accounts which could contain the developer name
|
||||
* @param jsonArray JSONArray
|
||||
* @return List<Account>
|
||||
* Parse json response an unique relationship
|
||||
* @param resobj JSONObject
|
||||
* @return Relationship
|
||||
*/
|
||||
private List<Account> parseDeveloperResponse(JSONArray jsonArray){
|
||||
private Relationship parseRelationshipResponse(JSONObject resobj){
|
||||
|
||||
List<Account> accounts = new ArrayList<>();
|
||||
Relationship relationship = new Relationship();
|
||||
try {
|
||||
int i = 0;
|
||||
Account account = null;
|
||||
while (i < jsonArray.length() ) {
|
||||
JSONObject resobj = jsonArray.getJSONObject(i);
|
||||
account = parseAccountResponse(context, resobj);
|
||||
if( account.getAcct().contains(Helper.DEVELOPER_INSTANCE))
|
||||
accounts.add(account);
|
||||
i++;
|
||||
}
|
||||
if( accounts.size() == 0)
|
||||
accounts.add(account);
|
||||
relationship.setId(resobj.get("id").toString());
|
||||
relationship.setFollowing(Boolean.valueOf(resobj.get("following").toString()));
|
||||
relationship.setFollowed_by(Boolean.valueOf(resobj.get("followed_by").toString()));
|
||||
relationship.setBlocking(Boolean.valueOf(resobj.get("blocking").toString()));
|
||||
relationship.setMuting(Boolean.valueOf(resobj.get("muting").toString()));
|
||||
relationship.setRequested(Boolean.valueOf(resobj.get("requested").toString()));
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return accounts;
|
||||
return relationship;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Parse json response for list of relationship
|
||||
* @param jsonArray JSONArray
|
||||
* @return List<Relationship>
|
||||
*/
|
||||
private List<Relationship> parseRelationshipResponse(JSONArray jsonArray){
|
||||
|
||||
List<Relationship> relationships = new ArrayList<>();
|
||||
try {
|
||||
int i = 0;
|
||||
while (i < jsonArray.length() ) {
|
||||
JSONObject resobj = jsonArray.getJSONObject(i);
|
||||
Relationship relationship = parseRelationshipResponse(resobj);
|
||||
relationships.add(relationship);
|
||||
i++;
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return relationships;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1469,26 +1524,7 @@ public class API {
|
|||
return attachment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse json response an unique relationship
|
||||
* @param resobj JSONObject
|
||||
* @return Relationship
|
||||
*/
|
||||
private Relationship parseRelationshipResponse(JSONObject resobj){
|
||||
|
||||
Relationship relationship = new Relationship();
|
||||
try {
|
||||
relationship.setId(resobj.get("id").toString());
|
||||
relationship.setFollowing(Boolean.valueOf(resobj.get("following").toString()));
|
||||
relationship.setFollowed_by(Boolean.valueOf(resobj.get("followed_by").toString()));
|
||||
relationship.setBlocking(Boolean.valueOf(resobj.get("blocking").toString()));
|
||||
relationship.setMuting(Boolean.valueOf(resobj.get("muting").toString()));
|
||||
relationship.setRequested(Boolean.valueOf(resobj.get("requested").toString()));
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return relationship;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse json response an unique notification
|
||||
|
|
|
@ -29,6 +29,7 @@ public class APIResponse {
|
|||
private List<Status> statuses = null;
|
||||
private List<Context> contexts = null;
|
||||
private List<Notification> notifications = null;
|
||||
private List<Relationship> relationships = null;
|
||||
private fr.gouv.etalab.mastodon.client.Entities.Error error = null;
|
||||
private String since_id, max_id;
|
||||
private Instance instance;
|
||||
|
@ -96,4 +97,12 @@ public class APIResponse {
|
|||
public void setInstance(Instance instance) {
|
||||
this.instance = instance;
|
||||
}
|
||||
|
||||
public List<Relationship> getRelationships() {
|
||||
return relationships;
|
||||
}
|
||||
|
||||
public void setRelationships(List<Relationship> relationships) {
|
||||
this.relationships = relationships;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,12 +45,18 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
|
||||
import fr.gouv.etalab.mastodon.asynctasks.RetrieveAccountsAsyncTask;
|
||||
import fr.gouv.etalab.mastodon.asynctasks.RetrieveManyRelationshipsAsyncTask;
|
||||
import fr.gouv.etalab.mastodon.asynctasks.RetrieveRelationshipAsyncTask;
|
||||
import fr.gouv.etalab.mastodon.client.API;
|
||||
import fr.gouv.etalab.mastodon.client.APIResponse;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Error;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Relationship;
|
||||
import fr.gouv.etalab.mastodon.client.PatchBaseImageDownloader;
|
||||
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||
import fr.gouv.etalab.mastodon.interfaces.OnPostActionInterface;
|
||||
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveManyRelationshipsInterface;
|
||||
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveRelationshipInterface;
|
||||
import mastodon.etalab.gouv.fr.mastodon.R;
|
||||
import fr.gouv.etalab.mastodon.activities.ShowAccountActivity;
|
||||
import fr.gouv.etalab.mastodon.asynctasks.PostActionAsyncTask;
|
||||
|
@ -60,7 +66,7 @@ import fr.gouv.etalab.mastodon.asynctasks.PostActionAsyncTask;
|
|||
* Created by Thomas on 27/04/2017.
|
||||
* Adapter for accounts
|
||||
*/
|
||||
public class AccountsListAdapter extends BaseAdapter implements OnPostActionInterface {
|
||||
public class AccountsListAdapter extends BaseAdapter implements OnPostActionInterface, OnRetrieveRelationshipInterface, OnRetrieveManyRelationshipsInterface {
|
||||
|
||||
private List<Account> accounts;
|
||||
private LayoutInflater layoutInflater;
|
||||
|
@ -78,6 +84,17 @@ public class AccountsListAdapter extends BaseAdapter implements OnPostActionInte
|
|||
this.targetedId = targetedId;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onRetrieveRelationship(APIResponse apiResponse) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRetrieveRelationship(Relationship relationship, Error error) {
|
||||
|
||||
}
|
||||
|
||||
public enum action{
|
||||
FOLLOW,
|
||||
UNFOLLOW,
|
||||
|
@ -146,7 +163,9 @@ public class AccountsListAdapter extends BaseAdapter implements OnPostActionInte
|
|||
account.setFollowType(Account.followAction.BLOCK);
|
||||
else if( action == RetrieveAccountsAsyncTask.Type.MUTED)
|
||||
account.setFollowType(Account.followAction.MUTE);
|
||||
|
||||
else {
|
||||
new RetrieveManyRelationshipsAsyncTask(context, accounts,AccountsListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
if (account.getFollowType() == Account.followAction.NOTHING){
|
||||
holder.account_follow.setVisibility(View.GONE);
|
||||
holder.account_follow_request.setVisibility(View.GONE);
|
||||
|
|
|
@ -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 16/09/2017.
|
||||
* Interface when relationships have been retrieved for several accounts
|
||||
*/
|
||||
public interface OnRetrieveManyRelationshipsInterface {
|
||||
void onRetrieveRelationship(APIResponse apiResponse);
|
||||
}
|
Loading…
Reference in New Issue