Files
SimpliTend/app/src/main/java/com/ssb/simplitend/patientprofile/RegisterCompleteFragment.java
2023-09-04 16:13:26 +05:30

208 lines
7.7 KiB
Java

package com.ssb.simplitend.patientprofile;
import android.animation.Animator;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.vectordrawable.graphics.drawable.Animatable2Compat;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.ssb.simplitend.R;
import com.ssb.simplitend.apputils.AppUtil;
import com.ssb.simplitend.apputils.RetrofitHelper;
import com.ssb.simplitend.databinding.RegisterationDoneFragmentBinding;
import com.ssb.simplitend.patient_dashboard.DashBoardActivity;
import com.ssb.simplitend.welcome.welcomepatient.mvvm.models.CallResponse;
import com.ssb.simplitend.welcome.welcomepatient.mvvm.models.PatientData;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class RegisterCompleteFragment extends Fragment implements ProfileContracts.ProfileProgressCallback{
public RegisterationDoneFragmentBinding binding;
public static final String PROFILE_PROGRESS = "profile_progress";
public static final String IS_CONNECTED_TO_CG = "is_connect_to_cg";
int profile_progress, is_connect_to_caregiver;
private ProgressDialog progressDialog;
public RegisterCompleteFragment() {
// required
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = RegisterationDoneFragmentBinding.inflate(inflater, container, false);
progressDialog = new ProgressDialog(requireContext());
if (getArguments() != null) {
profile_progress = getArguments().getInt(PROFILE_PROGRESS, 20);
is_connect_to_caregiver = getArguments().getInt(IS_CONNECTED_TO_CG, 0);
}
Glide.with(requireContext())
.asGif()
.load(R.raw.filling_anim)
.listener(new RequestListener<GifDrawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
if (resource == null) return false;
resource.setLoopCount(1);
resource.registerAnimationCallback(new Animatable2Compat.AnimationCallback() {
@Override
public void onAnimationEnd(Drawable drawable) {
super.onAnimationEnd(drawable);
String title = "Your profile is " + (profile_progress * 20) + "% complete!";
binding.title.setText(title);
YoYo.with(Techniques.FadeIn)
.duration(100)
.playOn(binding.title);
}
});
return false;
}
})
.into(binding.animIv);
binding.proceed.setOnClickListener(v -> {
checkConnectionStatus();
});
return binding.getRoot();
}
private void checkConnectionStatus() {
progressDialog = new ProgressDialog(requireContext());
progressDialog.setTitle("Please wait...");
progressDialog.setMessage("while we check the Caregiver connection status.");
progressDialog.setCancelable(false);
progressDialog.show();
PatientProfileAPIService apiService = RetrofitHelper.getRetrofit().create(PatientProfileAPIService.class);
String token = "Bearer " + AppUtil.getPatientToken(requireContext());
apiService.getUsrProfileProgress(token)
.enqueue(new Callback<CallResponse<PatientData>>() {
@Override
public void onResponse(Call<CallResponse<PatientData>> call, Response<CallResponse<PatientData>> response) {
if (response.body() != null) {
if (response.body().status != 200 || response.body().result == null) {
onProfileProgressFetchFailed(new Exception(), response.body().message);
return;
}
onProfileProgressFetched(response.body().result);
} else {
onProfileProgressFetchFailed(new Exception(), "Please try again later.");
}
}
@Override
public void onFailure(Call<CallResponse<PatientData>> call, Throwable t) {
onProfileProgressFetchFailed(new Exception(), "Please try again later.");
}
});
}
private void gotoPatientDashBoard() {
Intent intent = new Intent(requireActivity(), DashBoardActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
requireActivity().finish();
}
@Override
public void onProfileProgressFetched(PatientData patientData) {
progressDialog.dismiss();
if (patientData.isCareGiverConnectedWithPatient == 1) {
if (patientData.isCaregiverTakeSubscription == 1){
gotoPatientDashBoard();
}else{
AppUtil.showAlert(requireContext(),
"Ask Caregiver to Subscribe",
"Kindly ask CareGiver to complete SimpliTend subscription.",
"OK",
((dialogInterface, i) -> {
}), null, null);
}
} else {
AppUtil.showAlert(requireContext(),
"Ask Caregiver to register",
"Kindly ask CareGiver to complete registration.",
"OK",
((dialogInterface, i) -> {
}), null, null);
}
}
@Override
public void onProfileProgressFetchFailed(Throwable t, String message) {
progressDialog.dismiss();
Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show();
}
}
//binding.animIv.setAnimation(R.raw.done_anim_2);
// binding.animIv.playAnimation();
//
// binding.animIv.addAnimatorListener(new Animator.AnimatorListener() {
//@Override
//public void onAnimationStart(@NonNull Animator animator) {
//
// }
//
//@Override
//public void onAnimationEnd(@NonNull Animator animator) {
//
// String title = "Your profile is " + (profile_progress * 20) + "% complete!";
//
// binding.title.setText(title);
// YoYo.with(Techniques.FadeIn)
// .duration(100)
// .playOn(binding.title);
// }
//
//@Override
//public void onAnimationCancel(@NonNull Animator animator) {
//
// }
//
//@Override
//public void onAnimationRepeat(@NonNull Animator animator) {
//
// }
// });