performance improvement, removed redundant code

This commit is contained in:
nuclearfog 2020-11-09 18:08:48 +01:00
parent c1301a5411
commit e96fb3643f
No known key found for this signature in database
GPG Key ID: D5490E4A81F97B14
8 changed files with 199 additions and 222 deletions

View File

@ -90,7 +90,6 @@ public class TweetActivity extends AppCompatActivity implements OnClickListener,
private ImageView profile_img, mediaButton;
private View header, footer, sensitive_media;
private Dialog deleteDialog;
private FragmentAdapter adapter;
private GlobalSettings settings;
@Nullable
@ -126,8 +125,14 @@ public class TweetActivity extends AppCompatActivity implements OnClickListener,
tool.setTitle("");
setSupportActionBar(tool);
Bundle param = getIntent().getExtras();
FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager());
if (param != null) {
tweetId = param.getLong(KEY_TWEET_ID);
String username = param.getString(KEY_TWEET_NAME, "");
adapter.setupTweetPage(tweetId, username);
}
adapter = new FragmentAdapter(getSupportFragmentManager());
pager.setOffscreenPageLimit(1);
pager.setAdapter(adapter);
@ -156,10 +161,8 @@ public class TweetActivity extends AppCompatActivity implements OnClickListener,
if (statusAsync == null && param != null) {
if (param.containsKey(KEY_TWEET_ID) && param.containsKey(KEY_TWEET_NAME)) {
tweetId = param.getLong(KEY_TWEET_ID);
String username = param.getString(KEY_TWEET_NAME);
adapter.setupTweetPage(tweetId, username);
statusAsync = new TweetLoader(this, Action.LOAD);
statusAsync.execute(tweetId);
statusAsync = new TweetLoader(this, tweetId);
statusAsync.execute(Action.LOAD);
}
}
}
@ -302,18 +305,25 @@ public class TweetActivity extends AppCompatActivity implements OnClickListener,
@Override
public boolean onLongClick(View v) {
if (statusAsync != null && tweet != null && statusAsync.getStatus() != RUNNING) {
if (statusAsync != null && statusAsync.getStatus() != RUNNING && tweet != null) {
statusAsync = new TweetLoader(this, tweet);
// retweet the tweet
if (v.getId() == R.id.tweet_retweet) {
statusAsync = new TweetLoader(this, Action.RETWEET);
statusAsync.execute(tweet.getId());
if (tweet.retweeted()) {
statusAsync.execute(Action.UNRETWEET);
} else {
statusAsync.execute(Action.RETWEET);
}
Toast.makeText(this, R.string.info_loading, LENGTH_SHORT).show();
return true;
}
// favorite the tweet
else if (v.getId() == R.id.tweet_favorit) {
statusAsync = new TweetLoader(this, Action.FAVORITE);
statusAsync.execute(tweet.getId());
if (tweet.favored()) {
statusAsync.execute(Action.UNFAVORITE);
} else {
statusAsync.execute(Action.FAVORITE);
}
Toast.makeText(this, R.string.info_loading, LENGTH_SHORT).show();
return true;
}
@ -325,8 +335,8 @@ public class TweetActivity extends AppCompatActivity implements OnClickListener,
@Override
public void onConfirm(DialogBuilder.DialogType type) {
if (type == DELETE_TWEET && tweet != null) {
statusAsync = new TweetLoader(TweetActivity.this, Action.DELETE);
statusAsync.execute(tweet.getId());
statusAsync = new TweetLoader(this, tweet.getId());
statusAsync.execute(Action.DELETE);
}
}
@ -455,22 +465,23 @@ public class TweetActivity extends AppCompatActivity implements OnClickListener,
/**
* called after a tweet action
*
* @param tweet tweet information
* @param action action type
*/
public void onAction(Tweet tweet, Action action) {
public void onAction(Action action) {
switch (action) {
case RETWEET:
if (tweet.retweeted())
Toast.makeText(this, R.string.info_tweet_retweeted, LENGTH_SHORT).show();
else
break;
case UNRETWEET:
Toast.makeText(this, R.string.info_tweet_unretweeted, LENGTH_SHORT).show();
break;
case FAVORITE:
if (tweet.favored())
Toast.makeText(this, R.string.info_tweet_favored, LENGTH_SHORT).show();
else
break;
case UNFAVORITE:
Toast.makeText(this, R.string.info_tweet_unfavored, LENGTH_SHORT).show();
break;

View File

@ -70,6 +70,9 @@ import static org.nuclearfog.twidda.activity.UserLists.KEY_USERLIST_OWNER_ID;
import static org.nuclearfog.twidda.backend.ProfileLoader.Action.ACTION_BLOCK;
import static org.nuclearfog.twidda.backend.ProfileLoader.Action.ACTION_FOLLOW;
import static org.nuclearfog.twidda.backend.ProfileLoader.Action.ACTION_MUTE;
import static org.nuclearfog.twidda.backend.ProfileLoader.Action.ACTION_UNBLOCK;
import static org.nuclearfog.twidda.backend.ProfileLoader.Action.ACTION_UNFOLLOW;
import static org.nuclearfog.twidda.backend.ProfileLoader.Action.ACTION_UNMUTE;
import static org.nuclearfog.twidda.backend.ProfileLoader.Action.LDR_PROFILE;
import static org.nuclearfog.twidda.backend.utils.DialogBuilder.DialogType.PROFILE_BLOCK;
import static org.nuclearfog.twidda.backend.utils.DialogBuilder.DialogType.PROFILE_MUTE;
@ -322,14 +325,14 @@ public class UserProfile extends AppCompatActivity implements OnClickListener, O
} else if (menuId == R.id.profile_mute) {
if (relation.isMuted()) {
profileAsync = new ProfileLoader(this, user);
profileAsync.execute(ACTION_MUTE);
profileAsync.execute(ACTION_UNMUTE);
} else if (!muteConfirm.isShowing()) {
muteConfirm.show();
}
} else if (menuId == R.id.profile_block) {
if (relation.isBlocked()) {
profileAsync = new ProfileLoader(this, user);
profileAsync.execute(ACTION_BLOCK);
profileAsync.execute(ACTION_UNBLOCK);
} else if (!blockConfirm.isShowing()) {
blockConfirm.show();
}
@ -445,7 +448,7 @@ public class UserProfile extends AppCompatActivity implements OnClickListener, O
if (user != null) {
profileAsync = new ProfileLoader(this, user);
if (type == PROFILE_UNFOLLOW) {
profileAsync.execute(ACTION_FOLLOW);
profileAsync.execute(ACTION_UNFOLLOW);
} else if (type == PROFILE_BLOCK) {
profileAsync.execute(ACTION_BLOCK);
} else if (type == PROFILE_MUTE) {
@ -544,38 +547,38 @@ public class UserProfile extends AppCompatActivity implements OnClickListener, O
}
/**
* print messages after user action
* sets user relation information and checks for status changes
*
* @param properties connection to an user
* @param relation relation to an user
*/
public void onAction(UserRelation properties) {
if (relation != null) {
public void onAction(UserRelation relation) {
if (this.relation != null) {
// check if block status changed
if (properties.isBlocked() != relation.isBlocked()) {
if (properties.isBlocked()) {
if (relation.isBlocked() != this.relation.isBlocked()) {
if (relation.isBlocked()) {
Toast.makeText(this, R.string.info_user_blocked, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, R.string.info_user_unblocked, Toast.LENGTH_SHORT).show();
}
}
// check if following status changed
else if (properties.isFriend() != relation.isFriend()) {
if (properties.isFriend()) {
else if (relation.isFriend() != this.relation.isFriend()) {
if (relation.isFriend()) {
Toast.makeText(this, R.string.info_followed, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, R.string.info_unfollowed, Toast.LENGTH_SHORT).show();
}
}
// check if mute status changed
else if (properties.isMuted() != relation.isMuted()) {
if (properties.isMuted()) {
else if (relation.isMuted() != this.relation.isMuted()) {
if (relation.isMuted()) {
Toast.makeText(this, R.string.info_user_muted, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, R.string.info_user_unmuted, Toast.LENGTH_SHORT).show();
}
}
}
relation = properties;
this.relation = relation;
invalidateOptionsMenu();
}

View File

@ -14,17 +14,23 @@ import org.nuclearfog.twidda.database.AppDatabase;
import java.lang.ref.WeakReference;
/**
* task for loading user profile information and take actions
* This background task loads profile information about a twitter user and take actions like following
*
* @see UserProfile
*/
public class ProfileLoader extends AsyncTask<ProfileLoader.Action, TwitterUser, UserRelation> {
/**
* actions to be taken
*/
public enum Action {
LDR_PROFILE,
ACTION_FOLLOW,
ACTION_UNFOLLOW,
ACTION_BLOCK,
ACTION_MUTE
ACTION_UNBLOCK,
ACTION_MUTE,
ACTION_UNMUTE
}
@Nullable
@ -52,69 +58,65 @@ public class ProfileLoader extends AsyncTask<ProfileLoader.Action, TwitterUser,
@Override
protected UserRelation doInBackground(Action[] actions) {
UserRelation connection;
TwitterUser user;
Action action = actions[0];
protected UserRelation doInBackground(Action[] action) {
try {
switch (action) {
switch (action[0]) {
case LDR_PROFILE:
if (userId > 0) { // search user by ID
// load user information from database
TwitterUser user;
if (userId > 0) {
user = db.getUser(userId);
if (user != null) {
publishProgress(user);
}
user = mTwitter.getUser(userId);
} else { // Search user by name
user = mTwitter.getUser(screenName);
}
// load user information from twitter
user = mTwitter.getUser(userId, screenName);
publishProgress(user);
db.storeUser(user);
if (userId > 0) {
connection = mTwitter.getConnection(userId);
} else {
connection = mTwitter.getConnection(screenName);
// load user relations from twitter
UserRelation relation = mTwitter.getConnection(userId, screenName);
if (!relation.isHome()) {
boolean muteUser = relation.isBlocked() || relation.isMuted();
db.muteUser(userId, muteUser);
}
if (!connection.isHome()) {
db.muteUser(userId, connection.isBlocked() || connection.isMuted());
}
return connection;
return relation;
case ACTION_FOLLOW:
connection = mTwitter.getConnection(userId);
if (!connection.isFriend()) {
user = mTwitter.followUser(userId);
} else {
user = mTwitter.unfollowUser(userId);
}
publishProgress(user);
return mTwitter.getConnection(userId);
break;
case ACTION_UNFOLLOW:
user = mTwitter.unfollowUser(userId);
publishProgress(user);
break;
case ACTION_BLOCK:
connection = mTwitter.getConnection(userId);
if (!connection.isBlocked()) {
user = mTwitter.blockUser(userId);
db.muteUser(userId, true);
} else {
user = mTwitter.unblockUser(userId);
db.muteUser(userId, false);
}
publishProgress(user);
return mTwitter.getConnection(userId);
db.muteUser(userId, true);
break;
case ACTION_UNBLOCK:
user = mTwitter.unblockUser(userId);
publishProgress(user);
db.muteUser(userId, false);
break;
case ACTION_MUTE:
connection = mTwitter.getConnection(userId);
if (!connection.isMuted()) {
user = mTwitter.muteUser(userId);
db.muteUser(userId, true);
} else {
user = mTwitter.unmuteUser(userId);
db.muteUser(userId, false);
}
publishProgress(user);
return mTwitter.getConnection(userId);
db.muteUser(userId, true);
break;
case ACTION_UNMUTE:
user = mTwitter.unmuteUser(userId);
publishProgress(user);
db.muteUser(userId, false);
break;
}
return mTwitter.getConnection(userId, screenName);
} catch (EngineException twException) {
this.twException = twException;
} catch (Exception exception) {
@ -143,6 +145,4 @@ public class ProfileLoader extends AsyncTask<ProfileLoader.Action, TwitterUser,
}
}
}
}

View File

@ -18,12 +18,14 @@ import java.lang.ref.WeakReference;
*
* @see TweetActivity
*/
public class TweetLoader extends AsyncTask<Long, Tweet, Tweet> {
public class TweetLoader extends AsyncTask<TweetLoader.Action, Tweet, TweetLoader.Action> {
public enum Action {
LOAD,
RETWEET,
UNRETWEET,
FAVORITE,
UNFAVORITE,
DELETE
}
@ -32,27 +34,31 @@ public class TweetLoader extends AsyncTask<Long, Tweet, Tweet> {
private TwitterEngine mTwitter;
private WeakReference<TweetActivity> callback;
private AppDatabase db;
private final Action action;
private long tweetId, myRetweetId;
public TweetLoader(TweetActivity callback, Action action) {
public TweetLoader(TweetActivity callback, Tweet tweet) {
this(callback, tweet.getId());
this.myRetweetId = tweet.getMyRetweetId();
}
public TweetLoader(TweetActivity callback, long tweetId) {
super();
mTwitter = TwitterEngine.getInstance(callback);
db = new AppDatabase(callback);
mTwitter = TwitterEngine.getInstance(callback);
this.callback = new WeakReference<>(callback);
this.action = action;
this.tweetId = tweetId;
}
@Override
protected Tweet doInBackground(Long[] data) {
Tweet tweet = null;
long tweetId = data[0];
boolean updateStatus = false;
protected Action doInBackground(Action[] action) {
try {
switch (action) {
switch (action[0]) {
case LOAD:
tweet = db.getStatus(tweetId);
boolean updateStatus = false;
Tweet tweet = db.getStatus(tweetId);
if (tweet != null) {
publishProgress(tweet);
updateStatus = true;
@ -64,26 +70,33 @@ public class TweetLoader extends AsyncTask<Long, Tweet, Tweet> {
break;
case DELETE:
tweet = mTwitter.deleteTweet(tweetId);
mTwitter.deleteTweet(tweetId);
db.removeStatus(tweetId);
break;
case RETWEET:
tweet = mTwitter.retweet(tweetId);
tweet = mTwitter.retweet(tweetId, true);
publishProgress(tweet);
if (!tweet.retweeted())
db.removeRetweet(tweetId);
db.updateStatus(tweet);
break;
case FAVORITE:
tweet = mTwitter.favorite(tweetId);
case UNRETWEET:
tweet = mTwitter.retweet(tweetId, false);
publishProgress(tweet);
db.updateStatus(tweet);
// remove status pointing on the retweeted status
db.removeStatus(myRetweetId);
break;
if (tweet.favored())
case FAVORITE:
tweet = mTwitter.favorite(tweetId, true);
publishProgress(tweet);
db.storeFavorite(tweet);
else
break;
case UNFAVORITE:
tweet = mTwitter.favorite(tweetId, false);
publishProgress(tweet);
db.removeFavorite(tweetId);
break;
}
@ -95,7 +108,7 @@ public class TweetLoader extends AsyncTask<Long, Tweet, Tweet> {
} catch (Exception exception) {
exception.printStackTrace();
}
return tweet;
return action[0];
}
@ -109,13 +122,12 @@ public class TweetLoader extends AsyncTask<Long, Tweet, Tweet> {
@Override
protected void onPostExecute(Tweet tweet) {
protected void onPostExecute(Action action) {
if (callback.get() != null) {
if (tweet != null) {
callback.get().onAction(tweet, action);
}
if (twException != null) {
callback.get().onError(twException);
} else {
callback.get().onAction(action);
}
}
}

View File

@ -415,28 +415,17 @@ public class TwitterEngine {
* Get User Context
*
* @param userId User ID
* @param username User screen name, if user ID is defined, username can be empty
* @return User Object
* @throws EngineException if Access is unavailable
*/
public TwitterUser getUser(long userId) throws EngineException {
public TwitterUser getUser(long userId, String username) throws EngineException {
try {
if (userId > 0) {
return new TwitterUser(twitter.showUser(userId));
} catch (TwitterException err) {
throw new EngineException(err);
}
}
/**
* Get User Context
*
* @param username screen name of the user
* @return User Object
* @throws EngineException if Access is unavailable
*/
public TwitterUser getUser(String username) throws EngineException {
try {
} else {
return new TwitterUser(twitter.showUser(username));
}
} catch (TwitterException err) {
throw new EngineException(err);
}
@ -462,34 +451,22 @@ public class TwitterEngine {
* Efficient Access of Connection Information
*
* @param userId User ID compared with Home ID
* @param username User screen name
* @return User Properties
* @throws EngineException if Connection is unavailable
*/
public UserRelation getConnection(long userId) throws EngineException {
public UserRelation getConnection(long userId, String username) throws EngineException {
try {
if (userId > 0) {
return new UserRelation(twitter.showFriendship(twitterID, userId));
} catch (TwitterException err) {
throw new EngineException(err);
}
}
/**
* Efficient Access of Connection Information
*
* @param username screen name of the user
* @return User Properties
* @throws EngineException if Connection is unavailable
*/
public UserRelation getConnection(String username) throws EngineException {
try {
} else {
return new UserRelation(twitter.showFriendship(twitter.getScreenName(), username));
}
} catch (TwitterException err) {
throw new EngineException(err);
}
}
/**
* Follow Twitter user
*
@ -721,25 +698,21 @@ public class TwitterEngine {
* Retweet Action
*
* @param tweetId Tweet ID
* @param retweet true to retweet this tweet
* @throws EngineException if Access is unavailable
*/
public Tweet retweet(long tweetId) throws EngineException {
public Tweet retweet(long tweetId, boolean retweet) throws EngineException {
try {
Status tweet = twitter.showStatus(tweetId);
boolean retweeted = tweet.isRetweeted();
boolean favorited = tweet.isFavorited();
int retweetCount = tweet.getRetweetCount();
int favoritCount = tweet.getFavoriteCount();
if (tweet.isRetweeted()) {
twitter.unRetweetStatus(tweet.getId());
if (retweetCount > 0)
retweetCount--;
} else {
twitter.retweetStatus(tweet.getId());
if (retweet) {
twitter.retweetStatus(tweetId).getRetweetedStatus();
retweetCount++;
} else {
twitter.unRetweetStatus(tweetId);
retweetCount--;
}
return new Tweet(tweet, retweetCount, !retweeted, favoritCount, favorited);
return new Tweet(tweet, retweetCount, retweet, tweet.getFavoriteCount(), tweet.isFavorited());
} catch (TwitterException err) {
throw new EngineException(err);
}
@ -750,25 +723,21 @@ public class TwitterEngine {
* Favorite Action
*
* @param tweetId Tweet ID
* @param favorite true to favorite this tweet
* @throws EngineException if Access is unavailable
*/
public Tweet favorite(long tweetId) throws EngineException {
public Tweet favorite(long tweetId, boolean favorite) throws EngineException {
try {
Status tweet = twitter.showStatus(tweetId);
boolean retweeted = tweet.isRetweeted();
boolean favorited = tweet.isFavorited();
int retweetCount = tweet.getRetweetCount();
int favoritCount = tweet.getFavoriteCount();
if (tweet.isFavorited()) {
tweet = twitter.destroyFavorite(tweet.getId());
if (favoritCount > 0)
favoritCount--;
} else {
tweet = twitter.createFavorite(tweet.getId());
if (favorite) {
tweet = twitter.createFavorite(tweetId);
favoritCount++;
} else {
tweet = twitter.destroyFavorite(tweetId);
favoritCount--;
}
return new Tweet(tweet, retweetCount, retweeted, favoritCount, !favorited);
return new Tweet(tweet, tweet.getRetweetCount(), tweet.isRetweeted(), favoritCount, favorite);
} catch (TwitterException err) {
throw new EngineException(err);
}
@ -777,12 +746,11 @@ public class TwitterEngine {
/**
* @param tweetId Tweet ID
* @return dummy tweet
* @throws EngineException if Access is unavailable
*/
public Tweet deleteTweet(long tweetId) throws EngineException {
public void deleteTweet(long tweetId) throws EngineException {
try {
return new Tweet(twitter.destroyStatus(tweetId));
twitter.destroyStatus(tweetId);
} catch (TwitterException err) {
throw new EngineException(err);
}

View File

@ -18,13 +18,6 @@ public class UserListList extends LinkedList<TwitterList> {
super();
}
/**
* @param list single list item
*/
public UserListList(TwitterList list) {
super();
add(list);
}
/**
* @param prevCursor previous list cursor or 0 if list starts

View File

@ -9,8 +9,14 @@ import twitter4j.Place;
import twitter4j.Status;
import twitter4j.URLEntity;
/**
* Tweet class containing information about a tweet
*/
public class Tweet {
/**
* type of media attached to the tweet
*/
public enum MediaType {
IMAGE,
VIDEO,
@ -23,36 +29,35 @@ public class Tweet {
private static final String ANGIF = "animated_gif";
private static final String MEDIA_VIDEO = "application/x-mpegURL";
private final long tweetID;
private final long time;
private final String tweet;
private final String[] medias;
private final String source;
private long tweetID;
private long time;
private String tweet;
private String[] medias;
private String source;
private final TwitterUser user;
private final Tweet embedded;
private TwitterUser user;
private Tweet embedded;
private final long replyID;
private final long replyUserId;
private final String replyName;
private long replyID;
private long replyUserId;
private String replyName;
private final int retweetCount;
private final int favoriteCount;
private final long myRetweetId;
private final boolean retweeted;
private final boolean favored;
private final boolean sensitiveMedia;
private int retweetCount;
private int favoriteCount;
private long myRetweetId;
private boolean retweeted;
private boolean favored;
private boolean sensitiveMedia;
private final String locationName;
private final String locationCoordinates;
private final MediaType mediaType;
private String locationName;
private String locationCoordinates;
private MediaType mediaType;
/**
* Tweet Constructor
* constructor for tweets from twitter
*
* @param status Twitter4J status
* @param status tweet
*/
public Tweet(Status status) {
this(status, status.getRetweetCount(), status.isRetweeted(), status.getFavoriteCount(), status.isFavorited());

View File

@ -388,21 +388,6 @@ public class AppDatabase {
commit(db);
}
/**
* remove status containing a retweet
*
* @param tweetId tweet ID of retweet
*/
public void removeRetweet(long tweetId) {
Tweet tweet = getStatus(tweetId);
if (tweet != null) {
final String[] args = {Long.toString(tweet.getMyRetweetId())};
SQLiteDatabase db = getDbWrite();
db.delete("tweet", "tweetID=?", args);
commit(db);
}
}
/**
* remove status from favorites
*