restructured twitter engine, added new methods

Signed-off-by: nuclearfog <hatespirit666@gmail.com>
This commit is contained in:
nuclearfog 2021-08-28 13:48:05 +02:00
parent 0966123f8b
commit 51b255e364
No known key found for this signature in database
GPG Key ID: AA0271FBE406DB98
3 changed files with 127 additions and 143 deletions

View File

@ -2,14 +2,10 @@ package org.nuclearfog.twidda.backend;
import android.os.AsyncTask;
import androidx.annotation.Nullable;
import org.nuclearfog.twidda.backend.engine.EngineException;
import org.nuclearfog.twidda.backend.engine.TwitterEngine;
import org.nuclearfog.twidda.backend.model.Account;
import org.nuclearfog.twidda.backend.model.User;
import org.nuclearfog.twidda.database.AccountDatabase;
import org.nuclearfog.twidda.database.GlobalSettings;
import org.nuclearfog.twidda.database.AppDatabase;
import org.nuclearfog.twidda.fragment.AccountFragment;
import java.lang.ref.WeakReference;
@ -22,25 +18,18 @@ import java.util.List;
*/
public class AccountLoader extends AsyncTask<Account, Void, List<Account>> {
@Nullable
private EngineException err;
private AccountDatabase database;
private TwitterEngine mTwitter;
private AccountDatabase accountDatabase;
private AppDatabase appDatabase;
private WeakReference<AccountFragment> callback;
private boolean loggedIn;
/**
*
*/
public AccountLoader(AccountFragment fragment) {
super();
callback = new WeakReference<>(fragment);
database = AccountDatabase.getInstance(fragment.requireContext());
mTwitter = TwitterEngine.getInstance(fragment.requireContext());
GlobalSettings settings = GlobalSettings.getInstance(fragment.requireContext());
loggedIn = settings.isLoggedIn();
accountDatabase = AccountDatabase.getInstance(fragment.requireContext());
appDatabase = new AppDatabase(fragment.requireContext());
}
@ -50,28 +39,18 @@ public class AccountLoader extends AsyncTask<Account, Void, List<Account>> {
try {
// remove account if parameter is set
if (param != null && param.length > 0) {
database.removeLogin(param[0].getId());
accountDatabase.removeLogin(param[0].getId());
}
// get registered users
result = database.getLogins();
result = accountDatabase.getLogins();
// download user information
if (!result.isEmpty()) {
// get all user IDs
long[] ids = new long[result.size()];
for (int i = 0; i < ids.length; i++) {
ids[i] = result.get(i).getId();
}
// attach user information if logged in
if (loggedIn) {
// get user information
List<User> users = mTwitter.getUsers(ids);
for (int i = 0; i < users.size(); i++) {
result.get(i).attachUser(users.get(i));
}
for (Account account : result) {
long id = account.getId();
User user = appDatabase.getUser(id);
account.attachUser(user);
}
}
} catch (EngineException err) {
this.err = err;
} catch (Exception err) {
err.printStackTrace();
}
@ -85,8 +64,6 @@ public class AccountLoader extends AsyncTask<Account, Void, List<Account>> {
if (fragment != null) {
if (accounts != null) {
fragment.onSuccess(accounts);
} else {
fragment.onError(err);
}
}
}

View File

@ -214,14 +214,8 @@ public class TwitterEngine {
*/
public List<Tweet> getHome(long sinceId, long maxId) throws EngineException {
try {
Paging paging = new Paging();
paging.setCount(settings.getListSize());
if (sinceId > 0)
paging.setSinceId(sinceId);
if (maxId > 1)
paging.setMaxId(maxId - 1);
List<Status> homeTweets = twitter.getHomeTimeline(paging);
return convertStatusList(homeTweets);
Paging paging = createPaging(sinceId, maxId);
return convertStatusList(twitter.getHomeTimeline(paging));
} catch (Exception err) {
throw new EngineException(err);
}
@ -237,14 +231,8 @@ public class TwitterEngine {
*/
public List<Tweet> getMention(long sinceId, long maxId) throws EngineException {
try {
Paging paging = new Paging();
paging.setCount(settings.getListSize());
if (sinceId > 0)
paging.setSinceId(sinceId);
if (maxId > 1)
paging.setMaxId(maxId - 1);
List<Status> mentions = twitter.getMentionsTimeline(paging);
return convertStatusList(mentions);
Paging paging = createPaging(sinceId, maxId);
return convertStatusList(twitter.getMentionsTimeline(paging));
} catch (Exception err) {
throw new EngineException(err);
}
@ -351,12 +339,7 @@ public class TwitterEngine {
*/
public List<Tweet> getUserTweets(long userId, long sinceId, long maxId) throws EngineException {
try {
Paging paging = new Paging();
paging.setCount(settings.getListSize());
if (sinceId > 0)
paging.setSinceId(sinceId);
if (maxId > 1)
paging.setMaxId(maxId - 1);
Paging paging = createPaging(sinceId, maxId);
return convertStatusList(twitter.getUserTimeline(userId, paging));
} catch (Exception err) {
throw new EngineException(err);
@ -374,12 +357,7 @@ public class TwitterEngine {
*/
public List<Tweet> getUserTweets(String username, long sinceId, long maxId) throws EngineException {
try {
Paging paging = new Paging();
paging.setCount(settings.getListSize());
if (sinceId > 0)
paging.setSinceId(sinceId);
if (maxId > 0)
paging.setMaxId(maxId - 1);
Paging paging = createPaging(sinceId, maxId);
return convertStatusList(twitter.getUserTimeline(username, paging));
} catch (Exception err) {
throw new EngineException(err);
@ -397,12 +375,7 @@ public class TwitterEngine {
*/
public List<Tweet> getUserFavs(long userId, long sinceId, long maxId) throws EngineException {
try {
Paging paging = new Paging();
paging.setCount(settings.getListSize());
if (sinceId > 0)
paging.setSinceId(sinceId);
if (maxId > 1)
paging.setMaxId(maxId - 1);
Paging paging = createPaging(sinceId, maxId);
return convertStatusList(twitter.getFavorites(userId, paging));
} catch (Exception err) {
throw new EngineException(err);
@ -420,14 +393,8 @@ public class TwitterEngine {
*/
public List<Tweet> getUserFavs(String username, long sinceId, long maxId) throws EngineException {
try {
Paging paging = new Paging();
paging.setCount(settings.getListSize());
if (sinceId > 0)
paging.setSinceId(sinceId);
if (maxId > 0)
paging.setMaxId(maxId - 1);
List<Status> tweets = twitter.getFavorites(username, paging);
return convertStatusList(tweets);
Paging paging = createPaging(sinceId, maxId);
return convertStatusList(twitter.getFavorites(username, paging));
} catch (Exception err) {
throw new EngineException(err);
}
@ -448,22 +415,6 @@ public class TwitterEngine {
}
}
/**
* get a list of users
*
* @param users user IDs
* @return list of users
* @throws EngineException if Access is unavailable
*/
public List<User> getUsers(long[] users) throws EngineException {
try {
// todo add paging system
return convertUserList(twitter.lookupUsers(users));
} catch (Exception err) {
throw new EngineException(err);
}
}
/**
* Get User
*
@ -524,6 +475,20 @@ public class TwitterEngine {
}
}
/**
* block twitter user by screen name
*
* @param name screen name
* @throws EngineException if twitter service is unavailable
*/
public void blockUser(String name) throws EngineException {
try {
twitter.createBlock(name);
} catch (Exception err) {
throw new EngineException(err);
}
}
/**
* Block Twitter user
*
@ -554,6 +519,20 @@ public class TwitterEngine {
}
}
/**
* mute twitter user by screen name
*
* @param name screen name of the user
* @throws EngineException if twitter service is unavailable
*/
public void muteUser(String name) throws EngineException {
try {
twitter.createMute(name);
} catch (Exception err) {
throw new EngineException(err);
}
}
/**
* Mute Twitter user
*
@ -593,16 +572,8 @@ public class TwitterEngine {
*/
public UserList getFollowing(long userId, long cursor) throws EngineException {
try {
int load = settings.getListSize();
IDs userIDs = twitter.getFriendsIDs(userId, cursor, load);
long[] ids = userIDs.getIDs();
long prevCursor = cursor > 0 ? cursor : 0;
long nextCursor = userIDs.getNextCursor();
UserList result = new UserList(prevCursor, nextCursor);
if (ids.length > 0) {
result.addAll(convertUserList(twitter.lookupUsers(ids)));
}
return result;
PagableResponseList<twitter4j.User> list = twitter.getFriendsList(userId, cursor);
return createUserList(list, cursor);
} catch (Exception err) {
throw new EngineException(err);
}
@ -617,16 +588,40 @@ public class TwitterEngine {
*/
public UserList getFollower(long userId, long cursor) throws EngineException {
try {
int load = settings.getListSize();
IDs userIDs = twitter.getFollowersIDs(userId, cursor, load);
long[] ids = userIDs.getIDs();
long prevCursor = cursor > 0 ? cursor : 0;
long nextCursor = userIDs.getNextCursor();
UserList result = new UserList(prevCursor, nextCursor);
if (ids.length > 0) {
result.addAll(convertUserList(twitter.lookupUsers(ids)));
}
return result;
PagableResponseList<twitter4j.User> list = twitter.getFollowersList(userId, cursor);
return createUserList(list, cursor);
} catch (Exception err) {
throw new EngineException(err);
}
}
/**
* get a list of blocked users
*
* @param cursor list cursor
* @return user list
* @throws EngineException if twitter service is unavailable
*/
public UserList getBlockedUsers(long cursor) throws EngineException {
try {
PagableResponseList<twitter4j.User> list = twitter.getBlocksList(cursor);
return createUserList(list, cursor);
} catch (Exception err) {
throw new EngineException(err);
}
}
/**
* get a list of muted users
*
* @param cursor list cursor
* @return user list
* @throws EngineException if twitter service is unavailable
*/
public UserList getMutedUsers(long cursor) throws EngineException {
try {
PagableResponseList<twitter4j.User> list = twitter.getMutesList(cursor);
return createUserList(list, cursor);
} catch (Exception err) {
throw new EngineException(err);
}
@ -1052,12 +1047,8 @@ public class TwitterEngine {
*/
public UserList getListFollower(long listId, long cursor) throws EngineException {
try {
PagableResponseList<twitter4j.User> followerList = twitter.getUserListSubscribers(listId, cursor);
long prevCursor = cursor > 0 ? cursor : 0;
long nextCursor = followerList.getNextCursor();
UserList result = new UserList(prevCursor, nextCursor);
result.addAll(convertUserList(followerList));
return result;
PagableResponseList<twitter4j.User> users = twitter.getUserListSubscribers(listId, cursor);
return createUserList(users, cursor);
} catch (Exception err) {
throw new EngineException(err);
}
@ -1073,11 +1064,7 @@ public class TwitterEngine {
public UserList getListMember(long listId, long cursor) throws EngineException {
try {
PagableResponseList<twitter4j.User> users = twitter.getUserListMembers(listId, cursor);
long prevCursor = cursor > 0 ? cursor : 0;
long nextCursor = users.getNextCursor();
UserList result = new UserList(prevCursor, nextCursor);
result.addAll(convertUserList(users));
return result;
return createUserList(users, cursor);
} catch (Exception err) {
throw new EngineException(err);
}
@ -1094,12 +1081,7 @@ public class TwitterEngine {
*/
public List<Tweet> getListTweets(long listId, long sinceId, long maxId) throws EngineException {
try {
Paging paging = new Paging();
paging.setCount(settings.getListSize());
if (sinceId > 0)
paging.setSinceId(sinceId);
if (maxId > 1)
paging.setMaxId(maxId - 1);
Paging paging = createPaging(sinceId, maxId);
return convertStatusList(twitter.getUserListStatuses(listId, paging));
} catch (Exception err) {
throw new EngineException(err);
@ -1212,6 +1194,24 @@ public class TwitterEngine {
}
}
/**
* create user list from {@link PagableResponseList}
*
* @param list user list
* @param cursor prev cursor of the list
* @return user list
* @throws TwitterException if access is unavailable
*/
private UserList createUserList(PagableResponseList<twitter4j.User> list, long cursor) throws TwitterException {
long prevCursor = cursor > 0 ? cursor : 0;
long nextCursor = list.getNextCursor();
UserList result = new UserList(prevCursor, nextCursor);
if (!list.isEmpty()) {
result.addAll(convertUserList(list));
}
return result;
}
/**
* convert #twitter4j.User to User List
*
@ -1228,6 +1228,23 @@ public class TwitterEngine {
return result;
}
/**
* create paging for tweets
*
* @param minId minimum tweet ID
* @param maxId maximum tweet ID
* @return paging instance
*/
private Paging createPaging(long minId, long maxId) {
Paging paging = new Paging();
paging.setCount(settings.getListSize());
if (minId > 0)
paging.setSinceId(minId);
if (maxId > 1)
paging.setMaxId(maxId - 1);
return paging;
}
/**
* convert #twitter4j.Status to Tweet List
*

View File

@ -1,24 +1,22 @@
package org.nuclearfog.twidda.fragment;
import static android.os.AsyncTask.Status.RUNNING;
import static org.nuclearfog.twidda.activity.AccountActivity.RET_ACCOUNT_CHANGE;
import static org.nuclearfog.twidda.dialog.ConfirmDialog.DialogType;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import org.nuclearfog.twidda.adapter.AccountAdapter;
import org.nuclearfog.twidda.adapter.AccountAdapter.OnAccountClickListener;
import org.nuclearfog.twidda.backend.AccountLoader;
import org.nuclearfog.twidda.backend.engine.EngineException;
import org.nuclearfog.twidda.backend.model.Account;
import org.nuclearfog.twidda.backend.utils.ErrorHandler;
import org.nuclearfog.twidda.database.GlobalSettings;
import org.nuclearfog.twidda.dialog.ConfirmDialog;
import org.nuclearfog.twidda.dialog.ConfirmDialog.OnConfirmListener;
import java.util.List;
import static android.os.AsyncTask.Status.RUNNING;
import static org.nuclearfog.twidda.activity.AccountActivity.RET_ACCOUNT_CHANGE;
import static org.nuclearfog.twidda.dialog.ConfirmDialog.DialogType;
/**
* fragment class of the {@link org.nuclearfog.twidda.activity.AccountActivity}
@ -121,12 +119,4 @@ public class AccountFragment extends ListFragment implements OnAccountClickListe
adapter.setData(result);
setRefresh(false);
}
/**
* called from {@link AccountLoader} when an error occurs
*/
public void onError(EngineException err) {
ErrorHandler.handleFailure(requireContext(), err);
setRefresh(false);
}
}