From ad2b412a522d7d87a0f5334c3f7c9e5b3b810035 Mon Sep 17 00:00:00 2001 From: fawzy Date: Wed, 2 Oct 2019 16:26:01 +0200 Subject: [PATCH] ## showing progress while loading countries json from assets --- .../dialog/plus/ui/BaseDialogFragment.java | 21 ++++++-- .../java/com/dialog/plus/ui/DialogPlus.java | 6 ++- .../com/dialog/plus/utils/CommonUtil.java | 52 +++++++++++++++++++ .../com/dialog/plus/utils/KeyboardUtil.java | 32 ------------ 4 files changed, 74 insertions(+), 37 deletions(-) create mode 100644 dialogPlus/src/main/java/com/dialog/plus/utils/CommonUtil.java delete mode 100644 dialogPlus/src/main/java/com/dialog/plus/utils/KeyboardUtil.java diff --git a/dialogPlus/src/main/java/com/dialog/plus/ui/BaseDialogFragment.java b/dialogPlus/src/main/java/com/dialog/plus/ui/BaseDialogFragment.java index 36c114c..d5100fa 100644 --- a/dialogPlus/src/main/java/com/dialog/plus/ui/BaseDialogFragment.java +++ b/dialogPlus/src/main/java/com/dialog/plus/ui/BaseDialogFragment.java @@ -1,5 +1,6 @@ package com.dialog.plus.ui; +import android.app.ProgressDialog; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; @@ -20,7 +21,7 @@ import com.daimajia.androidanimations.library.YoYo; import com.dialog.plus.R; import com.dialog.plus.utils.AnimationUtils; -import com.dialog.plus.utils.KeyboardUtil; +import com.dialog.plus.utils.CommonUtil; import com.dialog.plus.utils.SampleAnimationListener; /** @@ -35,6 +36,7 @@ public abstract class BaseDialogFragment extend protected Binding binding; protected View mDialogView; private AnimationSet mModalInAnim, mModalOutAnim; + private ProgressDialog mProgressDialog; @Override public View onCreateView(@androidx.annotation.NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -104,11 +106,24 @@ protected void shakeView(EditText editText) { } protected void hideKeyboard(View view) { - KeyboardUtil.getInstance().hideKeyboard(view); + CommonUtil.getInstance().hideKeyboard(view); } protected void showKeyboard(View view) { - KeyboardUtil.getInstance().showKeyboard(view); + CommonUtil.getInstance().showKeyboard(view); + } + + public void showLoading() { + hideLoading(); + if (mProgressDialog == null) + mProgressDialog = CommonUtil.getInstance().getProgressDialog(getContext()); + mProgressDialog.show(); + } + + public void hideLoading() { + if (mProgressDialog != null && mProgressDialog.isShowing()) { + mProgressDialog.cancel(); + } } protected abstract Object getVariableValue(); diff --git a/dialogPlus/src/main/java/com/dialog/plus/ui/DialogPlus.java b/dialogPlus/src/main/java/com/dialog/plus/ui/DialogPlus.java index 04cb09f..30fa2a7 100644 --- a/dialogPlus/src/main/java/com/dialog/plus/ui/DialogPlus.java +++ b/dialogPlus/src/main/java/com/dialog/plus/ui/DialogPlus.java @@ -21,7 +21,7 @@ import com.dialog.plus.R; import com.dialog.plus.data.CountryRepo; import com.dialog.plus.databinding.DialogPlusBinding; -import com.dialog.plus.utils.KeyboardUtil; +import com.dialog.plus.utils.CommonUtil; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -98,11 +98,13 @@ private void setDialogType() { } private void setDialogCountriesRecycler() { + showLoading(); model.setCountryDataModels(new ArrayList<>(new CountryRepo(getContext()).getCountriesList())); CountryListDialogAdapter listDialogAdapter = new CountryListDialogAdapter(this, model.getCountryDataModels() , model.isShowCountryCode(), model.getCountriesDialogListener()); ((RecyclerView) getDialogAddedView(R.id.recycler)).setAdapter(listDialogAdapter); setSearchTextWatcher(listDialogAdapter.getFilter()); + hideLoading(); } @@ -236,7 +238,7 @@ private void timeUp() { model.setTimeLeft(0); getDialogAddedView(R.id.sendCode).setClickable(false); getDialogAddedView(R.id.txtPinEntry).setEnabled(false); - KeyboardUtil.getInstance().hideKeyboard(binding.getRoot()); + CommonUtil.getInstance().hideKeyboard(binding.getRoot()); if (getContext() != null) getDialogAddedView(R.id.sendCode).setBackgroundColor(ContextCompat.getColor(getContext(), R.color.carbon_grey_300)); if (model.getCodeTypeListener() != null) diff --git a/dialogPlus/src/main/java/com/dialog/plus/utils/CommonUtil.java b/dialogPlus/src/main/java/com/dialog/plus/utils/CommonUtil.java new file mode 100644 index 0000000..61c5298 --- /dev/null +++ b/dialogPlus/src/main/java/com/dialog/plus/utils/CommonUtil.java @@ -0,0 +1,52 @@ +package com.dialog.plus.utils; + +import android.app.ProgressDialog; +import android.content.Context; +import android.graphics.Color; +import android.graphics.drawable.ColorDrawable; +import android.view.View; +import android.view.inputmethod.InputMethodManager; + +import static android.content.Context.INPUT_METHOD_SERVICE; +import static android.view.Gravity.CENTER; + +/** + * Created by Muhammad Noamany + * muhammadnoamany@gmail.com + */ +public class CommonUtil { + private static CommonUtil commonUtil; + + public static CommonUtil getInstance() { + if (commonUtil == null) + commonUtil = new CommonUtil(); + return commonUtil; + } + + public ProgressDialog getProgressDialog(Context context) { + ProgressDialog progressDialog = new ProgressDialog(context); + if (progressDialog.getWindow() != null) { + progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); + progressDialog.getWindow().setGravity(CENTER); + } + progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); + progressDialog.setProgress(0); +// progressDialog.setContentView(R.layout.progress_dialog); + progressDialog.setIndeterminate(true); + progressDialog.setCancelable(false); + progressDialog.setCanceledOnTouchOutside(false); + return progressDialog; + } + + public void showKeyboard(View view) { + InputMethodManager inputMethodManager = (InputMethodManager) view.getContext().getSystemService(INPUT_METHOD_SERVICE); + view.requestFocus(); + inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_FORCED); + } + + public void hideKeyboard(View view) { + final InputMethodManager inputMethodManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); + inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0); + view.clearFocus(); + } +} diff --git a/dialogPlus/src/main/java/com/dialog/plus/utils/KeyboardUtil.java b/dialogPlus/src/main/java/com/dialog/plus/utils/KeyboardUtil.java deleted file mode 100644 index 8433d86..0000000 --- a/dialogPlus/src/main/java/com/dialog/plus/utils/KeyboardUtil.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.dialog.plus.utils; - -import android.content.Context; -import android.view.View; -import android.view.inputmethod.InputMethodManager; - -import static android.content.Context.INPUT_METHOD_SERVICE; -/** - * Created by Muhammad Noamany - * muhammadnoamany@gmail.com - */ -public class KeyboardUtil { - private static KeyboardUtil keyboardUtil; - - public static KeyboardUtil getInstance() { - if (keyboardUtil == null) - keyboardUtil = new KeyboardUtil(); - return keyboardUtil; - } - - public void showKeyboard(View view) { - InputMethodManager inputMethodManager = (InputMethodManager) view.getContext().getSystemService(INPUT_METHOD_SERVICE); - view.requestFocus(); - inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_FORCED); - } - - public void hideKeyboard(View view) { - final InputMethodManager inputMethodManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); - inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0); - view.clearFocus(); - } -}