diff options
10 files changed, 201 insertions, 203 deletions
diff --git a/app/src/main/java/net/lacolaco/smileessence/entity/DirectMessage.java b/app/src/main/java/net/lacolaco/smileessence/entity/DirectMessage.java index 8c80b0f2..e81ab0d3 100644 --- a/app/src/main/java/net/lacolaco/smileessence/entity/DirectMessage.java +++ b/app/src/main/java/net/lacolaco/smileessence/entity/DirectMessage.java @@ -4,6 +4,7 @@ import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import net.lacolaco.smileessence.util.ListUtils; +import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -14,6 +15,11 @@ public class DirectMessage extends EntitySupport { return storage.getIfPresent(statusId); } + @Deprecated + public synchronized static List<DirectMessage> cached() { + return new ArrayList<>(storage.asMap().values()); + } + public synchronized static void remove(long statusId) { storage.invalidate(statusId); } diff --git a/app/src/main/java/net/lacolaco/smileessence/view/dialog/DisposeDialog.java b/app/src/main/java/net/lacolaco/smileessence/view/dialog/DisposeDialog.java index 1dbbf057..eb3d3613 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/dialog/DisposeDialog.java +++ b/app/src/main/java/net/lacolaco/smileessence/view/dialog/DisposeDialog.java @@ -36,14 +36,6 @@ public final class DisposeDialog extends Dialog { super(context); } - public DisposeDialog(Context context, int theme) { - super(context, theme); - } - - protected DisposeDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { - super(context, cancelable, cancelListener); - } - @Override public void show() { dismiss(); diff --git a/app/src/main/java/net/lacolaco/smileessence/view/dialog/MenuDialogFragment.java b/app/src/main/java/net/lacolaco/smileessence/view/dialog/MenuDialogFragment.java index d53f89c5..71355d61 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/dialog/MenuDialogFragment.java +++ b/app/src/main/java/net/lacolaco/smileessence/view/dialog/MenuDialogFragment.java @@ -36,7 +36,7 @@ import net.lacolaco.smileessence.command.Command; import net.lacolaco.smileessence.command.IConfirmable; import net.lacolaco.smileessence.view.adapter.CustomListAdapter; -public abstract class MenuDialogFragment extends StackableDialogFragment { +public abstract class MenuDialogFragment extends StackableDialogFragment implements AdapterView.OnItemClickListener { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final MainActivity activity = (MainActivity) getActivity(); @@ -44,7 +44,7 @@ public abstract class MenuDialogFragment extends StackableDialogFragment { ListView listView = (ListView) body.findViewById(R.id.listview_dialog_menu_list); final CustomListAdapter<Command> adapter = new CustomListAdapter<>(activity); listView.setAdapter(adapter); - listView.setOnItemClickListener(onItemClickListener); + listView.setOnItemClickListener(this); setMenuItems(adapter); @@ -53,15 +53,14 @@ public abstract class MenuDialogFragment extends StackableDialogFragment { protected abstract void setMenuItems(final CustomListAdapter<Command> adapter); - protected final AdapterView.OnItemClickListener onItemClickListener = (adapterView, view, i, l) -> MenuDialogFragment.this.onItemClick(adapterView, i); - protected void executeCommand(Command command) { dismiss(); command.execute(); } - protected void onItemClick(AdapterView<?> adapterView, int i) { - final Command command = (Command) adapterView.getItemAtPosition(i); + @Override + public void onItemClick(AdapterView<?> parent, View view, int position, long id) { + final Command command = (Command) parent.getItemAtPosition(position); if (command != null) { if (command instanceof IConfirmable) { ConfirmDialogFragment.show(getActivity(), getString(R.string.dialog_confirm_commands), () -> executeCommand(command)); diff --git a/app/src/main/java/net/lacolaco/smileessence/view/dialog/MessageDetailDialogFragment.java b/app/src/main/java/net/lacolaco/smileessence/view/dialog/MessageDetailDialogFragment.java index 1d6b7809..968b8289 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/dialog/MessageDetailDialogFragment.java +++ b/app/src/main/java/net/lacolaco/smileessence/view/dialog/MessageDetailDialogFragment.java @@ -53,6 +53,7 @@ public class MessageDetailDialogFragment extends StackableDialogFragment impleme // ------------------------------ FIELDS ------------------------------ private static final String KEY_MESSAGE_ID = "messageID"; + private DirectMessage message; // --------------------- GETTER / SETTER METHODS --------------------- @@ -73,88 +74,68 @@ public class MessageDetailDialogFragment extends StackableDialogFragment impleme @Override public void onClick(final View v) { - DirectMessage message = DirectMessage.fetch(getMessageID()); - if (message != null) { - switch (v.getId()) { - case R.id.button_status_detail_reply: { - openSendMessageDialog(message); - break; - } - case R.id.button_status_detail_delete: { - deleteMessage(message); - break; - } - case R.id.button_status_detail_menu: { - openMenu(); - break; - } - default: { - dismiss(); - } + switch (v.getId()) { + case R.id.button_status_detail_reply: { + openSendMessageDialog(); + break; + } + case R.id.button_status_detail_delete: { + deleteMessage(); + break; + } + case R.id.button_status_detail_menu: { + openMenu(); + break; + } + default: { + dismiss(); } - } else { - dismiss(); // BUG } } // ------------------------ OVERRIDE METHODS ------------------------ @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + message = DirectMessage.fetch(getMessageID()); + } - DirectMessage selectedMessage = DirectMessage.fetch(getMessageID()); - if (selectedMessage == null) { + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + if (message == null) { Notificator.getInstance().alert(R.string.notice_error_get_messages); return new DisposeDialog(getActivity()); } - View header = getTitleView(selectedMessage); + + View header = getTitleView(); ListView listView = (ListView) header.findViewById(R.id.listview_status_detail_reply_to); final MessageListAdapter adapter = new MessageListAdapter(getActivity()); listView.setAdapter(adapter); - long replyToMessageId = -1; - // ArrayList<DirectMessage> allMessages = Lists.newArrayList(DirectMessageCache.getInstance().all()); - // Collections.sort(allMessages, new Comparator<DirectMessage>() { - // @Override - // public int compare(DirectMessage lhs, DirectMessage rhs) { - // return rhs.getCreatedAt().compareTo(lhs.getCreatedAt()); - // } - // }); - // for (DirectMessage directMessage : allMessages) { - // if (selectedMessage.getId() == directMessage.getId()) { - // continue; - // } - // if (directMessage.getCreatedAt().getTime() > selectedMessage.getCreatedAt().getTime()) { - // continue; - // } - // if (directMessage.getSender().getId() == selectedMessage.getRecipient().getId() && - // directMessage.getRecipient().getId() == selectedMessage.getSender().getId()) { - // replyToMessageId = directMessage.getId(); - // break; - // } - // } - - // if (replyToMessageId == -1) { - listView.setVisibility(View.GONE); - // } else { - // TwitterUtils.tryGetMessage(account, replyToMessageId, new TwitterUtils.MessageCallback() { - // @Override - // public void success(DirectMessage message) { - // adapter.addToTop(new MessageViewModel(message)); - // adapter.updateForce(); - // } - - // @Override - // public void error() { - - // } - // }); - // } + + // TODO: 効率的な探索どうする + DirectMessage replyTo = null; + for (DirectMessage mes : DirectMessage.cached()) { + if (message.getRecipient() == mes.getSender() && + message.getId() > mes.getId() && + (replyTo == null || replyTo.getId() < mes.getId())) { + replyTo = mes; + } + } + if (replyTo != null) { + listView.setVisibility(View.VISIBLE); + adapter.addToTop(new MessageViewModel(replyTo)); + adapter.updateForce(); + } else { + listView.setVisibility(View.GONE); + } + return new AlertDialog.Builder(getActivity()).setView(header).create(); } // -------------------------- OTHER METHODS -------------------------- - public void deleteMessage(final DirectMessage message) { + public void deleteMessage() { ConfirmDialogFragment.show(getActivity(), getString(R.string.dialog_confirm_commands), () -> { new DeleteMessageTask(Application.getInstance().getCurrentAccount(), message.getId()) .onDone(x -> Notificator.getInstance().publish(R.string.notice_message_delete_succeeded)) @@ -164,13 +145,13 @@ public class MessageDetailDialogFragment extends StackableDialogFragment impleme }); } - public void openSendMessageDialog(DirectMessage message) { + public void openSendMessageDialog() { SendMessageDialogFragment dialogFragment = new SendMessageDialogFragment(); dialogFragment.setScreenName(message.getSender().getScreenName()); DialogHelper.showDialog(getActivity(), dialogFragment); } - private ArrayList<Command> getCommands(DirectMessage message) { + private ArrayList<Command> getCommands() { ArrayList<Command> commands = new ArrayList<>(); // URL for (String url : message.getUrlsExpanded()) { @@ -182,7 +163,7 @@ public class MessageDetailDialogFragment extends StackableDialogFragment impleme return commands; } - private View getTitleView(DirectMessage message) { + private View getTitleView() { MainActivity activity = (MainActivity) getActivity(); View view = activity.getLayoutInflater().inflate(R.layout.dialog_status_detail, null); View messageHeader = view.findViewById(R.id.layout_status_header); @@ -201,7 +182,7 @@ public class MessageDetailDialogFragment extends StackableDialogFragment impleme LinearLayout commandsLayout = (LinearLayout) view.findViewById(R.id.linearlayout_status_detail_menu); commandsLayout.setClickable(true); // commands - ArrayList<Command> commands = getCommands(message); + ArrayList<Command> commands = getCommands(); Command.filter(commands); for (final Command command : commands) { View commandView = command.getView(activity, activity.getLayoutInflater(), null); diff --git a/app/src/main/java/net/lacolaco/smileessence/view/dialog/MessageMenuDialogFragment.java b/app/src/main/java/net/lacolaco/smileessence/view/dialog/MessageMenuDialogFragment.java index 2244c745..27be3e54 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/dialog/MessageMenuDialogFragment.java +++ b/app/src/main/java/net/lacolaco/smileessence/view/dialog/MessageMenuDialogFragment.java @@ -38,6 +38,7 @@ public class MessageMenuDialogFragment extends MenuDialogFragment { // ------------------------------ FIELDS ------------------------------ private static final String KEY_MESSAGE_ID = "messageID"; + private DirectMessage message; // --------------------- GETTER / SETTER METHODS --------------------- @@ -54,10 +55,15 @@ public class MessageMenuDialogFragment extends MenuDialogFragment { // ------------------------ OVERRIDE METHODS ------------------------ @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + message = DirectMessage.fetch(getMessageID()); + } + + @Override protected void setMenuItems(final CustomListAdapter<Command> adapter) { - DirectMessage message = DirectMessage.fetch(getMessageID()); if (message != null) { - List<Command> commands = getCommands(message); + List<Command> commands = getCommands(); Command.filter(commands); for (Command command : commands) { adapter.addToBottom(command); @@ -70,7 +76,7 @@ public class MessageMenuDialogFragment extends MenuDialogFragment { // -------------------------- OTHER METHODS -------------------------- - public void addBottomCommands(DirectMessage message, ArrayList<Command> commands) { + public void addBottomCommands(ArrayList<Command> commands) { Activity activity = getActivity(); commands.add(new CommandSaveAsTemplate(activity, message.getText())); //User @@ -80,7 +86,7 @@ public class MessageMenuDialogFragment extends MenuDialogFragment { for (String screenName : message.getMentions()) { commands.add(new CommandOpenUserDetail(activity, screenName)); } - for (Command command : getHashtagCommands(message)) { + for (Command command : getHashtagCommands()) { commands.add(command); } // Media @@ -92,23 +98,23 @@ public class MessageMenuDialogFragment extends MenuDialogFragment { } } - public boolean addMainCommands(DirectMessage message, ArrayList<Command> commands) { + public boolean addMainCommands(ArrayList<Command> commands) { Activity activity = getActivity(); return commands.addAll(Command.getMessageCommands(activity, message)); } - public List<Command> getCommands(DirectMessage message) { + public List<Command> getCommands() { ArrayList<Command> commands = new ArrayList<>(); - addMainCommands(message, commands); - addBottomCommands(message, commands); + addMainCommands(commands); + addBottomCommands(commands); return commands; } - private ArrayList<Command> getHashtagCommands(DirectMessage status) { + private ArrayList<Command> getHashtagCommands() { Activity activity = getActivity(); ArrayList<Command> commands = new ArrayList<>(); - if (status.getHashtags() != null) { - for (String hashtag : status.getHashtags()) { + if (message.getHashtags() != null) { + for (String hashtag : message.getHashtags()) { commands.add(new CommandOpenHashtagDialog(activity, hashtag)); } } diff --git a/app/src/main/java/net/lacolaco/smileessence/view/dialog/SelectSearchQueryDialogFragment.java b/app/src/main/java/net/lacolaco/smileessence/view/dialog/SelectSearchQueryDialogFragment.java index fa5d3218..6d95d3b6 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/dialog/SelectSearchQueryDialogFragment.java +++ b/app/src/main/java/net/lacolaco/smileessence/view/dialog/SelectSearchQueryDialogFragment.java @@ -81,7 +81,7 @@ public class SelectSearchQueryDialogFragment extends MenuDialogFragment implemen ListView listView = (ListView) body.findViewById(R.id.listview_dialog_menu_list); final CustomListAdapter<Command> adapter = new CustomListAdapter<>(activity); listView.setAdapter(adapter); - listView.setOnItemClickListener(onItemClickListener); + listView.setOnItemClickListener(this); // addition listView.setOnItemLongClickListener(this); 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 a7cf80ca..990cc5d3 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 @@ -79,27 +79,25 @@ public class StatusDetailDialogFragment extends StackableDialogFragment implemen @Override public void onClick(final View v) { - MainActivity activity = (MainActivity) getActivity(); - switch (v.getId()) { case R.id.button_status_detail_reply: { - replyToStatus(tweet); + replyToStatus(); break; } case R.id.button_status_detail_retweet: { - toggleRetweet(tweet); + toggleRetweet(); break; } case R.id.button_status_detail_favorite: { - toggleFavorite(tweet); + toggleFavorite(); break; } case R.id.button_status_detail_delete: { - deleteStatus(tweet); + deleteStatus(); break; } case R.id.button_status_detail_menu: { - openMenu(activity); + openMenu(); break; } default: { @@ -111,16 +109,21 @@ public class StatusDetailDialogFragment extends StackableDialogFragment implemen // ------------------------ OVERRIDE METHODS ------------------------ @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); tweet = Tweet.fetch(getStatusID()); - if (tweet == null) { // trying open deleted tweet + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + if (tweet == null) { // trying to open deleted tweet? Notificator.getInstance().publish(R.string.notice_error_show_status); return new DisposeDialog(getActivity()); } Account account = Application.getInstance().getCurrentAccount(); - View header = getTitleView(tweet); + View header = getTitleView(); ListView listView = (ListView) header.findViewById(R.id.listview_status_detail_reply_to); final StatusListAdapter adapter = new StatusListAdapter(getActivity()); @@ -141,7 +144,7 @@ public class StatusDetailDialogFragment extends StackableDialogFragment implemen return new AlertDialog.Builder(getActivity()).setView(header).create(); } - private View getTitleView(Tweet tweet) { + private View getTitleView() { MainActivity activity = ((MainActivity) getActivity()); View view = activity.getLayoutInflater().inflate(R.layout.dialog_status_detail, null); @@ -152,25 +155,25 @@ public class StatusDetailDialogFragment extends StackableDialogFragment implemen statusHeader.setClickable(false); view.setBackgroundColor(((ColorDrawable) statusHeader.getBackground()).getColor()); - updateViewReactions(view, tweet); - updateViewButtons(view, tweet); - updateViewMenu(view, tweet); + updateViewReactions(view); + updateViewButtons(view); + updateViewMenu(view); final WeakReference<View> weakView = new WeakReference<>(view); bundle.attach(tweet.getOriginalTweet(), changes -> { View strongView = weakView.get(); if (strongView != null && getActivity() != null) { if (changes.contains(RBinding.REACTION_COUNT)) - updateViewReactions(strongView, tweet); + updateViewReactions(strongView); if (changes.contains(RBinding.FAVORITERS) || changes.contains(RBinding.RETWEETERS)) - updateViewButtons(strongView, tweet); + updateViewButtons(strongView); } }); return view; } - private void updateViewReactions(View view, Tweet tweet) { + private void updateViewReactions(View view) { //--- favs/RTs count ImageView favCountIcon = (ImageView) view.findViewById(R.id.image_status_detail_fav_count); TextView favCountText = (TextView) view.findViewById(R.id.textview_status_detail_fav_count); @@ -195,7 +198,7 @@ public class StatusDetailDialogFragment extends StackableDialogFragment implemen } } - private void updateViewButtons(View view, Tweet tweet) { + private void updateViewButtons(View view) { int themeResId = Application.getInstance().getThemeResId(); Account account = Application.getInstance().getCurrentAccount(); @@ -230,14 +233,14 @@ public class StatusDetailDialogFragment extends StackableDialogFragment implemen delete.setVisibility(account.canDelete(tweet) ? View.VISIBLE : View.GONE); } - private void updateViewMenu(View view, Tweet tweet) { + private void updateViewMenu(View view) { MainActivity activity = ((MainActivity) getActivity()); //--- menu ImageButton menu = (ImageButton) view.findViewById(R.id.button_status_detail_menu); menu.setOnClickListener(this); LinearLayout commandsLayout = (LinearLayout) view.findViewById(R.id.linearlayout_status_detail_menu); commandsLayout.setClickable(true); - ArrayList<Command> commands = getCommands(tweet); + ArrayList<Command> commands = getCommands(); Command.filter(commands); for (final Command command : commands) { View commandView = command.getView(activity, activity.getLayoutInflater(), null); @@ -251,7 +254,7 @@ public class StatusDetailDialogFragment extends StackableDialogFragment implemen ConfirmDialogFragment.show(getActivity(), getString(R.string.dialog_confirm_commands), onYes); } - private void deleteStatus(final Tweet tweet) { + private void deleteStatus() { confirm(() -> { Account account = Application.getInstance().getCurrentAccount(); @@ -263,7 +266,7 @@ public class StatusDetailDialogFragment extends StackableDialogFragment implemen }); } - private ArrayList<Command> getCommands(Tweet tweet) { + private ArrayList<Command> getCommands() { ArrayList<Command> commands = new ArrayList<>(); // URL for (String url : tweet.getUrlsExpanded()) { @@ -275,13 +278,13 @@ public class StatusDetailDialogFragment extends StackableDialogFragment implemen return commands; } - private void openMenu(MainActivity activity) { + private void openMenu() { StatusMenuDialogFragment fragment = new StatusMenuDialogFragment(); fragment.setStatusID(getStatusID()); - DialogHelper.showDialog(activity, fragment); + DialogHelper.showDialog(getActivity(), fragment); } - private void replyToStatus(Tweet tweet) { + private void replyToStatus() { Account account = Application.getInstance().getCurrentAccount(); Tweet originalTweet = tweet.getOriginalTweet(); @@ -303,7 +306,7 @@ public class StatusDetailDialogFragment extends StackableDialogFragment implemen .commitWithOpen((MainActivity) getActivity()); } - private void toggleFavorite(Tweet tweet) { + private void toggleFavorite() { Account account = Application.getInstance().getCurrentAccount(); if (tweet.isFavoritedBy(account.getUserId())) { new UnfavoriteTask(account, tweet.getId()) @@ -318,7 +321,7 @@ public class StatusDetailDialogFragment extends StackableDialogFragment implemen } } - private void toggleRetweet(final Tweet tweet) { + private void toggleRetweet() { Account account = Application.getInstance().getCurrentAccount(); confirm(() -> { if (tweet.isRetweetedBy(account.getUserId())) { diff --git a/app/src/main/java/net/lacolaco/smileessence/view/dialog/StatusMenuDialogFragment.java b/app/src/main/java/net/lacolaco/smileessence/view/dialog/StatusMenuDialogFragment.java index 97a74789..7c626f57 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/dialog/StatusMenuDialogFragment.java +++ b/app/src/main/java/net/lacolaco/smileessence/view/dialog/StatusMenuDialogFragment.java @@ -39,6 +39,7 @@ public class StatusMenuDialogFragment extends MenuDialogFragment { // ------------------------------ FIELDS ------------------------------ private static final String KEY_STATUS_ID = "statusID"; + private Tweet tweet; // --------------------- GETTER / SETTER METHODS --------------------- @@ -53,12 +54,17 @@ public class StatusMenuDialogFragment extends MenuDialogFragment { } // ------------------------ OVERRIDE METHODS ------------------------ + @Override - protected void setMenuItems(final CustomListAdapter<Command> adapter) { - Tweet tweet = Tweet.fetch(getStatusID()); + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + tweet = Tweet.fetch(getStatusID()); + } + @Override + protected void setMenuItems(final CustomListAdapter<Command> adapter) { if (tweet != null) { - List<Command> commands = getCommands(tweet); + List<Command> commands = getCommands(); Command.filter(commands); for (Command command : commands) { adapter.addToBottom(command); @@ -71,7 +77,7 @@ public class StatusMenuDialogFragment extends MenuDialogFragment { // -------------------------- OTHER METHODS -------------------------- - public void addBottomCommands(Tweet tweet, ArrayList<Command> commands) { + public void addBottomCommands(ArrayList<Command> commands) { Activity activity = getActivity(); commands.add(new CommandSaveAsTemplate(activity, tweet.getOriginalTweet().getText())); @@ -80,7 +86,7 @@ public class StatusMenuDialogFragment extends MenuDialogFragment { for (String screenName : new ArrayList<>(new LinkedHashSet<>(tweet.getMentions()))) { // Array#uniq commands.add(new CommandOpenUserDetail(activity, screenName)); } - for (Command command : getHashtagCommands(tweet)) { + for (Command command : getHashtagCommands()) { commands.add(command); } // Media @@ -92,19 +98,19 @@ public class StatusMenuDialogFragment extends MenuDialogFragment { } } - public boolean addMainCommands(Tweet tweet, ArrayList<Command> commands) { + public boolean addMainCommands(ArrayList<Command> commands) { Activity activity = getActivity(); return commands.addAll(Command.getStatusCommands(activity, tweet)); } - public List<Command> getCommands(Tweet tweet) { + public List<Command> getCommands() { ArrayList<Command> commands = new ArrayList<>(); - addMainCommands(tweet, commands); - addBottomCommands(tweet, commands); + addMainCommands(commands); + addBottomCommands(commands); return commands; } - private ArrayList<Command> getHashtagCommands(Tweet tweet) { + private ArrayList<Command> getHashtagCommands() { Activity activity = getActivity(); ArrayList<Command> commands = new ArrayList<>(); for (String hashtag : tweet.getHashtags()) { diff --git a/app/src/main/java/net/lacolaco/smileessence/view/dialog/UserDetailDialogFragment.java b/app/src/main/java/net/lacolaco/smileessence/view/dialog/UserDetailDialogFragment.java index 97766e12..e6e8954c 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/dialog/UserDetailDialogFragment.java +++ b/app/src/main/java/net/lacolaco/smileessence/view/dialog/UserDetailDialogFragment.java @@ -85,6 +85,7 @@ public class UserDetailDialogFragment extends StackableDialogFragment implements private PullToRefreshListView listViewTimeline; private TabHost tabHost; private UIObserverBundle observerBundle; + private User user; // --------------------- GETTER / SETTER METHODS --------------------- @@ -105,44 +106,36 @@ public class UserDetailDialogFragment extends StackableDialogFragment implements @Override public void onClick(final View v) { - User user = User.fetch(getUserID()); - if (user != null) { - switch (v.getId()) { - case R.id.imageview_user_detail_menu: { - openUserMenu(user); - break; - } - case R.id.imageview_user_detail_icon: { - IntentUtils.openUri(getActivity(), user.getProfileImageUrlOriginal()); - break; - } - case R.id.textview_user_detail_screenname: { - IntentUtils.openUri(getActivity(), user.getUserHomeURL()); - break; - } - case R.id.textview_user_detail_tweet_count: { - IntentUtils.openUri(getActivity(), user.getUserHomeURL()); - break; - } - case R.id.textview_user_detail_friend_count: { - IntentUtils.openUri(getActivity(), String.format("%s/following", user.getUserHomeURL())); - break; - } - case R.id.textview_user_detail_follower_count: { - IntentUtils.openUri(getActivity(), String.format("%s/followers", user.getUserHomeURL())); - break; - } - case R.id.textview_user_detail_favorite_count: { - IntentUtils.openUri(getActivity(), String.format("%s/favorites", user.getUserHomeURL())); - break; - } - case R.id.button_user_detail_follow: { - ConfirmDialogFragment.show(getActivity(), getString(R.string.dialog_confirm_commands), () -> toggleFollowing(user)); - break; - } + switch (v.getId()) { + case R.id.imageview_user_detail_menu: { + openUserMenu(); + break; + } + case R.id.imageview_user_detail_icon: { + IntentUtils.openUri(getActivity(), user.getProfileImageUrlOriginal()); + break; + } + case R.id.textview_user_detail_screenname: + case R.id.textview_user_detail_tweet_count:{ + IntentUtils.openUri(getActivity(), user.getUserHomeURL()); + break; + } + case R.id.textview_user_detail_friend_count: { + IntentUtils.openUri(getActivity(), String.format("%s/following", user.getUserHomeURL())); + break; + } + case R.id.textview_user_detail_follower_count: { + IntentUtils.openUri(getActivity(), String.format("%s/followers", user.getUserHomeURL())); + break; + } + case R.id.textview_user_detail_favorite_count: { + IntentUtils.openUri(getActivity(), String.format("%s/favorites", user.getUserHomeURL())); + break; + } + case R.id.button_user_detail_follow: { + ConfirmDialogFragment.show(getActivity(), getString(R.string.dialog_confirm_commands), this::toggleFollowing); + break; } - } else { - dismiss(); // BUG } } @@ -185,10 +178,16 @@ public class UserDetailDialogFragment extends StackableDialogFragment implements // ------------------------ OVERRIDE METHODS ------------------------ @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + user = User.fetch(getUserID()); + } + + @Override public Dialog onCreateDialog(Bundle savedInstanceState) { MainActivity activity = (MainActivity) getActivity(); - User user = User.fetch(getUserID()); if (user == null) { + Notificator.getInstance().publish(R.string.notice_error_show_user); return new DisposeDialog(activity); } @@ -230,7 +229,7 @@ public class UserDetailDialogFragment extends StackableDialogFragment implements tabHost.addTab(tab2); tabHost.setCurrentTab(0); - initUserData(user); + initUserData(); return new AlertDialog.Builder(activity) .setView(v) @@ -238,7 +237,7 @@ public class UserDetailDialogFragment extends StackableDialogFragment implements .create(); } - private void executeUserTimelineTask(final User user, final StatusListAdapter adapter) { + private void executeUserTimelineTask(final StatusListAdapter adapter) { Account account = Application.getInstance().getCurrentAccount(); tabHost.getTabWidget().getChildTabViewAt(1).setVisibility(View.GONE); new UserTimelineTask(account, user.getId()) @@ -254,7 +253,8 @@ public class UserDetailDialogFragment extends StackableDialogFragment implements .execute(); } - private String getHtmlDescription(String description) { + private String getHtmlDescription() { + String description = user.getDescription(); if (TextUtils.isEmpty(description)) { return ""; } @@ -265,14 +265,14 @@ public class UserDetailDialogFragment extends StackableDialogFragment implements return html; } - private void updateUserDataBasic(User user) { + private void updateUserDataBasic() { textViewName.setText(user.getName()); textViewScreenName.setText(user.getScreenName()); textViewProtected.setVisibility(user.isProtected() ? View.VISIBLE : View.GONE); ImageCache.getInstance().setImageToView(user.getProfileImageUrlOriginal(), imageViewIcon); } - private void updateUserDataDetail(User user) { + private void updateUserDataDetail() { if (TextUtils.isEmpty(user.getLocation())) { textViewLocate.setVisibility(View.GONE); } else { @@ -285,8 +285,7 @@ public class UserDetailDialogFragment extends StackableDialogFragment implements textViewURL.setText(user.getUrl()); textViewURL.setVisibility(View.VISIBLE); } - String htmlDescription = getHtmlDescription(user.getDescription()); - textViewDescription.setText(Html.fromHtml(htmlDescription)); + textViewDescription.setText(Html.fromHtml(getHtmlDescription())); textViewTweetCount.setText(String.valueOf(user.getStatusesCount())); textViewFriendCount.setText(String.valueOf(user.getFriendsCount())); @@ -296,22 +295,22 @@ public class UserDetailDialogFragment extends StackableDialogFragment implements ImageCache.getInstance().setImageToView(user.getProfileBannerUrl(), imageViewHeader); } - private void initUserData(User user) { - updateUserDataBasic(user); - updateUserDataDetail(user); + private void initUserData() { + updateUserDataBasic(); + updateUserDataDetail(); MainActivity activity = (MainActivity) getActivity(); adapter = new StatusListAdapter(activity); listViewTimeline.setAdapter(adapter); - executeUserTimelineTask(user, adapter); - updateRelationship(user.getId()); + executeUserTimelineTask(adapter); + updateRelationship(); observerBundle.attach(user, changes -> { if (getActivity() != null) { if (changes.contains(RBinding.BASIC)) - updateUserDataBasic(user); + updateUserDataBasic(); if (changes.contains(RBinding.DETAIL)) - updateUserDataDetail(user); + updateUserDataDetail(); } }); } @@ -322,7 +321,7 @@ public class UserDetailDialogFragment extends StackableDialogFragment implements buttonFollow.setEnabled(false); } - private void openUserMenu(final User user) { + private void openUserMenu() { UserMenuDialogFragment menuFragment = new UserMenuDialogFragment() { @Override protected void executeCommand(Command command) { @@ -331,7 +330,7 @@ public class UserDetailDialogFragment extends StackableDialogFragment implements if (UserDetailDialogFragment.this.isDetached()) { return; } - updateRelationship(user.getId()); + updateRelationship(); }, 1000); } }; @@ -339,14 +338,18 @@ public class UserDetailDialogFragment extends StackableDialogFragment implements DialogHelper.showDialog(getActivity(), menuFragment); } - private void setFollowButtonState(boolean isFollowing, Drawable unfollowColor, Drawable followColor) { + private void setFollowButtonState(boolean isFollowing) { + MainActivity mainActivity = (MainActivity) getActivity(); + int theme = Application.getInstance().getThemeResId(); + final Drawable unfollowColor = Themes.getStyledDrawable(mainActivity, theme, R.attr.button_round_red); + final Drawable followColor = Themes.getStyledDrawable(mainActivity, theme, R.attr.button_round_blue); buttonFollow.setText(isFollowing ? R.string.user_detail_unfollow : R.string.user_detail_follow); buttonFollow.setBackground(isFollowing ? unfollowColor : followColor); buttonFollow.setTag(isFollowing); buttonFollow.setEnabled(true); } - private void toggleFollowing(final User user) { + private void toggleFollowing() { Account account = Application.getInstance().getCurrentAccount(); lockFollowButton(); Boolean isFollowing = buttonFollow.getTag() != null ? (Boolean) buttonFollow.getTag() : false; @@ -354,7 +357,7 @@ public class UserDetailDialogFragment extends StackableDialogFragment implements new UnfollowTask(account, user.getId()) .onDoneUI(result -> { Notificator.getInstance().publish(R.string.notice_unfollow_succeeded); - updateRelationship(user.getId()); + updateRelationship(); buttonFollow.setEnabled(true); }) .onFail(x -> @@ -364,7 +367,7 @@ public class UserDetailDialogFragment extends StackableDialogFragment implements new FollowTask(account, user.getId()) .onDoneUI(result -> { Notificator.getInstance().publish(R.string.notice_follow_succeeded); - updateRelationship(user.getId()); + updateRelationship(); buttonFollow.setEnabled(true); }) .onFail(x -> Notificator.getInstance().alert(R.string.notice_follow_failed)) @@ -395,22 +398,18 @@ public class UserDetailDialogFragment extends StackableDialogFragment implements } } - private void updateRelationship(final long userId) { - MainActivity mainActivity = (MainActivity) getActivity(); + private void updateRelationship() { Account account = Application.getInstance().getCurrentAccount(); - if (userId == account.getUserId()) { + if (user == account.getUser()) { textViewFollowed.setText(R.string.user_detail_followed_is_me); buttonFollow.setVisibility(View.GONE); } else { - int theme = Application.getInstance().getThemeResId(); lockFollowButton(); textViewFollowed.setText(R.string.user_detail_loading); - final Drawable red = Themes.getStyledDrawable(mainActivity, theme, R.attr.button_round_red); - final Drawable blue = Themes.getStyledDrawable(mainActivity, theme, R.attr.button_round_blue); - new ShowFriendshipTask(account, userId).onDoneUI(relationship -> { + new ShowFriendshipTask(account, user.getId()).onDoneUI(relationship -> { boolean isFollowing = relationship.isSourceFollowingTarget(); boolean isFollowed = relationship.isSourceFollowedByTarget(); - setFollowButtonState(isFollowing, red, blue); + setFollowButtonState(isFollowing); textViewFollowed.setText(isFollowed ? R.string.user_detail_followed : R.string.user_detail_not_followed); }).execute(); } diff --git a/app/src/main/java/net/lacolaco/smileessence/view/dialog/UserMenuDialogFragment.java b/app/src/main/java/net/lacolaco/smileessence/view/dialog/UserMenuDialogFragment.java index d5db5f77..c877ac25 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/dialog/UserMenuDialogFragment.java +++ b/app/src/main/java/net/lacolaco/smileessence/view/dialog/UserMenuDialogFragment.java @@ -39,6 +39,7 @@ public class UserMenuDialogFragment extends MenuDialogFragment { // ------------------------------ FIELDS ------------------------------ private static final String KEY_USER_ID = "userID"; + private User user; // --------------------- GETTER / SETTER METHODS --------------------- @@ -55,10 +56,15 @@ public class UserMenuDialogFragment extends MenuDialogFragment { // ------------------------ OVERRIDE METHODS ------------------------ @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + user = User.fetch(getUserID()); + } + + @Override protected void setMenuItems(final CustomListAdapter<Command> adapter) { - User user = User.fetch(getUserID()); if (user != null) { - List<Command> commands = getCommands(user); + List<Command> commands = getCommands(); Command.filter(commands); for (Command command : commands) { adapter.addToBottom(command); @@ -71,20 +77,20 @@ public class UserMenuDialogFragment extends MenuDialogFragment { // -------------------------- OTHER METHODS -------------------------- - public boolean addBottomCommands(User user, ArrayList<Command> commands) { + public boolean addBottomCommands(ArrayList<Command> commands) { Activity activity = getActivity(); return commands.add(new CommandSearchOnTwitter(activity, user.getScreenName())); } - public boolean addMainCommands(User user, ArrayList<Command> commands) { + public boolean addMainCommands(ArrayList<Command> commands) { Activity activity = getActivity(); return commands.addAll(Command.getUserCommands(activity, user)); } - public List<Command> getCommands(User user) { + public List<Command> getCommands() { ArrayList<Command> commands = new ArrayList<>(); - addMainCommands(user, commands); - addBottomCommands(user, commands); + addMainCommands(commands); + addBottomCommands(commands); return commands; } } |