From 49269eb06dbc6281422f93fe7cdac9040a5087c6 Mon Sep 17 00:00:00 2001 From: NudeDude Date: Tue, 14 Aug 2018 00:22:18 +0200 Subject: [PATCH] bugfix, cleanup --- .../nuclearfog/twidda/backend/MainPage.java | 3 +- .../twidda/backend/StatusUpload.java | 10 +++- .../nuclearfog/twidda/window/TweetDetail.java | 4 +- .../nuclearfog/twidda/window/TweetPopup.java | 59 ++++++++++--------- .../nuclearfog/twidda/window/UserDetail.java | 48 ++++++++++----- .../nuclearfog/twidda/window/UserProfile.java | 6 +- 6 files changed, 78 insertions(+), 52 deletions(-) diff --git a/app/src/main/java/org/nuclearfog/twidda/backend/MainPage.java b/app/src/main/java/org/nuclearfog/twidda/backend/MainPage.java index 5c726dae..c2d04041 100644 --- a/app/src/main/java/org/nuclearfog/twidda/backend/MainPage.java +++ b/app/src/main/java/org/nuclearfog/twidda/backend/MainPage.java @@ -36,6 +36,7 @@ public class MainPage extends AsyncTask { private TimelineRecycler timelineAdapter, mentionAdapter; private TrendRecycler trendsAdapter; + private DatabaseAdapter tweetDb; private ErrorLog errorLog; private int woeId; private int highlight, font; @@ -51,6 +52,7 @@ public class MainPage extends AsyncTask { ui = new WeakReference<>((MainActivity)context); mTwitter = TwitterEngine.getInstance(context); GlobalSettings settings = GlobalSettings.getInstance(context); + tweetDb = new DatabaseAdapter(ui.get()); errorLog = new ErrorLog(context); woeId = settings.getWoeId(); highlight = settings.getHighlightColor(); @@ -89,7 +91,6 @@ public class MainPage extends AsyncTask { long id = 1L; List tweets; try { - DatabaseAdapter tweetDb = new DatabaseAdapter(ui.get()); TrendDatabase trendDb = new TrendDatabase(ui.get()); switch (MODE) { case HOME: diff --git a/app/src/main/java/org/nuclearfog/twidda/backend/StatusUpload.java b/app/src/main/java/org/nuclearfog/twidda/backend/StatusUpload.java index aeb1c5fe..7521a289 100644 --- a/app/src/main/java/org/nuclearfog/twidda/backend/StatusUpload.java +++ b/app/src/main/java/org/nuclearfog/twidda/backend/StatusUpload.java @@ -18,6 +18,8 @@ import org.nuclearfog.twidda.window.TweetPopup; import java.lang.ref.WeakReference; +import twitter4j.TwitterException; + public class StatusUpload extends AsyncTask implements OnClickListener { private WeakReference ui; @@ -81,7 +83,13 @@ public class StatusUpload extends AsyncTask implements On mTwitter.sendStatus(tweet,id,path); } return true; - } catch(Exception err) { + } catch (TwitterException err) { + int returnCode = err.getErrorCode(); + if (returnCode > 0) { + errorLog.add("E: Upload, " + err.getMessage()); + } + return false; + } catch (Exception err) { errorLog.add("E: Upload, " + err.getMessage()); return false; } diff --git a/app/src/main/java/org/nuclearfog/twidda/window/TweetDetail.java b/app/src/main/java/org/nuclearfog/twidda/window/TweetDetail.java index 46175954..f8cad702 100644 --- a/app/src/main/java/org/nuclearfog/twidda/window/TweetDetail.java +++ b/app/src/main/java/org/nuclearfog/twidda/window/TweetDetail.java @@ -156,14 +156,14 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener, case R.id.no_rt_detail: intent = new Intent(this, UserDetail.class); intent.putExtra("tweetID", tweetID); - intent.putExtra("mode", 2L); + intent.putExtra("mode", 2); startActivity(intent); break; case R.id.no_fav_detail: intent = new Intent(this, UserDetail.class); intent.putExtra("tweetID", tweetID); - intent.putExtra("mode", 3L); + intent.putExtra("mode", 3); startActivity(intent); break; diff --git a/app/src/main/java/org/nuclearfog/twidda/window/TweetPopup.java b/app/src/main/java/org/nuclearfog/twidda/window/TweetPopup.java index c028210f..22744438 100644 --- a/app/src/main/java/org/nuclearfog/twidda/window/TweetPopup.java +++ b/app/src/main/java/org/nuclearfog/twidda/window/TweetPopup.java @@ -23,8 +23,7 @@ import org.nuclearfog.twidda.backend.StatusUpload; import java.util.ArrayList; import java.util.List; -public class TweetPopup extends AppCompatActivity implements OnClickListener, - DialogInterface.OnClickListener, StatusUpload.OnTweetSending { +public class TweetPopup extends AppCompatActivity implements OnClickListener, StatusUpload.OnTweetSending { private StatusUpload sendTweet; private View imageButton, previewBtn; @@ -77,33 +76,27 @@ public class TweetPopup extends AppCompatActivity implements OnClickListener, super.onActivityResult(reqCode,returnCode,i); if(returnCode == RESULT_OK){ String[] mode = {MediaStore.Images.Media.DATA}; - if(i.getData() == null) - return; - Cursor c = getContentResolver().query(i.getData(),mode,null,null,null); - if(c != null && c.moveToFirst()) { - if(imgIndex == 0) { - previewBtn.setVisibility(View.VISIBLE); + if (i.getData() != null) { + Cursor c = getContentResolver().query(i.getData(), mode, null, null, null); + if (c != null && c.moveToFirst()) { + if (imgIndex == 0) { + previewBtn.setVisibility(View.VISIBLE); + } + if (imgIndex < 4) { + int index = c.getColumnIndex(mode[0]); + mediaPath.add(c.getString(index)); + String count = Integer.toString(++imgIndex); + imgCount.setText(count); + } + if (imgIndex == 4) { + imageButton.setVisibility(View.INVISIBLE); + } + c.close(); } - if(imgIndex < 4) { - int index = c.getColumnIndex(mode[0]); - mediaPath.add(c.getString(index)); - String count = Integer.toString(++imgIndex); - imgCount.setText(count); - } - if(imgIndex == 4) { - imageButton.setVisibility(View.INVISIBLE); - } - c.close(); } } } - @Override - public void onClick(DialogInterface d, int id) { - if(sendTweet != null) - sendTweet.cancel(true); - finish(); - } @Override public void onClick(View v) { @@ -139,11 +132,19 @@ public class TweetPopup extends AppCompatActivity implements OnClickListener, private void showClosingMsg() { if( !addition.equals(tweet.getText().toString()) || imgIndex > 0) { - AlertDialog.Builder alerta = new AlertDialog.Builder(this); - alerta.setMessage(R.string.should_cancel_tweet); - alerta.setPositiveButton(R.string.yes_confirm,this); - alerta.setNegativeButton(R.string.no_confirm,null); - alerta.show(); + AlertDialog.Builder closeDialog = new AlertDialog.Builder(this); + closeDialog.setMessage(R.string.should_cancel_tweet); + closeDialog.setNegativeButton(R.string.no_confirm, null); + closeDialog.setPositiveButton(R.string.yes_confirm, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + if (sendTweet != null && !sendTweet.isCancelled()) + sendTweet.cancel(true); + finish(); + } + }); + + closeDialog.show(); } else { finish(); } diff --git a/app/src/main/java/org/nuclearfog/twidda/window/UserDetail.java b/app/src/main/java/org/nuclearfog/twidda/window/UserDetail.java index 6386bc17..aae363de 100644 --- a/app/src/main/java/org/nuclearfog/twidda/window/UserDetail.java +++ b/app/src/main/java/org/nuclearfog/twidda/window/UserDetail.java @@ -22,7 +22,7 @@ public class UserDetail extends AppCompatActivity implements OnItemClicked { private RecyclerView userList; private UserLists uList; - private long mode = -1; + private int mode = -1; private long userID = 0; private long tweetID = 0; @@ -32,7 +32,7 @@ public class UserDetail extends AppCompatActivity implements OnItemClicked { b = getIntent().getExtras(); if (b != null) { userID = b.getLong("userID"); - mode = b.getLong("mode"); + mode = b.getInt("mode"); if (b.containsKey("tweetID")) tweetID = b.getLong("tweetID"); } @@ -49,6 +49,13 @@ public class UserDetail extends AppCompatActivity implements OnItemClicked { getUsers(); } + @Override + protected void onDestroy() { + if (uList != null && !uList.isCancelled()) + uList.cancel(true); + super.onDestroy(); + } + @Override public boolean onCreateOptionsMenu( Menu m ) { getMenuInflater().inflate(R.menu.user, m); @@ -70,6 +77,7 @@ public class UserDetail extends AppCompatActivity implements OnItemClicked { TwitterUser user = uAdp.getData().get(position); long userID = user.userID; String username = user.screenname; + Intent intent = new Intent(this, UserProfile.class); intent.putExtra("userID", userID); intent.putExtra("username", username); @@ -78,24 +86,32 @@ public class UserDetail extends AppCompatActivity implements OnItemClicked { private void getUsers() { uList = new UserLists(UserDetail.this); - String title = ""; + int titleId = 0; - if (mode == 0L) { - title = getString(R.string.following); - uList.execute(userID, UserLists.FOLLOWING, -1L); - } else if (mode == 1L) { - title = getString(R.string.follower); - uList.execute(userID, UserLists.FOLLOWERS, -1L); - } else if (mode == 2L) { - title = getString(R.string.retweet); - uList.execute(tweetID, UserLists.RETWEETER, -1L); - } else if (mode == 3L) { - title = getString(R.string.favorite); - uList.execute(tweetID, UserLists.FAVORISER, -1L); + switch (mode) { + case 0: + titleId = R.string.following; + uList.execute(userID, UserLists.FOLLOWING, -1L); + break; + + case 1: + titleId = R.string.follower; + uList.execute(userID, UserLists.FOLLOWERS, -1L); + break; + + case 2: + titleId = R.string.retweet; + uList.execute(tweetID, UserLists.RETWEETER, -1L); + break; + + case 3: + titleId = R.string.favorite; + uList.execute(tweetID, UserLists.FAVORISER, -1L); + break; } if (getSupportActionBar() != null) { - getSupportActionBar().setTitle(title); + getSupportActionBar().setTitle(titleId); } } } \ No newline at end of file diff --git a/app/src/main/java/org/nuclearfog/twidda/window/UserProfile.java b/app/src/main/java/org/nuclearfog/twidda/window/UserProfile.java index e3e4abda..a5942369 100644 --- a/app/src/main/java/org/nuclearfog/twidda/window/UserProfile.java +++ b/app/src/main/java/org/nuclearfog/twidda/window/UserProfile.java @@ -155,10 +155,10 @@ public class UserProfile extends AppCompatActivity implements OnClickListener, public void onClick(View v) { switch(v.getId()) { case R.id.following: - getConnection(0L); + getConnection(0); break; case R.id.follower: - getConnection(1L); + getConnection(1); break; } } @@ -258,7 +258,7 @@ public class UserProfile extends AppCompatActivity implements OnClickListener, } - private void getConnection(long mode) { + private void getConnection(int mode) { Intent intent = new Intent(this, UserDetail.class); intent.putExtra("userID", userId); intent.putExtra("mode", mode);