diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2015-06-18 02:01:10 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2015-06-18 02:01:19 +0900 |
commit | 4fe9ae1292d2b80cde0784b331460355a0fd0f00 (patch) | |
tree | 6d2e1dbf72758a87b9d8e3b07a5bbeb9cc557502 | |
parent | 04d2ae52f87003fcf4659fcce4cc95d2303e2984 (diff) | |
download | SmileEssence-4fe9ae1292d2b80cde0784b331460355a0fd0f00.tar.gz |
StatusViewModel: cancel last embedded status(es)'s tryGetStatus task (rewrite required)
-rw-r--r-- | app/src/main/java/net/lacolaco/smileessence/twitter/util/TwitterUtils.java | 12 | ||||
-rw-r--r-- | app/src/main/java/net/lacolaco/smileessence/viewmodel/StatusViewModel.java | 14 |
2 files changed, 19 insertions, 7 deletions
diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/util/TwitterUtils.java b/app/src/main/java/net/lacolaco/smileessence/twitter/util/TwitterUtils.java index 95d06575..764c12a7 100644 --- a/app/src/main/java/net/lacolaco/smileessence/twitter/util/TwitterUtils.java +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/util/TwitterUtils.java @@ -39,6 +39,7 @@ import net.lacolaco.smileessence.twitter.task.ShowDirectMessageTask; import net.lacolaco.smileessence.twitter.task.ShowStatusTask; import net.lacolaco.smileessence.twitter.task.ShowUserTask; +import net.lacolaco.smileessence.twitter.task.TwitterTask; import twitter4j.*; import java.util.ArrayList; @@ -61,15 +62,15 @@ public class TwitterUtils { /** * Get status from api if not cached */ - public static void tryGetStatus(Account account, long statusID, final StatusCallback callback) { + public static TwitterTask tryGetStatus(Account account, long statusID, final StatusCallback callback) { Status status = StatusCache.getInstance().get(statusID); + ShowStatusTask task; if (status != null) { callback.success(status); //update cache - ShowStatusTask task = new ShowStatusTask(new TwitterApi(account).getTwitter(), statusID); - task.execute(); + task = new ShowStatusTask(new TwitterApi(account).getTwitter(), statusID); } else { - ShowStatusTask task = new ShowStatusTask(new TwitterApi(account).getTwitter(), statusID) { + task = new ShowStatusTask(new TwitterApi(account).getTwitter(), statusID) { @Override protected void onPostExecute(twitter4j.Status status) { super.onPostExecute(status); @@ -80,8 +81,9 @@ public class TwitterUtils { } } }; - task.execute(); } + task.execute(); + return task; } /** diff --git a/app/src/main/java/net/lacolaco/smileessence/viewmodel/StatusViewModel.java b/app/src/main/java/net/lacolaco/smileessence/viewmodel/StatusViewModel.java index 44dc4c29..b945b088 100644 --- a/app/src/main/java/net/lacolaco/smileessence/viewmodel/StatusViewModel.java +++ b/app/src/main/java/net/lacolaco/smileessence/viewmodel/StatusViewModel.java @@ -26,6 +26,7 @@ package net.lacolaco.smileessence.viewmodel; import android.app.Activity; import android.net.Uri; +import android.os.AsyncTask; import android.text.Html; import android.view.LayoutInflater; import android.view.View; @@ -81,6 +82,8 @@ public class StatusViewModel implements IViewModel { private boolean isMention; private boolean isRetweetOfMe; + private ArrayList<AsyncTask> lastTasks; // internal + // --------------------------- CONSTRUCTORS --------------------------- public StatusViewModel(Status status, Account account) { @@ -106,6 +109,8 @@ public class StatusViewModel implements IViewModel { setMention(isMention(account.screenName)); setMyStatus(isMyStatus(account.userID)); setRetweetOfMe(isRetweetOfMe(account.userID)); + + lastTasks = new ArrayList<>(); } // --------------------- GETTER / SETTER METHODS --------------------- @@ -288,6 +293,11 @@ public class StatusViewModel implements IViewModel { // -------------------------- OTHER METHODS -------------------------- public View getView(final Activity activity, final LayoutInflater inflater, View convertedView, boolean extendStatusURL) { + for (AsyncTask task : lastTasks) { + task.cancel(true); + } + lastTasks.clear(); + if (convertedView == null) { convertedView = inflater.inflate(R.layout.list_item_status, null); } @@ -351,7 +361,7 @@ public class StatusViewModel implements IViewModel { embeddedStatus.setVisibility(View.VISIBLE); final Account account = ((MainActivity) activity).getCurrentAccount(); for (long id : embeddedStatusIDs) { - TwitterUtils.tryGetStatus(account, id, new TwitterUtils.StatusCallback() { + AsyncTask task = TwitterUtils.tryGetStatus(account, id, new TwitterUtils.StatusCallback() { @Override public void success(Status status) { StatusViewModel viewModel = new StatusViewModel(status, account); @@ -362,9 +372,9 @@ public class StatusViewModel implements IViewModel { @Override public void error() { - } }); + lastTasks.add(task); } } |