From ea8649515cfedb50b50b4c102718bef51631e09f Mon Sep 17 00:00:00 2001 From: oogee Date: Thu, 22 Nov 2018 15:30:27 +0200 Subject: [PATCH] fix shoutbox keyboard issues --- .../activities/shoutbox/ShoutboxActivity.java | 17 +++++++++++- .../activities/shoutbox/ShoutboxFragment.java | 16 ++++++++---- .../thmmy/mthmmy/editorview/EditorView.java | 26 ++++++++++++------- 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxActivity.java index fffc8079..bdd026ad 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxActivity.java @@ -7,6 +7,8 @@ import gr.thmmy.mthmmy.base.BaseActivity; public class ShoutboxActivity extends BaseActivity { + private ShoutboxFragment shoutboxFragment; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -25,8 +27,9 @@ public class ShoutboxActivity extends BaseActivity { drawer.setSelection(SHOUTBOX_ID); if (savedInstanceState == null) { + shoutboxFragment = ShoutboxFragment.newInstance(); getSupportFragmentManager().beginTransaction() - .replace(R.id.container, ShoutboxFragment.newInstance()) + .replace(R.id.container, shoutboxFragment) .commitNow(); } } @@ -36,4 +39,16 @@ public class ShoutboxActivity extends BaseActivity { drawer.setSelection(SHOUTBOX_ID); super.onResume(); } + + @Override + public void onBackPressed() { + int count = getSupportFragmentManager().getBackStackEntryCount(); + + if (count == 0) { + if (!shoutboxFragment.onBackPressed()) + super.onBackPressed(); + } else { + getSupportFragmentManager().popBackStack(); + } + } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxFragment.java index 499e2017..5bf34d6c 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxFragment.java @@ -76,11 +76,6 @@ public class ShoutboxFragment extends Fragment { shoutboxViewModel.sendShout(editorView.getText().toString()); }); editorView.hideMarkdown(); - editorView.setOnTouchListener((view, motionEvent) -> { - editorView.showMarkdown(); - return false; - }); - editorView.setMarkdownVisible(false); editorView.showMarkdownOnfocus(); return rootView; @@ -162,4 +157,15 @@ public class ShoutboxFragment extends Fragment { Toast.makeText(getContext(), "Failed to retrieve shoutbox, please contact mthmmy developer team", Toast.LENGTH_LONG).show(); } } + + /** + * @return whether or not {@link ShoutboxFragment#onBackPressed()} consumed the event or not + */ + public boolean onBackPressed() { + if (emojiKeyboard.isVisible()) { + emojiKeyboard.setVisibility(View.GONE); + return true; + } + return false; + } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/editorview/EditorView.java b/app/src/main/java/gr/thmmy/mthmmy/editorview/EditorView.java index 5adf4aa7..55df571c 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/editorview/EditorView.java +++ b/app/src/main/java/gr/thmmy/mthmmy/editorview/EditorView.java @@ -38,7 +38,7 @@ import gr.thmmy.mthmmy.R; public class EditorView extends LinearLayout implements EmojiInputField { - private static final int ANIMATION_DURATION = 100; + private static final int ANIMATION_DURATION = 200; private SparseArray colors = new SparseArray<>(); private TextInputLayout edittextWrapper; @@ -308,24 +308,32 @@ public class EditorView extends LinearLayout implements EmojiInputField { this.emojiKeyboard = emojiKeyboard; } - public void setMarkdownVisible(boolean visible) { - formatButtonsRecyclerview.setVisibility(visible ? VISIBLE : GONE); - } - public void showMarkdownOnfocus() { edittextWrapper.setOnClickListener(view -> { showMarkdown(); }); editText.setOnClickListener(view -> { + if (!emojiKeyboard.isVisible()) { + InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Activity.INPUT_METHOD_SERVICE); + imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); + } else { + InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Activity.INPUT_METHOD_SERVICE); + imm.hideSoftInputFromWindow(getWindowToken(), 0); + requestEditTextFocus(); + } showMarkdown(); }); edittextWrapper.setOnFocusChangeListener((view, b) -> { - if (b) showMarkdown(); - else hideMarkdown(); + if (b) { + emojiKeyboard.onEmojiInputFieldFocused(EditorView.this); + showMarkdown(); + } else hideMarkdown(); }); editText.setOnFocusChangeListener((view, b) -> { - if (b) showMarkdown(); - else hideMarkdown(); + if (b) { + emojiKeyboard.onEmojiInputFieldFocused(EditorView.this); + showMarkdown(); + } else hideMarkdown(); }); }