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

View File

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

View File

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