2018-09-10 19:21:42 +02:00
|
|
|
/* Copyright 2018 Thomas Schneider
|
|
|
|
*
|
2019-05-18 11:10:30 +02:00
|
|
|
* This file is a part of Fedilab
|
2018-09-10 19:21:42 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2019-05-18 11:10:30 +02:00
|
|
|
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
2018-09-10 19:21:42 +02:00
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
2019-05-18 11:10:30 +02:00
|
|
|
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
|
2018-09-10 19:21:42 +02:00
|
|
|
* see <http://www.gnu.org/licenses>. */
|
2019-05-18 11:10:30 +02:00
|
|
|
package app.fedilab.android.activities;
|
2018-09-10 19:21:42 +02:00
|
|
|
|
2018-09-12 09:49:56 +02:00
|
|
|
import android.content.Intent;
|
2018-09-10 19:21:42 +02:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.os.Bundle;
|
2018-09-12 09:49:56 +02:00
|
|
|
import android.os.Handler;
|
2018-09-15 14:14:30 +02:00
|
|
|
import android.view.MenuItem;
|
2018-09-10 19:21:42 +02:00
|
|
|
import android.view.View;
|
2018-09-11 17:50:04 +02:00
|
|
|
import android.widget.Button;
|
2018-09-10 19:21:42 +02:00
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.RelativeLayout;
|
2018-09-12 09:49:56 +02:00
|
|
|
import android.widget.TextView;
|
2018-09-10 19:21:42 +02:00
|
|
|
import android.widget.Toast;
|
2018-11-25 10:45:16 +01:00
|
|
|
|
2019-11-15 16:32:25 +01:00
|
|
|
import androidx.appcompat.widget.Toolbar;
|
2019-11-20 12:11:37 +01:00
|
|
|
import androidx.core.content.ContextCompat;
|
2019-11-15 16:32:25 +01:00
|
|
|
|
2018-09-12 09:49:56 +02:00
|
|
|
import java.util.ArrayList;
|
2018-09-10 19:21:42 +02:00
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
2018-11-25 10:45:16 +01:00
|
|
|
|
2019-11-15 16:32:25 +01:00
|
|
|
import app.fedilab.android.R;
|
|
|
|
import app.fedilab.android.asynctasks.ManageListsAsyncTask;
|
|
|
|
import app.fedilab.android.asynctasks.PostActionAsyncTask;
|
|
|
|
import app.fedilab.android.asynctasks.WhoToFollowAsyncTask;
|
2019-05-18 11:10:30 +02:00
|
|
|
import app.fedilab.android.client.API;
|
|
|
|
import app.fedilab.android.client.APIResponse;
|
|
|
|
import app.fedilab.android.client.Entities.Account;
|
|
|
|
import app.fedilab.android.client.Entities.Error;
|
|
|
|
import app.fedilab.android.client.Entities.TrunkAccount;
|
|
|
|
import app.fedilab.android.drawers.WhoToFollowAccountsAdapter;
|
|
|
|
import app.fedilab.android.helper.Helper;
|
|
|
|
import app.fedilab.android.interfaces.OnListActionInterface;
|
|
|
|
import app.fedilab.android.interfaces.OnPostActionInterface;
|
|
|
|
import app.fedilab.android.interfaces.OnRetrieveWhoToFollowInterface;
|
2019-11-15 16:32:25 +01:00
|
|
|
import es.dmoral.toasty.Toasty;
|
2018-09-10 19:21:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Thomas on 10/09/2018.
|
|
|
|
* Display Who to follow accounts
|
|
|
|
*/
|
|
|
|
|
2018-09-12 09:49:56 +02:00
|
|
|
public class WhoToFollowActivity extends BaseActivity implements OnRetrieveWhoToFollowInterface, OnPostActionInterface, OnListActionInterface {
|
2018-09-10 19:21:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
private String item;
|
2018-09-12 09:49:56 +02:00
|
|
|
private List<String> followedId;
|
|
|
|
private List<String> accountListId;
|
|
|
|
private List<String> toFollowdId;
|
|
|
|
private TextView progess_action;
|
|
|
|
private List<TrunkAccount> trunkAccounts;
|
|
|
|
private RelativeLayout mainLoader;
|
|
|
|
private String listId, listTitle;
|
2018-09-12 10:50:50 +02:00
|
|
|
private RelativeLayout no_action;
|
2018-09-10 19:21:42 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2019-05-18 11:10:30 +02:00
|
|
|
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
|
2018-09-10 19:21:42 +02:00
|
|
|
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
|
2019-09-06 17:55:14 +02:00
|
|
|
switch (theme) {
|
2018-09-10 19:21:42 +02:00
|
|
|
case Helper.THEME_LIGHT:
|
2019-07-20 15:15:47 +02:00
|
|
|
setTheme(R.style.AppTheme_NoActionBar_Fedilab);
|
2018-09-10 19:21:42 +02:00
|
|
|
break;
|
|
|
|
case Helper.THEME_BLACK:
|
|
|
|
setTheme(R.style.AppThemeBlack_NoActionBar);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
setTheme(R.style.AppThemeDark_NoActionBar);
|
|
|
|
}
|
|
|
|
setContentView(R.layout.activity_who_to_follow);
|
2018-09-12 10:50:50 +02:00
|
|
|
no_action = findViewById(R.id.no_action);
|
2018-09-10 19:21:42 +02:00
|
|
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
2019-11-20 12:11:37 +01:00
|
|
|
toolbar.setBackgroundColor(ContextCompat.getColor(WhoToFollowActivity.this, R.color.cyanea_primary));
|
2018-09-12 09:49:56 +02:00
|
|
|
progess_action = findViewById(R.id.progess_action);
|
2018-09-10 19:21:42 +02:00
|
|
|
setSupportActionBar(toolbar);
|
2019-09-06 17:55:14 +02:00
|
|
|
if (getSupportActionBar() != null)
|
2018-09-10 19:21:42 +02:00
|
|
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
|
2018-09-12 09:49:56 +02:00
|
|
|
mainLoader = findViewById(R.id.loader);
|
2018-09-10 19:21:42 +02:00
|
|
|
Bundle b = getIntent().getExtras();
|
2019-09-06 17:55:14 +02:00
|
|
|
if (b != null) {
|
2018-09-10 19:21:42 +02:00
|
|
|
item = b.getString("item");
|
|
|
|
}
|
2018-09-12 09:49:56 +02:00
|
|
|
followedId = new ArrayList<>();
|
2019-09-06 17:55:14 +02:00
|
|
|
String lastDateListNameRefresh = sharedpreferences.getString(Helper.LAST_DATE_LIST_NAME_REFRESH + item, null);
|
2018-09-10 19:21:42 +02:00
|
|
|
Calendar cal = Calendar.getInstance();
|
2019-09-06 17:55:14 +02:00
|
|
|
cal.setTime(new Date());
|
2018-09-10 19:21:42 +02:00
|
|
|
cal.add(Calendar.MINUTE, -5);
|
|
|
|
Date dateAllowed = cal.getTime();
|
2019-09-06 17:55:14 +02:00
|
|
|
if (lastDateListNameRefresh == null || Helper.stringToDate(WhoToFollowActivity.this, lastDateListNameRefresh).before(dateAllowed)) {
|
2021-01-19 17:43:51 +01:00
|
|
|
new WhoToFollowAsyncTask(WhoToFollowActivity.this, item, WhoToFollowActivity.this);
|
2018-09-10 19:35:17 +02:00
|
|
|
mainLoader.setVisibility(View.VISIBLE);
|
2019-09-06 17:55:14 +02:00
|
|
|
} else {
|
2018-09-10 19:21:42 +02:00
|
|
|
String lastList = sharedpreferences.getString(Helper.LAST_LIST_NAME + item, null);
|
2018-09-12 09:49:56 +02:00
|
|
|
List<String> acctString = Helper.restoreArrayFromString(lastList);
|
2019-09-06 17:55:14 +02:00
|
|
|
if (acctString != null) {
|
2018-09-12 09:49:56 +02:00
|
|
|
trunkAccounts = new ArrayList<>();
|
|
|
|
for (String acct : acctString) {
|
|
|
|
TrunkAccount trunkAccount = new TrunkAccount();
|
|
|
|
trunkAccount.setAcct(acct);
|
|
|
|
trunkAccounts.add(trunkAccount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
displayResults();
|
2018-09-10 19:21:42 +02:00
|
|
|
}
|
|
|
|
setTitle(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
private void displayResults() {
|
2018-09-10 19:21:42 +02:00
|
|
|
mainLoader.setVisibility(View.GONE);
|
2018-09-12 09:49:56 +02:00
|
|
|
WhoToFollowAccountsAdapter whoToFollowAccountsAdapter;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (trunkAccounts != null) {
|
|
|
|
if (trunkAccounts.size() > 0) {
|
2018-09-12 10:50:50 +02:00
|
|
|
ListView lv_list = findViewById(R.id.lv_list);
|
2019-08-19 09:18:30 +02:00
|
|
|
whoToFollowAccountsAdapter = new WhoToFollowAccountsAdapter(trunkAccounts);
|
2018-09-12 10:50:50 +02:00
|
|
|
lv_list.setAdapter(whoToFollowAccountsAdapter);
|
2019-09-06 17:55:14 +02:00
|
|
|
} else {
|
2018-09-12 10:50:50 +02:00
|
|
|
no_action.setVisibility(View.VISIBLE);
|
|
|
|
return;
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
} else {
|
2020-04-08 12:42:15 +02:00
|
|
|
Toasty.error(WhoToFollowActivity.this, getString(R.string.toast_error), Toast.LENGTH_SHORT).show();
|
2018-09-12 09:49:56 +02:00
|
|
|
return;
|
2018-09-10 19:21:42 +02:00
|
|
|
}
|
2018-09-11 17:50:04 +02:00
|
|
|
|
|
|
|
Button follow_accounts_select = findViewById(R.id.follow_accounts_select);
|
2020-06-17 18:20:35 +02:00
|
|
|
follow_accounts_select.setOnClickListener(v -> {
|
|
|
|
if (follow_accounts_select.getText().equals(getString(R.string.select_all))) {
|
|
|
|
follow_accounts_select.setText(R.string.unselect_all);
|
|
|
|
} else {
|
|
|
|
follow_accounts_select.setText(R.string.select_all);
|
2018-09-12 09:49:56 +02:00
|
|
|
}
|
2020-06-17 18:20:35 +02:00
|
|
|
for (TrunkAccount trunkAccount : trunkAccounts) {
|
|
|
|
trunkAccount.setChecked(false);
|
|
|
|
}
|
|
|
|
whoToFollowAccountsAdapter.notifyDataSetChanged();
|
2018-09-12 09:49:56 +02:00
|
|
|
});
|
|
|
|
Button follow_accounts = findViewById(R.id.follow_accounts);
|
2020-06-17 18:20:35 +02:00
|
|
|
follow_accounts.setOnClickListener(v -> {
|
|
|
|
follow_accounts.setEnabled(false);
|
|
|
|
follow_accounts_select.setEnabled(false);
|
|
|
|
progess_action.setVisibility(View.VISIBLE);
|
|
|
|
toFollowdId = new ArrayList<>();
|
|
|
|
for (TrunkAccount trunkAccount : trunkAccounts) {
|
|
|
|
if (trunkAccount.isChecked()) {
|
|
|
|
toFollowdId.add(trunkAccount.getAcct());
|
2018-09-12 09:49:56 +02:00
|
|
|
}
|
2020-06-17 18:20:35 +02:00
|
|
|
}
|
|
|
|
if (toFollowdId.size() > 0) {
|
|
|
|
Account account = new Account();
|
|
|
|
String[] val = toFollowdId.get(0).split("@");
|
|
|
|
progess_action.setText(getString(R.string.follow_trunk, toFollowdId.get(0)));
|
|
|
|
if (val.length > 1) {
|
|
|
|
account.setAcct(val[0]);
|
|
|
|
account.setInstance(val[1]);
|
2021-01-19 17:43:51 +01:00
|
|
|
new PostActionAsyncTask(WhoToFollowActivity.this, null, account, API.StatusAction.FOLLOW, WhoToFollowActivity.this);
|
2020-06-17 18:20:35 +02:00
|
|
|
} else {
|
|
|
|
Toasty.error(WhoToFollowActivity.this, getString(R.string.toast_impossible_to_follow), Toast.LENGTH_LONG).show();
|
|
|
|
follow_accounts.setEnabled(true);
|
|
|
|
follow_accounts_select.setEnabled(true);
|
|
|
|
progess_action.setVisibility(View.GONE);
|
2018-09-12 09:49:56 +02:00
|
|
|
}
|
2018-09-11 17:50:04 +02:00
|
|
|
}
|
|
|
|
});
|
2018-09-10 19:21:42 +02:00
|
|
|
}
|
2018-09-12 09:49:56 +02:00
|
|
|
|
2018-09-15 14:14:30 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
2020-06-17 18:20:35 +02:00
|
|
|
if (item.getItemId() == android.R.id.home) {
|
|
|
|
finish();
|
|
|
|
return true;
|
2018-09-15 14:14:30 +02:00
|
|
|
}
|
2020-06-17 18:20:35 +02:00
|
|
|
return super.onOptionsItemSelected(item);
|
2018-09-15 14:14:30 +02:00
|
|
|
}
|
|
|
|
|
2018-09-12 09:49:56 +02:00
|
|
|
@Override
|
|
|
|
public void onRetrieveWhoToFollowList(List<String> list) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRetrieveWhoToFollowAccount(List<TrunkAccount> trunkAccounts) {
|
2019-09-06 17:55:14 +02:00
|
|
|
if (trunkAccounts != null) {
|
2019-05-18 11:10:30 +02:00
|
|
|
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
|
2018-09-12 09:49:56 +02:00
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
|
|
|
editor.putString(Helper.LAST_DATE_LIST_NAME_REFRESH + item, Helper.dateToString(new Date()));
|
|
|
|
List<String> accounts = new ArrayList<>();
|
2019-09-06 17:55:14 +02:00
|
|
|
for (TrunkAccount trunkAccount : trunkAccounts)
|
2018-09-12 09:49:56 +02:00
|
|
|
accounts.add(trunkAccount.getAcct());
|
|
|
|
editor.putString(Helper.LAST_LIST_NAME + item, Helper.arrayToStringStorage(accounts));
|
|
|
|
editor.apply();
|
|
|
|
this.trunkAccounts = trunkAccounts;
|
|
|
|
}
|
|
|
|
displayResults();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPostAction(int statusCode, API.StatusAction statusAction, String userId, Error error) {
|
|
|
|
followedId.add(userId);
|
2019-09-06 17:55:14 +02:00
|
|
|
if (followedId != null && followedId.size() >= toFollowdId.size()) {
|
2018-09-12 09:49:56 +02:00
|
|
|
progess_action.setText(getString(R.string.create_list_trunk, item));
|
2021-01-19 17:43:51 +01:00
|
|
|
new ManageListsAsyncTask(WhoToFollowActivity.this, ManageListsAsyncTask.action.CREATE_LIST, null, null, item, WhoToFollowActivity.this);
|
2019-09-06 17:55:14 +02:00
|
|
|
} else {
|
2018-09-12 09:49:56 +02:00
|
|
|
Account account = new Account();
|
2020-06-17 18:20:35 +02:00
|
|
|
assert followedId != null;
|
2018-09-12 09:49:56 +02:00
|
|
|
String[] val = toFollowdId.get(followedId.size()).split("@");
|
|
|
|
progess_action.setText(getString(R.string.follow_trunk, toFollowdId.get(followedId.size())));
|
2019-09-06 17:55:14 +02:00
|
|
|
if (val.length > 1) {
|
2018-09-12 09:49:56 +02:00
|
|
|
account.setAcct(val[0]);
|
|
|
|
account.setInstance(val[1]);
|
2021-01-19 17:43:51 +01:00
|
|
|
new PostActionAsyncTask(WhoToFollowActivity.this, null, account, API.StatusAction.FOLLOW, WhoToFollowActivity.this);
|
2018-09-12 09:49:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActionDone(ManageListsAsyncTask.action actionType, APIResponse apiResponse, int statusCode) {
|
|
|
|
|
2019-05-18 11:10:30 +02:00
|
|
|
List<app.fedilab.android.client.Entities.List> lists = apiResponse.getLists();
|
2019-09-06 17:55:14 +02:00
|
|
|
if (lists != null && lists.size() > 0 && actionType == ManageListsAsyncTask.action.CREATE_LIST) {
|
2018-09-12 09:49:56 +02:00
|
|
|
progess_action.setText(R.string.add_account_list_trunk);
|
|
|
|
listId = lists.get(0).getId();
|
|
|
|
listTitle = lists.get(0).getTitle();
|
2021-01-19 17:43:51 +01:00
|
|
|
new ManageListsAsyncTask(WhoToFollowActivity.this, ManageListsAsyncTask.action.ADD_USERS, new String[]{followedId.get(0)}, lists.get(0).getId(), null, WhoToFollowActivity.this);
|
2018-09-12 09:49:56 +02:00
|
|
|
accountListId = new ArrayList<>();
|
2019-09-06 17:55:14 +02:00
|
|
|
} else if (accountListId != null) {
|
2018-09-12 09:49:56 +02:00
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
if (accountListId.size() >= followedId.size() - 1) {
|
2018-09-12 09:49:56 +02:00
|
|
|
progess_action.setText(R.string.account_added_list_trunk);
|
|
|
|
Handler handler = new Handler();
|
2020-06-17 18:20:35 +02:00
|
|
|
handler.postDelayed(() -> {
|
|
|
|
Intent intent = new Intent(WhoToFollowActivity.this, ListActivity.class);
|
|
|
|
Bundle b = new Bundle();
|
|
|
|
b.putString("id", listId);
|
|
|
|
b.putString("title", listTitle);
|
|
|
|
intent.putExtras(b);
|
|
|
|
startActivity(intent);
|
|
|
|
finish();
|
2018-09-12 09:49:56 +02:00
|
|
|
}, 1000);
|
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
} else {
|
2018-09-12 09:49:56 +02:00
|
|
|
accountListId.add(followedId.get(accountListId.size()));
|
|
|
|
progess_action.setText(R.string.adding_account_list_trunk);
|
|
|
|
String userIdToAdd = followedId.get(accountListId.size());
|
2021-01-19 17:43:51 +01:00
|
|
|
new ManageListsAsyncTask(WhoToFollowActivity.this, ManageListsAsyncTask.action.ADD_USERS, new String[]{userIdToAdd}, listId, null, WhoToFollowActivity.this);
|
2018-09-12 09:49:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-09-10 19:21:42 +02:00
|
|
|
}
|