summaryrefslogtreecommitdiffstats
path: root/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/ManageListsAsyncTask.java
blob: 11d17e6dd6ed9f0b62432205b76dd600578505b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* 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_TIMELINE,
        GET_LIST_ACCOUNT,
        CREATE_LIST,
        DELETE_LIST,
        UPDATE_LIST,
        ADD_USERS,
        DELETE_USERS,
        SEARCH_USER
    }

    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;
    private String max_id, since_id;
    private int limit;
    private String search;

    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;
    }

    public ManageListsAsyncTask(Context context, String listId, String max_id, String since_id, OnListActionInterface onListActionInterface){
        contextReference = new WeakReference<>(context);
        this.listener = onListActionInterface;
        this.listId = listId;
        this.max_id = max_id;
        this.since_id = since_id;
        this.limit = 40;
        this.apiAction = action.GET_LIST_TIMELINE;
    }

    public ManageListsAsyncTask(Context context, String search, OnListActionInterface onListActionInterface){
        contextReference = new WeakReference<>(context);
        this.listener = onListActionInterface;
        this.search = search;
        this.apiAction = action.SEARCH_USER;
    }

    @Override
    protected Void doInBackground(Void... params) {
        if (apiAction == action.GET_LIST) {
            apiResponse = new API(contextReference.get()).getLists();
        }else if(apiAction == action.GET_LIST_TIMELINE){
            apiResponse = new API(contextReference.get()).getListTimeline(this.listId, this.max_id, this.since_id, this.limit);
        }else if(apiAction == action.GET_LIST_ACCOUNT){
            apiResponse = new API(contextReference.get()).getAccountsInList(this.listId,0);
        }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);
        }else if( apiAction == action.SEARCH_USER){
            apiResponse = new API(contextReference.get()).searchAccounts(this.search, 20, true);
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        listener.onActionDone(this.apiAction, apiResponse, statusCode);
    }

}