1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-14 10:47:30 +00:00

Wrap show keyboard logic in try catch. Fixes austinhuang0131/barinsta#1482

This commit is contained in:
Ammar Githam 2021-07-12 22:39:44 +09:00
parent d2e9cabccd
commit a9b9f8e2a8
2 changed files with 28 additions and 14 deletions

View File

@ -59,7 +59,9 @@ public class RepliesFragment extends Fragment {
@Override @Override
public void onCreate(@Nullable final Bundle savedInstanceState) { public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
viewModel = new ViewModelProvider(getParentFragment()).get(CommentsViewerViewModel.class); final Fragment parentFragment = getParentFragment();
if (parentFragment == null) return;
viewModel = new ViewModelProvider(parentFragment).get(CommentsViewerViewModel.class);
final Bundle bundle = getArguments(); final Bundle bundle = getArguments();
if (bundle == null) return; if (bundle == null) return;
final Serializable serializable = bundle.getSerializable(ARG_PARENT); final Serializable serializable = bundle.getSerializable(ARG_PARENT);
@ -117,10 +119,13 @@ public class RepliesFragment extends Fragment {
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
viewModel.clearReplies(); if (viewModel != null) {
viewModel.clearReplies();
}
} }
private void setupObservers() { private void setupObservers() {
if (viewModel == null) return;
viewModel.getCurrentUserId().observe(getViewLifecycleOwner(), currentUserId -> { viewModel.getCurrentUserId().observe(getViewLifecycleOwner(), currentUserId -> {
long userId = 0; long userId = 0;
if (currentUserId != null) { if (currentUserId != null) {
@ -151,7 +156,8 @@ public class RepliesFragment extends Fragment {
final Bundle bundle = getArguments(); final Bundle bundle = getArguments();
if (bundle == null) return; if (bundle == null) return;
final boolean focusInput = bundle.getBoolean(ARG_FOCUS_INPUT); final boolean focusInput = bundle.getBoolean(ARG_FOCUS_INPUT);
if (focusInput && viewModel.getRepliesParent() != null && viewModel.getRepliesParent().getUser() != null) { if (focusInput && viewModel.getRepliesParent() != null) {
viewModel.getRepliesParent().getUser();
binding.commentText.setText(String.format("@%s ", viewModel.getRepliesParent().getUser().getUsername())); binding.commentText.setText(String.format("@%s ", viewModel.getRepliesParent().getUser().getUsername()));
Utils.showKeyboard(binding.commentText); Utils.showKeyboard(binding.commentText);
} }
@ -167,8 +173,9 @@ public class RepliesFragment extends Fragment {
break; break;
case ERROR: case ERROR:
binding.swipeRefreshLayout.setRefreshing(false); binding.swipeRefreshLayout.setRefreshing(false);
if (!TextUtils.isEmpty(listResource.message)) { final String message = listResource.message;
Snackbar.make(binding.getRoot(), listResource.message, Snackbar.LENGTH_LONG).show(); if (!TextUtils.isEmpty(message)) {
Snackbar.make(binding.getRoot(), message, Snackbar.LENGTH_LONG).show();
} }
break; break;
case LOADING: case LOADING:
@ -188,6 +195,7 @@ public class RepliesFragment extends Fragment {
} }
private void setupAdapter(final long currentUserId) { private void setupAdapter(final long currentUserId) {
if (viewModel == null) return;
final Context context = getContext(); final Context context = getContext();
if (context == null) return; if (context == null) return;
commentsAdapter = new CommentsAdapter( commentsAdapter = new CommentsAdapter(
@ -215,7 +223,9 @@ public class RepliesFragment extends Fragment {
final Context context = getContext(); final Context context = getContext();
if (context == null) return; if (context == null) return;
final LinearLayoutManager layoutManager = new LinearLayoutManager(context); final LinearLayoutManager layoutManager = new LinearLayoutManager(context);
final RecyclerLazyLoader lazyLoader = new RecyclerLazyLoader(layoutManager, (page, totalItemsCount) -> viewModel.fetchReplies()); final RecyclerLazyLoader lazyLoader = new RecyclerLazyLoader(layoutManager, (page, totalItemsCount) -> {
if (viewModel != null) viewModel.fetchReplies();
});
Helper.setupList(context, binding.comments, layoutManager, lazyLoader); Helper.setupList(context, binding.comments, layoutManager, lazyLoader);
} }

View File

@ -355,14 +355,18 @@ public final class Utils {
// } // }
public static void showKeyboard(@NonNull final View view) { public static void showKeyboard(@NonNull final View view) {
final Context context = view.getContext(); try {
if (context == null) return; final Context context = view.getContext();
final InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); if (context == null) return;
if (imm == null) return; final InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
view.requestFocus(); if (imm == null) return;
final boolean shown = imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); view.requestFocus();
if (!shown) { final boolean shown = imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
Log.e(TAG, "showKeyboard: System did not display the keyboard"); if (!shown) {
Log.e(TAG, "showKeyboard: System did not display the keyboard");
}
} catch (Exception e) {
Log.e(TAG, "showKeyboard: ", e);
} }
} }