bug fix, code cleanup

This commit is contained in:
nuclearfog 2020-06-02 21:17:48 +02:00
parent 9e64c0b26e
commit 163ebadf7f
No known key found for this signature in database
GPG Key ID: ED35E22099354A64
3 changed files with 106 additions and 113 deletions

View File

@ -69,11 +69,7 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
public static final String KEY_TWEET_ID = "tweetID"; public static final String KEY_TWEET_ID = "tweetID";
public static final String KEY_TWEET_NAME = "username"; public static final String KEY_TWEET_NAME = "username";
public static final Pattern linkPattern = Pattern.compile(".*/@?[\\w_]+/status/\\d{1,20}/?.*"); public static final Pattern linkPattern = Pattern.compile("https://twitter.com/\\w+/status/\\d+");
@Nullable
private TweetLoader statusAsync;
private GlobalSettings settings;
private TextView tweet_api, tweetDate, tweetText, scrName, usrName, tweetLocName; private TextView tweet_api, tweetDate, tweetText, scrName, usrName, tweetLocName;
private Button rtwButton, favButton, replyName, tweetLocGPS; private Button rtwButton, favButton, replyName, tweetLocGPS;
@ -81,10 +77,11 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
private View header, footer; private View header, footer;
private ViewPager pager; private ViewPager pager;
private GlobalSettings settings;
@Nullable
private TweetLoader statusAsync;
@Nullable @Nullable
private Tweet tweet; private Tweet tweet;
private String username;
private long tweetID;
@Override @Override
protected void onCreate(@Nullable Bundle b) { protected void onCreate(@Nullable Bundle b) {
@ -136,8 +133,8 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
Bundle param = getIntent().getExtras(); Bundle param = getIntent().getExtras();
if (statusAsync == null && param != null) { if (statusAsync == null && param != null) {
if (param.containsKey(KEY_TWEET_ID) && param.containsKey(KEY_TWEET_NAME)) { if (param.containsKey(KEY_TWEET_ID) && param.containsKey(KEY_TWEET_NAME)) {
tweetID = param.getLong(KEY_TWEET_ID); long tweetID = param.getLong(KEY_TWEET_ID);
username = param.getString(KEY_TWEET_NAME); String username = param.getString(KEY_TWEET_NAME);
FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager()); FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager());
adapter.setupTweetPage(tweetID, username); adapter.setupTweetPage(tweetID, username);
pager.setAdapter(adapter); pager.setAdapter(adapter);
@ -173,7 +170,7 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
@Override @Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) { public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (statusAsync != null && statusAsync.getStatus() != RUNNING) { if (statusAsync != null && tweet != null && statusAsync.getStatus() != RUNNING) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.delete_tweet: case R.id.delete_tweet:
Builder deleteDialog = new Builder(this, R.style.ConfirmDialog); Builder deleteDialog = new Builder(this, R.style.ConfirmDialog);
@ -182,7 +179,7 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
statusAsync = new TweetLoader(TweetDetail.this, Action.DELETE); statusAsync = new TweetLoader(TweetDetail.this, Action.DELETE);
statusAsync.execute(tweetID); statusAsync.execute(tweet.getId());
} }
}); });
deleteDialog.setNegativeButton(R.string.confirm_no, null); deleteDialog.setNegativeButton(R.string.confirm_no, null);
@ -190,7 +187,8 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
break; break;
case R.id.tweet_link: case R.id.tweet_link:
String tweetLink = "https://twitter.com/" + username.substring(1) + "/status/" + tweetID; String username = tweet.getUser().getScreenname().substring(1);
String tweetLink = "https://twitter.com/" + username + "/status/" + tweet.getId();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(tweetLink)); Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(tweetLink));
if (intent.resolveActivity(getPackageManager()) != null) if (intent.resolveActivity(getPackageManager()) != null)
startActivity(intent); startActivity(intent);
@ -199,7 +197,8 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
break; break;
case R.id.link_copy: case R.id.link_copy:
tweetLink = "https://twitter.com/" + username.substring(1) + "/status/" + tweetID; username = tweet.getUser().getScreenname().substring(1);
tweetLink = "https://twitter.com/" + username + "/status/" + tweet.getId();
ClipboardManager clip = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); ClipboardManager clip = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
if (clip != null) { if (clip != null) {
ClipData linkClip = ClipData.newPlainText("tweet link", tweetLink); ClipData linkClip = ClipData.newPlainText("tweet link", tweetLink);
@ -217,18 +216,18 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (statusAsync != null && statusAsync.getStatus() != RUNNING) { if (statusAsync != null && tweet != null && statusAsync.getStatus() != RUNNING) {
switch (v.getId()) { switch (v.getId()) {
case R.id.tweet_answer: case R.id.tweet_answer:
Intent tweetPopup = new Intent(this, TweetPopup.class); Intent tweetPopup = new Intent(this, TweetPopup.class);
tweetPopup.putExtra(KEY_TWEETPOPUP_REPLYID, tweetID); tweetPopup.putExtra(KEY_TWEETPOPUP_REPLYID, tweet.getId());
tweetPopup.putExtra(KEY_TWEETPOPUP_PREFIX, username); tweetPopup.putExtra(KEY_TWEETPOPUP_PREFIX, tweet.getUser().getScreenname());
startActivity(tweetPopup); startActivity(tweetPopup);
break; break;
case R.id.tweet_retweet: case R.id.tweet_retweet:
Intent userList = new Intent(this, UserDetail.class); Intent userList = new Intent(this, UserDetail.class);
userList.putExtra(KEY_USERDETAIL_ID, tweetID); userList.putExtra(KEY_USERDETAIL_ID, tweet.getId());
userList.putExtra(KEY_USERDETAIL_MODE, USERLIST_RETWEETS); userList.putExtra(KEY_USERDETAIL_MODE, USERLIST_RETWEETS);
startActivity(userList); startActivity(userList);
break; break;
@ -288,17 +287,17 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
@Override @Override
public boolean onLongClick(View v) { public boolean onLongClick(View v) {
if (statusAsync != null && statusAsync.getStatus() != RUNNING) { if (statusAsync != null && tweet != null && statusAsync.getStatus() != RUNNING) {
switch (v.getId()) { switch (v.getId()) {
case R.id.tweet_retweet: case R.id.tweet_retweet:
statusAsync = new TweetLoader(this, Action.RETWEET); statusAsync = new TweetLoader(this, Action.RETWEET);
statusAsync.execute(tweetID); statusAsync.execute(tweet.getId());
Toast.makeText(this, R.string.info_loading, LENGTH_SHORT).show(); Toast.makeText(this, R.string.info_loading, LENGTH_SHORT).show();
return true; return true;
case R.id.tweet_favorit: case R.id.tweet_favorit:
statusAsync = new TweetLoader(this, Action.FAVORITE); statusAsync = new TweetLoader(this, Action.FAVORITE);
statusAsync.execute(tweetID); statusAsync.execute(tweet.getId());
Toast.makeText(this, R.string.info_loading, LENGTH_SHORT).show(); Toast.makeText(this, R.string.info_loading, LENGTH_SHORT).show();
return true; return true;
} }
@ -316,15 +315,19 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
@Override @Override
public void onLinkClick(String link) { public void onLinkClick(String tag) {
if (linkPattern.matcher(link).matches()) { if (linkPattern.matcher(tag).matches()) {
String name = tag.substring(20, tag.indexOf('/', 20));
long id = Long.parseLong(tag.substring(tag.lastIndexOf('/') + 1));
Intent intent = new Intent(this, TweetDetail.class); Intent intent = new Intent(this, TweetDetail.class);
intent.setData(Uri.parse(link)); intent.putExtra(KEY_TWEET_ID, id);
intent.putExtra(KEY_TWEET_NAME, name);
startActivity(intent); startActivity(intent);
} else { } else {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link)); Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(tag));
if (intent.resolveActivity(getPackageManager()) != null) if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent); startActivity(intent);
}
} }
} }
@ -432,7 +435,7 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
case DELETE: case DELETE:
Toast.makeText(this, R.string.info_tweet_removed, LENGTH_SHORT).show(); Toast.makeText(this, R.string.info_tweet_removed, LENGTH_SHORT).show();
Intent returnData = new Intent(); Intent returnData = new Intent();
returnData.putExtra(INTENT_TWEET_REMOVED_ID, tweetID); returnData.putExtra(INTENT_TWEET_REMOVED_ID, tweet.getId());
setResult(RETURN_TWEET_CHANGED, returnData); setResult(RETURN_TWEET_CHANGED, returnData);
finish(); finish();
break; break;
@ -447,12 +450,14 @@ 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(); EngineException.ErrorType errorType = error.getErrorType();
if (errorType == RESOURCE_NOT_FOUND || errorType == NOT_AUTHORIZED) { if (tweet != null) {
Intent returnData = new Intent(); if (errorType == RESOURCE_NOT_FOUND || errorType == NOT_AUTHORIZED) {
returnData.putExtra(INTENT_TWEET_REMOVED_ID, tweetID); Intent returnData = new Intent();
setResult(RETURN_TWEET_CHANGED, returnData); returnData.putExtra(INTENT_TWEET_REMOVED_ID, tweet.getId());
finish(); setResult(RETURN_TWEET_CHANGED, returnData);
} else if (tweet == null) { finish();
}
} else {
finish(); finish();
} }
} }

View File

@ -17,6 +17,7 @@ import org.nuclearfog.twidda.database.GlobalSettings;
public class TwitterList extends AppCompatActivity { public class TwitterList extends AppCompatActivity {
public static final String KEY_USERLIST_ID = "userlist-owner"; public static final String KEY_USERLIST_ID = "userlist-owner";
private FragmentAdapter adapter; private FragmentAdapter adapter;
private ViewPager pager; private ViewPager pager;

View File

@ -87,7 +87,7 @@ public class UserProfile extends AppCompatActivity implements OnClickListener,
private UserProperties properties; private UserProperties properties;
private TwitterUser user; private TwitterUser user;
private long userId; private boolean isHome;
@Override @Override
protected void onCreate(@Nullable Bundle b) { protected void onCreate(@Nullable Bundle b) {
@ -157,7 +157,8 @@ public class UserProfile extends AppCompatActivity implements OnClickListener,
super.onStart(); super.onStart();
Bundle param = getIntent().getExtras(); Bundle param = getIntent().getExtras();
if (profileAsync == null && param != null && param.containsKey(KEY_PROFILE_ID)) { if (profileAsync == null && param != null && param.containsKey(KEY_PROFILE_ID)) {
userId = param.getLong(KEY_PROFILE_ID); long userId = param.getLong(KEY_PROFILE_ID);
isHome = userId == settings.getUserId();
adapter = new FragmentAdapter(getSupportFragmentManager()); adapter = new FragmentAdapter(getSupportFragmentManager());
adapter.setupProfilePage(userId); adapter.setupProfilePage(userId);
pager.setAdapter(adapter); pager.setAdapter(adapter);
@ -192,7 +193,7 @@ public class UserProfile extends AppCompatActivity implements OnClickListener,
@Override @Override
public boolean onCreateOptionsMenu(Menu m) { public boolean onCreateOptionsMenu(Menu m) {
getMenuInflater().inflate(R.menu.profile, m); getMenuInflater().inflate(R.menu.profile, m);
if (userId == settings.getUserId()) { if (isHome) {
MenuItem dmIcon = m.findItem(R.id.profile_message); MenuItem dmIcon = m.findItem(R.id.profile_message);
MenuItem setting = m.findItem(R.id.profile_settings); MenuItem setting = m.findItem(R.id.profile_settings);
dmIcon.setVisible(true); dmIcon.setVisible(true);
@ -217,7 +218,7 @@ public class UserProfile extends AppCompatActivity implements OnClickListener,
followIcon.setIcon(R.drawable.follow_requested); followIcon.setIcon(R.drawable.follow_requested);
followIcon.setTitle(R.string.follow_requested); followIcon.setTitle(R.string.follow_requested);
} }
if (user.isLocked() && userId != settings.getUserId()) { if (user.isLocked() && !isHome) {
MenuItem listItem = m.findItem(R.id.profile_lists); MenuItem listItem = m.findItem(R.id.profile_lists);
listItem.setVisible(false); listItem.setVisible(false);
} }
@ -253,78 +254,70 @@ public class UserProfile extends AppCompatActivity implements OnClickListener,
@Override @Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) { public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (profileAsync != null && profileAsync.getStatus() != RUNNING) { if (profileAsync != null && user != null && profileAsync.getStatus() != RUNNING) {
switch (item.getItemId()) { if (user != null && properties != null) {
case R.id.profile_tweet: switch (item.getItemId()) {
if (user != null) { case R.id.profile_tweet:
Intent tweet = new Intent(this, TweetPopup.class); Intent tweet = new Intent(this, TweetPopup.class);
if (userId != settings.getUserId()) if (user.getId() != settings.getUserId())
tweet.putExtra(KEY_TWEETPOPUP_PREFIX, user.getScreenname()); tweet.putExtra(KEY_TWEETPOPUP_PREFIX, user.getScreenname());
startActivity(tweet); startActivity(tweet);
} break;
break;
case R.id.profile_settings: case R.id.profile_settings:
Intent editProfile = new Intent(this, ProfileEditor.class); Intent editProfile = new Intent(this, ProfileEditor.class);
startActivityForResult(editProfile, REQUEST_PROFILE_CHANGED); startActivityForResult(editProfile, REQUEST_PROFILE_CHANGED);
break; break;
case R.id.profile_follow: case R.id.profile_follow:
if (properties != null) {
profileAsync = new ProfileLoader(this, ProfileLoader.Action.ACTION_FOLLOW); profileAsync = new ProfileLoader(this, ProfileLoader.Action.ACTION_FOLLOW);
if (!properties.isFriend()) { if (!properties.isFriend()) {
profileAsync.execute(userId); profileAsync.execute(user.getId());
} else { } else {
new Builder(this).setMessage(R.string.confirm_unfollow) new Builder(this).setMessage(R.string.confirm_unfollow)
.setNegativeButton(R.string.confirm_no, null) .setNegativeButton(R.string.confirm_no, null)
.setPositiveButton(R.string.confirm_yes, new DialogInterface.OnClickListener() { .setPositiveButton(R.string.confirm_yes, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
profileAsync.execute(userId); profileAsync.execute(user.getId());
} }
}).show(); }).show();
} }
} break;
break;
case R.id.profile_mute: case R.id.profile_mute:
if (properties != null) {
profileAsync = new ProfileLoader(this, ProfileLoader.Action.ACTION_MUTE); profileAsync = new ProfileLoader(this, ProfileLoader.Action.ACTION_MUTE);
if (properties.isMuted()) { if (properties.isMuted()) {
profileAsync.execute(userId); profileAsync.execute(user.getId());
} else { } else {
new Builder(this).setMessage(R.string.confirm_mute) new Builder(this).setMessage(R.string.confirm_mute)
.setNegativeButton(R.string.confirm_no, null) .setNegativeButton(R.string.confirm_no, null)
.setPositiveButton(R.string.confirm_yes, new DialogInterface.OnClickListener() { .setPositiveButton(R.string.confirm_yes, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
profileAsync.execute(userId); profileAsync.execute(user.getId());
} }
}).show(); }).show();
} }
} break;
break;
case R.id.profile_block: case R.id.profile_block:
if (properties != null) {
profileAsync = new ProfileLoader(this, ProfileLoader.Action.ACTION_BLOCK); profileAsync = new ProfileLoader(this, ProfileLoader.Action.ACTION_BLOCK);
if (properties.isBlocked()) { if (properties.isBlocked()) {
profileAsync.execute(userId); profileAsync.execute(user.getId());
} else { } else {
new Builder(this).setMessage(R.string.confirm_block) new Builder(this).setMessage(R.string.confirm_block)
.setNegativeButton(R.string.confirm_no, null) .setNegativeButton(R.string.confirm_no, null)
.setPositiveButton(R.string.confirm_yes, new DialogInterface.OnClickListener() { .setPositiveButton(R.string.confirm_yes, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
profileAsync.execute(userId); profileAsync.execute(user.getId());
} }
}).show(); }).show();
} }
} break;
break;
case R.id.profile_message: case R.id.profile_message:
if (properties != null) {
Intent dmPage; Intent dmPage;
if (properties.isHome()) { if (properties.isHome()) {
dmPage = new Intent(this, DirectMessage.class); dmPage = new Intent(this, DirectMessage.class);
@ -333,14 +326,14 @@ public class UserProfile extends AppCompatActivity implements OnClickListener,
dmPage.putExtra(KEY_DM_PREFIX, properties.getTargetScreenname()); dmPage.putExtra(KEY_DM_PREFIX, properties.getTargetScreenname());
} }
startActivity(dmPage); startActivity(dmPage);
} break;
break;
case R.id.profile_lists: case R.id.profile_lists:
Intent listPage = new Intent(this, TwitterList.class); Intent listPage = new Intent(this, TwitterList.class);
listPage.putExtra(KEY_USERLIST_ID, userId); listPage.putExtra(KEY_USERLIST_ID, user.getId());
startActivity(listPage); startActivity(listPage);
break; break;
}
} }
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
@ -383,57 +376,51 @@ public class UserProfile extends AppCompatActivity implements OnClickListener,
@Override @Override
public void onClick(View v) { public void onClick(View v) {
switch (v.getId()) { if (user != null && properties != null) {
case R.id.following: switch (v.getId()) {
if (user != null && properties != null) { case R.id.following:
if (!user.isLocked() || properties.isFriend() || userId == settings.getUserId()) { if (!user.isLocked() || properties.isFriend() || isHome) {
Intent following = new Intent(this, UserDetail.class); Intent following = new Intent(this, UserDetail.class);
following.putExtra(KEY_USERDETAIL_ID, userId); following.putExtra(KEY_USERDETAIL_ID, user.getId());
following.putExtra(KEY_USERDETAIL_MODE, USERLIST_FRIENDS); following.putExtra(KEY_USERDETAIL_MODE, USERLIST_FRIENDS);
startActivity(following); startActivity(following);
} }
} break;
break;
case R.id.follower: case R.id.follower:
if (user != null && properties != null) { if (!user.isLocked() || properties.isFriend() || isHome) {
if (!user.isLocked() || properties.isFriend() || userId == settings.getUserId()) {
Intent follower = new Intent(this, UserDetail.class); Intent follower = new Intent(this, UserDetail.class);
follower.putExtra(KEY_USERDETAIL_ID, userId); follower.putExtra(KEY_USERDETAIL_ID, user.getId());
follower.putExtra(KEY_USERDETAIL_MODE, USERLIST_FOLLOWER); follower.putExtra(KEY_USERDETAIL_MODE, USERLIST_FOLLOWER);
startActivity(follower); startActivity(follower);
} }
} break;
break;
case R.id.links: case R.id.links:
if (user != null && !user.getLink().isEmpty()) { if (!user.getLink().isEmpty()) {
String link = user.getLink(); String link = user.getLink();
Intent browserIntent = new Intent(ACTION_VIEW, Uri.parse(link)); Intent browserIntent = new Intent(ACTION_VIEW, Uri.parse(link));
if (browserIntent.resolveActivity(getPackageManager()) != null) if (browserIntent.resolveActivity(getPackageManager()) != null)
startActivity(browserIntent); startActivity(browserIntent);
else else
Toast.makeText(this, R.string.error_connection_failed, LENGTH_SHORT).show(); Toast.makeText(this, R.string.error_connection_failed, LENGTH_SHORT).show();
} }
break; break;
case R.id.profile_img: case R.id.profile_img:
if (user != null) { Intent mediaImage = new Intent(this, MediaViewer.class);
Intent image = new Intent(this, MediaViewer.class); mediaImage.putExtra(KEY_MEDIA_LINK, new String[]{user.getImageLink()});
image.putExtra(KEY_MEDIA_LINK, new String[]{user.getImageLink()}); mediaImage.putExtra(KEY_MEDIA_TYPE, MEDIAVIEWER_IMAGE);
image.putExtra(KEY_MEDIA_TYPE, MEDIAVIEWER_IMAGE); startActivity(mediaImage);
startActivity(image); break;
}
break;
case R.id.profile_banner: case R.id.profile_banner:
if (user != null) { Intent mediaBanner = new Intent(this, MediaViewer.class);
Intent image = new Intent(this, MediaViewer.class); mediaBanner.putExtra(KEY_MEDIA_LINK, new String[]{user.getBannerLink() + "/1500x500"});
image.putExtra(KEY_MEDIA_LINK, new String[]{user.getBannerLink() + "/1500x500"}); mediaBanner.putExtra(KEY_MEDIA_TYPE, MEDIAVIEWER_IMAGE);
image.putExtra(KEY_MEDIA_TYPE, MEDIAVIEWER_IMAGE); startActivity(mediaBanner);
startActivity(image); break;
} }
break;
} }
} }