2023-06-29 17:54:41 +05:30
|
|
|
package com.ssb.simplitend.apputils;
|
|
|
|
|
|
|
|
|
|
import android.app.Activity;
|
2023-07-13 21:12:17 +05:30
|
|
|
import android.app.AlertDialog;
|
|
|
|
|
import android.app.ProgressDialog;
|
2023-06-29 17:54:41 +05:30
|
|
|
import android.content.Context;
|
2023-07-13 21:12:17 +05:30
|
|
|
import android.content.DialogInterface;
|
|
|
|
|
import android.content.SharedPreferences;
|
2023-07-07 21:07:04 +05:30
|
|
|
import android.os.Handler;
|
2023-07-13 21:12:17 +05:30
|
|
|
import android.util.Log;
|
2023-07-07 21:07:04 +05:30
|
|
|
import android.view.LayoutInflater;
|
2023-06-29 17:54:41 +05:30
|
|
|
import android.view.View;
|
2023-07-31 20:36:26 +05:30
|
|
|
import android.view.WindowManager;
|
2023-06-29 17:54:41 +05:30
|
|
|
import android.view.inputmethod.InputMethodManager;
|
|
|
|
|
|
2023-07-07 21:07:04 +05:30
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
import androidx.annotation.RawRes;
|
|
|
|
|
|
|
|
|
|
import com.bumptech.glide.Glide;
|
|
|
|
|
import com.google.android.material.bottomsheet.BottomSheetDialog;
|
|
|
|
|
import com.ssb.simplitend.R;
|
|
|
|
|
import com.ssb.simplitend.databinding.DecisionBottomsheetBinding;
|
|
|
|
|
import com.ssb.simplitend.databinding.DoneBottomsheetBinding;
|
|
|
|
|
|
2023-06-29 17:54:41 +05:30
|
|
|
public abstract class AppUtil {
|
|
|
|
|
|
2023-07-13 21:12:17 +05:30
|
|
|
private static final String TAG = "AppUtil";
|
|
|
|
|
|
|
|
|
|
// fields
|
|
|
|
|
public static final String USER_DETAILS = "user_details";
|
|
|
|
|
|
|
|
|
|
public static final String USER_TOKEN = "user_token";
|
2023-07-18 19:38:59 +05:30
|
|
|
public static final String PATIENT_UID = "patient_uid";
|
2023-07-13 21:12:17 +05:30
|
|
|
|
|
|
|
|
// util functions
|
|
|
|
|
|
2023-06-29 17:54:41 +05:30
|
|
|
// closes keyboard
|
|
|
|
|
public static void closeKeyboard(Activity activity){
|
|
|
|
|
if (activity != null){
|
|
|
|
|
View view = activity.getCurrentFocus();
|
|
|
|
|
if (view != null) {
|
|
|
|
|
InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
|
|
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-07 21:07:04 +05:30
|
|
|
// show decision dialog
|
|
|
|
|
public static void showSOSDecision(@NonNull Context context,
|
|
|
|
|
@NonNull String decisionText,
|
|
|
|
|
@NonNull String positiveText, @NonNull String negativeText,
|
|
|
|
|
View.OnClickListener positiveClickListener,
|
|
|
|
|
View.OnClickListener negativeClickListener) {
|
|
|
|
|
|
|
|
|
|
DecisionBottomsheetBinding binding = DecisionBottomsheetBinding.inflate(LayoutInflater.from(context));
|
|
|
|
|
|
|
|
|
|
BottomSheetDialog bsd = new BottomSheetDialog(context, R.style.BottomSheetDialog);
|
|
|
|
|
bsd.setContentView(binding.getRoot());
|
2023-07-11 19:01:00 +05:30
|
|
|
bsd.setCancelable(true);
|
2023-07-07 21:07:04 +05:30
|
|
|
|
|
|
|
|
binding.text.setText(decisionText);
|
|
|
|
|
|
|
|
|
|
binding.positiveBtn.setText(positiveText);
|
|
|
|
|
binding.negativeBtn.setText(negativeText);
|
|
|
|
|
|
|
|
|
|
binding.negativeBtn.setOnClickListener(v -> {
|
|
|
|
|
bsd.dismiss();
|
|
|
|
|
negativeClickListener.onClick(v);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
binding.positiveBtn.setOnClickListener(v -> {
|
|
|
|
|
bsd.dismiss();
|
|
|
|
|
positiveClickListener.onClick(v);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
bsd.show();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void showAnimateDBS(@NonNull Context context,
|
|
|
|
|
@NonNull String title,
|
|
|
|
|
@RawRes int rawRes, long doneInterval,
|
|
|
|
|
@NonNull View.OnClickListener doneListener){
|
|
|
|
|
DoneBottomsheetBinding binding = DoneBottomsheetBinding.inflate(LayoutInflater.from(context));
|
|
|
|
|
|
|
|
|
|
BottomSheetDialog bsd = new BottomSheetDialog(context, R.style.BottomSheetDialog);
|
|
|
|
|
bsd.setContentView(binding.getRoot());
|
|
|
|
|
bsd.setCancelable(false);
|
|
|
|
|
|
|
|
|
|
binding.text.setText(title);
|
|
|
|
|
|
|
|
|
|
Glide.with(context)
|
|
|
|
|
.asGif()
|
|
|
|
|
.load(rawRes)
|
|
|
|
|
.into(binding.doneAnim);
|
|
|
|
|
|
|
|
|
|
bsd.show();
|
|
|
|
|
|
|
|
|
|
new Handler().postDelayed(() -> {
|
|
|
|
|
|
|
|
|
|
bsd.dismiss();
|
|
|
|
|
doneListener.onClick(null);
|
|
|
|
|
|
|
|
|
|
}, doneInterval);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 21:12:17 +05:30
|
|
|
// Show alert dialog
|
|
|
|
|
public static synchronized void showAlert(@NonNull Context context,
|
|
|
|
|
@NonNull String title, @NonNull String message,
|
|
|
|
|
@NonNull String positiveText, @NonNull DialogInterface.OnClickListener positiveClickListener,
|
|
|
|
|
String negativeText, DialogInterface.OnClickListener negativeClickListener) {
|
|
|
|
|
|
|
|
|
|
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
|
|
|
|
|
|
|
|
|
|
alertBuilder.setTitle(title);
|
|
|
|
|
alertBuilder.setMessage(message);
|
|
|
|
|
|
|
|
|
|
alertBuilder.setPositiveButton(positiveText, positiveClickListener /* Call back*/);
|
|
|
|
|
|
|
|
|
|
if (negativeText != null && negativeClickListener != null) {
|
|
|
|
|
// Negative button
|
|
|
|
|
alertBuilder.setNegativeButton(negativeText, negativeClickListener);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
alertBuilder.create().show(); // Showing alert dialog
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-18 19:38:59 +05:30
|
|
|
public static void saveUserCache(String token, int patient_uid, Context context){
|
2023-07-13 21:12:17 +05:30
|
|
|
SharedPreferences sp = context.getSharedPreferences(USER_DETAILS, Context.MODE_PRIVATE);
|
|
|
|
|
SharedPreferences.Editor editor = sp.edit();
|
|
|
|
|
|
|
|
|
|
editor.putString(USER_TOKEN, token);
|
2023-07-18 19:38:59 +05:30
|
|
|
editor.putInt(PATIENT_UID, patient_uid);
|
2023-07-13 21:12:17 +05:30
|
|
|
|
|
|
|
|
editor.apply();
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "saveToken: user token saved successful");
|
|
|
|
|
|
2023-07-18 19:38:59 +05:30
|
|
|
// TODO: 17-07-2023 remove below line afterwards
|
|
|
|
|
Log.d(TAG, "saveUserCache: " + token);
|
|
|
|
|
|
2023-07-13 21:12:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getUserToken(Context context){
|
|
|
|
|
SharedPreferences sp = context.getSharedPreferences(USER_DETAILS, Context.MODE_PRIVATE);
|
|
|
|
|
return sp.getString(USER_TOKEN, "");
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-18 19:38:59 +05:30
|
|
|
public static int getPatientUid(Context context){
|
|
|
|
|
SharedPreferences sp = context.getSharedPreferences(USER_DETAILS, Context.MODE_PRIVATE);
|
|
|
|
|
return sp.getInt(PATIENT_UID, -1);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-29 17:54:41 +05:30
|
|
|
}
|