bug fix, code cleanup

This commit is contained in:
nuclearfog 2022-11-18 19:29:57 +01:00
parent fb8fc73396
commit de35575423
No known key found for this signature in database
GPG Key ID: 03488A185C476379
18 changed files with 89 additions and 63 deletions

View File

@ -118,22 +118,27 @@ public class AccountAdapter extends Adapter<AccountHolder> {
/**
* sets login data
*
* @param data list with login items
* @param newData list with login items
*/
@MainThread
public void setData(List<Account> data) {
this.data.clear();
this.data.addAll(data);
public void setData(List<Account> newData) {
data.clear();
data.addAll(newData);
notifyDataSetChanged();
}
/**
* clear adapter data
* remove single item with specific ID
*
* @param id Id of the element to remove
*/
@MainThread
public void clear() {
data.clear();
notifyDataSetChanged();
public void removeItem(long id) {
for (int i = data.size() - 1; i >= 0; i--) {
if (data.get(i).getId() == id) {
data.remove(i);
break;
}
}
}
/**

View File

@ -14,33 +14,47 @@ import java.util.List;
*
* @author nuclearfog
*/
public class AccountLoader extends AsyncTask<Account, Void, List<Account>> {
public class AccountLoader extends AsyncTask<Long, Void, List<Account>> {
/**
* load all saved logins
*/
public static final int MODE_LOAD = 1;
/**
* delete specific login
*/
public static final int MODE_DELETE = 2;
private AccountDatabase accountDatabase;
private WeakReference<AccountFragment> weakRef;
private int mode;
private long deleteId;
public AccountLoader(AccountFragment fragment) {
/**
* @param mode action to take {@link #MODE_LOAD,#MODE_DELETE}
*/
public AccountLoader(AccountFragment fragment, int mode) {
super();
weakRef = new WeakReference<>(fragment);
accountDatabase = new AccountDatabase(fragment.requireContext());
this.mode = mode;
}
@Override
protected List<Account> doInBackground(Account... param) {
List<Account> result = null;
try {
// remove account if parameter is set
if (param.length > 0 && param[0] != null) {
accountDatabase.removeLogin(param[0].getId());
}
// get registered users
result = accountDatabase.getLogins();
} catch (Exception err) {
err.printStackTrace();
protected List<Account> doInBackground(Long... param) {
// get all logins
if (mode == MODE_LOAD) {
return accountDatabase.getLogins();
}
return result;
// delete login
else if (mode == MODE_DELETE) {
accountDatabase.removeLogin(param[0]);
deleteId = param[0];
}
return null;
}
@ -48,10 +62,10 @@ public class AccountLoader extends AsyncTask<Account, Void, List<Account>> {
protected void onPostExecute(List<Account> accounts) {
AccountFragment fragment = weakRef.get();
if (fragment != null) {
if (accounts != null) {
if (mode == MODE_LOAD) {
fragment.onSuccess(accounts);
} else {
fragment.onError();
} else if (mode == MODE_DELETE) {
fragment.onDelete(deleteId);
}
}
}

View File

@ -59,7 +59,7 @@ public class FilterLoader extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String[] names) {
protected Void doInBackground(String... names) {
try {
switch (mode) {
case REFRESH:

View File

@ -47,7 +47,7 @@ public class ImageLoader extends AsyncTask<Uri, Uri, Boolean> {
@Override
protected Boolean doInBackground(Uri[] links) {
protected Boolean doInBackground(Uri... links) {
try {
// download imaged to a local cache folder
for (Uri link : links) {

View File

@ -51,7 +51,7 @@ public class LinkLoader extends AsyncTask<Uri, Void, LinkLoader.DataHolder> {
@Override
protected DataHolder doInBackground(Uri[] links) {
protected DataHolder doInBackground(Uri... links) {
try {
Uri link = links[0];
List<String> pathSeg = link.getPathSegments();

View File

@ -60,7 +60,7 @@ public class ListLoader extends AsyncTask<Long, Void, UserLists> {
@Override
protected UserLists doInBackground(Long[] param) {
protected UserLists doInBackground(Long... param) {
try {
switch (listType) {
case LOAD_USERLISTS:

View File

@ -36,7 +36,7 @@ public class LocationLoader extends AsyncTask<Void, Void, List<Location>> {
@Override
protected List<Location> doInBackground(Void[] v) {
protected List<Location> doInBackground(Void... v) {
try {
return connection.getLocations();
} catch (ConnectionException exception) {

View File

@ -42,7 +42,7 @@ public class MessageUpdater extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void[] v) {
protected Boolean doInBackground(Void... v) {
try {
// first check if user exists
long id = connection.showUser(message.getReceiver()).getId();

View File

@ -34,7 +34,7 @@ public class MetricsLoader extends AsyncTask<Long, Void, Metrics> {
@Override
protected Metrics doInBackground(Long[] ids) {
protected Metrics doInBackground(Long... ids) {
try {
return connection.getStatusMetrics(ids[0]);
} catch (ConnectionException exception) {

View File

@ -96,7 +96,7 @@ public class StatusLoader extends AsyncTask<Long, Void, List<Status>> {
@Override
protected List<org.nuclearfog.twidda.model.Status> doInBackground(Long[] param) {
protected List<org.nuclearfog.twidda.model.Status> doInBackground(Long... param) {
List<org.nuclearfog.twidda.model.Status> statuses = null;
long sinceId = param[0];
long maxId = param[1];

View File

@ -43,7 +43,7 @@ public class TrendLoader extends AsyncTask<Integer, Void, List<Trend>> {
@Override
protected List<Trend> doInBackground(Integer[] param) {
protected List<Trend> doInBackground(Integer... param) {
List<Trend> trends;
int woeId = param[0];
try {

View File

@ -104,7 +104,7 @@ public class UserLoader extends AsyncTask<Long, Void, Users> {
@Override
protected Users doInBackground(Long[] param) {
protected Users doInBackground(Long... param) {
try {
long cursor = param[0];
switch (type) {

View File

@ -40,7 +40,7 @@ public class UserUpdater extends AsyncTask<Void, Void, User> {
@Override
protected User doInBackground(Void[] v) {
protected User doInBackground(Void... v) {
try {
if (profile.getProfileImageStream() != null) {
connection.updateProfileImage(profile.getProfileImageStream());

View File

@ -384,9 +384,9 @@ public class UserlistActivity extends AppCompatActivity implements OnTabSelected
* @param user user to remove from the lsit
*/
public void onDelete(User user) {
this.user = user;
if (!confirmDialog.isShowing()) {
confirmDialog.show(ConfirmDialog.LIST_REMOVE_USER);
this.user = user;
}
}

View File

@ -33,6 +33,8 @@ import org.nuclearfog.twidda.backend.utils.AppStyles;
import org.nuclearfog.twidda.backend.utils.ErrorHandler;
import org.nuclearfog.twidda.database.GlobalSettings;
import java.util.regex.Pattern;
/**
* Activity to show one or more lists of users
*
@ -94,6 +96,11 @@ public class UsersActivity extends AppCompatActivity implements OnTabSelectedLis
*/
public static final int USERS_REQUESTS = 0x0948693;
/**
* regex pattern to validate username
*/
private static final Pattern USERNAME_PATTERN = Pattern.compile("@?\\w{1,15}");
private GlobalSettings settings;
private FilterLoader userExclTask;
private FragmentAdapter adapter;
@ -257,17 +264,21 @@ public class UsersActivity extends AppCompatActivity implements OnTabSelectedLis
@Override
public boolean onQueryTextSubmit(String query) {
if (userExclTask == null || userExclTask.getStatus() != RUNNING) {
if (tablayout.getSelectedTabPosition() == 0) {
userExclTask = new FilterLoader(this, MUTE_USER);
userExclTask.execute(query);
return true;
}
if (tablayout.getSelectedTabPosition() == 1) {
userExclTask = new FilterLoader(this, BLOCK_USER);
userExclTask.execute(query);
return true;
if (USERNAME_PATTERN.matcher(query).matches()) {
if (userExclTask == null || userExclTask.getStatus() != RUNNING) {
if (tablayout.getSelectedTabPosition() == 0) {
userExclTask = new FilterLoader(this, MUTE_USER);
userExclTask.execute(query);
return true;
}
if (tablayout.getSelectedTabPosition() == 1) {
userExclTask = new FilterLoader(this, BLOCK_USER);
userExclTask.execute(query);
return true;
}
}
} else {
Toast.makeText(this, R.string.error_username_format, Toast.LENGTH_SHORT).show();
}
return false;
}

View File

@ -53,7 +53,7 @@ public class AccountFragment extends ListFragment implements OnAccountClickListe
super.onStart();
if (loginTask == null) {
setRefresh(true);
loginTask = new AccountLoader(this);
loginTask = new AccountLoader(this, AccountLoader.MODE_LOAD);
loginTask.execute();
}
}
@ -69,17 +69,13 @@ public class AccountFragment extends ListFragment implements OnAccountClickListe
@Override
protected void onReload() {
loginTask = new AccountLoader(this);
loginTask = new AccountLoader(this, AccountLoader.MODE_LOAD);
loginTask.execute();
}
@Override
protected void onReset() {
adapter.clear();
loginTask = new AccountLoader(this);
loginTask.execute();
setRefresh(true);
}
@ -120,13 +116,13 @@ public class AccountFragment extends ListFragment implements OnAccountClickListe
@Override
public void onConfirm(int type, boolean rememberChoice) {
if (type == ConfirmDialog.REMOVE_ACCOUNT) {
loginTask = new AccountLoader(this);
loginTask.execute(selection);
loginTask = new AccountLoader(this, AccountLoader.MODE_DELETE);
loginTask.execute(selection.getId());
}
}
/**
* called from {@link AccountLoader} to set login information
* called from {@link AccountLoader} to add accounts to list
*
* @param result login information
*/
@ -136,9 +132,11 @@ public class AccountFragment extends ListFragment implements OnAccountClickListe
}
/**
* called from {@link AccountLoader} when an error occurs
* called from {@link AccountLoader} to remove account from list
*
* @param id ID of the account to delete
*/
public void onError() {
Toast.makeText(requireContext(), R.string.error_login_information, Toast.LENGTH_SHORT).show();
public void onDelete(long id) {
adapter.removeItem(id);
}
}

View File

@ -215,7 +215,6 @@
<string name="toolbar_tweet_favoriter">Tweet favorisiert von</string>
<string name="toolbar_tweet_liker">Tweet gelikt von</string>
<string name="error_forbidden_api_access">Diese API unterstützt nicht diese Aktion!</string>
<string name="error_login_information">Login Informationen konnten nicht geladen werden!</string>
<string name="error_adding_media">Fehler, Mediendatei konnte nicht hinzugefügt werden!</string>
<string name="confirm_open_link">im Browser öffnen</string>
<string name="error_media_init">Fehler beim Vorbreiten der Mediendateien!</string>

View File

@ -55,7 +55,7 @@
<string name="info_tweet_text_copied">Tweet text copied</string>
<string name="info_tweet_location_copied">Location coordinates copied</string>
<string name="info_tweet_medialink_copied">Media link copied</string>
<string name="info_account_selected">\@%1$s selected</string>
<string name="info_account_selected">%1$s selected</string>
<string name="info_error">Error</string>
<!-- toast messages for error information -->
@ -95,7 +95,6 @@
<string name="error_invalid_media">Invalid media file!</string>
<string name="error_not_defined">Not specified error!</string>
<string name="error_forbidden_api_access">API does not support this operation!</string>
<string name="error_login_information">could not load login information!</string>
<string name="error_adding_media">Error occurred while adding media!</string>
<string name="error_media_init">Error while preparing media files for upload!</string>
<string name="error_empty_token">token key missing!</string>