diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2015-09-13 19:45:46 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2015-09-13 19:45:46 +0900 |
commit | 32196e2761b7219634f0b502268145b902f764f5 (patch) | |
tree | 7458bfd37e354c79f6c3d00c00c25f7d30dcfe6b /app/src/main/java/net/lacolaco/smileessence | |
parent | d010a1af8b35c18f99d07c41a6b6a78c67aa1cdd (diff) | |
download | SmileEssence-32196e2761b7219634f0b502268145b902f764f5.tar.gz |
Adapter のインスタンスを MainActivity ではなく個々の Fragment に持たせた
Diffstat (limited to 'app/src/main/java/net/lacolaco/smileessence')
33 files changed, 362 insertions, 404 deletions
diff --git a/app/src/main/java/net/lacolaco/smileessence/IntentRouter.java b/app/src/main/java/net/lacolaco/smileessence/IntentRouter.java index 28a51a21..befa8c4e 100644 --- a/app/src/main/java/net/lacolaco/smileessence/IntentRouter.java +++ b/app/src/main/java/net/lacolaco/smileessence/IntentRouter.java @@ -37,10 +37,9 @@ import net.lacolaco.smileessence.notification.NotificationType; import net.lacolaco.smileessence.notification.Notificator; import net.lacolaco.smileessence.twitter.util.TwitterUtils; import net.lacolaco.smileessence.util.UIHandler; -import net.lacolaco.smileessence.view.adapter.PostState; +import net.lacolaco.smileessence.data.PostState; import net.lacolaco.smileessence.view.DialogHelper; import net.lacolaco.smileessence.view.dialog.StatusDetailDialogFragment; -import org.w3c.dom.Text; import java.util.regex.Matcher; import java.util.regex.Pattern; diff --git a/app/src/main/java/net/lacolaco/smileessence/activity/MainActivity.java b/app/src/main/java/net/lacolaco/smileessence/activity/MainActivity.java index de0aa7ce..50230313 100644 --- a/app/src/main/java/net/lacolaco/smileessence/activity/MainActivity.java +++ b/app/src/main/java/net/lacolaco/smileessence/activity/MainActivity.java @@ -43,6 +43,7 @@ import net.lacolaco.smileessence.Application; import net.lacolaco.smileessence.BuildConfig; import net.lacolaco.smileessence.IntentRouter; import net.lacolaco.smileessence.R; +import net.lacolaco.smileessence.data.PostState; import net.lacolaco.smileessence.data.UserListCache; import net.lacolaco.smileessence.entity.*; import net.lacolaco.smileessence.entity.DirectMessage; @@ -72,19 +73,6 @@ public class MainActivity extends Activity { // ------------------------------ FIELDS ------------------------------ - public enum AdapterID { - Home, - Mentions, - History, - Messages, - Search, - UserList; - - public static AdapterID get(int ordinal) { - return values()[ordinal]; - } - } - public static final int REQUEST_OAUTH = 10; public static final int REQUEST_GET_PICTURE_FROM_GALLERY = 11; public static final int REQUEST_GET_PICTURE_FROM_CAMERA = 12; @@ -95,7 +83,6 @@ public class MainActivity extends Activity { private PageListAdapter pagerAdapter; private Account currentAccount; private TwitterStream stream; - private HashMap<AdapterID, CustomListAdapter<?>> adapterMap = new HashMap<>(); private boolean streaming = false; private Uri cameraTempFilePath; @@ -121,7 +108,7 @@ public class MainActivity extends Activity { this.currentAccount = account; } - private String getLastSearch() { + public String getLastSearch() { return getAppPreferenceHelper().getValue(KEY_LAST_USED_SEARCH_QUERY, ""); } @@ -142,14 +129,10 @@ public class MainActivity extends Activity { getAppPreferenceHelper().putValue(KEY_LAST_USED_ACCOUNT_ID, account.getId()); } - private String getLastUserList() { + public String getLastUserList() { return getAppPreferenceHelper().getValue(KEY_LAST_USER_LIST, ""); } - public Collection<CustomListAdapter<?>> getListAdapters() { - return adapterMap.values(); - } - public int getThemeIndex() { return ((Application) getApplication()).getThemeIndex(); } @@ -303,28 +286,10 @@ public class MainActivity extends Activity { // -------------------------- OTHER METHODS -------------------------- - @Deprecated // visible is 何 - public void addListPage(Class<? extends CustomListFragment> fragmentClass, String name, CustomListAdapter<?> adapter, AdapterID adapterId, boolean visible) { - if (visible) { - Bundle args = new Bundle(); - adapterMap.put(adapterId, adapter); - addPage(fragmentClass, name, args, false); - } - } - - @Deprecated - public void addPage(Class<? extends PageFragment> fragmentClass, String name, Bundle args, boolean withNotify) { - pagerAdapter.addPage(fragmentClass, name, args, withNotify); - } - public void forceFinish() { super.finish(); } - public CustomListAdapter<?> getListAdapter(AdapterID adapterId) { - return adapterMap.get(adapterId); - } - public void openHomePage() { setSelectedPageIndex(pagerAdapter.getIndex(HomeFragment.class)); } @@ -360,13 +325,19 @@ public class MainActivity extends Activity { * Open search page with given query */ public void openSearchPage(final String query) { - startNewSearch(TwitterApi.getTwitter(getCurrentAccount()), query); - openSearchPage(); + SearchFragment fragment = pagerAdapter.getFragment(SearchFragment.class); + if (fragment != null) { + fragment.startSearch(TwitterApi.getTwitter(getCurrentAccount()), query); + openSearchPage(); + } } public void openUserListPage(String listFullName) { - startUserList(TwitterApi.getTwitter(getCurrentAccount()), listFullName); - openUserListPage(); + UserListFragment fragment = pagerAdapter.getFragment(UserListFragment.class); + if (fragment != null) { + fragment.startUserList(TwitterApi.getTwitter(getCurrentAccount()), listFullName); + openUserListPage(); + } } public void saveLastUserList(String lastUserList) { @@ -383,44 +354,11 @@ public class MainActivity extends Activity { } public void startMainLogic() { - initializeView(); CommandSetting.initialize(); + initializeView(); startTwitter(); } - public void startNewSearch(final Twitter twitter, final String queryString) { - setLastSearch(queryString); - if (!TextUtils.isEmpty(queryString)) { - final SearchListAdapter adapter = (SearchListAdapter) getListAdapter(AdapterID.Search); - adapter.initSearch(queryString); - adapter.clear(); - adapter.updateForce(); - final Query query = new Query(); - query.setQuery(queryString); - query.setCount(TwitterUtils.getPagingCount(this)); - query.setResultType(Query.RECENT); - new SearchTask(twitter, query) { - @Override - protected void onPostExecute(QueryResult queryResult) { - super.onPostExecute(queryResult); - if (queryResult != null) { - List<twitter4j.Status> tweets = queryResult.getTweets(); - for (int i = tweets.size() - 1; i >= 0; i--) { - twitter4j.Status status = tweets.get(i); - if (!status.isRetweet()) { - StatusViewModel viewModel = new StatusViewModel(Tweet.fromTwitter(status)); - adapter.addToTop(viewModel); - StatusFilter.filter(MainActivity.this, viewModel); - } - } - adapter.setTopID(queryResult.getMaxId()); - adapter.updateForce(); - } - } - }.execute(); - } - } - public boolean startStream() { if (!new NetworkHelper(this).canConnect()) { return false; @@ -442,14 +380,8 @@ public class MainActivity extends Activity { } int count = TwitterUtils.getPagingCount(this); Twitter twitter = TwitterApi.getTwitter(currentAccount); - Paging paging = TwitterUtils.getPaging(count); initInvisibleUser(twitter); initUserListCache(twitter); - initHome(twitter, paging); - initMentions(twitter, paging); - initMessages(twitter, paging); - initSearch(twitter); - initUserList(twitter); updateActionBarIcon(); return true; } @@ -471,44 +403,6 @@ public class MainActivity extends Activity { userTask.execute(); } - private void addPostPage() { - addPage(PostFragment.class, getString(R.string.page_name_post), null, true); - } - - private void addHomePage() { - StatusListAdapter homeAdapter = new StatusListAdapter(this); - addListPage( HomeFragment.class, getString(R.string.page_name_home), homeAdapter, AdapterID.Home, true); - } - - private void addMentionsPage() { - StatusListAdapter mentionsAdapter = new StatusListAdapter(this); - addListPage(MentionsFragment.class, getString(R.string.page_name_mentions), mentionsAdapter, AdapterID.Mentions, true); - } - - private void addHistoryPage() { - boolean visible = getUserPreferenceHelper().getValue(R.string.key_page_history_visibility, true); - EventListAdapter historyAdapter = new EventListAdapter(this); - addListPage(HistoryFragment.class, getString(R.string.page_name_history), historyAdapter, AdapterID.History, visible); - } - - private void addMessagesPage() { - boolean visible = getUserPreferenceHelper().getValue(R.string.key_page_messages_visibility, true); - MessageListAdapter messagesAdapter = new MessageListAdapter(this); - addListPage(MessagesFragment.class, getString(R.string.page_name_messages), messagesAdapter, AdapterID.Messages, visible); - } - - private void addSearchPage() { - boolean visible = getUserPreferenceHelper().getValue(R.string.key_page_search_visibility, true); - SearchListAdapter searchAdapter = new SearchListAdapter(this); - addListPage(SearchFragment.class, getString(R.string.page_name_search), searchAdapter, AdapterID.Search, visible); - } - - private void addUserListPage() { - boolean visible = getUserPreferenceHelper().getValue(R.string.key_page_list_visibility, true); - UserListListAdapter userListAdapter = new UserListListAdapter(this); - addListPage(UserListFragment.class, getString(R.string.page_name_list), userListAdapter, AdapterID.UserList, visible); - } - private void getImageUri(int requestCode, int resultCode, Intent data) { if (resultCode != RESULT_OK) { Logger.error(requestCode); @@ -525,20 +419,9 @@ public class MainActivity extends Activity { openPostPageWithImage(uri); } - private void initHome(final Twitter twitter, final Paging paging) { - new HomeTimelineTask(twitter, paging) { - @Override - protected void onPostExecute(List<Tweet> tweets) { - super.onPostExecute(tweets); - StatusListAdapter adapter = (StatusListAdapter) getListAdapter(AdapterID.Home); - for (Tweet tweet : tweets) { - StatusViewModel statusViewModel = new StatusViewModel(tweet); - adapter.addToBottom(statusViewModel); - StatusFilter.filter(MainActivity.this, statusViewModel); - } - adapter.updateForce(); - } - }.execute(); + @Deprecated + public void addPage(Class<? extends PageFragment> fragmentClass, String name, Bundle args, boolean withNotify) { + pagerAdapter.addPage(fragmentClass, name, args, withNotify); } private void initInvisibleUser(Twitter twitter) { @@ -546,85 +429,27 @@ public class MainActivity extends Activity { new MutesIDsTask(twitter).execute(); } - private void initMentions(final Twitter twitter, final Paging paging) { - new MentionsTimelineTask(twitter, paging) { - @Override - protected void onPostExecute(List<Tweet> tweets) { - super.onPostExecute(tweets); - StatusListAdapter adapter = (StatusListAdapter) getListAdapter(AdapterID.Mentions); - for (Tweet tweet : tweets) { - adapter.addToBottom(new StatusViewModel(tweet)); - } - adapter.updateForce(); - } - }.execute(); - } - - private void initMessages(final Twitter twitter, final Paging paging) { - if (pagerAdapter.getIndex(MessagesFragment.class) == -1) { - return; - } - new DirectMessagesTask(twitter, paging) { - @Override - protected void onPostExecute(List<DirectMessage> directMessages) { - super.onPostExecute(directMessages); - MessageListAdapter adapter = (MessageListAdapter) getListAdapter(AdapterID.Messages); - for (DirectMessage message : directMessages) { - adapter.addToBottom(new MessageViewModel(message)); - } - adapter.notifyDataSetChanged(); - } - }.execute(); - new SentDirectMessagesTask(twitter, paging) { - @Override - protected void onPostExecute(List<DirectMessage> directMessages) { - super.onPostExecute(directMessages); - MessageListAdapter adapter = (MessageListAdapter) getListAdapter(AdapterID.Messages); - for (DirectMessage message : directMessages) { - adapter.addToBottom(new MessageViewModel(message)); - } - adapter.notifyDataSetChanged(); - } - }.execute(); - } - private void initPostState() { PostState.newState().beginTransaction().commit(); } - private void initSearch(Twitter twitter) { - if (pagerAdapter.getIndex(SearchFragment.class) == -1) { - return; - } - String lastUsedSearchQuery = getLastSearch(); - if (!TextUtils.isEmpty(lastUsedSearchQuery)) { - startNewSearch(twitter, lastUsedSearchQuery); - } - } - - private void initUserList(Twitter twitter) { - if (pagerAdapter.getIndex(UserListFragment.class) == -1) { - return; - } - String lastUserList = getLastUserList(); - if (!TextUtils.isEmpty(lastUserList)) { - startUserList(twitter, lastUserList); - } - } - private void initUserListCache(Twitter twitter) { UserListCache.getInstance().clear(); new GetUserListsTask(twitter).execute(); } private void initializePages() { - addPostPage(); - addHomePage(); - addMentionsPage(); - addHistoryPage(); - addMessagesPage(); - addSearchPage(); - addUserListPage(); + addPage(PostFragment.class, getString(R.string.page_name_post), null, false); + addPage(HomeFragment.class, getString(R.string.page_name_home), null, false); + addPage(MentionsFragment.class, getString(R.string.page_name_mentions), null, false); + if (getUserPreferenceHelper().getValue(R.string.key_page_history_visibility, true)) + addPage(HistoryFragment.class, getString(R.string.page_name_history), null, false); + if (getUserPreferenceHelper().getValue(R.string.key_page_messages_visibility, true)) + addPage(MessagesFragment.class, getString(R.string.page_name_messages), null, false); + if (getUserPreferenceHelper().getValue(R.string.key_page_search_visibility, true)) + addPage(SearchFragment.class, getString(R.string.page_name_search), null, false); + if (getUserPreferenceHelper().getValue(R.string.key_page_list_visibility, true)) + addPage(UserListFragment.class, getString(R.string.page_name_list), null, false); pagerAdapter.notifyDataSetChanged(); viewPager.setOffscreenPageLimit(pagerAdapter.getCount()); initPostState(); @@ -676,23 +501,4 @@ public class MainActivity extends Activity { startActivityForResult(new Intent(this, OAuthActivity.class), REQUEST_OAUTH); } - private void startUserList(Twitter twitter, String listFullName) { - saveLastUserList(listFullName); - final UserListListAdapter adapter = (UserListListAdapter) getListAdapter(AdapterID.UserList); - adapter.setListFullName(listFullName); - adapter.clear(); - adapter.updateForce(); - new UserListStatusesTask(twitter, this, listFullName) { - @Override - protected void onPostExecute(List<Tweet> tweets) { - super.onPostExecute(tweets); - for (Tweet tweet : tweets) { - StatusViewModel statusViewModel = new StatusViewModel(tweet); - adapter.addToBottom(statusViewModel); - StatusFilter.filter(MainActivity.this, statusViewModel); - } - adapter.updateForce(); - } - }.execute(); - } } diff --git a/app/src/main/java/net/lacolaco/smileessence/command/CommandPasteToPost.java b/app/src/main/java/net/lacolaco/smileessence/command/CommandPasteToPost.java index 34bc227a..75d4b6c8 100644 --- a/app/src/main/java/net/lacolaco/smileessence/command/CommandPasteToPost.java +++ b/app/src/main/java/net/lacolaco/smileessence/command/CommandPasteToPost.java @@ -28,7 +28,7 @@ import android.app.Activity; import net.lacolaco.smileessence.R; import net.lacolaco.smileessence.activity.MainActivity; -import net.lacolaco.smileessence.view.adapter.PostState; +import net.lacolaco.smileessence.data.PostState; public class CommandPasteToPost extends Command { diff --git a/app/src/main/java/net/lacolaco/smileessence/command/post/PostCommand.java b/app/src/main/java/net/lacolaco/smileessence/command/post/PostCommand.java index 56f648ff..f7e13995 100644 --- a/app/src/main/java/net/lacolaco/smileessence/command/post/PostCommand.java +++ b/app/src/main/java/net/lacolaco/smileessence/command/post/PostCommand.java @@ -27,7 +27,7 @@ package net.lacolaco.smileessence.command.post; import android.app.Activity; import net.lacolaco.smileessence.command.Command; -import net.lacolaco.smileessence.view.adapter.PostState; +import net.lacolaco.smileessence.data.PostState; public abstract class PostCommand extends Command { diff --git a/app/src/main/java/net/lacolaco/smileessence/command/post/PostCommandMakeAnonymous.java b/app/src/main/java/net/lacolaco/smileessence/command/post/PostCommandMakeAnonymous.java index 5ac98a1b..802827a7 100644 --- a/app/src/main/java/net/lacolaco/smileessence/command/post/PostCommandMakeAnonymous.java +++ b/app/src/main/java/net/lacolaco/smileessence/command/post/PostCommandMakeAnonymous.java @@ -28,7 +28,7 @@ import android.app.Activity; import android.text.TextUtils; import net.lacolaco.smileessence.R; -import net.lacolaco.smileessence.view.adapter.PostState; +import net.lacolaco.smileessence.data.PostState; public class PostCommandMakeAnonymous extends PostCommand { diff --git a/app/src/main/java/net/lacolaco/smileessence/command/post/PostCommandMorse.java b/app/src/main/java/net/lacolaco/smileessence/command/post/PostCommandMorse.java index a03f6684..64a3b35f 100644 --- a/app/src/main/java/net/lacolaco/smileessence/command/post/PostCommandMorse.java +++ b/app/src/main/java/net/lacolaco/smileessence/command/post/PostCommandMorse.java @@ -29,7 +29,7 @@ import android.text.TextUtils; import net.lacolaco.smileessence.R; import net.lacolaco.smileessence.util.Morse; -import net.lacolaco.smileessence.view.adapter.PostState; +import net.lacolaco.smileessence.data.PostState; public class PostCommandMorse extends PostCommand { diff --git a/app/src/main/java/net/lacolaco/smileessence/command/post/PostCommandZekamashi.java b/app/src/main/java/net/lacolaco/smileessence/command/post/PostCommandZekamashi.java index 323f2e28..5c8955c4 100644 --- a/app/src/main/java/net/lacolaco/smileessence/command/post/PostCommandZekamashi.java +++ b/app/src/main/java/net/lacolaco/smileessence/command/post/PostCommandZekamashi.java @@ -28,7 +28,7 @@ import android.app.Activity; import android.text.TextUtils; import net.lacolaco.smileessence.R; -import net.lacolaco.smileessence.view.adapter.PostState; +import net.lacolaco.smileessence.data.PostState; public class PostCommandZekamashi extends PostCommand { diff --git a/app/src/main/java/net/lacolaco/smileessence/command/status/StatusCommandAddToReply.java b/app/src/main/java/net/lacolaco/smileessence/command/status/StatusCommandAddToReply.java index 39c1b80e..a4f08ebd 100644 --- a/app/src/main/java/net/lacolaco/smileessence/command/status/StatusCommandAddToReply.java +++ b/app/src/main/java/net/lacolaco/smileessence/command/status/StatusCommandAddToReply.java @@ -29,9 +29,7 @@ import android.app.Activity; import net.lacolaco.smileessence.R; import net.lacolaco.smileessence.entity.Tweet; import net.lacolaco.smileessence.notification.Notificator; -import net.lacolaco.smileessence.view.adapter.PostState; - -import twitter4j.Status; +import net.lacolaco.smileessence.data.PostState; public class StatusCommandAddToReply extends StatusCommand { diff --git a/app/src/main/java/net/lacolaco/smileessence/command/status/StatusCommandCopy.java b/app/src/main/java/net/lacolaco/smileessence/command/status/StatusCommandCopy.java index 64c220b0..463adf1a 100644 --- a/app/src/main/java/net/lacolaco/smileessence/command/status/StatusCommandCopy.java +++ b/app/src/main/java/net/lacolaco/smileessence/command/status/StatusCommandCopy.java @@ -29,9 +29,7 @@ import android.app.Activity; import net.lacolaco.smileessence.R; import net.lacolaco.smileessence.activity.MainActivity; import net.lacolaco.smileessence.entity.Tweet; -import net.lacolaco.smileessence.view.adapter.PostState; - -import twitter4j.Status; +import net.lacolaco.smileessence.data.PostState; public class StatusCommandCopy extends StatusCommand { diff --git a/app/src/main/java/net/lacolaco/smileessence/command/status/StatusCommandTextQuote.java b/app/src/main/java/net/lacolaco/smileessence/command/status/StatusCommandTextQuote.java index 3c320066..534e7e1a 100644 --- a/app/src/main/java/net/lacolaco/smileessence/command/status/StatusCommandTextQuote.java +++ b/app/src/main/java/net/lacolaco/smileessence/command/status/StatusCommandTextQuote.java @@ -30,9 +30,7 @@ import net.lacolaco.smileessence.R; import net.lacolaco.smileessence.activity.MainActivity; import net.lacolaco.smileessence.entity.Tweet; import net.lacolaco.smileessence.twitter.TweetBuilder; -import net.lacolaco.smileessence.view.adapter.PostState; - -import twitter4j.Status; +import net.lacolaco.smileessence.data.PostState; public class StatusCommandTextQuote extends StatusCommand { diff --git a/app/src/main/java/net/lacolaco/smileessence/command/status/StatusCommandURLQuote.java b/app/src/main/java/net/lacolaco/smileessence/command/status/StatusCommandURLQuote.java index 29a5ec38..46275e23 100644 --- a/app/src/main/java/net/lacolaco/smileessence/command/status/StatusCommandURLQuote.java +++ b/app/src/main/java/net/lacolaco/smileessence/command/status/StatusCommandURLQuote.java @@ -29,10 +29,7 @@ import android.app.Activity; import net.lacolaco.smileessence.R; import net.lacolaco.smileessence.activity.MainActivity; import net.lacolaco.smileessence.entity.Tweet; -import net.lacolaco.smileessence.twitter.util.TwitterUtils; -import net.lacolaco.smileessence.view.adapter.PostState; - -import twitter4j.Status; +import net.lacolaco.smileessence.data.PostState; public class StatusCommandURLQuote extends StatusCommand { diff --git a/app/src/main/java/net/lacolaco/smileessence/command/user/UserCommandAddToReply.java b/app/src/main/java/net/lacolaco/smileessence/command/user/UserCommandAddToReply.java index fa3ba714..537e9948 100644 --- a/app/src/main/java/net/lacolaco/smileessence/command/user/UserCommandAddToReply.java +++ b/app/src/main/java/net/lacolaco/smileessence/command/user/UserCommandAddToReply.java @@ -28,7 +28,7 @@ import android.app.Activity; import net.lacolaco.smileessence.R; import net.lacolaco.smileessence.notification.Notificator; -import net.lacolaco.smileessence.view.adapter.PostState; +import net.lacolaco.smileessence.data.PostState; import net.lacolaco.smileessence.entity.User; diff --git a/app/src/main/java/net/lacolaco/smileessence/command/user/UserCommandIntroduce.java b/app/src/main/java/net/lacolaco/smileessence/command/user/UserCommandIntroduce.java index 566f3687..e9d27c2e 100644 --- a/app/src/main/java/net/lacolaco/smileessence/command/user/UserCommandIntroduce.java +++ b/app/src/main/java/net/lacolaco/smileessence/command/user/UserCommandIntroduce.java @@ -28,7 +28,7 @@ import android.app.Activity; import net.lacolaco.smileessence.R; import net.lacolaco.smileessence.activity.MainActivity; -import net.lacolaco.smileessence.view.adapter.PostState; +import net.lacolaco.smileessence.data.PostState; import net.lacolaco.smileessence.entity.User; diff --git a/app/src/main/java/net/lacolaco/smileessence/command/user/UserCommandReply.java b/app/src/main/java/net/lacolaco/smileessence/command/user/UserCommandReply.java index 6c9cae7c..46124f82 100644 --- a/app/src/main/java/net/lacolaco/smileessence/command/user/UserCommandReply.java +++ b/app/src/main/java/net/lacolaco/smileessence/command/user/UserCommandReply.java @@ -29,7 +29,7 @@ import android.app.Activity; import net.lacolaco.smileessence.R; import net.lacolaco.smileessence.activity.MainActivity; import net.lacolaco.smileessence.twitter.TweetBuilder; -import net.lacolaco.smileessence.view.adapter.PostState; +import net.lacolaco.smileessence.data.PostState; import net.lacolaco.smileessence.entity.User; diff --git a/app/src/main/java/net/lacolaco/smileessence/view/adapter/PostState.java b/app/src/main/java/net/lacolaco/smileessence/data/PostState.java index 72399541..291070dd 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/adapter/PostState.java +++ b/app/src/main/java/net/lacolaco/smileessence/data/PostState.java @@ -22,7 +22,7 @@ * SOFTWARE. */ -package net.lacolaco.smileessence.view.adapter; +package net.lacolaco.smileessence.data; import net.lacolaco.smileessence.activity.MainActivity; import net.lacolaco.smileessence.view.DialogHelper; diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/StatusFilter.java b/app/src/main/java/net/lacolaco/smileessence/twitter/StatusFilter.java index 9c07fb6d..a4926ec1 100644 --- a/app/src/main/java/net/lacolaco/smileessence/twitter/StatusFilter.java +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/StatusFilter.java @@ -24,37 +24,46 @@ package net.lacolaco.smileessence.twitter; -import net.lacolaco.smileessence.activity.MainActivity; -import net.lacolaco.smileessence.entity.ExtractionWord; -import net.lacolaco.smileessence.view.adapter.StatusListAdapter; -import net.lacolaco.smileessence.viewmodel.StatusViewModel; +import net.lacolaco.smileessence.util.Consumer; +import net.lacolaco.smileessence.viewmodel.IViewModel; -import java.util.regex.Pattern; +import java.util.*; public class StatusFilter { + private Map<Class<?>, Map<Object, Consumer<?>>> handlers = new HashMap<>(); + private static StatusFilter instance = new StatusFilter(); - // -------------------------- STATIC METHODS -------------------------- - - public static void filter(MainActivity activity, StatusViewModel status) { - extract(activity, status); + public static StatusFilter getInstance() { + return instance; } - private static void extract(MainActivity activity, StatusViewModel status) { - if (status.getTweet().isRetweet()) { - return; + public synchronized <T> void register(Object key, Class<T> klass, Consumer<T> handler) { + Map<Object, Consumer<?>> map = handlers.get(klass); + if (map == null) { + map = new WeakHashMap<>(); + handlers.put(klass, map); } - Pattern pattern; - for (ExtractionWord word : ExtractionWord.getAll()) { - pattern = Pattern.compile(word.text); - if (pattern.matcher(status.getTweet().getText()).find()) { - addToMentions(activity, status); + map.put(key, handler); + } + + // -------------------------- STATIC METHODS -------------------------- + + public <T extends IViewModel> void filter(T status) { + Map<Object, Consumer<?>> map = handlers.get(status.getClass()); + if (map != null) { + for(Consumer f_ : map.values()) { + ((Consumer<T>) f_).accept(status); } } } - private static void addToMentions(MainActivity activity, StatusViewModel status) { - StatusListAdapter adapter = (StatusListAdapter) activity.getListAdapter(MainActivity.AdapterID.Mentions); - adapter.addToTop(status); - adapter.update(); - } + // public void remove(Class<? extends IViewModel> klass, long id) { + // Map<Object, Consumer<?>> map = handlers.get(klass); + // + // if (map != null) { + // for(Consumer f_ : map.values()) { + // ((Consumer<T>) f_).accept(status); + // } + // } + // } } diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/UserStreamListener.java b/app/src/main/java/net/lacolaco/smileessence/twitter/UserStreamListener.java index fd7455de..c4806a06 100644 --- a/app/src/main/java/net/lacolaco/smileessence/twitter/UserStreamListener.java +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/UserStreamListener.java @@ -89,7 +89,6 @@ public class UserStreamListener implements twitter4j.UserStreamListener, Connect public void onStatus(Status status) { Tweet tweet = Tweet.fromTwitter(status); StatusViewModel vm = new StatusViewModel(tweet); - addToHome(vm); if (tweet.isRetweet()) { //if (viewModel.isRetweetOfMe()) { // addToHistory(new EventViewModel(EnumEvent.RETWEETED, status.getUser(), status)); @@ -99,19 +98,20 @@ public class UserStreamListener implements twitter4j.UserStreamListener, Connect // EventViewModel mentioned = new EventViewModel(EnumEvent.MENTIONED, status.getUser(), status); // Notificator.getInstance().publish(mentioned.getFormattedString(activity)); //} - StatusFilter.filter(activity, vm); + StatusFilter.getInstance().filter(vm); FavoriteCache.getInstance().put(status); } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { - for (CustomListAdapter<?> adapter : activity.getListAdapters()) { - if (adapter != null && adapter instanceof StatusListAdapter) { - StatusListAdapter statusListAdapter = (StatusListAdapter) adapter; - statusListAdapter.removeByStatusID(statusDeletionNotice.getStatusId()); - statusListAdapter.updateForce(); - } - } + // TODO: StatusFilter.getInstance().remove(StatusViewModel.class, statusDeletionNotice.getStatusId()); + // for (CustomListAdapter<?> adapter : activity.getListAdapters()) { + // if (adapter != null && adapter instanceof StatusListAdapter) { + // StatusListAdapter statusListAdapter = (StatusListAdapter) adapter; + // statusListAdapter.removeByStatusID(statusDeletionNotice.getStatusId()); + // statusListAdapter.updateForce(); + // } + // } Tweet.remove(statusDeletionNotice.getStatusId()); } @@ -138,9 +138,10 @@ public class UserStreamListener implements twitter4j.UserStreamListener, Connect @Override public void onDeletionNotice(long directMessageId, long userId) { - MessageListAdapter messages = (MessageListAdapter) activity.getListAdapter(MainActivity.AdapterID.Messages); - messages.removeByMessageID(directMessageId); - messages.updateForce(); + // TODO: StatusFilter.getInstance().remove(MessageViewModel.class, directMessageId); + // MessageListAdapter messages = (MessageListAdapter) activity.getListAdapter(MainActivity.AdapterID.Messages); + // messages.removeByMessageID(directMessageId); + // messages.updateForce(); DirectMessage.remove(directMessageId); } @@ -156,8 +157,8 @@ public class UserStreamListener implements twitter4j.UserStreamListener, Connect } if (isMe(User.fromTwitter(source))) { FavoriteCache.getInstance().put(favoritedStatus, true); - activity.getListAdapter(MainActivity.AdapterID.Home).update(); - activity.getListAdapter(MainActivity.AdapterID.Mentions).update(); + // TODO: activity.getAdapter(MainActivity.AdapterID.Home).update(); + // TODO: activity.getAdapter(MainActivity.AdapterID.Mentions).update(); } } @@ -170,8 +171,8 @@ public class UserStreamListener implements twitter4j.UserStreamListener, Connect } if (isMe(User.fromTwitter(source))) { //FavoriteCache.getInstance().put(unfavoritedStatus, false); - activity.getListAdapter(MainActivity.AdapterID.Home).update(); - activity.getListAdapter(MainActivity.AdapterID.Mentions).update(); + // TODO: activity.getAdapter(MainActivity.AdapterID.Home).update(); + // TODO: activity.getAdapter(MainActivity.AdapterID.Mentions).update(); } } @@ -193,7 +194,7 @@ public class UserStreamListener implements twitter4j.UserStreamListener, Connect addToHistory(new EventViewModel(EnumEvent.RECEIVE_MESSAGE, User.fromTwitter(directMessage.getSender()))); } MessageViewModel vm = new MessageViewModel(message); - addToMessages(vm); + StatusFilter.getInstance().filter(vm); } @Override @@ -267,28 +268,8 @@ public class UserStreamListener implements twitter4j.UserStreamListener, Connect } private void addToHistory(EventViewModel mentioned) { - EventListAdapter history = (EventListAdapter) activity.getListAdapter(MainActivity.AdapterID.History); + StatusFilter.getInstance().filter(mentioned); Notificator.getInstance().publish(mentioned.getFormattedString(activity)); - history.addToTop(mentioned); - history.update(); - } - - private void addToHome(StatusViewModel viewModel) { - StatusListAdapter home = (StatusListAdapter) activity.getListAdapter(MainActivity.AdapterID.Home); - home.addToTop(viewModel); - home.update(); - } - - private void addToMentions(StatusViewModel viewModel) { - StatusListAdapter mentions = (StatusListAdapter) activity.getListAdapter(MainActivity.AdapterID.Mentions); - mentions.addToTop(viewModel); - mentions.update(); - } - - private void addToMessages(MessageViewModel message) { - MessageListAdapter messages = (MessageListAdapter) activity.getListAdapter(MainActivity.AdapterID.Messages); - messages.addToTop(message); - messages.update(); } private boolean isMe(User user) { diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/UserListStatusesTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/UserListStatusesTask.java index beb8890c..a25b7511 100644 --- a/app/src/main/java/net/lacolaco/smileessence/twitter/task/UserListStatusesTask.java +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/UserListStatusesTask.java @@ -44,11 +44,7 @@ public class UserListStatusesTask extends TwitterTask<List<Tweet>> { // --------------------------- CONSTRUCTORS --------------------------- - public UserListStatusesTask(Twitter twitter, MainActivity activity, String listFullName) { - this(twitter, activity, listFullName, TwitterUtils.getPaging(TwitterUtils.getPagingCount(activity))); - } - - public UserListStatusesTask(Twitter twitter, MainActivity activity, String listFullName, Paging paging) { + public UserListStatusesTask(Twitter twitter, String listFullName, Paging paging) { super(twitter); this.listFullName = listFullName; this.paging = paging; @@ -58,9 +54,6 @@ public class UserListStatusesTask extends TwitterTask<List<Tweet>> { @Override protected void onPostExecute(List<Tweet> tweets) { - for (Tweet tweet : tweets) { - // FavoriteCache.getInstance().put(tweet); - } } @Override diff --git a/app/src/main/java/net/lacolaco/smileessence/util/Consumer.java b/app/src/main/java/net/lacolaco/smileessence/util/Consumer.java new file mode 100644 index 00000000..a8712293 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/util/Consumer.java @@ -0,0 +1,5 @@ +package net.lacolaco.smileessence.util; + +public interface Consumer<T> { + void accept(T t); +} diff --git a/app/src/main/java/net/lacolaco/smileessence/view/CustomListFragment.java b/app/src/main/java/net/lacolaco/smileessence/view/CustomListFragment.java index e9eccabc..51dd6477 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/CustomListFragment.java +++ b/app/src/main/java/net/lacolaco/smileessence/view/CustomListFragment.java @@ -24,7 +24,6 @@ package net.lacolaco.smileessence.view; -import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -37,25 +36,17 @@ import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshListView; import net.lacolaco.smileessence.R; -import net.lacolaco.smileessence.activity.MainActivity; import net.lacolaco.smileessence.notification.Notificator; import net.lacolaco.smileessence.view.adapter.CustomListAdapter; -public abstract class CustomListFragment extends PageFragment implements AbsListView.OnScrollListener, +public abstract class CustomListFragment<T extends CustomListAdapter> extends PageFragment<T> implements AbsListView.OnScrollListener, PullToRefreshBase.OnRefreshListener2<ListView> { // ------------------------------ FIELDS ------------------------------ public static final int SCROLL_DURATION = 1500; - // --------------------- GETTER / SETTER METHODS --------------------- - protected abstract MainActivity.AdapterID getAdapterIndex(); - - protected CustomListAdapter<?> getListAdapter() { - return ((MainActivity) getActivity()).getListAdapter(getAdapterIndex()); - } - protected PullToRefreshBase.Mode getRefreshMode() { return PullToRefreshBase.Mode.DISABLED; } @@ -79,7 +70,7 @@ public abstract class CustomListFragment extends PageFragment implements AbsList @Override public void onScrollStateChanged(AbsListView absListView, int scrollState) { - CustomListAdapter<?> adapter = getListAdapter(); + T adapter = getAdapter(); adapter.setNotifiable(false); if (absListView.getFirstVisiblePosition() == 0 && absListView.getChildAt(0) != null && absListView.getChildAt(0).getTop() == 0) { @@ -104,7 +95,7 @@ public abstract class CustomListFragment extends PageFragment implements AbsList public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View page = inflater.inflate(R.layout.fragment_list, container, false); PullToRefreshListView listView = getListView(page); - ListAdapter adapter = getListAdapter(); + ListAdapter adapter = getAdapter(); listView.setAdapter(adapter); listView.setOnScrollListener(this); listView.setOnRefreshListener(this); diff --git a/app/src/main/java/net/lacolaco/smileessence/view/HistoryFragment.java b/app/src/main/java/net/lacolaco/smileessence/view/HistoryFragment.java index a26832c1..6e6d96bd 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/HistoryFragment.java +++ b/app/src/main/java/net/lacolaco/smileessence/view/HistoryFragment.java @@ -24,31 +24,41 @@ package net.lacolaco.smileessence.view; +import android.os.Bundle; import android.widget.ListView; import com.handmark.pulltorefresh.library.PullToRefreshBase; import net.lacolaco.smileessence.R; import net.lacolaco.smileessence.activity.MainActivity; +import net.lacolaco.smileessence.twitter.StatusFilter; +import net.lacolaco.smileessence.view.adapter.EventListAdapter; +import net.lacolaco.smileessence.viewmodel.EventViewModel; /** * Fragment for notice history */ -public class HistoryFragment extends CustomListFragment { +public class HistoryFragment extends CustomListFragment<EventListAdapter> { // --------------------- GETTER / SETTER METHODS --------------------- @Override - protected MainActivity.AdapterID getAdapterIndex() { - return MainActivity.AdapterID.History; - } - - @Override protected PullToRefreshBase.Mode getRefreshMode() { return PullToRefreshBase.Mode.DISABLED; } // ------------------------ INTERFACE METHODS ------------------------ + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + EventListAdapter adapter = new EventListAdapter(getActivity()); + setAdapter(adapter); + + StatusFilter.getInstance().register(this, EventViewModel.class, (EventViewModel vm) -> { + adapter.addToTop(vm); + adapter.update(); + }); + } // --------------------- Interface OnRefreshListener2 --------------------- diff --git a/app/src/main/java/net/lacolaco/smileessence/view/HomeFragment.java b/app/src/main/java/net/lacolaco/smileessence/view/HomeFragment.java index 23b1339b..8f090869 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/HomeFragment.java +++ b/app/src/main/java/net/lacolaco/smileessence/view/HomeFragment.java @@ -24,11 +24,11 @@ package net.lacolaco.smileessence.view; +import android.os.Bundle; import android.widget.ListView; import com.handmark.pulltorefresh.library.PullToRefreshBase; -import net.lacolaco.smileessence.R; import net.lacolaco.smileessence.activity.MainActivity; import net.lacolaco.smileessence.entity.Account; import net.lacolaco.smileessence.entity.Tweet; @@ -46,33 +46,54 @@ import twitter4j.Twitter; import java.util.List; import java.util.ListIterator; -public class HomeFragment extends CustomListFragment { +public class HomeFragment extends CustomListFragment<StatusListAdapter> { // --------------------- GETTER / SETTER METHODS --------------------- @Override - protected MainActivity.AdapterID getAdapterIndex() { - return MainActivity.AdapterID.Home; - } - - @Override protected PullToRefreshBase.Mode getRefreshMode() { return PullToRefreshBase.Mode.BOTH; } // ------------------------ INTERFACE METHODS ------------------------ + @Override // onCreate って Fragment のインスタンスが作られるときは必ず呼ばれるって認識でいいんだよね? + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + StatusListAdapter adapter = new StatusListAdapter(getActivity()); + setAdapter(adapter); + + StatusFilter.getInstance().register(this, StatusViewModel.class, (StatusViewModel tweet) -> { + adapter.addToTop(tweet); + adapter.update(); + }); + final Twitter twitter = TwitterApi.getTwitter(((MainActivity) getActivity()).getCurrentAccount()); + final Paging paging = TwitterUtils.getPaging(TwitterUtils.getPagingCount((MainActivity) getActivity())); + new HomeTimelineTask(twitter, paging) { + @Override + protected void onPostExecute(List<Tweet> tweets) { + super.onPostExecute(tweets); + for (Tweet tweet : tweets) { + StatusViewModel statusViewModel = new StatusViewModel(tweet); + adapter.addToBottom(statusViewModel); + StatusFilter.getInstance().filter(statusViewModel); + } + adapter.updateForce(); + } + }.execute(); + } + // --------------------- Interface OnRefreshListener2 --------------------- @Override public void onPullDownToRefresh(final PullToRefreshBase<ListView> refreshView) { final MainActivity activity = (MainActivity) getActivity(); + final StatusListAdapter adapter = getAdapter(); if (activity.isStreaming()) { new UIHandler() { @Override public void run() { - StatusListAdapter adapter = (StatusListAdapter) getListAdapter(); updateListViewWithNotice(refreshView.getRefreshableView(), adapter, true); refreshView.onRefreshComplete(); } @@ -81,7 +102,6 @@ public class HomeFragment extends CustomListFragment { } final Account currentAccount = activity.getCurrentAccount(); Twitter twitter = TwitterApi.getTwitter(currentAccount); - final StatusListAdapter adapter = (StatusListAdapter) getListAdapter(); Paging paging = TwitterUtils.getPaging(TwitterUtils.getPagingCount(activity)); if (adapter.getCount() > 0) { paging.setSinceId(adapter.getTopID()); @@ -94,7 +114,7 @@ public class HomeFragment extends CustomListFragment { while (li.hasPrevious()) { StatusViewModel viewModel = new StatusViewModel(li.previous()); adapter.addToTop(viewModel); - StatusFilter.filter(activity, viewModel); + StatusFilter.getInstance().filter(viewModel); } updateListViewWithNotice(refreshView.getRefreshableView(), adapter, true); refreshView.onRefreshComplete(); @@ -105,9 +125,9 @@ public class HomeFragment extends CustomListFragment { @Override public void onPullUpToRefresh(final PullToRefreshBase<ListView> refreshView) { final MainActivity activity = (MainActivity) getActivity(); + final StatusListAdapter adapter = getAdapter(); final Account currentAccount = activity.getCurrentAccount(); Twitter twitter = TwitterApi.getTwitter(currentAccount); - final StatusListAdapter adapter = (StatusListAdapter) getListAdapter(); Paging paging = TwitterUtils.getPaging(TwitterUtils.getPagingCount(activity)); if (adapter.getCount() > 0) { paging.setMaxId(adapter.getLastID() - 1); @@ -119,7 +139,7 @@ public class HomeFragment extends CustomListFragment { for (Tweet tweet : tweets) { StatusViewModel viewModel = new StatusViewModel(tweet); adapter.addToBottom(viewModel); - StatusFilter.filter(activity, viewModel); + StatusFilter.getInstance().filter(viewModel); } updateListViewWithNotice(refreshView.getRefreshableView(), adapter, false); refreshView.onRefreshComplete(); diff --git a/app/src/main/java/net/lacolaco/smileessence/view/MentionsFragment.java b/app/src/main/java/net/lacolaco/smileessence/view/MentionsFragment.java index 831bd41e..a78042c2 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/MentionsFragment.java +++ b/app/src/main/java/net/lacolaco/smileessence/view/MentionsFragment.java @@ -24,14 +24,16 @@ package net.lacolaco.smileessence.view; +import android.os.Bundle; import android.widget.ListView; import com.handmark.pulltorefresh.library.PullToRefreshBase; -import net.lacolaco.smileessence.R; import net.lacolaco.smileessence.activity.MainActivity; import net.lacolaco.smileessence.entity.Account; +import net.lacolaco.smileessence.entity.ExtractionWord; import net.lacolaco.smileessence.entity.Tweet; +import net.lacolaco.smileessence.twitter.StatusFilter; import net.lacolaco.smileessence.twitter.TwitterApi; import net.lacolaco.smileessence.twitter.task.MentionsTimelineTask; import net.lacolaco.smileessence.twitter.util.TwitterUtils; @@ -42,23 +44,50 @@ import twitter4j.Paging; import twitter4j.Twitter; import java.util.List; +import java.util.regex.Pattern; -public class MentionsFragment extends CustomListFragment { +public class MentionsFragment extends CustomListFragment<StatusListAdapter> { // --------------------- GETTER / SETTER METHODS --------------------- @Override - protected MainActivity.AdapterID getAdapterIndex() { - return MainActivity.AdapterID.Mentions; - } - - @Override protected PullToRefreshBase.Mode getRefreshMode() { return PullToRefreshBase.Mode.BOTH; } // ------------------------ INTERFACE METHODS ------------------------ + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + StatusListAdapter adapter = new StatusListAdapter(getActivity()); + setAdapter(adapter); + StatusFilter.getInstance().register(this, StatusViewModel.class, (StatusViewModel tweet) -> { + for (ExtractionWord word : ExtractionWord.getAll()) { + Pattern pattern = Pattern.compile(word.text); + if (pattern.matcher(tweet.getTweet().getText()).find()) { + adapter.addToTop(tweet); + adapter.update(); + return; + } + } + }); + final Twitter twitter = TwitterApi.getTwitter(((MainActivity) getActivity()).getCurrentAccount()); + final Paging paging = TwitterUtils.getPaging(TwitterUtils.getPagingCount((MainActivity) getActivity())); + final StatusListAdapter adapter_ = adapter; + new MentionsTimelineTask(twitter, paging) { + @Override + protected void onPostExecute(List<Tweet> tweets) { + super.onPostExecute(tweets); + for (Tweet tweet : tweets) { + StatusViewModel statusViewModel = new StatusViewModel(tweet); + adapter_.addToBottom(statusViewModel); + StatusFilter.getInstance().filter(statusViewModel); + } + adapter_.updateForce(); + } + }.execute(); + } // --------------------- Interface OnRefreshListener2 --------------------- @@ -66,8 +95,8 @@ public class MentionsFragment extends CustomListFragment { public void onPullDownToRefresh(final PullToRefreshBase<ListView> refreshView) { final MainActivity activity = (MainActivity) getActivity(); final Account currentAccount = activity.getCurrentAccount(); + final StatusListAdapter adapter = getAdapter(); Twitter twitter = TwitterApi.getTwitter(currentAccount); - final StatusListAdapter adapter = (StatusListAdapter) getListAdapter(); Paging paging = TwitterUtils.getPaging(TwitterUtils.getPagingCount(activity)); if (adapter.getCount() > 0) { paging.setSinceId(adapter.getTopID()); @@ -89,8 +118,8 @@ public class MentionsFragment extends CustomListFragment { public void onPullUpToRefresh(final PullToRefreshBase<ListView> refreshView) { final MainActivity activity = (MainActivity) getActivity(); final Account currentAccount = activity.getCurrentAccount(); + final StatusListAdapter adapter = getAdapter(); Twitter twitter = TwitterApi.getTwitter(currentAccount); - final StatusListAdapter adapter = (StatusListAdapter) getListAdapter(); Paging paging = TwitterUtils.getPaging(TwitterUtils.getPagingCount(activity)); if (adapter.getCount() > 0) { paging.setMaxId(adapter.getLastID() - 1); diff --git a/app/src/main/java/net/lacolaco/smileessence/view/MessagesFragment.java b/app/src/main/java/net/lacolaco/smileessence/view/MessagesFragment.java index ac0e8067..29a8a9c3 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/MessagesFragment.java +++ b/app/src/main/java/net/lacolaco/smileessence/view/MessagesFragment.java @@ -24,15 +24,17 @@ package net.lacolaco.smileessence.view; +import android.os.Bundle; import android.widget.ListView; import com.handmark.pulltorefresh.library.PullToRefreshBase; -import net.lacolaco.smileessence.R; import net.lacolaco.smileessence.activity.MainActivity; import net.lacolaco.smileessence.entity.Account; +import net.lacolaco.smileessence.twitter.StatusFilter; import net.lacolaco.smileessence.twitter.TwitterApi; import net.lacolaco.smileessence.twitter.task.DirectMessagesTask; +import net.lacolaco.smileessence.twitter.task.SentDirectMessagesTask; import net.lacolaco.smileessence.twitter.util.TwitterUtils; import net.lacolaco.smileessence.view.adapter.MessageListAdapter; import net.lacolaco.smileessence.viewmodel.MessageViewModel; @@ -46,22 +48,50 @@ import java.util.List; /** * Fragment of messages list */ -public class MessagesFragment extends CustomListFragment { +public class MessagesFragment extends CustomListFragment<MessageListAdapter> { // --------------------- GETTER / SETTER METHODS --------------------- @Override - protected MainActivity.AdapterID getAdapterIndex() { - return MainActivity.AdapterID.Messages; - } - - @Override protected PullToRefreshBase.Mode getRefreshMode() { return PullToRefreshBase.Mode.BOTH; } // ------------------------ INTERFACE METHODS ------------------------ + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + MessageListAdapter adapter = new MessageListAdapter(getActivity()); + setAdapter(adapter); + + StatusFilter.getInstance().register(this, MessageViewModel.class, (MessageViewModel message) -> { + adapter.addToTop(message); + adapter.update(); + }); + final Twitter twitter = TwitterApi.getTwitter(((MainActivity) getActivity()).getCurrentAccount()); + final Paging paging = TwitterUtils.getPaging(TwitterUtils.getPagingCount((MainActivity) getActivity())); + new DirectMessagesTask(twitter, paging) { + @Override + protected void onPostExecute(List<DirectMessage> directMessages) { + super.onPostExecute(directMessages); + for (DirectMessage message : directMessages) { + adapter.addToBottom(new MessageViewModel(message)); + } + adapter.notifyDataSetChanged(); + } + }.execute(); + new SentDirectMessagesTask(twitter, paging) { + @Override + protected void onPostExecute(List<DirectMessage> directMessages) { + super.onPostExecute(directMessages); + for (DirectMessage message : directMessages) { + adapter.addToBottom(new MessageViewModel(message)); + } + adapter.notifyDataSetChanged(); + } + }.execute(); + } // --------------------- Interface OnRefreshListener2 --------------------- @@ -70,7 +100,7 @@ public class MessagesFragment extends CustomListFragment { final MainActivity activity = (MainActivity) getActivity(); final Account currentAccount = activity.getCurrentAccount(); Twitter twitter = TwitterApi.getTwitter(currentAccount); - final MessageListAdapter adapter = (MessageListAdapter) getListAdapter(); + final MessageListAdapter adapter = getAdapter(); Paging paging = TwitterUtils.getPaging(TwitterUtils.getPagingCount(activity)); if (adapter.getCount() > 0) { paging.setSinceId(adapter.getTopID()); @@ -93,7 +123,7 @@ public class MessagesFragment extends CustomListFragment { final MainActivity activity = (MainActivity) getActivity(); final Account currentAccount = activity.getCurrentAccount(); Twitter twitter = TwitterApi.getTwitter(currentAccount); - final MessageListAdapter adapter = (MessageListAdapter) getListAdapter(); + final MessageListAdapter adapter = getAdapter(); Paging paging = TwitterUtils.getPaging(TwitterUtils.getPagingCount(activity)); if (adapter.getCount() > 0) { paging.setMaxId(adapter.getLastID() - 1); diff --git a/app/src/main/java/net/lacolaco/smileessence/view/PageFragment.java b/app/src/main/java/net/lacolaco/smileessence/view/PageFragment.java index b5f54474..a9108276 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/PageFragment.java +++ b/app/src/main/java/net/lacolaco/smileessence/view/PageFragment.java @@ -1,6 +1,17 @@ package net.lacolaco.smileessence.view; import android.app.Fragment; +import android.widget.Adapter; -public abstract class PageFragment extends Fragment { +public abstract class PageFragment<T extends Adapter> extends Fragment { + private T adapter; + + protected T getAdapter() { + if (adapter == null) throw new IllegalStateException("adapter is not initialized"); + return adapter; + } + + protected void setAdapter(T _adapter) { + adapter = _adapter; + } } diff --git a/app/src/main/java/net/lacolaco/smileessence/view/PostFragment.java b/app/src/main/java/net/lacolaco/smileessence/view/PostFragment.java index 4b5318f6..797069d1 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/PostFragment.java +++ b/app/src/main/java/net/lacolaco/smileessence/view/PostFragment.java @@ -51,7 +51,7 @@ import net.lacolaco.smileessence.twitter.util.TwitterUtils; import net.lacolaco.smileessence.util.BitmapThumbnailTask; import net.lacolaco.smileessence.util.IntentUtils; import net.lacolaco.smileessence.util.UIHandler; -import net.lacolaco.smileessence.view.adapter.PostState; +import net.lacolaco.smileessence.data.PostState; import net.lacolaco.smileessence.view.dialog.PostMenuDialogFragment; import net.lacolaco.smileessence.view.dialog.SelectImageDialogFragment; import net.lacolaco.smileessence.viewmodel.StatusViewModel; diff --git a/app/src/main/java/net/lacolaco/smileessence/view/SearchFragment.java b/app/src/main/java/net/lacolaco/smileessence/view/SearchFragment.java index f1a069bc..d82b740a 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/SearchFragment.java +++ b/app/src/main/java/net/lacolaco/smileessence/view/SearchFragment.java @@ -58,13 +58,14 @@ import net.lacolaco.smileessence.view.adapter.SearchListAdapter; import net.lacolaco.smileessence.view.dialog.SelectSearchQueryDialogFragment; import net.lacolaco.smileessence.viewmodel.StatusViewModel; +import twitter4j.Paging; import twitter4j.Query; import twitter4j.QueryResult; import twitter4j.Twitter; import java.util.List; -public class SearchFragment extends CustomListFragment implements View.OnClickListener, View.OnFocusChangeListener, +public class SearchFragment extends CustomListFragment<SearchListAdapter> implements View.OnClickListener, View.OnFocusChangeListener, SearchListAdapter.OnQueryChangeListener { // ------------------------------ FIELDS ------------------------------ @@ -74,17 +75,26 @@ public class SearchFragment extends CustomListFragment implements View.OnClickLi // --------------------- GETTER / SETTER METHODS --------------------- @Override - protected MainActivity.AdapterID getAdapterIndex() { - return MainActivity.AdapterID.Search; - } - - @Override protected PullToRefreshBase.Mode getRefreshMode() { return PullToRefreshBase.Mode.BOTH; } // ------------------------ INTERFACE METHODS ------------------------ + @Override // onCreate って Fragment のインスタンスが作られるときは必ず呼ばれるって認識でいいんだよね? + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setHasOptionsMenu(true); + SearchListAdapter adapter = new SearchListAdapter(getActivity()); + setAdapter(adapter); + + final MainActivity activity = (MainActivity) getActivity(); + final Twitter twitter = TwitterApi.getTwitter(activity.getCurrentAccount()); + String lastUsedSearchQuery = activity.getLastSearch(); + if (!TextUtils.isEmpty(lastUsedSearchQuery)) { + startSearch(twitter, lastUsedSearchQuery); + } + } // --------------------- Interface OnClickListener --------------------- @@ -130,7 +140,7 @@ public class SearchFragment extends CustomListFragment implements View.OnClickLi final MainActivity activity = (MainActivity) getActivity(); final Account currentAccount = activity.getCurrentAccount(); Twitter twitter = TwitterApi.getTwitter(currentAccount); - final SearchListAdapter adapter = getListAdapter(activity); + final SearchListAdapter adapter = getAdapter(); String queryString = adapter.getQuery(); if (TextUtils.isEmpty(queryString)) { new UIHandler() { @@ -160,7 +170,7 @@ public class SearchFragment extends CustomListFragment implements View.OnClickLi if (!status.isRetweet()) { StatusViewModel viewModel = new StatusViewModel(Tweet.fromTwitter(status)); adapter.addToTop(viewModel); - StatusFilter.filter(activity, viewModel); + StatusFilter.getInstance().filter(viewModel); } } updateListViewWithNotice(refreshView.getRefreshableView(), adapter, true); @@ -176,7 +186,7 @@ public class SearchFragment extends CustomListFragment implements View.OnClickLi final MainActivity activity = (MainActivity) getActivity(); final Account currentAccount = activity.getCurrentAccount(); Twitter twitter = TwitterApi.getTwitter(currentAccount); - final SearchListAdapter adapter = getListAdapter(activity); + final SearchListAdapter adapter = getAdapter(); String queryString = adapter.getQuery(); if (TextUtils.isEmpty(queryString)) { new UIHandler() { @@ -205,7 +215,7 @@ public class SearchFragment extends CustomListFragment implements View.OnClickLi if (!status.isRetweet()) { StatusViewModel viewModel = new StatusViewModel(Tweet.fromTwitter(status)); adapter.addToBottom(viewModel); - StatusFilter.filter(activity, viewModel); + StatusFilter.getInstance().filter(viewModel); } } updateListViewWithNotice(refreshView.getRefreshableView(), adapter, false); @@ -223,12 +233,6 @@ public class SearchFragment extends CustomListFragment implements View.OnClickLi } @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setHasOptionsMenu(true); - } - - @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); menu.removeItem(R.id.actionbar_search); @@ -238,7 +242,7 @@ public class SearchFragment extends CustomListFragment implements View.OnClickLi public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View page = inflater.inflate(R.layout.fragment_search, container, false); PullToRefreshListView listView = getListView(page); - SearchListAdapter adapter = (SearchListAdapter) getListAdapter(); + SearchListAdapter adapter = getAdapter(); listView.setAdapter(adapter); listView.setOnScrollListener(this); listView.setOnRefreshListener(this); @@ -289,10 +293,6 @@ public class SearchFragment extends CustomListFragment implements View.OnClickLi return (ImageButton) page.findViewById(R.id.button_search_execute); } - private SearchListAdapter getListAdapter(MainActivity activity) { - return (SearchListAdapter) activity.getListAdapter(MainActivity.AdapterID.Search); - } - private ImageButton getQueriesButton(View page) { return (ImageButton) page.findViewById(R.id.button_search_queries); } @@ -347,4 +347,37 @@ public class SearchFragment extends CustomListFragment implements View.OnClickLi } } } + + public void startSearch(final Twitter twitter, final String queryString) { + ((MainActivity) getActivity()).setLastSearch(queryString); + if (!TextUtils.isEmpty(queryString)) { + final SearchListAdapter adapter = getAdapter(); + adapter.initSearch(queryString); + adapter.clear(); + adapter.updateForce(); + final Query query = new Query(); + query.setQuery(queryString); + query.setCount(TwitterUtils.getPagingCount((MainActivity) getActivity())); + query.setResultType(Query.RECENT); + new SearchTask(twitter, query) { + @Override + protected void onPostExecute(QueryResult queryResult) { + super.onPostExecute(queryResult); + if (queryResult != null) { + List<twitter4j.Status> tweets = queryResult.getTweets(); + for (int i = tweets.size() - 1; i >= 0; i--) { + twitter4j.Status status = tweets.get(i); + if (!status.isRetweet()) { + StatusViewModel viewModel = new StatusViewModel(Tweet.fromTwitter(status)); + adapter.addToTop(viewModel); + StatusFilter.getInstance().filter(viewModel); + } + } + adapter.setTopID(queryResult.getMaxId()); + adapter.updateForce(); + } + } + }.execute(); + } + } } diff --git a/app/src/main/java/net/lacolaco/smileessence/view/UserListFragment.java b/app/src/main/java/net/lacolaco/smileessence/view/UserListFragment.java index 3733358a..6b805cc5 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/UserListFragment.java +++ b/app/src/main/java/net/lacolaco/smileessence/view/UserListFragment.java @@ -56,7 +56,7 @@ import twitter4j.Twitter; import java.util.List; -public class UserListFragment extends CustomListFragment implements View.OnClickListener { +public class UserListFragment extends CustomListFragment<UserListListAdapter> implements View.OnClickListener { // ------------------------------ FIELDS ------------------------------ @@ -69,18 +69,24 @@ public class UserListFragment extends CustomListFragment implements View.OnClick } @Override - protected MainActivity.AdapterID getAdapterIndex() { - return MainActivity.AdapterID.UserList; - } - - @Override protected PullToRefreshBase.Mode getRefreshMode() { return PullToRefreshBase.Mode.BOTH; } // ------------------------ INTERFACE METHODS ------------------------ + @Override // onCreate って Fragment のインスタンスが作られるときは必ず呼ばれるって認識でいいんだよね? + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + UserListListAdapter adapter = new UserListListAdapter(getActivity()); + setAdapter(adapter); + final Twitter twitter = TwitterApi.getTwitter(((MainActivity) getActivity()).getCurrentAccount()); + String lastUserList = getMainActivity().getLastUserList(); + if (!TextUtils.isEmpty(lastUserList)) { + startUserList(twitter, lastUserList); + } + } // --------------------- Interface OnClickListener --------------------- @Override @@ -101,7 +107,7 @@ public class UserListFragment extends CustomListFragment implements View.OnClick final MainActivity activity = getMainActivity(); final Account currentAccount = activity.getCurrentAccount(); Twitter twitter = TwitterApi.getTwitter(currentAccount); - final UserListListAdapter adapter = (UserListListAdapter) getListAdapter(); + final UserListListAdapter adapter = (UserListListAdapter) getAdapter(); String listFullName = adapter.getListFullName(); if (TextUtils.isEmpty(listFullName)) { new UIHandler() { @@ -117,14 +123,14 @@ public class UserListFragment extends CustomListFragment implements View.OnClick if (adapter.getCount() > 0) { paging.setSinceId(adapter.getTopID()); } - new UserListStatusesTask(twitter, activity, listFullName, paging) { + new UserListStatusesTask(twitter, listFullName, paging) { @Override protected void onPostExecute(List<Tweet> tweets) { super.onPostExecute(tweets); for (int i = tweets.size() - 1; i >= 0; i--) { StatusViewModel statusViewModel = new StatusViewModel(tweets.get(i)); adapter.addToTop(statusViewModel); - StatusFilter.filter(activity, statusViewModel); + StatusFilter.getInstance().filter(statusViewModel); } updateListViewWithNotice(refreshView.getRefreshableView(), adapter, true); refreshView.onRefreshComplete(); @@ -137,7 +143,7 @@ public class UserListFragment extends CustomListFragment implements View.OnClick final MainActivity activity = getMainActivity(); final Account currentAccount = activity.getCurrentAccount(); Twitter twitter = TwitterApi.getTwitter(currentAccount); - final UserListListAdapter adapter = (UserListListAdapter) getListAdapter(); + final UserListListAdapter adapter = (UserListListAdapter) getAdapter(); String listFullName = adapter.getListFullName(); if (TextUtils.isEmpty(listFullName)) { new UIHandler() { @@ -153,14 +159,14 @@ public class UserListFragment extends CustomListFragment implements View.OnClick if (adapter.getCount() > 0) { paging.setMaxId(adapter.getLastID() - 1); } - new UserListStatusesTask(twitter, activity, listFullName, paging) { + new UserListStatusesTask(twitter, listFullName, paging) { @Override protected void onPostExecute(List<Tweet> tweets) { super.onPostExecute(tweets); for (int i = 0; i < tweets.size(); i++) { StatusViewModel statusViewModel = new StatusViewModel(tweets.get(i)); adapter.addToBottom(statusViewModel); - StatusFilter.filter(activity, statusViewModel); + StatusFilter.getInstance().filter(statusViewModel); } updateListViewWithNotice(refreshView.getRefreshableView(), adapter, false); refreshView.onRefreshComplete(); @@ -179,7 +185,7 @@ public class UserListFragment extends CustomListFragment implements View.OnClick public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View page = inflater.inflate(R.layout.fragment_userlist, container, false); PullToRefreshListView listView = getListView(page); - UserListListAdapter adapter = (UserListListAdapter) getListAdapter(); + UserListListAdapter adapter = (UserListListAdapter) getAdapter(); listView.setAdapter(adapter); listView.setOnScrollListener(this); listView.setOnRefreshListener(this); @@ -208,8 +214,28 @@ public class UserListFragment extends CustomListFragment implements View.OnClick @Override public void onDismiss(DialogInterface dialog) { super.onDismiss(dialog); - textListName.setText(((UserListListAdapter) getListAdapter()).getListFullName()); + textListName.setText(getAdapter().getListFullName()); } }); } + + public void startUserList(Twitter twitter, String listFullName) { + getMainActivity().saveLastUserList(listFullName); + final UserListListAdapter adapter = getAdapter(); + adapter.setListFullName(listFullName); + adapter.clear(); + adapter.updateForce(); + new UserListStatusesTask(twitter, listFullName, TwitterUtils.getPaging(TwitterUtils.getPagingCount(getMainActivity()))) { + @Override + protected void onPostExecute(List<Tweet> tweets) { + super.onPostExecute(tweets); + for (Tweet tweet : tweets) { + StatusViewModel statusViewModel = new StatusViewModel(tweet); + adapter.addToBottom(statusViewModel); + StatusFilter.getInstance().filter(statusViewModel); + } + adapter.updateForce(); + } + }.execute(); + } } diff --git a/app/src/main/java/net/lacolaco/smileessence/view/adapter/PageListAdapter.java b/app/src/main/java/net/lacolaco/smileessence/view/adapter/PageListAdapter.java index c2ffa399..737c03aa 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/adapter/PageListAdapter.java +++ b/app/src/main/java/net/lacolaco/smileessence/view/adapter/PageListAdapter.java @@ -26,10 +26,12 @@ package net.lacolaco.smileessence.view.adapter; import android.app.ActionBar; import android.app.Fragment; +import android.content.Context; import android.os.Bundle; import android.support.v13.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; +import android.widget.Adapter; import android.widget.ArrayAdapter; import net.lacolaco.smileessence.R; import net.lacolaco.smileessence.activity.MainActivity; @@ -37,6 +39,7 @@ import net.lacolaco.smileessence.logging.Logger; import net.lacolaco.smileessence.view.HomeFragment; import net.lacolaco.smileessence.view.PageFragment; +import java.lang.ref.WeakReference; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; @@ -102,7 +105,7 @@ public class PageListAdapter extends FragmentPagerAdapter implements ViewPager.O @Override public synchronized Fragment getItem(int position) { PageInfo info = pages.get(position); - return Fragment.instantiate(context, info.getFragmentClass().getName(), info.getArgs()); + return info.instantiate(context); } public void addPage(Class<? extends PageFragment> klass, String name, Bundle args) { @@ -143,10 +146,21 @@ public class PageListAdapter extends FragmentPagerAdapter implements ViewPager.O return -1; } + @Deprecated + public <T extends PageFragment> T getFragment(Class<T> fragmentClass) { + for(PageInfo info : pages) { + if (info.getFragmentClass() == fragmentClass) { + return (T) info.getCachedInstance(); + } + } + return null; + } + private static final class PageInfo { private final Class<? extends PageFragment> fragmentClass; private final Bundle args; private final String name; + private WeakReference<PageFragment> fragmentCache; PageInfo(Class<? extends PageFragment> _fragmentClass, String _name, Bundle _args) { fragmentClass = _fragmentClass; @@ -163,5 +177,15 @@ public class PageListAdapter extends FragmentPagerAdapter implements ViewPager.O public Bundle getArgs() { return args; } + + public PageFragment instantiate(Context context) { + PageFragment fragment = (PageFragment) Fragment.instantiate(context, getFragmentClass().getName(), getArgs()); + fragmentCache = new WeakReference<>(fragment); + return fragment; + } + + public PageFragment getCachedInstance() { + return fragmentCache.get(); + } } } diff --git a/app/src/main/java/net/lacolaco/smileessence/view/dialog/ReviewDialogFragment.java b/app/src/main/java/net/lacolaco/smileessence/view/dialog/ReviewDialogFragment.java index 1e95d8be..df72ca35 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/dialog/ReviewDialogFragment.java +++ b/app/src/main/java/net/lacolaco/smileessence/view/dialog/ReviewDialogFragment.java @@ -36,7 +36,7 @@ import android.widget.RatingBar; import net.lacolaco.smileessence.R; import net.lacolaco.smileessence.activity.MainActivity; import net.lacolaco.smileessence.entity.Tweet; -import net.lacolaco.smileessence.view.adapter.PostState; +import net.lacolaco.smileessence.data.PostState; import net.lacolaco.smileessence.viewmodel.StatusViewModel; diff --git a/app/src/main/java/net/lacolaco/smileessence/view/dialog/SendMessageDialogFragment.java b/app/src/main/java/net/lacolaco/smileessence/view/dialog/SendMessageDialogFragment.java index b17e77c2..7713b0c9 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/dialog/SendMessageDialogFragment.java +++ b/app/src/main/java/net/lacolaco/smileessence/view/dialog/SendMessageDialogFragment.java @@ -45,7 +45,7 @@ import net.lacolaco.smileessence.activity.MainActivity; import net.lacolaco.smileessence.twitter.TwitterApi; import net.lacolaco.smileessence.twitter.task.SendMessageTask; import net.lacolaco.smileessence.twitter.util.TwitterUtils; -import net.lacolaco.smileessence.view.adapter.PostState; +import net.lacolaco.smileessence.data.PostState; import twitter4j.Twitter; diff --git a/app/src/main/java/net/lacolaco/smileessence/view/dialog/StatusDetailDialogFragment.java b/app/src/main/java/net/lacolaco/smileessence/view/dialog/StatusDetailDialogFragment.java index 2f0a50dc..a8a6ca40 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/dialog/StatusDetailDialogFragment.java +++ b/app/src/main/java/net/lacolaco/smileessence/view/dialog/StatusDetailDialogFragment.java @@ -51,7 +51,7 @@ import net.lacolaco.smileessence.twitter.task.RetweetTask; import net.lacolaco.smileessence.twitter.task.UnfavoriteTask; import net.lacolaco.smileessence.twitter.util.TwitterUtils; import net.lacolaco.smileessence.view.DialogHelper; -import net.lacolaco.smileessence.view.adapter.PostState; +import net.lacolaco.smileessence.data.PostState; import net.lacolaco.smileessence.view.adapter.StatusListAdapter; import net.lacolaco.smileessence.view.listener.ListItemClickListener; import net.lacolaco.smileessence.viewmodel.StatusViewModel; diff --git a/app/src/main/java/net/lacolaco/smileessence/viewmodel/menu/MainActivityMenuHelper.java b/app/src/main/java/net/lacolaco/smileessence/viewmodel/menu/MainActivityMenuHelper.java index 1b421582..aa02ded6 100644 --- a/app/src/main/java/net/lacolaco/smileessence/viewmodel/menu/MainActivityMenuHelper.java +++ b/app/src/main/java/net/lacolaco/smileessence/viewmodel/menu/MainActivityMenuHelper.java @@ -33,7 +33,7 @@ import net.lacolaco.smileessence.R; import net.lacolaco.smileessence.activity.*; import net.lacolaco.smileessence.command.CommandOpenURL; import net.lacolaco.smileessence.twitter.util.TwitterUtils; -import net.lacolaco.smileessence.view.adapter.PostState; +import net.lacolaco.smileessence.data.PostState; public class MainActivityMenuHelper { |