summaryrefslogtreecommitdiffstats
path: root/app/src/main/java/app/fedilab/android/mastodon/viewmodel/mastodon/FiltersVM.java
blob: a063616f8a226a30125d883a57696e6275c867dc (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package app.fedilab.android.mastodon.viewmodel.mastodon;
/* Copyright 2022 Thomas Schneider
 *
 * This file is a part of Fedilab
 *
 * 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.
 *
 * Fedilab 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 Fedilab; if not,
 * see <http://www.gnu.org/licenses>. */


import android.app.Application;
import android.os.Handler;
import android.os.Looper;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import java.net.IDN;
import java.util.List;
import java.util.concurrent.TimeUnit;

import app.fedilab.android.BaseMainActivity;
import app.fedilab.android.mastodon.client.endpoints.MastodonFiltersService;
import app.fedilab.android.mastodon.client.entities.api.Filter;
import app.fedilab.android.mastodon.helper.Helper;
import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FiltersVM extends AndroidViewModel {


    final OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .readTimeout(60, TimeUnit.SECONDS)
            .connectTimeout(60, TimeUnit.SECONDS)
            .callTimeout(60, TimeUnit.SECONDS)
            .proxy(Helper.getProxy(getApplication().getApplicationContext()))
            .build();


    private MutableLiveData<Filter> filterMutableLiveData;
    private MutableLiveData<List<Filter>> filterListMutableLiveData;

    public FiltersVM(@NonNull Application application) {
        super(application);
    }

    private MastodonFiltersService initV2(String instance) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://" + (instance != null ? IDN.toASCII(instance, IDN.ALLOW_UNASSIGNED) : null) + "/api/v2/")
                //    .addConverterFactory(GsonConverterFactory.create(Helper.getDateBuilder()))
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .build();
        return retrofit.create(MastodonFiltersService.class);
    }


    /**
     * View all filters created by the user
     *
     * @return {@link LiveData} containing a {@link List} of {@link Filter}s
     */
    public LiveData<List<Filter>> getFilters(@NonNull String instance, String token) {
        filterListMutableLiveData = new MutableLiveData<>();
        MastodonFiltersService mastodonFiltersService = initV2(instance);
        new Thread(() -> {
            List<Filter> filterList = null;
            Call<List<Filter>> getFiltersCall = mastodonFiltersService.getFilters(token);
            if (getFiltersCall != null) {
                try {
                    Response<List<Filter>> getFiltersResponse = getFiltersCall.execute();
                    if (getFiltersResponse.isSuccessful()) {
                        BaseMainActivity.filterFetched = true;
                        filterList = getFiltersResponse.body();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            Handler mainHandler = new Handler(Looper.getMainLooper());
            List<Filter> finalFilterList = filterList;
            Runnable myRunnable = () -> filterListMutableLiveData.setValue(finalFilterList);
            mainHandler.post(myRunnable);
        }).start();
        return filterListMutableLiveData;
    }

    /**
     * View a single filter
     *
     * @param id the id of the filter
     * @return {@link LiveData} containing a {@link Filter}
     */
    public LiveData<Filter> getFilter(@NonNull String instance, String token, @NonNull String id) {
        filterMutableLiveData = new MutableLiveData<>();
        MastodonFiltersService mastodonFiltersService = initV2(instance);
        new Thread(() -> {
            Filter filter = null;
            Call<Filter> getFilterCall = mastodonFiltersService.getFilter(token, id);
            if (getFilterCall != null) {
                try {
                    Response<Filter> getFiltersResponse = getFilterCall.execute();
                    if (getFiltersResponse.isSuccessful()) {
                        filter = getFiltersResponse.body();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            Handler mainHandler = new Handler(Looper.getMainLooper());
            Filter finalFilter = filter;
            Runnable myRunnable = () -> filterMutableLiveData.setValue(finalFilter);
            mainHandler.post(myRunnable);
        }).start();
        return filterMutableLiveData;
    }

    /**
     * Create a filter
     *
     * @return {@link LiveData} containing a {@link Filter}
     */
    public LiveData<Filter> addFilter(@NonNull String instance, String token, @NonNull Filter.FilterParams filterParams) {
        filterMutableLiveData = new MutableLiveData<>();
        MastodonFiltersService mastodonFiltersService = initV2(instance);
        new Thread(() -> {
            Filter filter = null;
            Call<Filter> addFilterCall = mastodonFiltersService.addFilter(token, filterParams);
            if (addFilterCall != null) {
                try {
                    Response<Filter> addFiltersResponse = addFilterCall.execute();
                    if (addFiltersResponse.isSuccessful()) {
                        filter = addFiltersResponse.body();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            Handler mainHandler = new Handler(Looper.getMainLooper());
            Filter finalFilter = filter;
            Runnable myRunnable = () -> filterMutableLiveData.setValue(finalFilter);
            mainHandler.post(myRunnable);
        }).start();
        return filterMutableLiveData;
    }

    /**
     * Update a filter
     *
     * @return {@link LiveData} containing a {@link Filter}
     */
    public LiveData<Filter> editFilter(@NonNull String instance, String token, @NonNull Filter.FilterParams filterParams) {
        filterMutableLiveData = new MutableLiveData<>();
        MastodonFiltersService mastodonFiltersService = initV2(instance);
        new Thread(() -> {
            Filter filter = null;
           /* List<String> keywordsId = new ArrayList<>();
            List<String> keywords = new ArrayList<>();
            List<Boolean> whole_words = new ArrayList<>();
            for(Filter.KeywordsAttributes attributes: keywordsAttributes) {
                keywordsId.add(attributes.id);
                keywords.add(attributes.keyword);
                whole_words.add(attributes.whole_word);
            }*/

            Call<Filter> editFilterCall = mastodonFiltersService.editFilter(token, filterParams.id, filterParams);
            if (editFilterCall != null) {
                try {
                    Response<Filter> editFiltersResponse = editFilterCall.execute();
                    if (editFiltersResponse.isSuccessful()) {
                        filter = editFiltersResponse.body();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            Handler mainHandler = new Handler(Looper.getMainLooper());
            Filter finalFilter = filter;
            Runnable myRunnable = () -> filterMutableLiveData.setValue(finalFilter);
            mainHandler.post(myRunnable);
        }).start();
        return filterMutableLiveData;
    }

    /**
     * Remove a filter
     *
     * @param id ID of the filter
     */
    public void removeFilter(@NonNull String instance, String token, @NonNull String id) {
        MastodonFiltersService mastodonAccountsService = initV2(instance);
        new Thread(() -> {
            Call<Void> removeFilterCall = mastodonAccountsService.removeFilter(token, id);
            if (removeFilterCall != null) {
                try {
                    removeFilterCall.execute();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    /**
     * Remove a filter
     *
     * @param id ID of the filter
     */
    public void removeKeyword(@NonNull String instance, String token, @NonNull String id) {
        MastodonFiltersService mastodonAccountsService = initV2(instance);
        new Thread(() -> {
            Call<Void> removeFilterCall = mastodonAccountsService.removeKeywordFilter(token, id);
            if (removeFilterCall != null) {
                try {
                    removeFilterCall.execute();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

}