This commit is contained in:
nuclearfog 2020-06-05 15:19:02 +02:00
parent 53bce045d3
commit 945c6d3e19
No known key found for this signature in database
GPG Key ID: ED35E22099354A64
11 changed files with 46 additions and 37 deletions

View File

@ -58,8 +58,6 @@ import static org.nuclearfog.twidda.activity.TweetPopup.KEY_TWEETPOPUP_REPLYID;
import static org.nuclearfog.twidda.activity.UserDetail.KEY_USERDETAIL_ID; import static org.nuclearfog.twidda.activity.UserDetail.KEY_USERDETAIL_ID;
import static org.nuclearfog.twidda.activity.UserDetail.KEY_USERDETAIL_MODE; import static org.nuclearfog.twidda.activity.UserDetail.KEY_USERDETAIL_MODE;
import static org.nuclearfog.twidda.activity.UserDetail.USERLIST_RETWEETS; import static org.nuclearfog.twidda.activity.UserDetail.USERLIST_RETWEETS;
import static org.nuclearfog.twidda.backend.engine.EngineException.ErrorType.NOT_AUTHORIZED;
import static org.nuclearfog.twidda.backend.engine.EngineException.ErrorType.RESOURCE_NOT_FOUND;
import static org.nuclearfog.twidda.fragment.TweetFragment.INTENT_TWEET_REMOVED_ID; import static org.nuclearfog.twidda.fragment.TweetFragment.INTENT_TWEET_REMOVED_ID;
import static org.nuclearfog.twidda.fragment.TweetFragment.RETURN_TWEET_CHANGED; import static org.nuclearfog.twidda.fragment.TweetFragment.RETURN_TWEET_CHANGED;
@ -450,15 +448,16 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
*/ */
public void onError(EngineException error) { public void onError(EngineException error) {
ErrorHandler.handleFailure(this, error); ErrorHandler.handleFailure(this, error);
EngineException.ErrorType errorType = error.getErrorType(); if (error.resourceNotFound()) {
if (tweet != null) { Bundle param = getIntent().getExtras();
if (errorType == RESOURCE_NOT_FOUND || errorType == NOT_AUTHORIZED) { if (param != null) {
Intent returnData = new Intent(); Intent returnData = new Intent();
returnData.putExtra(INTENT_TWEET_REMOVED_ID, tweet.getId()); long tweetID = param.getLong(KEY_TWEET_ID);
returnData.putExtra(INTENT_TWEET_REMOVED_ID, tweetID);
setResult(RETURN_TWEET_CHANGED, returnData); setResult(RETURN_TWEET_CHANGED, returnData);
finish();
} }
} else { finish();
} else if (tweet == null) {
finish(); finish();
} }
} }

View File

@ -164,7 +164,7 @@ public class UserProfile extends AppCompatActivity implements OnClickListener,
adapter.setupProfilePage(userId); adapter.setupProfilePage(userId);
profileAsync.execute(userId); profileAsync.execute(userId);
} else { } else {
String username = param.getString(KEY_PROFILE_NAME); String username = param.getString(KEY_PROFILE_NAME, "");
adapter.setupProfilePage(username); adapter.setupProfilePage(username);
profileAsync.execute(username); profileAsync.execute(username);
} }

View File

@ -13,8 +13,6 @@ import org.nuclearfog.twidda.fragment.MessageFragment;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.List; import java.util.List;
import static org.nuclearfog.twidda.backend.engine.EngineException.ErrorType.RESOURCE_NOT_FOUND;
/** /**
* task to download a direct message list from twitter and handle message actions * task to download a direct message list from twitter and handle message actions
@ -81,7 +79,7 @@ public class MessageListLoader extends AsyncTask<Long, Void, List<Message>> {
} }
} catch (EngineException twException) { } catch (EngineException twException) {
this.twException = twException; this.twException = twException;
if (twException.getErrorType() == RESOURCE_NOT_FOUND) if (twException.resourceNotFound())
db.deleteMessage(messageId); db.deleteMessage(messageId);
} catch (Exception exception) { } catch (Exception exception) {
exception.printStackTrace(); exception.printStackTrace();
@ -97,7 +95,7 @@ public class MessageListLoader extends AsyncTask<Long, Void, List<Message>> {
callback.get().setData(messages); callback.get().setData(messages);
} else if (twException != null) { } else if (twException != null) {
callback.get().onError(twException); callback.get().onError(twException);
if (twException.getErrorType() == RESOURCE_NOT_FOUND) { if (twException.resourceNotFound()) {
callback.get().removeItem(id); callback.get().removeItem(id);
} }
} else if (action == Action.DEL) { } else if (action == Action.DEL) {

View File

@ -57,13 +57,13 @@ public class ProfileLoader extends AsyncTask<Object, TwitterUser, UserProperties
try { try {
switch (action) { switch (action) {
case LDR_PROFILE: case LDR_PROFILE:
if (userId > 0) { if (userId > 0) { // search user by ID
user = db.getUser(userId); user = db.getUser(userId);
if (user != null) { if (user != null) {
publishProgress(user); publishProgress(user);
} }
user = mTwitter.getUser(userId); user = mTwitter.getUser(userId);
} else { } else { // Search user by name
user = mTwitter.getUser(username); user = mTwitter.getUser(username);
} }
publishProgress(user); publishProgress(user);

View File

@ -99,7 +99,7 @@ public class TweetListLoader extends AsyncTask<Object, Void, List<Tweet>> {
case USR_TWEETS: case USR_TWEETS:
page = (int) param[1]; page = (int) param[1];
if (param[0] instanceof Long) { if (param[0] instanceof Long) { // search by user ID
id = (long) param[0]; id = (long) param[0];
if (sinceId == LIST_EMPTY) { if (sinceId == LIST_EMPTY) {
tweets = db.getUserTweets(id); tweets = db.getUserTweets(id);
@ -111,16 +111,15 @@ public class TweetListLoader extends AsyncTask<Object, Void, List<Tweet>> {
tweets = mTwitter.getUserTweets(id, sinceId, page); tweets = mTwitter.getUserTweets(id, sinceId, page);
db.storeUserTweets(tweets); db.storeUserTweets(tweets);
} }
} else if (param[0] instanceof String) { } else if (param[0] instanceof String) { // search by username
search = (String) param[0]; search = (String) param[0];
tweets = mTwitter.getUserTweets(search, sinceId, page); tweets = mTwitter.getUserTweets(search, sinceId, page);
db.storeUserTweets(tweets);
} }
break; break;
case USR_FAVORS: case USR_FAVORS:
page = (int) param[1]; page = (int) param[1];
if (param[0] instanceof Long) { if (param[0] instanceof Long) { // search by user ID
id = (long) param[0]; id = (long) param[0];
if (sinceId == LIST_EMPTY) { if (sinceId == LIST_EMPTY) {
tweets = db.getUserFavs(id); tweets = db.getUserFavs(id);
@ -132,7 +131,7 @@ public class TweetListLoader extends AsyncTask<Object, Void, List<Tweet>> {
tweets = mTwitter.getUserFavs(id, page); tweets = mTwitter.getUserFavs(id, page);
db.storeUserFavs(tweets, id); db.storeUserFavs(tweets, id);
} }
} else if (param[0] instanceof String) { } else if (param[0] instanceof String) { // search by username
search = (String) param[0]; search = (String) param[0];
tweets = mTwitter.getUserFavs(search, page); tweets = mTwitter.getUserFavs(search, page);
} }

View File

@ -12,8 +12,6 @@ import org.nuclearfog.twidda.database.AppDatabase;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import static org.nuclearfog.twidda.backend.engine.EngineException.ErrorType.RESOURCE_NOT_FOUND;
/** /**
* Background task to download tweet informations and to take actions * Background task to download tweet informations and to take actions
@ -89,7 +87,7 @@ public class TweetLoader extends AsyncTask<Long, Tweet, Tweet> {
} }
} catch (EngineException twException) { } catch (EngineException twException) {
this.twException = twException; this.twException = twException;
if (twException.getErrorType() == RESOURCE_NOT_FOUND) { if (twException.resourceNotFound()) {
db.removeStatus(tweetId); db.removeStatus(tweetId);
} }
} catch (Exception exception) { } catch (Exception exception) {

View File

@ -2,6 +2,9 @@ package org.nuclearfog.twidda.backend.engine;
import twitter4j.TwitterException; import twitter4j.TwitterException;
import static org.nuclearfog.twidda.backend.engine.EngineException.ErrorType.NOT_AUTHORIZED;
import static org.nuclearfog.twidda.backend.engine.EngineException.ErrorType.RESOURCE_NOT_FOUND;
public class EngineException extends Exception { public class EngineException extends Exception {
@ -122,15 +125,26 @@ public class EngineException extends Exception {
/** /**
* get type of error defined by twitter API * get type of error defined by twitter API
*
* @return type of error {@link ErrorType} * @return type of error {@link ErrorType}
*/ */
public ErrorType getErrorType() { public ErrorType getErrorType() {
return errorType; return errorType;
} }
/**
* check if a resource was not found or current user is not authorized
*
* @return true if resource not found or access denied
*/
public boolean resourceNotFound() {
return errorType == RESOURCE_NOT_FOUND || errorType == NOT_AUTHORIZED;
}
/** /**
* return time to wait after unlock access in seconds * return time to wait after unlock access in seconds
*
* @return time in seconds * @return time in seconds
*/ */
public int getTimeToWait() { public int getTimeToWait() {

View File

@ -105,7 +105,7 @@ public class MessageFragment extends Fragment implements OnRefreshListener, OnIt
@Override @Override
public void onLinkClick(String link) { public void onLinkClick(String link) {
if (getContext() != null && reload != null && !reload.isRefreshing()) { if (getContext() != null && !reload.isRefreshing()) {
if (TweetDetail.linkPattern.matcher(link).matches()) { if (TweetDetail.linkPattern.matcher(link).matches()) {
Intent intent = new Intent(getContext(), TweetDetail.class); Intent intent = new Intent(getContext(), TweetDetail.class);
intent.setData(Uri.parse(link)); intent.setData(Uri.parse(link));
@ -124,7 +124,7 @@ public class MessageFragment extends Fragment implements OnRefreshListener, OnIt
@Override @Override
public void onClick(final Message message, Action action) { public void onClick(final Message message, Action action) {
if (getContext() != null && reload != null && !reload.isRefreshing()) { if (getContext() != null && !reload.isRefreshing()) {
switch (action) { switch (action) {
case ANSWER: case ANSWER:
Intent sendDm = new Intent(getContext(), MessagePopup.class); Intent sendDm = new Intent(getContext(), MessagePopup.class);

View File

@ -79,9 +79,10 @@ public class TrendFragment extends Fragment implements OnRefreshListener, TrendC
@Override @Override
public void onRefresh() { public void onRefresh() {
if (trendTask != null && trendTask.getStatus() != RUNNING) if (trendTask != null && trendTask.getStatus() != RUNNING) {
load(); load();
} }
}
@Override @Override
@ -96,7 +97,7 @@ public class TrendFragment extends Fragment implements OnRefreshListener, TrendC
@Override @Override
public void onReset() { public void onReset() {
if (getView() != null) { if (reload != null && list != null && adapter != null) {
reload.setProgressBackgroundColorSchemeColor(settings.getHighlightColor()); reload.setProgressBackgroundColorSchemeColor(settings.getHighlightColor());
list.setAdapter(adapter); // force redrawing list list.setAdapter(adapter); // force redrawing list
adapter.clear(); adapter.clear();
@ -107,7 +108,7 @@ public class TrendFragment extends Fragment implements OnRefreshListener, TrendC
@Override @Override
public void onTabChange() { public void onTabChange() {
if (getView() != null) { if (list != null) {
list.smoothScrollToPosition(0); list.smoothScrollToPosition(0);
} }
} }
@ -127,7 +128,7 @@ public class TrendFragment extends Fragment implements OnRefreshListener, TrendC
* @return true if list is empty * @return true if list is empty
*/ */
public boolean isEmpty() { public boolean isEmpty() {
return adapter == null || adapter.isEmpty(); return adapter.isEmpty();
} }

View File

@ -128,7 +128,7 @@ public class TweetFragment extends Fragment implements OnRefreshListener, TweetC
@Override @Override
public void onReset() { public void onReset() {
if (getView() != null) { if (reload != null && list != null && adapter != null) {
reload.setProgressBackgroundColorSchemeColor(settings.getHighlightColor()); reload.setProgressBackgroundColorSchemeColor(settings.getHighlightColor());
list.setAdapter(adapter); // force redrawing list list.setAdapter(adapter); // force redrawing list
adapter.clear(); adapter.clear();
@ -139,7 +139,7 @@ public class TweetFragment extends Fragment implements OnRefreshListener, TweetC
@Override @Override
public void onTabChange() { public void onTabChange() {
if (getView() != null) { if (list != null) {
list.smoothScrollToPosition(0); list.smoothScrollToPosition(0);
} }
} }
@ -222,17 +222,17 @@ public class TweetFragment extends Fragment implements OnRefreshListener, TweetC
case TWEET_FRAG_TWEETS: case TWEET_FRAG_TWEETS:
tweetTask = new TweetListLoader(this, Action.USR_TWEETS); tweetTask = new TweetListLoader(this, Action.USR_TWEETS);
if (param.containsKey(KEY_FRAG_TWEET_ID)) if (param.containsKey(KEY_FRAG_TWEET_ID)) // Search with User ID
tweetTask.execute(id, 1); tweetTask.execute(id, 1);
else if (param.containsKey(KEY_FRAG_TWEET_SEARCH)) else if (param.containsKey(KEY_FRAG_TWEET_SEARCH)) // With user screen name
tweetTask.execute(search, 1); tweetTask.execute(search, 1);
break; break;
case TWEET_FRAG_FAVORS: case TWEET_FRAG_FAVORS:
tweetTask = new TweetListLoader(this, Action.USR_FAVORS); tweetTask = new TweetListLoader(this, Action.USR_FAVORS);
if (param.containsKey(KEY_FRAG_TWEET_ID)) if (param.containsKey(KEY_FRAG_TWEET_ID)) // Search with User ID
tweetTask.execute(id, 1); tweetTask.execute(id, 1);
else if (param.containsKey(KEY_FRAG_TWEET_SEARCH)) else if (param.containsKey(KEY_FRAG_TWEET_SEARCH)) // With user screen name
tweetTask.execute(search, 1); tweetTask.execute(search, 1);
break; break;

View File

@ -97,7 +97,7 @@ public class UserFragment extends Fragment implements OnRefreshListener, UserCli
@Override @Override
public void onUserClick(TwitterUser user) { public void onUserClick(TwitterUser user) {
if (getContext() != null && reload != null && !reload.isRefreshing()) { if (getContext() != null && !reload.isRefreshing()) {
Intent intent = new Intent(getContext(), UserProfile.class); Intent intent = new Intent(getContext(), UserProfile.class);
intent.putExtra(KEY_PROFILE_ID, user.getId()); intent.putExtra(KEY_PROFILE_ID, user.getId());
startActivity(intent); startActivity(intent);
@ -107,7 +107,7 @@ public class UserFragment extends Fragment implements OnRefreshListener, UserCli
@Override @Override
public void onTabChange() { public void onTabChange() {
if (getView() != null) { if (list != null) {
list.smoothScrollToPosition(0); list.smoothScrollToPosition(0);
} }
} }